mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-13 18:32:12 +03:00
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) Two shards on release/v3.8.51 went red in one day with the same signature — "ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only .github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass alone and on re-run: the cleanup races something still writing into the directory (SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner the window opens. 1154 test files do their own cleanup with fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries. One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record): every rm / rmSync / rmdirSync option object with `recursive: true` and no `maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292 files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included. Only the option object changes: no call site, assertion or import is touched. Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292 files; a random 20-file sample runs green (quota-redis-store hangs identically on the untouched tree — it needs a Redis on localhost, an environment matter). The four unit shards on this PR are the full run. * fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff The gate shells out to `git diff` through execFileSync with Node's default 1 MB maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing anything. 64 MB is far above any real PR and costs nothing when unused.
312 lines
11 KiB
TypeScript
312 lines
11 KiB
TypeScript
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 { SignJWT } from "jose";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-clientapi-policy-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
process.env.API_KEY_SECRET = "test-secret";
|
|
|
|
const apiKeysDb = await import("../../../src/lib/db/apiKeys.ts");
|
|
const featureFlagsDb = await import("../../../src/lib/db/featureFlags.ts");
|
|
const core = await import("../../../src/lib/db/core.ts");
|
|
|
|
const ORIGINAL_OMNIROUTE_API_KEY = process.env.OMNIROUTE_API_KEY;
|
|
const ORIGINAL_ROUTER_API_KEY = process.env.ROUTER_API_KEY;
|
|
const ORIGINAL_JWT_SECRET = process.env.JWT_SECRET;
|
|
const ORIGINAL_REQUIRE_API_KEY = process.env.REQUIRE_API_KEY;
|
|
|
|
function resetStorage() {
|
|
core.resetDbInstance();
|
|
apiKeysDb.resetApiKeyState();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
delete process.env.OMNIROUTE_API_KEY;
|
|
delete process.env.ROUTER_API_KEY;
|
|
delete process.env.JWT_SECRET;
|
|
process.env.REQUIRE_API_KEY = "true";
|
|
}
|
|
|
|
test.beforeEach(() => {
|
|
resetStorage();
|
|
});
|
|
|
|
test.after(() => {
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
if (ORIGINAL_OMNIROUTE_API_KEY === undefined) delete process.env.OMNIROUTE_API_KEY;
|
|
else process.env.OMNIROUTE_API_KEY = ORIGINAL_OMNIROUTE_API_KEY;
|
|
if (ORIGINAL_ROUTER_API_KEY === undefined) delete process.env.ROUTER_API_KEY;
|
|
else process.env.ROUTER_API_KEY = ORIGINAL_ROUTER_API_KEY;
|
|
if (ORIGINAL_JWT_SECRET === undefined) delete process.env.JWT_SECRET;
|
|
else process.env.JWT_SECRET = ORIGINAL_JWT_SECRET;
|
|
if (ORIGINAL_REQUIRE_API_KEY === undefined) delete process.env.REQUIRE_API_KEY;
|
|
else process.env.REQUIRE_API_KEY = ORIGINAL_REQUIRE_API_KEY;
|
|
});
|
|
|
|
async function loadPolicy() {
|
|
const mod = await import(`../../../src/server/authz/policies/clientApi.ts?ts=${Date.now()}`);
|
|
return mod.clientApiPolicy;
|
|
}
|
|
|
|
function ctx(headers: Headers, method = "POST", normalizedPath = "/api/v1/chat/completions") {
|
|
const pathOnly = normalizedPath.split("?")[0];
|
|
return {
|
|
request: { method, headers, url: `http://localhost${normalizedPath}` },
|
|
classification: {
|
|
routeClass: "CLIENT_API" as const,
|
|
reason: "client_api_v1" as const,
|
|
normalizedPath: pathOnly,
|
|
},
|
|
requestId: "req_test",
|
|
};
|
|
}
|
|
|
|
function relativeUrlCtx(
|
|
headers: Headers,
|
|
method = "POST",
|
|
normalizedPath = "/api/v1/chat/completions"
|
|
) {
|
|
const pathOnly = normalizedPath.split("?")[0];
|
|
return {
|
|
request: { method, headers, url: normalizedPath },
|
|
classification: {
|
|
routeClass: "CLIENT_API" as const,
|
|
reason: "client_api_v1" as const,
|
|
normalizedPath: pathOnly,
|
|
},
|
|
requestId: "req_test",
|
|
};
|
|
}
|
|
|
|
async function dashboardCookie(): Promise<string> {
|
|
process.env.JWT_SECRET = "client-api-dashboard-jwt-secret";
|
|
const secret = new TextEncoder().encode(process.env.JWT_SECRET);
|
|
const token = await new SignJWT({ authenticated: true })
|
|
.setProtectedHeader({ alg: "HS256" })
|
|
.setExpirationTime("1h")
|
|
.sign(secret);
|
|
return `auth_token=${token}`;
|
|
}
|
|
|
|
test("clientApiPolicy: missing bearer is rejected with 401", async () => {
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(ctx(new Headers()));
|
|
assert.equal(out.allow, false);
|
|
if (!out.allow) {
|
|
assert.equal(out.status, 401);
|
|
assert.equal(out.code, "AUTH_002");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: websocket descriptor handshake can reach the route handler", async () => {
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(ctx(new Headers(), "GET", "/api/v1/ws?handshake=1"));
|
|
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "anonymous");
|
|
assert.equal(out.subject.id, "ws-handshake");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: websocket descriptor handshake accepts relative request URLs", async () => {
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(relativeUrlCtx(new Headers(), "GET", "/api/v1/ws?handshake=1"));
|
|
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "anonymous");
|
|
assert.equal(out.subject.id, "ws-handshake");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: REQUIRE_API_KEY DB feature flag override rejects anonymous", async () => {
|
|
process.env.REQUIRE_API_KEY = "false";
|
|
featureFlagsDb.setFeatureFlagOverride("REQUIRE_API_KEY", "true");
|
|
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(ctx(new Headers()));
|
|
assert.equal(out.allow, false);
|
|
if (!out.allow) {
|
|
assert.equal(out.status, 401);
|
|
assert.equal(out.code, "AUTH_002");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: REQUIRE_API_KEY DB feature flag override can disable env requirement", async () => {
|
|
process.env.REQUIRE_API_KEY = "true";
|
|
featureFlagsDb.setFeatureFlagOverride("REQUIRE_API_KEY", "false");
|
|
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(ctx(new Headers()));
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "anonymous");
|
|
assert.equal(out.subject.id, "local");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: dashboard session can read the model catalog without bearer", async () => {
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(
|
|
ctx(new Headers({ cookie: await dashboardCookie() }), "GET", "/api/v1/models")
|
|
);
|
|
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "dashboard_session");
|
|
assert.equal(out.subject.id, "dashboard");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: dashboard session is accepted for client API routes without bearer", async () => {
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(
|
|
ctx(new Headers({ cookie: await dashboardCookie() }), "POST", "/api/v1/chat/completions")
|
|
);
|
|
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "dashboard_session");
|
|
assert.equal(out.subject.id, "dashboard");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: invalid bearer is rejected with 401", async () => {
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({ authorization: "Bearer sk-totally-bogus" });
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, false);
|
|
if (!out.allow) {
|
|
assert.equal(out.status, 401);
|
|
assert.equal(out.code, "AUTH_002");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: valid bearer is accepted as client_api_key subject", async () => {
|
|
const created = await apiKeysDb.createApiKey("policy-test-key", "machine-test-1234");
|
|
assert.ok(created?.key, "createApiKey must return a key");
|
|
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({ authorization: `Bearer ${created.key}` });
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
assert.match(out.subject.id, /^key_/);
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: revoked bearer is rejected", async () => {
|
|
const created = await apiKeysDb.createApiKey("policy-revoked-key", "machine-revoked");
|
|
assert.ok(await apiKeysDb.revokeApiKey(created.id));
|
|
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({ authorization: `Bearer ${created.key}` });
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, false);
|
|
});
|
|
|
|
test("clientApiPolicy: environment API key remains accepted for client API routes", async () => {
|
|
process.env.OMNIROUTE_API_KEY = "sk-env-policy-test";
|
|
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(
|
|
ctx(new Headers({ authorization: "Bearer sk-env-policy-test" }))
|
|
);
|
|
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: x-api-key header is accepted as client_api_key subject", async () => {
|
|
const created = await apiKeysDb.createApiKey("policy-test-xkey", "machine-xkey-1234");
|
|
assert.ok(created?.key, "createApiKey must return a key");
|
|
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({ "x-api-key": created.key });
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
assert.match(out.subject.id, /^key_/);
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: x-goog-api-key header is accepted as client_api_key subject (#7034)", async () => {
|
|
const created = await apiKeysDb.createApiKey("policy-test-googkey", "machine-googkey-1234");
|
|
assert.ok(created?.key, "createApiKey must return a key");
|
|
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({ "x-goog-api-key": created.key });
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
assert.match(out.subject.id, /^key_/);
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: Authorization Bearer wins over x-goog-api-key when both present (#7034)", async () => {
|
|
const created = await apiKeysDb.createApiKey("policy-test-goog-precedence", "machine-goog-2345");
|
|
assert.ok(created?.key, "createApiKey must return a key");
|
|
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({
|
|
authorization: `Bearer ${created.key}`,
|
|
"x-goog-api-key": "sk-goog-should-lose",
|
|
});
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
assert.match(out.subject.id, /^key_/);
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: existing x-api-key still wins over x-goog-api-key when both present (#7034)", async () => {
|
|
const created = await apiKeysDb.createApiKey("policy-test-xkey-precedence", "machine-xkey-2345");
|
|
assert.ok(created?.key, "createApiKey must return a key");
|
|
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({
|
|
"x-api-key": created.key,
|
|
"x-goog-api-key": "sk-goog-should-lose",
|
|
});
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
assert.match(out.subject.id, /^key_/);
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: invalid x-goog-api-key is rejected with 401 AUTH_002 (#7034)", async () => {
|
|
const policy = await loadPolicy();
|
|
const headers = new Headers({ "x-goog-api-key": "sk-invalid-goog-key" });
|
|
const out = await policy.evaluate(ctx(headers));
|
|
assert.equal(out.allow, false);
|
|
if (!out.allow) {
|
|
assert.equal(out.status, 401);
|
|
assert.equal(out.code, "AUTH_002");
|
|
}
|
|
});
|
|
|
|
test("clientApiPolicy: ROUTER_API_KEY remains accepted for client API routes", async () => {
|
|
process.env.ROUTER_API_KEY = "sk-router-policy-test";
|
|
|
|
const policy = await loadPolicy();
|
|
const out = await policy.evaluate(
|
|
ctx(new Headers({ authorization: "Bearer sk-router-policy-test" }))
|
|
);
|
|
|
|
assert.equal(out.allow, true);
|
|
if (out.allow) {
|
|
assert.equal(out.subject.kind, "client_api_key");
|
|
}
|
|
});
|