mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-21 15:05:56 +00:00
fix(authz): cap /a2a request bodies and isolate the a2a test database
The /a2a JSON-RPC endpoint keeps its own normalized path, so the pipeline's /api/-only body-size guard never ran for it; route it through the same cap. Move the /a2a credential check into an isA2aPath helper so clientApiPolicy keeps its cognitive-complexity budget, and point the #19832 test at a throwaway DATA_DIR that is closed and removed after the run.
This commit is contained in:
committed by
diegosouzapw
parent
4e3eb46239
commit
4273a5e34f
@@ -258,6 +258,14 @@ async function getBodySizeSettings(): Promise<Record<string, unknown> | undefine
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Paths whose bodies are capped before any handler parses them: every `/api/*` route (the `/v1`
|
||||
* aliases normalize into it) plus the `/a2a` JSON-RPC endpoint, which keeps its own path (#19832).
|
||||
*/
|
||||
function isBodySizeGuardedPath(pathname: string): boolean {
|
||||
return pathname.startsWith("/api/") || pathname === "/a2a" || pathname.startsWith("/a2a/");
|
||||
}
|
||||
|
||||
export async function runAuthzPipeline(
|
||||
request: NextRequest,
|
||||
options: AuthzPipelineOptions = {}
|
||||
@@ -315,7 +323,7 @@ export async function runAuthzPipeline(
|
||||
return response;
|
||||
}
|
||||
|
||||
if (guardedPathname.startsWith("/api/") && method !== "GET" && method !== "OPTIONS") {
|
||||
if (isBodySizeGuardedPath(guardedPathname) && method !== "GET" && method !== "OPTIONS") {
|
||||
const bodySizeSettings = await getBodySizeSettings();
|
||||
const bodySizeRejection = checkBodySize(
|
||||
request,
|
||||
|
||||
@@ -22,6 +22,11 @@ function isWsHandshake(ctx: PolicyContext): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/** `/a2a` accepts header credentials only (#19832): never a URL-borne token. */
|
||||
function isA2aPath(normalizedPath: string): boolean {
|
||||
return normalizedPath === "/a2a" || normalizedPath.startsWith("/a2a/");
|
||||
}
|
||||
|
||||
function maskKeyId(apiKey: string): string {
|
||||
const tail = apiKey.slice(-4);
|
||||
return `key_${tail}`;
|
||||
@@ -110,9 +115,7 @@ export const clientApiPolicy: RoutePolicy = {
|
||||
// NOT short-circuit auth, and neither may an unrelated Bearer sent ALONGSIDE
|
||||
// a valid x-api-key (Claude Code with a claude.ai session or
|
||||
// ANTHROPIC_AUTH_TOKEN): the request is accepted when ANY credential validates.
|
||||
const allowUrl =
|
||||
ctx.classification.normalizedPath !== "/a2a" &&
|
||||
!ctx.classification.normalizedPath.startsWith("/a2a/");
|
||||
const allowUrl = !isA2aPath(ctx.classification.normalizedPath);
|
||||
const credentials = extractApiKeyCandidates(ctx.request as Request, { allowUrl });
|
||||
if (credentials.length === 0) {
|
||||
// The WS descriptor handshake is a metadata read; the route handler
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
import { config } from "../../../src/proxy.ts";
|
||||
import { classifyRoute } from "../../../src/server/authz/classify.ts";
|
||||
import { clientApiPolicy } from "../../../src/server/authz/policies/clientApi.ts";
|
||||
import { authenticateA2ARequest } from "../../../src/lib/a2a/authenticate.ts";
|
||||
// The A2A/clientApi auth paths read feature flags and API keys from SQLite: point them at a
|
||||
// throwaway DATA_DIR before those modules load, so an inherited DATA_DIR is never touched.
|
||||
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-a2a-connect-19832-"));
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
|
||||
const core = await import("../../../src/lib/db/core.ts");
|
||||
const { config } = await import("../../../src/proxy.ts");
|
||||
const { classifyRoute } = await import("../../../src/server/authz/classify.ts");
|
||||
const { clientApiPolicy } = await import("../../../src/server/authz/policies/clientApi.ts");
|
||||
const { authenticateA2ARequest } = await import("../../../src/lib/a2a/authenticate.ts");
|
||||
|
||||
test.after(() => {
|
||||
core.resetDbInstance();
|
||||
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||||
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
|
||||
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
|
||||
});
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { tryToParsePath } = require("next/dist/lib/try-to-parse-path.js");
|
||||
|
||||
@@ -311,6 +311,21 @@ test("runAuthzPipeline rejects oversized API bodies before auth", async () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("runAuthzPipeline rejects oversized /a2a JSON-RPC bodies before auth (#19832)", async () => {
|
||||
for (const url of ["http://localhost/a2a", "http://localhost/A2A"]) {
|
||||
const response = await pipeline.runAuthzPipeline(
|
||||
request(url, {
|
||||
method: "POST",
|
||||
headers: { "content-length": String(99 * 1024 * 1024) },
|
||||
}),
|
||||
{ enforce: true }
|
||||
);
|
||||
|
||||
assert.equal(response.status, 413, `${url} must be capped by the body-size guard`);
|
||||
assert.equal(response.headers.get("x-omniroute-route-class"), "CLIENT_API");
|
||||
}
|
||||
});
|
||||
|
||||
test("runAuthzPipeline rejects oversized rewritten alias API bodies before auth", async () => {
|
||||
const response = await pipeline.runAuthzPipeline(
|
||||
request("http://localhost/v1/chat/completions", {
|
||||
|
||||
Reference in New Issue
Block a user