UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

67 lines (63 loc) • 2.48 kB
const HEADER_NAME_PATTERN = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/; const BLOCKED_FORWARDED_CLIENT_HEADERS = new Set([ "authorization", "chatgpt-account-id", "connection", "content-length", "content-type", "cookie", "expect", "host", "keep-alive", "proxy-authorization", "proxy-connection", "set-cookie", "te", "trailer", "transfer-encoding", "upgrade", "x-amz-security-token", "x-api-key", "api-key", "x-goog-api-key", "x-oai-attestation", "x-openai-agent-assertion", ]); const ALLOWED_FORWARDED_CLIENT_HEADERS = new Set([ "originator", "x-client-request-id", "x-codex-app-version", "user-agent", // Preserve caller-supplied identifiers only when the provider explicitly opts in. // Do not admit arbitrary x-codex-* fields or authentication evidence. "x-codex-window-id", "x-codex-installation-id", ]); const MAX_FORWARDED_CLIENT_HEADERS = 64; /** Normalize one opt-in caller header while refusing credential and transport-owned fields. */ export function normalizeForwardedClientHeaderName(value: unknown): string | null { if (typeof value !== "string") return null; const name = value.trim().toLowerCase(); if (!name || !HEADER_NAME_PATTERN.test(name) || !ALLOWED_FORWARDED_CLIENT_HEADERS.has(name)) return null; return name; } /** Validate provider.forwardClientHeaders without ever admitting credential-bearing headers. */ export function providerForwardClientHeadersConfigError(value: unknown): string | null { if (value === undefined) return null; if (!Array.isArray(value)) return "forwardClientHeaders must be an array of header names"; if (value.length > MAX_FORWARDED_CLIENT_HEADERS) { return `forwardClientHeaders must contain at most ${MAX_FORWARDED_CLIENT_HEADERS} names`; } const seen = new Set<string>(); for (const raw of value) { if (typeof raw !== "string" || !raw.trim() || !HEADER_NAME_PATTERN.test(raw.trim())) { return "forwardClientHeaders must use valid HTTP header names"; } const name = raw.trim().toLowerCase(); if (BLOCKED_FORWARDED_CLIENT_HEADERS.has(name)) { return `forwardClientHeaders must not include credential or transport-owned header "${raw.trim()}"`; } if (!ALLOWED_FORWARDED_CLIENT_HEADERS.has(name)) { return "forwardClientHeaders must use the supported client metadata allowlist"; } if (seen.has(name)) return `forwardClientHeaders must not repeat header "${raw.trim()}"`; seen.add(name); } return null; }