Files
OmniRoute/tests/unit/provider-models-route-lan-guard.test.ts
InkshadeWoods fd76271515 fix(providers): make upstream model sync opt-in and preserve manual overrides (#10603)
* fix(providers): make upstream model sync opt-in and preserve manual overrides

(cherry picked from commit 0a84f5496896a95856e834112b3d813fa1b87d38)

* test(providers): cover upstream model sync controls

* fix(providers): fix pre-existing tests broken by opt-in model sync + sync i18n keys

The upstream model auto-fetch opt-in default flip made 3 pre-existing tests
short-circuit before reaching the paths they exercise, because their
connection fixtures never set providerSpecificData.autoFetchModels: true:

- tests/unit/provider-models-route-lan-guard.test.ts (#6939 SSRF-guard tests)
- tests/unit/openrouter-embeddings-catalog-6976.test.ts (live discovery merge/dedup)
- tests/unit/provider-models-route.test.ts (Kimi Coding auth-header test —
  this was mislabeled as base/catalog drift during review, but is the same
  root cause: without autoFetchModels the mocked fetch is never reached and
  the route falls back to local catalog data instead)

Also syncs the 11 new providers.autoFetchModels*/overridesUpstreamModel*/
resetToUpstreamDefaults* i18n keys from en.json/zh-CN.json to the remaining
40 locale files via a narrowly-scoped ad-hoc translation script (only these
11 keys — leaves each locale's pre-existing, unrelated missing-key backlog
untouched).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(providers): fix remaining pre-existing tests broken by opt-in model sync

Rebase surfaced that the 'Kimi Coding' CI failure flagged as possible base
drift during review was actually the same root cause as the lan-guard and
openrouter-embeddings fixes: 29 pre-existing tests in
tests/unit/provider-models-route.test.ts (of 59 total) short-circuit under
the new autoFetchModels opt-in default because their connection fixtures
never set providerSpecificData.autoFetchModels: true, so they never reach
the live-fetch/validation paths they were written to exercise (fetch mocks
never called, base-URL validation never reached, live models never merged).

Adds providerSpecificData.autoFetchModels: true to each affected fixture.
No production code or test assertions changed — same TEST-fixture-only
pattern as the lan-guard and openrouter-embeddings fixes. All 59 tests in
the file now pass (was 30/59).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-08-18 10:58:40 -03:00

145 lines
6.2 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 });
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 });
});
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);
});