fix(cloud): harden edge gateways, api-server ssrf, supabase fail-closed quota, and web/admin lifecycle
This commit is contained in:
parent
2a009e421a
commit
c68e81e0aa
46 changed files with 2553 additions and 294 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue