Files
OmniRoute/tests/unit/provider-models-route-lan-guard.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

145 lines
6.3 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";
// #6939 — model-list fetch for a LAN-local OpenAI-compatible provider (e.g. LM Studio on
// 192.168.x.x) was rejected by the SSRF guard even though the connection test for the same
// host succeeds, under the default (local-first) settings. Root cause: the models route used
// getProviderOutboundGuard() (only "none" | "public-only", never consults the local-first
// default) instead of getProviderValidationGuard() (used by the test-connection path, which
// resolves to "block-metadata" — allow LAN, still block cloud-metadata/link-local — when
// local-first is ON, which is the default).
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-lan-guard-models-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const providerModelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");
const outboundUrlGuard = await import("../../src/shared/network/outboundUrlGuard.ts");
const outboundUrlGuardPolicy = await import("../../src/shared/network/outboundUrlGuardPolicy.ts");
const originalFetch = globalThis.fetch;
const originalAllowPrivateProviderUrls = process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
const originalAllowLocalProviderUrls = process.env.OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS;
async function resetStorage() {
globalThis.fetch = originalFetch;
if (originalAllowPrivateProviderUrls === undefined) {
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
} else {
process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS = originalAllowPrivateProviderUrls;
}
if (originalAllowLocalProviderUrls === undefined) {
delete process.env.OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS;
} else {
process.env.OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS = originalAllowLocalProviderUrls;
}
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
async function seedConnection(provider: string, overrides: Record<string, unknown> = {}) {
return providersDb.createProviderConnection({
provider,
authType: (overrides.authType as string) || "apikey",
name: (overrides.name as string) || `${provider}-${Math.random().toString(16).slice(2, 8)}`,
apiKey: overrides.apiKey as string | undefined,
accessToken: overrides.accessToken as string | undefined,
isActive: (overrides.isActive as boolean) ?? true,
testStatus: (overrides.testStatus as string) || "active",
providerSpecificData: (overrides.providerSpecificData as Record<string, unknown>) || {},
});
}
async function callRoute(connectionId: string, search = "") {
return providerModelsRoute.GET(
new Request(`http://localhost/api/providers/${connectionId}/models${search}`),
{ params: { id: connectionId } }
);
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
globalThis.fetch = originalFetch;
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("#6939: getProviderOutboundGuard() and getProviderValidationGuard() agree for LAN hosts under the default local-first setting", () => {
// Default settings: OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS unset (ON by default),
// OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS unset (OFF by default).
delete process.env.OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS;
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
const validationGuard = outboundUrlGuardPolicy.getProviderValidationGuard();
assert.equal(validationGuard, "block-metadata");
// The models route must resolve a guard for LAN-local model discovery that is at least as
// permissive as the validation (test-connection) guard — "public-only" would reject LAN.
assert.notEqual(
validationGuard,
"public-only",
"sanity: validation guard should allow LAN under the local-first default"
);
});
test("#6939: LM Studio (LAN host, local OpenAI-compatible provider) model-list fetch is not SSRF-blocked under default settings", async () => {
delete process.env.OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS;
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
const connection = await seedConnection("lm-studio", {
providerSpecificData: { baseUrl: "http://192.168.1.50:1234/v1", autoFetchModels: true },
});
let fetchCalled = false;
globalThis.fetch = async () => {
fetchCalled = true;
return Response.json({ data: [{ id: "llama-3-8b-instruct" }] });
};
const response = await callRoute(connection.id);
const body = (await response.json()) as Record<string, unknown>;
// RED before the fix: the guard blocked the LAN host before the fetch mock ever ran,
// surfacing a 400 "Blocked private or local provider URL" — even though the equivalent
// test-connection call for the same host succeeds under the same default settings.
assert.ok(
fetchCalled,
`expected the LAN model-list fetch to reach the network layer, got status=${response.status} body=${JSON.stringify(body)}`
);
assert.notEqual(response.status, 400);
assert.ok(
!String(body?.error || "").includes(outboundUrlGuard.PROVIDER_URL_BLOCKED_MESSAGE),
`LAN host must not be SSRF-blocked by default: ${JSON.stringify(body)}`
);
});
test("#6939: LAN model-list fetch is still blocked when the local-first default is explicitly disabled", async () => {
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
process.env.OMNIROUTE_ALLOW_LOCAL_PROVIDER_URLS = "false";
const connection = await seedConnection("lm-studio", {
providerSpecificData: { baseUrl: "http://192.168.1.50:1234/v1", autoFetchModels: true },
});
let fetchCalled = false;
globalThis.fetch = async () => {
fetchCalled = true;
return Response.json({ data: [{ id: "llama-3-8b-instruct" }] });
};
const response = await callRoute(connection.id);
const body = (await response.json()) as Record<string, unknown>;
assert.equal(fetchCalled, false, "guard should block before the network layer is reached");
assert.equal(response.status, 400);
assert.equal(body.error, outboundUrlGuard.PROVIDER_URL_BLOCKED_MESSAGE);
});