refactor(gateway): share input allowlist normalizer

This commit is contained in:
Peter Steinberger
2026-03-07 16:18:10 +00:00
parent 70da80bcb5
commit 4204c96105
3 changed files with 14 additions and 19 deletions

View File

@@ -0,0 +1,9 @@
export function normalizeInputHostnameAllowlist(
values: string[] | undefined,
): string[] | undefined {
if (!values || values.length === 0) {
return undefined;
}
const normalized = values.map((value) => value.trim()).filter((value) => value.length > 0);
return normalized.length > 0 ? normalized : undefined;
}

View File

@@ -28,6 +28,7 @@ import type { ResolvedGatewayAuth } from "./auth.js";
import { sendJson, setSseHeaders, writeDone } from "./http-common.js";
import { handleGatewayPostJsonEndpoint } from "./http-endpoint-helpers.js";
import { resolveGatewayRequestContext } from "./http-utils.js";
import { normalizeInputHostnameAllowlist } from "./input-allowlist.js";
type OpenAiHttpOptions = {
auth: ResolvedGatewayAuth;
@@ -70,14 +71,6 @@ type ResolvedOpenAiChatCompletionsLimits = {
images: InputImageLimits;
};
function normalizeHostnameAllowlist(values: string[] | undefined): string[] | undefined {
if (!values || values.length === 0) {
return undefined;
}
const normalized = values.map((value) => value.trim()).filter((value) => value.length > 0);
return normalized.length > 0 ? normalized : undefined;
}
function resolveOpenAiChatCompletionsLimits(
config: GatewayHttpChatCompletionsConfig | undefined,
): ResolvedOpenAiChatCompletionsLimits {
@@ -94,7 +87,7 @@ function resolveOpenAiChatCompletionsLimits(
: DEFAULT_OPENAI_MAX_TOTAL_IMAGE_BYTES,
images: {
allowUrl: imageConfig?.allowUrl ?? DEFAULT_OPENAI_IMAGE_LIMITS.allowUrl,
urlAllowlist: normalizeHostnameAllowlist(imageConfig?.urlAllowlist),
urlAllowlist: normalizeInputHostnameAllowlist(imageConfig?.urlAllowlist),
allowedMimes: normalizeMimeList(imageConfig?.allowedMimes, DEFAULT_INPUT_IMAGE_MIMES),
maxBytes: imageConfig?.maxBytes ?? DEFAULT_INPUT_IMAGE_MAX_BYTES,
maxRedirects: imageConfig?.maxRedirects ?? DEFAULT_INPUT_MAX_REDIRECTS,

View File

@@ -35,6 +35,7 @@ import type { ResolvedGatewayAuth } from "./auth.js";
import { sendJson, setSseHeaders, writeDone } from "./http-common.js";
import { handleGatewayPostJsonEndpoint } from "./http-endpoint-helpers.js";
import { resolveGatewayRequestContext } from "./http-utils.js";
import { normalizeInputHostnameAllowlist } from "./input-allowlist.js";
import {
CreateResponseBodySchema,
type CreateResponseBody,
@@ -69,14 +70,6 @@ type ResolvedResponsesLimits = {
images: InputImageLimits;
};
function normalizeHostnameAllowlist(values: string[] | undefined): string[] | undefined {
if (!values || values.length === 0) {
return undefined;
}
const normalized = values.map((value) => value.trim()).filter((value) => value.length > 0);
return normalized.length > 0 ? normalized : undefined;
}
function resolveResponsesLimits(
config: GatewayHttpResponsesConfig | undefined,
): ResolvedResponsesLimits {
@@ -91,11 +84,11 @@ function resolveResponsesLimits(
: DEFAULT_MAX_URL_PARTS,
files: {
...fileLimits,
urlAllowlist: normalizeHostnameAllowlist(files?.urlAllowlist),
urlAllowlist: normalizeInputHostnameAllowlist(files?.urlAllowlist),
},
images: {
allowUrl: images?.allowUrl ?? true,
urlAllowlist: normalizeHostnameAllowlist(images?.urlAllowlist),
urlAllowlist: normalizeInputHostnameAllowlist(images?.urlAllowlist),
allowedMimes: normalizeMimeList(images?.allowedMimes, DEFAULT_INPUT_IMAGE_MIMES),
maxBytes: images?.maxBytes ?? DEFAULT_INPUT_IMAGE_MAX_BYTES,
maxRedirects: images?.maxRedirects ?? DEFAULT_INPUT_MAX_REDIRECTS,