mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 10:52:17 +03:00
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.
222 lines
7.8 KiB
TypeScript
222 lines
7.8 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";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cloud-write-auth-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
|
|
process.env.JWT_SECRET = "cloud-write-auth-jwt";
|
|
process.env.INITIAL_PASSWORD = "bootstrap-password";
|
|
process.env.API_KEY_SECRET = "cloud-write-auth-api-key-secret";
|
|
|
|
type ApiKeyRecord = { key: string };
|
|
type ProviderConnectionRecord = {
|
|
id: string;
|
|
accessToken?: string | null;
|
|
refreshToken?: string | null;
|
|
expiresAt?: string | null;
|
|
};
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const localDb = await import("../../src/lib/localDb.ts");
|
|
const credentialsRoute = await import("../../src/app/api/cloud/credentials/update/route.ts");
|
|
const aliasRoute = await import("../../src/app/api/cloud/models/alias/route.ts");
|
|
|
|
async function resetStorage() {
|
|
delete process.env.OMNIROUTE_API_KEY;
|
|
delete process.env.ROUTER_API_KEY;
|
|
process.env.INITIAL_PASSWORD = "bootstrap-password";
|
|
process.env.JWT_SECRET = "cloud-write-auth-jwt";
|
|
process.env.API_KEY_SECRET = "cloud-write-auth-api-key-secret";
|
|
core.resetDbInstance();
|
|
localDb.resetApiKeyState();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
await localDb.updateSettings({ requireLogin: true, password: "" });
|
|
}
|
|
|
|
async function createKey(scopes: string[] = []): Promise<ApiKeyRecord> {
|
|
return localDb.createApiKey(`cloud-write-${scopes.join("-") || "none"}`, "machine-test", scopes);
|
|
}
|
|
|
|
async function createActiveConnection(): Promise<ProviderConnectionRecord> {
|
|
const connection = await localDb.createProviderConnection({
|
|
provider: "openai",
|
|
authType: "oauth",
|
|
name: "OpenAI OAuth",
|
|
email: "owner@example.test",
|
|
isActive: true,
|
|
accessToken: "old-access-token",
|
|
refreshToken: "old-refresh-token",
|
|
expiresAt: "2026-01-01T00:00:00.000Z",
|
|
});
|
|
assert.ok(connection?.id);
|
|
return connection as ProviderConnectionRecord;
|
|
}
|
|
|
|
async function readActiveConnection(): Promise<ProviderConnectionRecord> {
|
|
const [connection] = (await localDb.getProviderConnections({
|
|
provider: "openai",
|
|
isActive: true,
|
|
})) as ProviderConnectionRecord[];
|
|
assert.ok(connection);
|
|
return connection;
|
|
}
|
|
|
|
function credentialUpdateBody() {
|
|
return {
|
|
provider: "openai",
|
|
credentials: {
|
|
accessToken: "new-access-secret",
|
|
refreshToken: "new-refresh-secret",
|
|
expiresIn: 3600,
|
|
},
|
|
};
|
|
}
|
|
|
|
function aliasUpdateBody() {
|
|
return {
|
|
model: "openai/gpt-4o-mini",
|
|
alias: "fast-default",
|
|
};
|
|
}
|
|
|
|
function cloudCredentialsRequest(token: string | null, body = credentialUpdateBody()) {
|
|
const headers = new Headers({ "content-type": "application/json" });
|
|
if (token) headers.set("authorization", `Bearer ${token}`);
|
|
return new Request("http://localhost/api/cloud/credentials/update", {
|
|
method: "PUT",
|
|
headers,
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
function cloudAliasRequest(token: string | null, body = aliasUpdateBody()) {
|
|
const headers = new Headers({ "content-type": "application/json" });
|
|
if (token) headers.set("authorization", `Bearer ${token}`);
|
|
return new Request("http://localhost/api/cloud/models/alias", {
|
|
method: "PUT",
|
|
headers,
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
async function captureConsoleLog<T>(fn: () => Promise<T>): Promise<{ value: T; logs: string }> {
|
|
const originalLog = console.log;
|
|
const entries: string[] = [];
|
|
console.log = (...args: unknown[]) => {
|
|
entries.push(args.map((arg) => String(arg)).join(" "));
|
|
};
|
|
try {
|
|
return { value: await fn(), logs: entries.join("\n") };
|
|
} finally {
|
|
console.log = originalLog;
|
|
}
|
|
}
|
|
|
|
function assertTextDoesNotLeakSecrets(text: string, label: string, secrets: string[]) {
|
|
for (const secret of secrets) {
|
|
assert.equal(text.includes(secret), false, `${label} leaked secret: ${secret}`);
|
|
}
|
|
}
|
|
|
|
async function assertResponseDoesNotLeakSecrets(response: Response, secrets: string[]) {
|
|
const text = await response.text();
|
|
assertTextDoesNotLeakSecrets(text, "response", secrets);
|
|
return text.length > 0 ? JSON.parse(text) : null;
|
|
}
|
|
|
|
test.beforeEach(async () => {
|
|
await resetStorage();
|
|
});
|
|
|
|
test.after(async () => {
|
|
core.resetDbInstance();
|
|
localDb.resetApiKeyState();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
});
|
|
|
|
test("PUT /api/cloud/credentials/update rejects valid API key without manage/admin scope and leaves credentials unchanged", async () => {
|
|
await createActiveConnection();
|
|
const key = await createKey();
|
|
|
|
const { value: response, logs } = await captureConsoleLog(() =>
|
|
credentialsRoute.PUT(cloudCredentialsRequest(key.key))
|
|
);
|
|
const body = await assertResponseDoesNotLeakSecrets(response, [
|
|
"new-access-secret",
|
|
"new-refresh-secret",
|
|
]);
|
|
assertTextDoesNotLeakSecrets(logs, "logs", ["new-access-secret", "new-refresh-secret"]);
|
|
const connection = await readActiveConnection();
|
|
|
|
assert.equal(response.status, 403);
|
|
assert.match(body.error?.message || "", /manage/);
|
|
assert.equal(connection.accessToken, "old-access-token");
|
|
assert.equal(connection.refreshToken, "old-refresh-token");
|
|
assert.equal(connection.expiresAt, "2026-01-01T00:00:00.000Z");
|
|
});
|
|
|
|
test("PUT /api/cloud/models/alias rejects valid API key without manage/admin scope and leaves aliases unchanged", async () => {
|
|
await localDb.setModelAlias("fast-default", "openai/original-model");
|
|
const key = await createKey();
|
|
|
|
const { value: response, logs } = await captureConsoleLog(() =>
|
|
aliasRoute.PUT(cloudAliasRequest(key.key))
|
|
);
|
|
const body = await assertResponseDoesNotLeakSecrets(response, ["openai/gpt-4o-mini"]);
|
|
assertTextDoesNotLeakSecrets(logs, "logs", ["openai/gpt-4o-mini"]);
|
|
const aliases = await localDb.getModelAliases();
|
|
|
|
assert.equal(response.status, 403);
|
|
assert.match(body.error?.message || "", /manage/);
|
|
assert.equal(aliases["fast-default"], "openai/original-model");
|
|
});
|
|
|
|
test("PUT /api/cloud/credentials/update accepts API key with manage scope", async () => {
|
|
await createActiveConnection();
|
|
const key = await createKey(["manage"]);
|
|
|
|
const response = await credentialsRoute.PUT(cloudCredentialsRequest(key.key));
|
|
const body = await response.json();
|
|
const connection = await readActiveConnection();
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.success, true);
|
|
assert.equal(connection.accessToken, "new-access-secret");
|
|
assert.equal(connection.refreshToken, "new-refresh-secret");
|
|
assert.notEqual(connection.expiresAt, "2026-01-01T00:00:00.000Z");
|
|
});
|
|
|
|
test("PUT /api/cloud/models/alias accepts API key with manage scope", async () => {
|
|
const key = await createKey(["manage"]);
|
|
|
|
const response = await aliasRoute.PUT(cloudAliasRequest(key.key));
|
|
const body = await response.json();
|
|
const aliases = await localDb.getModelAliases();
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(body.success, true);
|
|
assert.equal(aliases["fast-default"], "openai/gpt-4o-mini");
|
|
});
|
|
|
|
test("cloud write routes keep 401 for missing or invalid Bearer credentials", async () => {
|
|
await createActiveConnection();
|
|
|
|
const { value: missing, logs: missingLogs } = await captureConsoleLog(() =>
|
|
credentialsRoute.PUT(cloudCredentialsRequest(null))
|
|
);
|
|
const { value: invalid, logs: invalidLogs } = await captureConsoleLog(() =>
|
|
aliasRoute.PUT(cloudAliasRequest("sk-invalid"))
|
|
);
|
|
await assertResponseDoesNotLeakSecrets(missing, ["new-access-secret", "new-refresh-secret"]);
|
|
await assertResponseDoesNotLeakSecrets(invalid, ["openai/gpt-4o-mini"]);
|
|
assertTextDoesNotLeakSecrets(missingLogs, "logs", ["new-access-secret", "new-refresh-secret"]);
|
|
assertTextDoesNotLeakSecrets(invalidLogs, "logs", ["openai/gpt-4o-mini"]);
|
|
|
|
assert.equal(missing.status, 401);
|
|
assert.equal(invalid.status, 401);
|
|
});
|