174 lines
6.2 KiB
C#
174 lines
6.2 KiB
C#
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;
|
|
}
|
|
}
|