fix(cloud): harden edge gateways, api-server ssrf, supabase fail-closed quota, and web/admin lifecycle

This commit is contained in:
Yun Chan 2026-11-14 15:10:32 +09:00
parent 2a009e421a
commit c68e81e0aa
46 changed files with 2553 additions and 294 deletions

View file

@ -0,0 +1,174 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
namespace D3ROVoice.Api.Services;
/// <summary>
/// Central validation rules for upstream model/STT endpoint URLs, language parameters,
/// numeric bounds, and provider identifiers to prevent SSRF, query/prompt injection, and NaN leaks.
/// </summary>
public static partial class EndpointUrlPolicy
{
private static readonly string[] BlockedHostSuffixes =
[
".localhost",
".local",
".internal",
".localdomain",
".home.arpa"
];
[GeneratedRegex("^(?:auto|[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2})$", RegexOptions.CultureInvariant)]
private static partial Regex LanguageTagRegex();
[GeneratedRegex("^[A-Za-z0-9_-]{1,128}$", RegexOptions.CultureInvariant)]
private static partial Regex AssemblyAiIdRegex();
public static bool AllowsLoopbackForLlmProvider(string? provider)
{
if (string.IsNullOrWhiteSpace(provider)) return false;
var trimmed = provider.Trim();
return trimmed.Contains("ollama", StringComparison.OrdinalIgnoreCase)
|| trimmed.Equals("local", StringComparison.OrdinalIgnoreCase)
|| trimmed.Equals("local-sidecar", StringComparison.OrdinalIgnoreCase);
}
public static bool AllowsLoopbackForSttProvider(string? providerType)
{
if (string.IsNullOrWhiteSpace(providerType)) return false;
return providerType.Trim().Equals("local-sidecar", StringComparison.OrdinalIgnoreCase);
}
public static bool IsValidEndpointUrl(string? value, bool allowLoopback = false)
{
if (string.IsNullOrWhiteSpace(value)) return false;
if (!Uri.TryCreate(value.Trim(), UriKind.Absolute, out var uri)) return false;
if (!string.IsNullOrEmpty(uri.UserInfo)) return false;
var isHttp = uri.Scheme == Uri.UriSchemeHttp;
var isHttps = uri.Scheme == Uri.UriSchemeHttps;
if (!isHttp && !isHttps) return false;
var host = uri.IdnHost.Trim().TrimEnd('.');
if (string.IsNullOrWhiteSpace(host)) return false;
if (IPAddress.TryParse(host, out var ip))
{
if (ip.IsIPv4MappedToIPv6) ip = ip.MapToIPv4();
if (IPAddress.IsLoopback(ip))
{
return allowLoopback;
}
if (!isHttps || IsNonPublicIp(ip))
{
return false;
}
return true;
}
if (isHttp && !IsLoopbackHostname(host))
{
return false;
}
if (IsLoopbackHostname(host))
{
return allowLoopback;
}
foreach (var suffix in BlockedHostSuffixes)
{
if (host.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
// Reject single-label internal service names (e.g., "redis", "metadata") and raw integer aliases.
if (!host.Contains('.', StringComparison.Ordinal))
{
return false;
}
return isHttps;
}
public static string ValidateLanguageCode(string? rawLanguage, string fallback = "ko")
{
if (string.IsNullOrWhiteSpace(rawLanguage))
{
return fallback;
}
var trimmed = rawLanguage.Trim();
if (trimmed.Length > 32 || !LanguageTagRegex().IsMatch(trimmed))
{
throw new ArgumentException("Invalid language code format.", "language");
}
return trimmed;
}
public static bool TryValidateLanguageCode(string? rawLanguage)
{
if (string.IsNullOrWhiteSpace(rawLanguage)) return true;
var trimmed = rawLanguage.Trim();
return trimmed.Length <= 32 && LanguageTagRegex().IsMatch(trimmed);
}
public static bool IsValidFiniteDouble(double value, double min, double max) =>
double.IsFinite(value) && value >= min && value <= max;
public static bool IsValidAssemblyAiTranscriptId(string? id) =>
!string.IsNullOrWhiteSpace(id) && AssemblyAiIdRegex().IsMatch(id.Trim());
private static bool IsLoopbackHostname(string host) =>
host.Equals("localhost", StringComparison.OrdinalIgnoreCase)
|| host.EndsWith(".localhost", StringComparison.OrdinalIgnoreCase);
private static bool IsNonPublicIp(IPAddress ip)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
var bytes = ip.GetAddressBytes();
return bytes[0] switch
{
0 => true, // 0.0.0.0/8 unspecified
10 => true, // 10.0.0.0/8 RFC1918
100 when bytes[1] is >= 64 and <= 127 => true, // 100.64.0.0/10 CGNAT
127 => true, // 127.0.0.0/8 loopback
169 when bytes[1] == 254 => true, // 169.254.0.0/16 link-local & cloud metadata
172 when bytes[1] is >= 16 and <= 31 => true, // 172.16.0.0/12 RFC1918
192 when bytes[1] == 0 && bytes[2] == 0 => true, // 192.0.0.0/24 IETF protocol assignments
192 when bytes[1] == 0 && bytes[2] == 2 => true, // TEST-NET-1
192 when bytes[1] == 168 => true, // 192.168.0.0/16 RFC1918
198 when bytes[1] is 18 or 19 => true, // 198.18.0.0/15 benchmark
>= 224 => true, // multicast / reserved / broadcast
_ => false,
};
}
if (ip.AddressFamily == AddressFamily.InterNetworkV6)
{
if (ip.Equals(IPAddress.IPv6Any) || ip.Equals(IPAddress.IPv6None) || ip.Equals(IPAddress.IPv6Loopback))
{
return true;
}
if (ip.IsIPv6LinkLocal || ip.IsIPv6Multicast || ip.IsIPv6SiteLocal)
{
return true;
}
var bytes = ip.GetAddressBytes();
// fc00::/7 Unique Local Addresses (ULA)
if ((bytes[0] & 0xfe) == 0xfc)
{
return true;
}
return false;
}
return true;
}
}

View file

@ -73,7 +73,14 @@ public class LlmProxyService : ILlmProxyService
{
try
{
using var client = _httpClientFactory.CreateClient();
if (!EndpointUrlPolicy.IsValidEndpointUrl(
endpoint.EndpointUrl,
EndpointUrlPolicy.AllowsLoopbackForLlmProvider(endpoint.Provider)))
{
throw new InvalidOperationException("Configured model endpoint URL is not permitted.");
}
using var client = _httpClientFactory.CreateClient("UpstreamNoRedirect");
client.Timeout = TimeSpan.FromSeconds(60);
var payload = new
@ -93,9 +100,10 @@ public class LlmProxyService : ILlmProxyService
Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json")
};
if (!string.IsNullOrWhiteSpace(endpoint.ApiKey))
var trimmedKey = endpoint.ApiKey?.Trim();
if (!string.IsNullOrWhiteSpace(trimmedKey))
{
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey);
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", trimmedKey);
}
using var httpResponse = await client.SendAsync(httpRequest, cancellationToken);

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
@ -82,6 +83,15 @@ public class SttProxyService : ISttProxyService
{
cancellationToken.ThrowIfCancellationRequested();
if (!string.IsNullOrWhiteSpace(request.Language))
{
EndpointUrlPolicy.ValidateLanguageCode(request.Language);
}
if (request.Temperature is { } requestedTemp && !EndpointUrlPolicy.IsValidFiniteDouble(requestedTemp, 0.0, 2.0))
{
throw new ArgumentException("Temperature must be finite and between 0.0 and 2.0.", "temperature");
}
// 1. Resolve Audio Bytes
byte[] finalAudioBytes;
if (audioBytes != null && audioBytes.Length > 0)
@ -296,11 +306,18 @@ public class SttProxyService : ISttProxyService
CancellationToken cancellationToken
)
{
using var client = _httpClientFactory.CreateClient();
var providerType = endpoint.ProviderType.ToLowerInvariant();
if (!EndpointUrlPolicy.IsValidEndpointUrl(
endpoint.EndpointUrl,
EndpointUrlPolicy.AllowsLoopbackForSttProvider(providerType)))
{
throw new InvalidOperationException("STT provider endpoint URL is not permitted.");
}
using var client = _httpClientFactory.CreateClient("UpstreamNoRedirect");
client.Timeout = TimeSpan.FromSeconds(90);
var providerType = endpoint.ProviderType.ToLowerInvariant();
var apiKey = endpoint.ApiKey?.Trim() ?? "";
var apiKey = GetTrimmedApiKey(endpoint);
if (RequiresApiKey(providerType) && string.IsNullOrWhiteSpace(apiKey))
{
@ -360,8 +377,8 @@ public class SttProxyService : ISttProxyService
form.Add(new StringContent(endpoint.ModelId), "model");
var language = request.Language ?? endpoint.Language ?? "ko";
if (!string.IsNullOrWhiteSpace(language) && language != "auto")
var language = ResolveValidatedLanguage(request.Language, endpoint.Language, "ko");
if (language != "auto")
{
form.Add(new StringContent(language), "language");
}
@ -373,7 +390,8 @@ public class SttProxyService : ISttProxyService
}
var temp = request.Temperature ?? endpoint.Temperature;
form.Add(new StringContent(temp.ToString("0.0")), "temperature");
if (!EndpointUrlPolicy.IsValidFiniteDouble(temp, 0.0, 2.0)) temp = 0.0;
form.Add(new StringContent(temp.ToString("0.0", CultureInfo.InvariantCulture)), "temperature");
form.Add(new StringContent("verbose_json"), "response_format");
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
@ -381,9 +399,10 @@ public class SttProxyService : ISttProxyService
Content = form
};
if (!string.IsNullOrWhiteSpace(endpoint.ApiKey))
var apiKey = GetTrimmedApiKey(endpoint);
if (!string.IsNullOrWhiteSpace(apiKey))
{
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey);
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
}
ApplyExtraHeaders(httpRequest, endpoint.ExtraHeadersJson);
@ -423,12 +442,11 @@ public class SttProxyService : ISttProxyService
CancellationToken cancellationToken
)
{
var lang = request.Language ?? endpoint.Language ?? "ko";
var model = endpoint.ModelId ?? "nova-3";
var lang = ResolveValidatedLanguage(request.Language, endpoint.Language, "ko");
var model = string.IsNullOrWhiteSpace(endpoint.ModelId) ? "nova-3" : endpoint.ModelId.Trim();
var baseUrl = endpoint.EndpointUrl.TrimEnd('/');
var url = baseUrl.Contains("?")
? $"{baseUrl}&model={model}&language={lang}&smart_format=true&punctuate=true"
: $"{baseUrl}?model={model}&language={lang}&smart_format=true&punctuate=true";
var separator = baseUrl.Contains('?', StringComparison.Ordinal) ? "&" : "?";
var url = $"{baseUrl}{separator}model={Uri.EscapeDataString(model)}&language={Uri.EscapeDataString(lang)}&smart_format=true&punctuate=true";
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{
@ -436,9 +454,10 @@ public class SttProxyService : ISttProxyService
};
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
if (!string.IsNullOrWhiteSpace(endpoint.ApiKey))
var apiKey = GetTrimmedApiKey(endpoint);
if (!string.IsNullOrWhiteSpace(apiKey))
{
httpRequest.Headers.Add("Authorization", $"Token {endpoint.ApiKey}");
httpRequest.Headers.Add("Authorization", $"Token {apiKey}");
}
ApplyExtraHeaders(httpRequest, endpoint.ExtraHeadersJson);
@ -484,13 +503,12 @@ public class SttProxyService : ISttProxyService
)
{
var base64Audio = Convert.ToBase64String(audioBytes);
var lang = request.Language ?? endpoint.Language ?? "ko";
var lang = ResolveValidatedLanguage(request.Language, endpoint.Language, "ko");
var apiKey = GetTrimmedApiKey(endpoint);
// If endpoint is Gemini Flash Multimodal API
if (endpoint.EndpointUrl.Contains("generativelanguage.googleapis.com") || endpoint.ModelId.Contains("gemini"))
{
var apiKey = endpoint.ApiKey;
var geminiPayload = new
{
contents = new[]
@ -543,8 +561,6 @@ public class SttProxyService : ISttProxyService
else
{
// Google Cloud Speech-to-Text v1
var apiKey = endpoint.ApiKey;
var gcpPayload = new
{
config = new
@ -606,20 +622,25 @@ public class SttProxyService : ISttProxyService
CancellationToken cancellationToken
)
{
var apiKey = GetTrimmedApiKey(endpoint);
// 1. Upload audio
using var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.assemblyai.com/v2/upload")
{
Content = new ByteArrayContent(audioBytes)
};
uploadRequest.Headers.Add("Authorization", endpoint.ApiKey);
uploadRequest.Headers.Add("Authorization", apiKey);
var uploadBody = await SendForStringAsync(client, uploadRequest, cancellationToken);
using var uploadDoc = JsonDocument.Parse(uploadBody);
var uploadUrl = uploadDoc.RootElement.GetProperty("upload_url").GetString()!;
var uploadUrl = uploadDoc.RootElement.GetProperty("upload_url").GetString();
if (!EndpointUrlPolicy.IsValidEndpointUrl(uploadUrl, allowLoopback: false))
{
throw new JsonException("AssemblyAI returned an invalid upload URL.");
}
// 2. Submit transcript job
var lang = request.Language ?? endpoint.Language ?? "ko";
var lang = ResolveValidatedLanguage(request.Language, endpoint.Language, "ko");
var transcriptPayload = new
{
audio_url = uploadUrl,
@ -632,19 +653,24 @@ public class SttProxyService : ISttProxyService
{
Content = new StringContent(JsonSerializer.Serialize(transcriptPayload), Encoding.UTF8, "application/json")
};
transRequest.Headers.Add("Authorization", endpoint.ApiKey);
transRequest.Headers.Add("Authorization", apiKey);
var transBody = await SendForStringAsync(client, transRequest, cancellationToken);
using var transDoc = JsonDocument.Parse(transBody);
var id = transDoc.RootElement.GetProperty("id").GetString()!;
var id = transDoc.RootElement.GetProperty("id").GetString();
if (!EndpointUrlPolicy.IsValidAssemblyAiTranscriptId(id))
{
throw new JsonException("AssemblyAI returned an invalid transcript identifier.");
}
var safeId = Uri.EscapeDataString(id!.Trim());
// 3. Poll for result (up to 20 seconds)
for (int i = 0; i < 20; i++)
{
await Task.Delay(1000, cancellationToken);
using var pollReq = new HttpRequestMessage(HttpMethod.Get, $"https://api.assemblyai.com/v2/transcript/{id}");
pollReq.Headers.Add("Authorization", endpoint.ApiKey);
using var pollReq = new HttpRequestMessage(HttpMethod.Get, $"https://api.assemblyai.com/v2/transcript/{safeId}");
pollReq.Headers.Add("Authorization", apiKey);
var pollBody = await SendForStringAsync(client, pollReq, cancellationToken);
@ -683,12 +709,11 @@ public class SttProxyService : ISttProxyService
CancellationToken cancellationToken
)
{
var lang = request.Language ?? endpoint.Language ?? "ko-KR";
var lang = ResolveValidatedLanguage(request.Language, endpoint.Language, "ko-KR");
if (lang == "ko") lang = "ko-KR";
var url = endpoint.EndpointUrl.Contains("?")
? $"{endpoint.EndpointUrl}&language={lang}&format=detailed"
: $"{endpoint.EndpointUrl}?language={lang}&format=detailed";
var separator = endpoint.EndpointUrl.Contains('?', StringComparison.Ordinal) ? "&" : "?";
var url = $"{endpoint.EndpointUrl}{separator}language={Uri.EscapeDataString(lang)}&format=detailed";
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{
@ -696,9 +721,10 @@ public class SttProxyService : ISttProxyService
};
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType.Contains("wav") ? "audio/wav" : "audio/webm");
if (!string.IsNullOrWhiteSpace(endpoint.ApiKey))
var apiKey = GetTrimmedApiKey(endpoint);
if (!string.IsNullOrWhiteSpace(apiKey))
{
httpRequest.Headers.Add("Ocp-Apim-Subscription-Key", endpoint.ApiKey);
httpRequest.Headers.Add("Ocp-Apim-Subscription-Key", apiKey);
}
var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
@ -746,16 +772,17 @@ public class SttProxyService : ISttProxyService
form.Add(audioContent, "file", fileName);
form.Add(new StringContent(endpoint.ModelId), "model");
form.Add(new StringContent(request.Language ?? endpoint.Language ?? "ko"), "language");
form.Add(new StringContent(ResolveValidatedLanguage(request.Language, endpoint.Language, "ko")), "language");
using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
{
Content = form
};
if (!string.IsNullOrWhiteSpace(endpoint.ApiKey))
var apiKey = GetTrimmedApiKey(endpoint);
if (!string.IsNullOrWhiteSpace(apiKey))
{
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey);
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
}
var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
@ -1061,6 +1088,17 @@ public class SttProxyService : ISttProxyService
};
}
private static string GetTrimmedApiKey(SttProviderEndpoint endpoint) =>
endpoint.ApiKey?.Trim() ?? string.Empty;
private static string ResolveValidatedLanguage(string? requested, string? endpointDefault, string fallback)
{
var candidate = !string.IsNullOrWhiteSpace(requested)
? requested
: (!string.IsNullOrWhiteSpace(endpointDefault) ? endpointDefault : fallback);
return EndpointUrlPolicy.ValidateLanguageCode(candidate, fallback);
}
private static void ApplyExtraHeaders(HttpRequestMessage req, string? extraHeadersJson)
{
if (string.IsNullOrWhiteSpace(extraHeadersJson)) return;