mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-26 09:02:11 +03:00
fix(security): match client-API aliases case-insensitively in the authz matcher
Next compiles the proxy matcher from `regexp.source` only, dropping path-to-regexp's case-insensitive flag, so `/v1/:path*` never matched `/V1/...` while the rewrite layer (flag kept) still routed it to the handler — an unauthenticated inference bypass via uppercase / mixed-case paths (/V1, /V1BETA, /CHAT, /RESPONSES, /CODEX, /MODELS). Expressing the casing inside a path-to-regexp custom group (`([vV]1)`) survives the flag-drop. classify.ts normalizes the control segment case so uppercase aliases resolve to CLIENT_API (honoring REQUIRE_API_KEY) instead of the management fallback. Reported by @Evgeny-SPB via GHSA-jvqc-mp9f-q936.
This commit is contained in:
28
src/proxy.ts
28
src/proxy.ts
@@ -24,6 +24,14 @@ export async function proxy(request: NextRequest) {
|
||||
return runAuthzPipeline(request, { enforce: true });
|
||||
}
|
||||
|
||||
// Next compiles the middleware/proxy matcher from `regexp.source` only, dropping
|
||||
// path-to-regexp's default case-insensitive flag — so a lowercase literal like
|
||||
// `/v1/:path*` never matches `/V1/...`, while the rewrite matcher (flag kept)
|
||||
// still routes it to the handler. That skipped the authz pipeline entirely
|
||||
// (GHSA-jvqc-mp9f-q936). Expressing the case-insensitivity inside a custom
|
||||
// path-to-regexp group (`([vV]1)`) survives the flag-drop because it needs no
|
||||
// flag. Keep these in sync with the client-API aliases in
|
||||
// next.config.mjs rewrites and src/server/authz/classify.ts.
|
||||
export const config = {
|
||||
matcher: [
|
||||
"/",
|
||||
@@ -31,15 +39,15 @@ export const config = {
|
||||
"/home",
|
||||
"/home/:path*",
|
||||
"/api/:path*",
|
||||
"/v1/:path*",
|
||||
"/v1",
|
||||
"/v1beta/:path*",
|
||||
"/v1beta",
|
||||
"/chat/:path*",
|
||||
"/responses/:path*",
|
||||
"/responses",
|
||||
"/codex/:path*",
|
||||
"/codex",
|
||||
"/models",
|
||||
"/:v1seg([vV]1)/:path*",
|
||||
"/:v1seg([vV]1)",
|
||||
"/:v1betaseg([vV]1[bB][eE][tT][aA])/:path*",
|
||||
"/:v1betaseg([vV]1[bB][eE][tT][aA])",
|
||||
"/:chatseg([cC][hH][aA][tT])/:path*",
|
||||
"/:respseg([rR][eE][sS][pP][oO][nN][sS][eE][sS])/:path*",
|
||||
"/:respseg([rR][eE][sS][pP][oO][nN][sS][eE][sS])",
|
||||
"/:codexseg([cC][oO][dD][eE][xX])/:path*",
|
||||
"/:codexseg([cC][oO][dD][eE][xX])",
|
||||
"/:modelsseg([mM][oO][dD][eE][lL][sS])",
|
||||
],
|
||||
};
|
||||
|
||||
@@ -16,30 +16,39 @@ function normalizePathname(rawPath: string): { path: string; reason?: Classifica
|
||||
if (!path.startsWith("/")) path = "/" + path;
|
||||
if (path.length > 1 && path.endsWith("/")) path = path.slice(0, -1);
|
||||
|
||||
if (path === "/codex" || path.startsWith("/codex/")) {
|
||||
// Client-API aliases are matched case-insensitively on the control segment.
|
||||
// Next's rewrite layer accepts `/V1/...`, `/CODEX`, etc. and routes them to
|
||||
// the client handler, so the classifier must recognize the same casing —
|
||||
// otherwise an uppercase alias falls through to the management fallback and
|
||||
// the request is treated as a different route class than it is actually
|
||||
// dispatched to (GHSA-jvqc-mp9f-q936). Only the leading control segment is
|
||||
// lowercased for detection; the original-case tail is preserved.
|
||||
const lower = path.toLowerCase();
|
||||
|
||||
if (lower === "/codex" || lower.startsWith("/codex/")) {
|
||||
return { path: "/api/v1/responses", reason: "client_api_codex_alias" };
|
||||
}
|
||||
|
||||
if (path === "/v1/v1" || path.startsWith("/v1/v1/")) {
|
||||
if (lower === "/v1/v1" || lower.startsWith("/v1/v1/")) {
|
||||
const tail = path.slice("/v1/v1".length) || "";
|
||||
return { path: "/api/v1" + tail, reason: "client_api_double_prefix" };
|
||||
}
|
||||
|
||||
if (path === "/v1beta" || path.startsWith("/v1beta/")) {
|
||||
if (lower === "/v1beta" || lower.startsWith("/v1beta/")) {
|
||||
const tail = path.slice("/v1beta".length) || "";
|
||||
return { path: "/api/v1beta" + tail, reason: "client_api_alias" };
|
||||
}
|
||||
|
||||
if (path === "/v1" || path.startsWith("/v1/")) {
|
||||
if (lower === "/v1" || lower.startsWith("/v1/")) {
|
||||
const tail = path.slice("/v1".length) || "";
|
||||
return { path: "/api/v1" + tail, reason: "client_api_alias" };
|
||||
}
|
||||
|
||||
for (const { alias, canonical } of CLIENT_API_ALIAS_PREFIXES) {
|
||||
if (path === alias) {
|
||||
if (lower === alias) {
|
||||
return { path: canonical, reason: "client_api_alias" };
|
||||
}
|
||||
if (path.startsWith(alias + "/")) {
|
||||
if (lower.startsWith(alias + "/")) {
|
||||
return { path: canonical + path.slice(alias.length), reason: "client_api_alias" };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user