Allow public HTTPS model provider origins
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user