Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
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 { fileURLToPath } from "node:url";
import {
getInternalServiceAuthHeaders,
INTERNAL_SERVICE_AUTH_HEADER,
isInternalServiceRequest,
isTrustedLoopbackInternalServiceRequest,
} from "../../src/lib/api/internalServiceAuth.ts";
import { AUTHZ_HEADER_PEER_LOCALITY } from "../../src/server/authz/headers.ts";
const originalInline = process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN;
const originalFile = process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN_FILE;
const repoRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "../..");
test.afterEach(() => {
if (originalInline === undefined) delete process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN;
else process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN = originalInline;
if (originalFile === undefined) delete process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN_FILE;
else process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN_FILE = originalFile;
});
test("internal service auth is disabled when no token is configured", () => {
delete process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN;
delete process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN_FILE;
assert.deepEqual(getInternalServiceAuthHeaders(), {});
assert.equal(isInternalServiceRequest(new Request("http://localhost")), false);
});
test("internal service auth preserves a separate constant-time token channel", () => {
process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN = "test-internal-token-0123456789";
delete process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN_FILE;
const headers = new Headers({
...getInternalServiceAuthHeaders(),
[AUTHZ_HEADER_PEER_LOCALITY]: "loopback",
});
const request = new Request("http://localhost", { headers });
assert.equal(headers.get(INTERNAL_SERVICE_AUTH_HEADER), "test-internal-token-0123456789");
assert.equal(isInternalServiceRequest(request), true);
assert.equal(isTrustedLoopbackInternalServiceRequest(request), true);
const remote = new Request("https://example.test", {
headers: {
[INTERNAL_SERVICE_AUTH_HEADER]: "test-internal-token-0123456789",
[AUTHZ_HEADER_PEER_LOCALITY]: "remote",
},
});
assert.equal(isTrustedLoopbackInternalServiceRequest(remote), false);
});
test("internal service token file is read without exposing it to process env", () => {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "omr-internal-auth-"));
const tokenFile = path.join(directory, "token");
try {
fs.writeFileSync(tokenFile, "file-backed-token-0123456789\n", { mode: 0o600 });
delete process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN;
process.env.OMNIROUTE_INTERNAL_SERVICE_TOKEN_FILE = tokenFile;
assert.deepEqual(getInternalServiceAuthHeaders(), {
[INTERNAL_SERVICE_AUTH_HEADER]: "file-backed-token-0123456789",
});
} finally {
fs.rmSync(directory, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("cache API accepts internal auth only through the trusted loopback verifier", () => {
const source = fs.readFileSync(path.join(repoRoot, "src/app/api/cache/route.ts"), "utf8");
// contract changed by #15430: a validated cache-scoped API key is a third,
// independent way in (read scope for GET, write scope for DELETE). The internal
// service token is still accepted ONLY through the trusted loopback verifier.
const trustedGuard =
/if \(\s*!isTrustedLoopbackInternalServiceRequest\(req\) &&\s*!\(await isAuthenticated\(req\)\)(?:\s*&&\s*!\(await isCacheScopedKey\(req, CACHE_(?:READ|WRITE)_SCOPE\)\))?\s*\)/g;
assert.equal(
[...source.matchAll(trustedGuard)].length,
2,
"GET and DELETE must accept the internal token only after loopback locality is verified"
);
assert.doesNotMatch(
source,
/isInternalServiceRequest\(req\)/,
"the cache route must not trust the service token without the loopback guard"
);
});