providers/oauth: sync models when a connection is created

A provider whose catalog is authoritative only advertises the models its
account returned on the last sync. A connection added through OAuth therefore
stayed active with an empty catalog until the six-hour scheduler reached it, and
every request for a model only that account has failed with "not available in
the active live catalog" until someone pressed sync by hand.

Both create paths now hand the connection to one shared trigger. Eligibility is
the scheduler's own predicate, so antigravity and agy connections qualify on
their default autoSync while API-key providers keep their explicit
autoFetchModels opt-in. The sync goes through the same loopback helper as the
scheduler, keeps the import semantics the API-key path had, and runs at most
once per connection per five minutes so a single sign-in cannot fire one sync
per path it touches.

Signed-off-by: Minxi Hou <houminxi@gmail.com>
This commit is contained in:
Minxi Hou
2026-11-19 14:27:15 -05:00
parent 6d87faf236
commit d5ea56822e
10 changed files with 637 additions and 68 deletions

View File

@@ -0,0 +1 @@
- **fix(providers):** A connection added through an OAuth sign-in now syncs its model catalog right away instead of waiting for the next six-hour auto-sync cycle, so the new account's models appear without pressing sync by hand. The trigger keeps the API-key create path's import semantics and runs at most once per connection per five minutes.

View File

@@ -31,6 +31,7 @@ import {
antigravityPersistStatus,
} from "@/lib/oauth/antigravityProjectGate";
import { syncToCloud } from "@/lib/cloudSync";
import { triggerAutoModelSync } from "@/lib/providerModels/autoModelSyncTrigger";
import { startLocalServer } from "@/lib/oauth/utils/server";
import { runWithProxyContextOrDirect } from "@omniroute/open-sse/utils/proxyFetch.ts";
import {
@@ -607,6 +608,9 @@ export async function POST(
}
// Auto sync to Cloud if enabled
// A connection added here has no catalog until its first sync; the
// scheduler only reaches it on the next six-hour tick.
triggerAutoModelSync(connection);
await syncToCloudIfEnabled();
return NextResponse.json({
@@ -699,6 +703,9 @@ export async function POST(
}
// Auto sync to Cloud if enabled
// A connection added here has no catalog until its first sync; the
// scheduler only reaches it on the next six-hour tick.
triggerAutoModelSync(connection);
await syncToCloudIfEnabled();
return NextResponse.json({
@@ -836,6 +843,9 @@ export async function POST(
);
}
// A connection added here has no catalog until its first sync; the
// scheduler only reaches it on the next six-hour tick.
triggerAutoModelSync(connection);
await syncToCloudIfEnabled();
return NextResponse.json({
@@ -920,6 +930,9 @@ export async function POST(
);
}
// A connection added here has no catalog until its first sync; the
// scheduler only reaches it on the next six-hour tick.
triggerAutoModelSync(connection);
await syncToCloudIfEnabled();
return NextResponse.json({

View File

@@ -55,11 +55,6 @@ import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { isManagedProviderConnectionId } from "@/lib/providers/catalog";
import { isApiKeyRevealEnabled, maskStoredApiKey } from "@/lib/apiKeyExposure";
import { cleanupProviderModelsAfterConnectionDelete } from "@/lib/db/models";
import {
buildModelSyncInternalHeaders,
fetchModelSyncInternal,
getModelSyncInternalBaseUrl,
} from "@/shared/services/modelSyncScheduler";
// Dynamically imported below, inside the one `provider === "chatgpt-web-codex"`
// branch that needs it: this module's transitive chain pulls in tiktoken's
// WASM tokenizer, which Turbopack dev mode fails to resolve for this graph
@@ -69,7 +64,7 @@ import {
// /api/providers request regardless of provider, turning an unrelated-
// provider bug into a route-wide 500. Loading it lazily, only when actually
// needed, avoids paying that cost (and that risk) on the common path.
import { isAutoFetchModelsEnabled } from "@/lib/providerModels/modelDiscovery";
import { triggerAutoModelSync } from "@/lib/providerModels/autoModelSyncTrigger";
import { testSingleConnection } from "./[id]/test/route";
import { rejectRetiredCommonChatGptWebProvider } from "@/lib/providers/chatgptWebRetirementResponse";
import {
@@ -434,54 +429,14 @@ export async function POST(request: Request) {
testStatus: testStatus || "unknown",
});
// Auto-trigger model discovery only for an explicit autoFetchModels opt-in.
// Fire-and-forget: model sync can take seconds and should NOT block the
// POST response. If it fails, we log and move on — the connection itself
// is already persisted and the user can manually trigger a sync later.
// We use a self-fetch against our own /sync-models route, forwarding the
// incoming cookies (preserves management auth) plus the internal sync
// auth header (defense in depth) and an X-Internal-Auto-Sync marker for
// log correlation. The dashboard skips this server-owned copy when it
// performs the same sync itself so it can render progress.
if (
isAutoFetchModelsEnabled(providerSpecificData) &&
request.headers.get("x-skip-model-sync") !== "true"
) {
try {
// SECURITY: use the trusted loopback/env-pinned origin, NOT
// `new URL(request.url).origin` — the latter comes from the client-
// controlled Host header, which would let a caller redirect this
// credential-bearing internal self-fetch to an arbitrary host
// (SSRF + internal-auth-header exfiltration; CodeQL js/request-forgery).
const internalOrigin = getModelSyncInternalBaseUrl();
const cookieHeader = request.headers.get("cookie") || "";
const syncHeaders: Record<string, string> = {
"Content-Type": "application/json",
"X-Internal-Auto-Sync": "true",
...(cookieHeader ? { cookie: cookieHeader } : {}),
...buildModelSyncInternalHeaders(),
};
const syncUrl = `${internalOrigin}/api/providers/${encodeURIComponent(newConnection.id)}/sync-models?mode=import`;
// Not awaited by the response, but tracked: graceful shutdown (and tests) wait for it.
runConnectionFollowUp(`Auto-sync error for ${newConnection.id}`, async () => {
const syncRes = await fetchModelSyncInternal(syncUrl, {
method: "POST",
headers: syncHeaders,
redirect: "error",
});
if (!syncRes.ok) {
console.log(`[providers] Auto-sync failed for ${newConnection.id}: ${syncRes.status}`);
}
});
} catch (syncSetupError) {
// Defensive: if URL parsing or header construction itself throws, do
// not let it break the (already successful) POST response.
console.log(
`[providers] Auto-sync setup failed for ${newConnection.id}:`,
syncSetupError?.message || syncSetupError
);
}
}
// Opt-in model discovery for the new connection: without it a connection
// added between scheduler ticks stays active with an empty catalog until
// the next six-hour cycle. Fire-and-forget; the helper logs failures and
// never touches the response.
triggerAutoModelSync(
{ id: newConnection.id, provider, providerSpecificData },
{ skipHeaderValue: request.headers.get("x-skip-model-sync"), mode: "import" }
);
// Auto-test the newly created connection so `testStatus` reflects reality
// shortly after creation instead of sitting at "unknown" until the

View File

@@ -0,0 +1,138 @@
/**
* Opt-in model discovery for a connection that was just created.
*
* A provider whose catalog is authoritative only advertises what the account
* returned on its last sync, so a connection added between scheduler ticks
* contributes nothing to /v1/models until the next six-hour cycle. The create
* paths call this so a connection that takes part in auto-sync refreshes right
* away instead of sitting active with an empty catalog.
*
* Eligibility is the scheduler's own predicate. `autoSync` is the field the
* provider handlers write at OAuth time (antigravity and agy default to it), so
* gating on autoFetchModels alone would make this a no-op for exactly the
* connections that need it. autoFetchModels stays honoured for API-key
* providers, where it is the only opt-in.
*
* Fire-and-forget by design: the sync takes seconds and must not delay the
* create response. The follow-up registry keeps shutdown and tests from racing
* it. The sync goes through the same loopback helper the scheduler and the
* reactive trigger use, so the origin stays pinned to the trusted internal base
* URL rather than anything derived from the request Host header.
*/
import {
getModelSyncInternalBaseUrl,
isAutoSyncEnabled,
syncConnectionModels,
} from "@/shared/services/modelSyncScheduler";
import { runConnectionFollowUp } from "@/lib/providers/connectionFollowUps";
import { isAutoFetchModelsEnabled } from "./modelDiscovery";
export type SyncConnectionModels = typeof syncConnectionModels;
export interface AutoModelSyncOptions {
/** Value of the caller's X-Skip-Model-Sync header, when it runs its own sync. */
skipHeaderValue?: string | null;
/** Test seam; defaults to the shared loopback sync. */
syncFn?: SyncConnectionModels;
/** Override for the per-connection window; 0 disables deduplication. */
dedupeWindowMs?: number;
/** `import` keeps the catalog-import semantics of the API-key create path. */
mode?: "import" | "sync";
}
/**
* One sync per connection per window.
*
* A single sign-in can reach the create paths more than once (exchange, poll,
* retry, a dashboard refresh), and every pass pulls the whole upstream catalog.
* The sync is idempotent, so a second pass seconds later only multiplies
* upstream /models traffic, and two concurrent passes can write the same
* connection from stale snapshots.
*/
const DEFAULT_DEDUPE_WINDOW_MS = 5 * 60_000;
const inFlight = new Set<string>();
/** Insertion order is time order, so expired entries sit at the front. */
const lastStartedAt = new Map<string, number>();
function pruneExpired(now: number, windowMs: number): void {
if (windowMs <= 0) {
lastStartedAt.clear();
return;
}
for (const [key, startedAt] of lastStartedAt) {
if (now - startedAt < windowMs) break;
lastStartedAt.delete(key);
}
}
function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
function readTarget(
connection: { id?: unknown; provider?: unknown } | null
): { id: string; provider: string } | null {
const id = typeof connection?.id === "string" ? connection.id : "";
const provider = typeof connection?.provider === "string" ? connection.provider : "";
return id && provider ? { id, provider } : null;
}
function takesPartInAutoSync(provider: string, providerSpecificData: unknown): boolean {
const data = asRecord(providerSpecificData);
return isAutoSyncEnabled(provider, data) || isAutoFetchModelsEnabled(data);
}
/** Claim the connection's slot, or return false when a sync is already due or running. */
function claimSyncSlot(id: string, windowMs: number): boolean {
const now = Date.now();
pruneExpired(now, windowMs);
if (inFlight.has(id)) return false;
if (windowMs > 0 && now - (lastStartedAt.get(id) ?? 0) < windowMs) return false;
inFlight.add(id);
lastStartedAt.set(id, now);
return true;
}
function startFollowUp(id: string, provider: string, options: AutoModelSyncOptions): void {
const syncFn = options.syncFn ?? syncConnectionModels;
const baseUrl = getModelSyncInternalBaseUrl();
runConnectionFollowUp(`Auto-sync error for ${id}`, () =>
Promise.resolve()
.then(() => syncFn(id, provider, baseUrl, { mode: options.mode }))
.finally(() => inFlight.delete(id))
);
}
/**
* Start a model sync for `connection` when it takes part in auto-sync.
*
* Returns true when a sync was started. Never throws: an error here must not
* turn an already successful create into a failure, and provider data is
* caller-supplied, so its accessors are evaluated inside the guard too.
*/
export function triggerAutoModelSync(
connection: { id?: unknown; provider?: unknown; providerSpecificData?: unknown } | null,
options: AutoModelSyncOptions = {}
): boolean {
try {
if (options.skipHeaderValue === "true") return false;
const target = readTarget(connection);
if (!target) return false;
if (!takesPartInAutoSync(target.provider, connection?.providerSpecificData)) return false;
if (!claimSyncSlot(target.id, options.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS)) {
return false;
}
startFollowUp(target.id, target.provider, options);
return true;
} catch (error) {
// Reading provider data or resolving the origin failed. The connection is
// already persisted, and the scheduler will still reach it on its own.
console.log(
"[providers] Auto-sync setup failed:",
(error as { message?: string } | null)?.message || error
);
return false;
}
}

View File

@@ -238,11 +238,15 @@ async function getAutoSyncConnections(): Promise<
export async function syncConnectionModels(
connectionId: string,
provider: string,
baseUrl: string
baseUrl: string,
options: { mode?: "import" | "sync" } = {}
): Promise<boolean> {
try {
// `mode=import` keeps the catalog-import semantics the API-key create path
// had before it moved to this shared helper.
const query = options.mode === "import" ? "?mode=import" : "";
const res = await fetchModelSyncInternal(
`${baseUrl}/api/providers/${connectionId}/sync-models`,
`${baseUrl}/api/providers/${connectionId}/sync-models${query}`,
{
method: "POST",
redirect: "error",

View File

@@ -0,0 +1,273 @@
// Behaviour of the opt-in model-sync trigger used by connection create paths.
//
// A provider whose catalog is authoritative only advertises what the account
// synced last, so a connection created between scheduler ticks stays active with
// no models until the next six-hour cycle. The trigger closes that window.
//
// Eligibility follows the scheduler's own predicate: antigravity and agy take
// part by default and write `autoSync` at OAuth time, so gating on
// autoFetchModels alone would skip exactly the connections that need this.
//
// The trigger also caps itself: one sync per connection per window, and never two
// at once. A single sign-in can reach the create paths several times, and each
// pass pulls the whole upstream catalog.
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-auto-model-sync-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const { triggerAutoModelSync } = await import(
"../../src/lib/providerModels/autoModelSyncTrigger.ts"
);
const { getModelSyncInternalBaseUrl } = await import(
"../../src/shared/services/modelSyncScheduler.ts"
);
const { waitForConnectionFollowUps, getPendingConnectionFollowUpCount } = await import(
"../../src/lib/providers/connectionFollowUps.ts"
);
test.after(async () => {
await waitForConnectionFollowUps(2000);
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
type Call = {
id: string;
provider: string;
baseUrl: string;
mode?: string;
};
function recorder(result: boolean | Error | Promise<boolean> = true) {
const calls: Call[] = [];
const syncFn = (async (
id: string,
provider: string,
baseUrl: string,
options: { mode?: string } = {}
) => {
calls.push({ id, provider, baseUrl, mode: options.mode });
if (result instanceof Error) throw result;
return result;
}) as never;
return { calls, syncFn };
}
/** Gating cases run with deduplication off so one case cannot mask the next. */
function call(
connection: Parameters<typeof triggerAutoModelSync>[0],
syncFn: unknown,
extra: Record<string, unknown> = {}
) {
return triggerAutoModelSync(connection, { syncFn: syncFn as never, dedupeWindowMs: 0, ...extra });
}
test("an agy connection syncs without any per-connection flag", async () => {
const { calls, syncFn } = recorder();
assert.equal(
call({ id: "conn-agy", provider: "agy", providerSpecificData: {} }, syncFn),
true,
"agy is in the scheduler's default set"
);
assert.equal(
call({ id: "conn-at", provider: "antigravity", providerSpecificData: null }, syncFn),
true
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 2);
});
test("an explicit autoSync:false is honoured even for agy", async () => {
const { calls, syncFn } = recorder();
assert.equal(
call({ id: "conn-off", provider: "agy", providerSpecificData: { autoSync: false } }, syncFn),
false
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 0);
});
test("autoFetchModels still opts an API-key connection in", async () => {
const { calls, syncFn } = recorder();
assert.equal(
call(
{ id: "conn-key", provider: "openai", providerSpecificData: { autoFetchModels: true } },
syncFn
),
true
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 1);
});
test("a provider outside the default set with no flag does not sync", async () => {
const { calls, syncFn } = recorder();
for (const providerSpecificData of [{}, null, { autoFetchModels: "true" }, { autoSync: "yes" }]) {
assert.equal(call({ id: "conn-x", provider: "openai", providerSpecificData }, syncFn), false);
}
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 0);
});
test("X-Skip-Model-Sync suppresses the trigger", async () => {
const { calls, syncFn } = recorder();
assert.equal(
call({ id: "conn-skip", provider: "agy", providerSpecificData: {} }, syncFn, {
skipHeaderValue: "true",
}),
false
);
assert.equal(
call({ id: "conn-run", provider: "agy", providerSpecificData: {} }, syncFn, {
skipHeaderValue: "false",
}),
true,
"only the literal true skips"
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 1);
});
test("a hostile options getter is swallowed by the guard", async () => {
const { calls } = recorder();
const hostileOptions = {
dedupeWindowMs: 0,
get skipHeaderValue(): string {
throw new Error("boom from the options getter");
},
};
assert.equal(
triggerAutoModelSync({ id: "conn-opt", provider: "agy" }, hostileOptions as never),
false,
"must not throw"
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 0);
});
test("a missing id or provider is ignored", async () => {
const { calls, syncFn } = recorder();
assert.equal(triggerAutoModelSync(null, { syncFn, dedupeWindowMs: 0 }), false);
assert.equal(call({ provider: "agy" }, syncFn), false);
assert.equal(call({ id: "conn-y" }, syncFn), false);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 0);
});
test("the sync uses the pinned internal origin, not anything request-derived", async () => {
const { calls, syncFn } = recorder();
call({ id: "conn-url", provider: "agy" }, syncFn);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 1);
assert.equal(calls[0].id, "conn-url");
assert.equal(calls[0].provider, "agy");
assert.equal(calls[0].baseUrl, getModelSyncInternalBaseUrl());
assert.equal(calls[0].mode, undefined, "the scheduler path keeps its default mode");
assert.match(getModelSyncInternalBaseUrl(), /^http:\/\/127\.0\.0\.1:\d+$/);
});
test("a throwing provider-data accessor is swallowed by the guard", async () => {
const { calls, syncFn } = recorder();
const hostile = {
id: "conn-hostile",
provider: "agy",
get providerSpecificData(): unknown {
throw new Error("boom from a getter");
},
};
assert.equal(call(hostile, syncFn), false, "must not throw");
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 0);
});
test("one sync per connection inside the default dedupe window", async () => {
const { calls, syncFn } = recorder();
assert.equal(triggerAutoModelSync({ id: "conn-dup", provider: "agy" }, { syncFn }), true);
await waitForConnectionFollowUps(2000);
assert.equal(
triggerAutoModelSync({ id: "conn-dup", provider: "agy" }, { syncFn }),
false,
"a second create event inside the default window must not sync again, even after the first settled"
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 1);
assert.equal(
triggerAutoModelSync({ id: "conn-other", provider: "agy" }, { syncFn }),
true,
"the window is per connection, not global"
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 2);
});
test("the window expires instead of latching for the life of the process", async () => {
const { calls, syncFn } = recorder();
assert.equal(
triggerAutoModelSync({ id: "conn-exp", provider: "agy" }, { syncFn, dedupeWindowMs: 25 }),
true
);
await waitForConnectionFollowUps(2000);
await new Promise((resolve) => setTimeout(resolve, 60));
assert.equal(
triggerAutoModelSync({ id: "conn-exp", provider: "agy" }, { syncFn, dedupeWindowMs: 25 }),
true,
"an expired entry must allow the next sync"
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 2);
});
test("no second sync while the first is still running", async () => {
let release: (value: boolean) => void = () => {};
const gate = new Promise<boolean>((resolve) => {
release = resolve;
});
const { calls, syncFn } = recorder(gate);
assert.equal(
triggerAutoModelSync({ id: "conn-busy", provider: "agy" }, { syncFn, dedupeWindowMs: 0 }),
true
);
assert.equal(
triggerAutoModelSync({ id: "conn-busy", provider: "agy" }, { syncFn, dedupeWindowMs: 0 }),
false,
"in-flight syncs are deduplicated regardless of the window"
);
release(true);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 1);
assert.equal(
triggerAutoModelSync({ id: "conn-busy", provider: "agy" }, { syncFn, dedupeWindowMs: 0 }),
true,
"the connection is usable again once the sync settles"
);
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 2);
});
test("the caller's sync mode is forwarded", async () => {
const { calls, syncFn } = recorder();
call({ id: "conn-import", provider: "agy" }, syncFn, { mode: "import" });
await waitForConnectionFollowUps(2000);
assert.equal(calls.length, 1);
assert.equal(calls[0].mode, "import", "the API-key create path relies on import semantics");
});
test("a rejected sync is swallowed and leaves no pending follow-up", async () => {
const { syncFn } = recorder(new Error("connect ECONNREFUSED 127.0.0.1:20128"));
assert.equal(call({ id: "conn-rej", provider: "agy" }, syncFn), true);
assert.equal(await waitForConnectionFollowUps(2000), true);
assert.equal(getPendingConnectionFollowUpCount(), 0);
});
test("a false sync result does not throw into the caller", async () => {
const { syncFn } = recorder(false);
assert.equal(call({ id: "conn-false", provider: "agy" }, syncFn), true);
assert.equal(await waitForConnectionFollowUps(2000), true);
});

View File

@@ -0,0 +1,148 @@
// Structural guard: every connection the OAuth route creates must start a model
// sync, and the call has to receive the connection object itself.
//
// The behaviour lives in triggerAutoModelSync (covered by
// auto-model-sync-trigger.test.ts). What is easy to lose is the call: a new
// OAuth branch that persists a connection and forgets the trigger leaves the
// account active with an empty catalog until the scheduler's next six-hour tick,
// which is silent from the operator's side. This walks the real AST instead of
// matching text, so renames and reformatting do not weaken it.
//
// Two shapes are rejected on purpose, because both were green in an earlier
// revision: a trigger nested inside some extra condition, and a trigger handed
// something other than the connection that was just persisted.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import ts from "typescript";
import { settleConnectionFollowUpsAfterEachTest } from "../helpers/connectionFollowUps.ts";
settleConnectionFollowUpsAfterEachTest();
const ROOT = process.cwd();
function parse(relativePath: string) {
const fullPath = path.join(ROOT, relativePath);
const text = fs.readFileSync(fullPath, "utf8");
return { fullPath, sourceFile: ts.createSourceFile(fullPath, text, ts.ScriptTarget.Latest, true) };
}
const oauthRoute = parse("src/app/api/oauth/[provider]/[action]/route.ts");
const providersRoute = parse("src/app/api/providers/route.ts");
function calleeName(node: ts.CallExpression): string | null {
const expression = node.expression;
if (ts.isIdentifier(expression)) return expression.text;
if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.name)) {
return expression.name.text;
}
return null;
}
function collectCalls(root: ts.Node, name: string): ts.CallExpression[] {
const found: ts.CallExpression[] = [];
const visit = (node: ts.Node) => {
if (ts.isCallExpression(node) && calleeName(node) === name) found.push(node);
ts.forEachChild(node, visit);
};
visit(root);
return found;
}
function enclosingBlocks(node: ts.Node): ts.Block[] {
const blocks: ts.Block[] = [];
let current: ts.Node | undefined = node.parent;
while (current) {
if (ts.isBlock(current)) blocks.push(current);
current = current.parent;
}
return blocks;
}
function lineOf(sourceFile: ts.SourceFile, node: ts.Node): number {
return sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
}
/** True when `call` is a plain statement of `block`, not nested in a condition. */
function isDirectStatementOf(call: ts.CallExpression, block: ts.Block): boolean {
const statement = call.parent;
return ts.isExpressionStatement(statement) && statement.parent === block;
}
/** The first argument, as written. */
function firstArgumentText(sourceFile: ts.SourceFile, call: ts.CallExpression): string {
const [first] = call.arguments;
return first ? first.getText(sourceFile) : "";
}
test("the OAuth route still has connection creation sites to check", () => {
const createSites = collectCalls(oauthRoute.sourceFile, "createProviderConnection");
const syncSites = collectCalls(oauthRoute.sourceFile, "syncToCloudIfEnabled");
assert.ok(createSites.length >= 1, "expected at least one createProviderConnection call");
assert.ok(syncSites.length >= createSites.length, "every creation path also syncs to cloud");
});
test("each created OAuth connection starts a model sync as a plain statement", () => {
const createSites = collectCalls(oauthRoute.sourceFile, "createProviderConnection");
for (const createCall of createSites) {
const line = lineOf(oauthRoute.sourceFile, createCall);
const block = enclosingBlocks(createCall).find(
(candidate) => collectCalls(candidate, "syncToCloudIfEnabled").length > 0
);
assert.ok(
block,
`createProviderConnection at route.ts:${line} has no sibling cloud-sync block to anchor the check`
);
const triggers = collectCalls(block as ts.Block, "triggerAutoModelSync");
assert.equal(
triggers.length,
1,
`createProviderConnection at route.ts:${line} must call triggerAutoModelSync once in the same path`
);
assert.ok(
isDirectStatementOf(triggers[0], block as ts.Block),
`triggerAutoModelSync at route.ts:${lineOf(oauthRoute.sourceFile, triggers[0])} must be a plain statement of that path, not nested in another condition`
);
assert.equal(
firstArgumentText(oauthRoute.sourceFile, triggers[0]),
"connection",
`triggerAutoModelSync at route.ts:${lineOf(oauthRoute.sourceFile, triggers[0])} must receive the connection it just persisted`
);
}
});
test("the API-key create path hands the trigger the connection it just persisted", () => {
const createSites = collectCalls(providersRoute.sourceFile, "createProviderConnection");
const triggers = collectCalls(providersRoute.sourceFile, "triggerAutoModelSync");
assert.ok(createSites.length >= 1, "expected the API-key create path to keep its create call");
assert.equal(triggers.length, createSites.length, "one trigger per create site");
for (const trigger of triggers) {
const line = lineOf(providersRoute.sourceFile, trigger);
const argument = firstArgumentText(providersRoute.sourceFile, trigger);
assert.match(argument, /newConnection\.id/, `providers/route.ts:${line} must pass the new id`);
assert.match(argument, /provider\b/, `providers/route.ts:${line} must pass the provider`);
const optionsText = trigger.arguments[1]?.getText(providersRoute.sourceFile) ?? "";
assert.match(
optionsText,
/skipHeaderValue/,
`providers/route.ts:${line} must forward the caller's skip signal instead of syncing over it`
);
assert.match(
optionsText,
/mode:\s*["']import["']/,
`providers/route.ts:${line} must keep the import semantics the create path had`
);
const block = enclosingBlocks(trigger)[0];
assert.ok(
block && collectCalls(block, "createProviderConnection").length >= 1,
`providers/route.ts:${line} must sit in the path that creates the connection`
);
assert.ok(
isDirectStatementOf(trigger, block as ts.Block),
`providers/route.ts:${line} must be a plain statement of that path, not nested in another condition`
);
}
});

View File

@@ -233,11 +233,16 @@ test("ghe-copilot post-login lookups do not follow a redirect from the GHE host"
if (init?.redirect === "manual") {
return new Response(null, { status: 303, headers: { location: METADATA_URL } });
}
requestedUrls.push(METADATA_URL);
return new Response(JSON.stringify({ login: METADATA_SECRET }), {
status: 200,
headers: { "content-type": "application/json" },
});
if (url === METADATA_URL) {
requestedUrls.push(METADATA_URL);
return new Response(JSON.stringify({ login: METADATA_SECRET }), {
status: 200,
headers: { "content-type": "application/json" },
});
}
// Anything else (the internal model-sync loopback, for instance) is not a
// GHE lookup. Answer it without routing it through the metadata path.
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
}) as typeof fetch;
const res = await poll("https://ghe.example.test");

View File

@@ -25,7 +25,16 @@ const originalFetch = globalThis.fetch;
function stubAnthropic(): boolean[] {
const inProxyContext: boolean[] = [];
globalThis.fetch = (async () => {
globalThis.fetch = (async (input: RequestInfo | URL) => {
const url = String(input instanceof Request ? input.url : input);
// The create path also fires an internal loopback model sync. That call is
// not an upstream verification, so it must not be counted as one.
if (/^https?:\/\/(127\.0\.0\.1|localhost)(:|\/)/.test(url)) {
return new Response(JSON.stringify({ syncedModels: 0 }), {
status: 200,
headers: { "content-type": "application/json" },
});
}
inProxyContext.push(hasAmbientProxyContext());
return new Response(
JSON.stringify({ id: "msg_test", type: "message", role: "assistant", content: [] }),

View File

@@ -45,10 +45,23 @@ const profileCatalogSrc = readFileSync(
"utf8"
);
const autoSyncHelperSrc = readFileSync(
join(import.meta.dirname, "../../src/lib/providerModels/autoModelSyncTrigger.ts"),
"utf8"
);
const modelSyncSchedulerSrc = readFileSync(
join(import.meta.dirname, "../../src/shared/services/modelSyncScheduler.ts"),
"utf8"
);
test("POST /api/providers auto-sync uses the trusted internal origin (not request.url) — #323", () => {
assert.ok(
routeSrc.includes("getModelSyncInternalBaseUrl()"),
"auto-sync self-fetch must derive its origin from getModelSyncInternalBaseUrl()"
// The route no longer builds the self-fetch itself; it hands the connection to
// the shared trigger. The origin guarantee therefore has to be checked where
// the URL is now built, not only where it used to be.
assert.match(
routeSrc,
/triggerAutoModelSync\(/,
"the create path must delegate its auto-sync to the shared trigger"
);
assert.doesNotMatch(
routeSrc,
@@ -56,9 +69,19 @@ test("POST /api/providers auto-sync uses the trusted internal origin (not reques
"auto-sync origin must NOT be derived from the client-controlled request.url/Host (SSRF, CodeQL js/request-forgery #323)"
);
assert.match(
routeSrc,
/fetchModelSyncInternal\(syncUrl,\s*\{[^}]*redirect:\s*["']error["']/s,
"credential-bearing auto-sync self-fetch must reject redirects"
autoSyncHelperSrc,
/getModelSyncInternalBaseUrl\(\)/,
"the trigger must take its origin from the trusted internal base URL"
);
assert.doesNotMatch(
autoSyncHelperSrc,
/request\s*\.\s*(url|headers)/,
"the trigger must not read the incoming request at all"
);
assert.match(
modelSyncSchedulerSrc,
/redirect:\s*["']error["']/,
"the credential-bearing auto-sync self-fetch must reject redirects"
);
});