Files
OmniRoute/tests/unit/image-generation-route-auth.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* 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.
2026-08-29 01:17:40 -03:00

346 lines
12 KiB
TypeScript

/**
* Auth + call-log attribution contract for the image generation routes.
*
* Split out of image-generation-route.test.ts to stay under the 800-line
* new-test-file cap enforced by `npm run check:file-size`.
*/
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";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-image-route-auth-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "image-route-test-api-key-secret";
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const { getCallLogs } = await import("../../src/lib/usage/callLogs.ts");
const imageRoute = await import("../../src/app/api/v1/images/generations/route.ts");
const providerImageRoute =
await import("../../src/app/api/v1/providers/[provider]/images/generations/route.ts");
const v1ModelsCatalog = await import("../../src/app/api/v1/models/catalog.ts");
const originalFetch = globalThis.fetch;
async function resetStorage() {
globalThis.fetch = originalFetch;
apiKeysDb.resetApiKeyState();
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
v1ModelsCatalog.__resetCatalogBuilderRunsForTest();
}
async function seedConnection(provider: string, apiKey: string) {
return providersDb.createProviderConnection({
provider,
authType: "apikey",
name: `${provider}-${Math.random().toString(16).slice(2, 8)}`,
apiKey,
isActive: true,
testStatus: "active",
providerSpecificData: {},
});
}
async function waitForCallLog(apiKeyId: string, timeoutMs = 2000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const logs = await getCallLogs({ apiKey: apiKeyId, limit: 5 });
const match = logs.find((log: { apiKeyId?: string | null }) => log.apiKeyId === apiKeyId);
if (match) return match;
await new Promise((resolve) => setTimeout(resolve, 25));
}
return null;
}
async function readErrorMessage(response: Response): Promise<string> {
const body = (await response.json()) as { error?: { message?: unknown } };
return typeof body.error?.message === "string" ? body.error.message : "";
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(() => {
globalThis.fetch = originalFetch;
apiKeysDb.resetApiKeyState();
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("v1 image generation POST requires an API key when REQUIRE_API_KEY is enabled", async () => {
const originalRequireApiKey = process.env.REQUIRE_API_KEY;
process.env.REQUIRE_API_KEY = "true";
try {
const response = await imageRoute.POST(
new Request("http://localhost/api/v1/images/generations", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "openai/gpt-image-2",
prompt: "authentication test",
}),
})
);
assert.equal(response.status, 401);
assert.match(await readErrorMessage(response), /Authentication required/);
} finally {
if (originalRequireApiKey === undefined) {
delete process.env.REQUIRE_API_KEY;
} else {
process.env.REQUIRE_API_KEY = originalRequireApiKey;
}
}
});
test("v1 image generation POST rejects an invalid presented API key", async () => {
const originalOmniRouteApiKey = process.env.OMNIROUTE_API_KEY;
const originalRequireApiKey = process.env.REQUIRE_API_KEY;
process.env.OMNIROUTE_API_KEY = "valid-image-route-key";
process.env.REQUIRE_API_KEY = "true";
try {
const response = await imageRoute.POST(
new Request("http://localhost/api/v1/images/generations", {
method: "POST",
headers: {
Authorization: "Bearer invalid-image-route-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/gpt-image-2",
prompt: "invalid authentication test",
}),
})
);
assert.equal(response.status, 401);
assert.match(await readErrorMessage(response), /Invalid API key/);
} finally {
if (originalOmniRouteApiKey === undefined) {
delete process.env.OMNIROUTE_API_KEY;
} else {
process.env.OMNIROUTE_API_KEY = originalOmniRouteApiKey;
}
if (originalRequireApiKey === undefined) {
delete process.env.REQUIRE_API_KEY;
} else {
process.env.REQUIRE_API_KEY = originalRequireApiKey;
}
}
});
// Issue #2257: with enforcement off, a stale key in a CLI config must degrade to
// anonymous exactly like clientApiPolicy does — the route guard must not be
// stricter than the middleware that already fronts it.
test("v1 image generation POST ignores an invalid presented key while REQUIRE_API_KEY is off", async () => {
const originalOmniRouteApiKey = process.env.OMNIROUTE_API_KEY;
const originalRequireApiKey = process.env.REQUIRE_API_KEY;
process.env.OMNIROUTE_API_KEY = "valid-image-route-key";
process.env.REQUIRE_API_KEY = "false";
globalThis.fetch = async (url) => {
assert.equal(String(url), "http://localhost:7860/sdapi/v1/txt2img");
return new Response(JSON.stringify({ images: ["YW5vbnltb3Vz"] }), {
status: 200,
headers: { "content-type": "application/json" },
});
};
try {
const response = await imageRoute.POST(
new Request("http://localhost/api/v1/images/generations", {
method: "POST",
headers: {
Authorization: "Bearer stale-cli-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "sdwebui/stable-diffusion-v1-5",
prompt: "stale key degrades to anonymous",
}),
})
);
assert.equal(response.status, 200);
} finally {
if (originalOmniRouteApiKey === undefined) {
delete process.env.OMNIROUTE_API_KEY;
} else {
process.env.OMNIROUTE_API_KEY = originalOmniRouteApiKey;
}
if (originalRequireApiKey === undefined) {
delete process.env.REQUIRE_API_KEY;
} else {
process.env.REQUIRE_API_KEY = originalRequireApiKey;
}
}
});
// The dashboard Media page and Playground call these routes with a session
// cookie and no Bearer. clientApiPolicy admits them; the route guard must too.
test("v1 image generation POST accepts a dashboard session when REQUIRE_API_KEY is enabled", async () => {
const originalRequireApiKey = process.env.REQUIRE_API_KEY;
const originalJwtSecret = process.env.JWT_SECRET;
process.env.REQUIRE_API_KEY = "true";
process.env.JWT_SECRET = "image-route-dashboard-session-secret";
globalThis.fetch = async (url) => {
assert.equal(String(url), "http://localhost:7860/sdapi/v1/txt2img");
return new Response(JSON.stringify({ images: ["ZGFzaGJvYXJk"] }), {
status: 200,
headers: { "content-type": "application/json" },
});
};
try {
const { SignJWT } = await import("jose");
const token = await new SignJWT({ sub: "dashboard" })
.setProtectedHeader({ alg: "HS256" })
.setExpirationTime("1h")
.sign(new TextEncoder().encode(process.env.JWT_SECRET));
const response = await imageRoute.POST(
new Request("http://localhost/api/v1/images/generations", {
method: "POST",
headers: {
Cookie: `auth_token=${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "sdwebui/stable-diffusion-v1-5",
prompt: "dashboard session test",
}),
})
);
assert.equal(response.status, 200);
} finally {
if (originalRequireApiKey === undefined) {
delete process.env.REQUIRE_API_KEY;
} else {
process.env.REQUIRE_API_KEY = originalRequireApiKey;
}
if (originalJwtSecret === undefined) {
delete process.env.JWT_SECRET;
} else {
process.env.JWT_SECRET = originalJwtSecret;
}
}
});
test("v1 image generation POST attributes its call log to the validated API key", async () => {
const createdKey = await apiKeysDb.createApiKey(
"Image generation caller",
"machine-image-generation"
);
await seedConnection("openai", "image-provider-key");
globalThis.fetch = async (url) => {
assert.equal(String(url), "https://api.openai.com/v1/images/generations");
return new Response(
JSON.stringify({
created: 123,
data: [{ url: "https://cdn.example.com/attributed-image.png" }],
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
};
const response = await imageRoute.POST(
new Request("http://localhost/api/v1/images/generations", {
method: "POST",
headers: {
Authorization: `Bearer ${createdKey.key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/gpt-image-2",
prompt: "call log attribution test",
}),
})
);
assert.equal(response.status, 200);
const logged = await waitForCallLog(createdKey.id);
assert.ok(logged, "expected an attributed image-generation call log");
assert.equal(logged.apiKeyId, createdKey.id);
assert.equal(logged.apiKeyName, "Image generation caller");
});
test("provider-scoped image generation requires an API key when configured", async () => {
const originalRequireApiKey = process.env.REQUIRE_API_KEY;
process.env.REQUIRE_API_KEY = "true";
try {
const response = await providerImageRoute.POST(
new Request("http://localhost/api/v1/providers/openai/images/generations", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "gpt-image-2",
prompt: "provider-scoped authentication test",
}),
}),
{ params: Promise.resolve({ provider: "openai" }) }
);
assert.equal(response.status, 401);
assert.match(await readErrorMessage(response), /Authentication required/);
} finally {
if (originalRequireApiKey === undefined) {
delete process.env.REQUIRE_API_KEY;
} else {
process.env.REQUIRE_API_KEY = originalRequireApiKey;
}
}
});
test("provider-scoped image generation attributes its call log to the validated API key", async () => {
const createdKey = await apiKeysDb.createApiKey(
"Provider-scoped image caller",
"machine-provider-image"
);
await seedConnection("openai", "provider-scoped-upstream-key");
globalThis.fetch = async (url) => {
assert.equal(String(url), "https://api.openai.com/v1/images/generations");
return new Response(
JSON.stringify({
created: 456,
data: [{ url: "https://cdn.example.com/provider-scoped-image.png" }],
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
};
const response = await providerImageRoute.POST(
new Request("http://localhost/api/v1/providers/openai/images/generations", {
method: "POST",
headers: {
Authorization: `Bearer ${createdKey.key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-image-2",
prompt: "provider-scoped attribution test",
}),
}),
{ params: Promise.resolve({ provider: "openai" }) }
);
assert.equal(response.status, 200);
const logged = await waitForCallLog(createdKey.id);
assert.ok(logged, "expected an attributed provider-scoped image-generation call log");
assert.equal(logged.apiKeyId, createdKey.id);
assert.equal(logged.apiKeyName, "Provider-scoped image caller");
});