Allow public HTTPS model provider origins
Staging Backend Quality Gate / validate (push) Successful in 12m28s
Staging Backend Quality Gate / publish (push) Successful in 22m47s

This commit is contained in:
Jesse_Chen
2026-08-08 11:18:50 +08:00
parent 0676b6c402
commit 5fa1b8304e
4 changed files with 47 additions and 35 deletions
+6 -22
View File
@@ -8,7 +8,6 @@ const blockedHostnames = new Set([
"metadata.google.internal",
]);
const blockedHostnameSuffixes = [".localhost", ".local", ".internal", ".lan", ".home", ".arpa"];
const defaultModelProviderOrigins = new Set(["https://api.openai.com", "https://api.anthropic.com"]);
const defaultLookup: HostLookup = (hostname, options) => dns.lookup(hostname, options);
type ResolvedAddress = Readonly<{ address: string; family: number }>;
@@ -124,32 +123,17 @@ export async function assertPublicGatewayUrl(
return (await resolvePublicUrl(configured, lookup)).url;
}
function addAllowedOrigin(origins: Set<string>, value: string | undefined) {
if (!value) return;
try {
const url = assertPublicEpayGateway(value.trim());
if (url.protocol === "https:") origins.add(url.origin);
} catch {
// Invalid server-owned entries do not widen the allowlist.
}
}
function modelProviderOrigins(environment: ModelProviderEnvironment) {
const origins = new Set(defaultModelProviderOrigins);
environment.MODEL_PROVIDER_BASE_URL_ALLOWLIST?.split(",").forEach((value) => addAllowedOrigin(origins, value));
return origins;
}
export async function assertAllowedModelProviderUrl(
value: URL | string,
environment: ModelProviderEnvironment = process.env,
_environment: ModelProviderEnvironment = process.env,
lookup: HostLookup = defaultLookup,
) {
const resolved = await resolvePublicUrl(value, lookup);
if (resolved.url.protocol !== "https:" || !modelProviderOrigins(environment).has(resolved.url.origin)) {
throw new Error("模型供应商地址不在服务器允许列表中");
void _environment;
const url = value instanceof URL ? value : new URL(value);
if (url.protocol !== "https:") {
throw new Error("网关地址不允许使用 HTTP;模型供应商必须使用 HTTPS");
}
return resolved;
return resolvePublicUrl(url, lookup);
}
async function withinTimeout<T>(operation: Promise<T>, timeoutMs: number, message: string) {