Files
OmniRoute/tests/unit/authz/proxy-matcher-case.test.ts
Praveen K Palaniswamy 65e81158ab fix(ollama): route models by advertised capability (#11088)
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host.

Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean.

Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
2026-08-23 11:45:01 -03:00

77 lines
2.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createRequire } from "node:module";
import { config } from "../../../src/proxy.ts";
import { classifyRoute } from "../../../src/server/authz/classify.ts";
// Regression guard — GHSA-jvqc-mp9f-q936 (case-sensitive authz-matcher bypass).
//
// Next.js 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*` does NOT match `/V1/...`. The rewrite matcher keeps
// the flag, so `/V1/chat/completions` was still rewritten to the handler while
// skipping the authz pipeline entirely — an unauthenticated inference bypass.
//
// The fix expresses the case-insensitivity inside a path-to-regexp custom group
// (`/:seg([vV]1)/:path*`), which survives the flag-drop because it needs no
// flag. This test compiles the matcher exactly the way Next does and asserts the
// uppercase / mixed-case client aliases are covered.
const require = createRequire(import.meta.url);
const { tryToParsePath } = require("next/dist/lib/try-to-parse-path.js");
function compiledMatcherRegexes(): RegExp[] {
return (config.matcher as string[]).map((entry) => {
const parsed = tryToParsePath(entry);
// Mirror Next's middleware-route-matcher: source only, no flags.
return new RegExp(parsed.regexStr as string);
});
}
function isMatchedByProxy(path: string): boolean {
return compiledMatcherRegexes().some((re) => re.test(path));
}
test("proxy matcher still covers the canonical lowercase client aliases", () => {
for (const p of [
"/v1/chat/completions",
"/v1/models",
"/v1beta/models",
"/responses",
"/codex/x",
"/models",
]) {
assert.equal(isMatchedByProxy(p), true, `expected proxy matcher to cover ${p}`);
}
});
test("proxy matcher covers uppercase / mixed-case client aliases (GHSA-jvqc-mp9f-q936)", () => {
for (const p of [
"/V1/chat/completions",
"/V1/models",
"/V1BETA/models",
"/CHAT/completions",
"/RESPONSES",
"/CODEX/x",
"/MODELS",
"/Responses/x",
"/v1BeTa/models",
]) {
assert.equal(
isMatchedByProxy(p),
true,
`uppercase alias ${p} must reach the authz pipeline, not skip it`
);
}
});
test("classifyRoute treats uppercase client aliases as CLIENT_API, not management fallback", () => {
assert.equal(classifyRoute("/V1/chat/completions", "POST").routeClass, "CLIENT_API");
assert.equal(classifyRoute("/V1BETA/models", "GET").routeClass, "CLIENT_API");
assert.equal(classifyRoute("/MODELS", "GET").routeClass, "CLIENT_API");
assert.equal(classifyRoute("/CODEX", "POST").routeClass, "CLIENT_API");
// Lowercase behavior is unchanged.
assert.equal(classifyRoute("/v1/chat/completions", "POST").routeClass, "CLIENT_API");
});