Skip to content

Commit 830ab16

Browse files
authored
fix(openrouter): isolate custom proxy credentials and transport security (#118773)
* fix(openrouter): confine custom endpoint credentials and transport policy * test(openrouter): preserve typed fetch arguments in proxy fixtures
1 parent 616b0c9 commit 830ab16

8 files changed

Lines changed: 856 additions & 28 deletions

File tree

‎extensions/openrouter/index.test.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ vi.mock("openclaw/plugin-sdk/provider-stream-family", async (importOriginal) =>
3030
});
3131

3232
import openrouterPlugin from "./index.js";
33+
import * as openRouterCatalog from "./provider-catalog.js";
3334
import {
3435
buildOpenrouterProvider,
3536
isOpenRouterProxyReasoningUnsupportedModel,
@@ -252,6 +253,135 @@ describe("openrouter provider hooks", () => {
252253
expect(buildOpenrouterProvider().models?.map((model) => model.id)).not.toContain("auto");
253254
});
254255

256+
it("forwards configured proxy destination and request policy into authenticated catalog discovery", async () => {
257+
const provider = await registerSingleProviderPlugin(openrouterPlugin);
258+
const configuredProvider = {
259+
apiKey: "synthetic-private-proxy-key",
260+
baseUrl: "https://private.example.invalid/router/v1///",
261+
request: { headers: { "X-Private-Proxy-Tenant": "synthetic-tenant" } },
262+
models: [],
263+
};
264+
const catalogSpy = vi
265+
.spyOn(openRouterCatalog, "buildOpenrouterLiveProvider")
266+
.mockResolvedValue(buildOpenrouterProvider());
267+
268+
try {
269+
await provider.catalog?.run({
270+
config: { models: { providers: { openrouter: configuredProvider } } },
271+
resolveProviderApiKey: () => ({
272+
apiKey: "OPENROUTER_API_KEY",
273+
discoveryApiKey: "synthetic-private-proxy-key",
274+
}),
275+
} as never);
276+
277+
expect(catalogSpy).toHaveBeenCalledWith(
278+
expect.objectContaining({
279+
baseUrl: configuredProvider.baseUrl,
280+
request: configuredProvider.request,
281+
}),
282+
);
283+
} finally {
284+
catalogSpy.mockRestore();
285+
}
286+
});
287+
288+
it("keeps dynamic proxy models on their configured credential destination", async () => {
289+
const provider = await registerSingleProviderPlugin(openrouterPlugin);
290+
const model = provider.resolveDynamicModel?.({
291+
provider: "openrouter",
292+
modelId: "private/unknown-model",
293+
modelRegistry: { find: vi.fn(() => null) },
294+
providerConfig: { baseUrl: "https://private.example.invalid/router/v1///" },
295+
} as never);
296+
297+
expect(model?.baseUrl).toBe("https://private.example.invalid/router/v1");
298+
});
299+
300+
it("resolves dynamic proxy destinations from canonical provider config when runtime config is absent", async () => {
301+
const provider = await registerSingleProviderPlugin(openrouterPlugin);
302+
const model = provider.resolveDynamicModel?.({
303+
provider: "openrouter",
304+
modelId: "private/unknown-model",
305+
modelRegistry: { find: vi.fn(() => null) },
306+
config: {
307+
models: {
308+
providers: {
309+
openrouter: { baseUrl: "https://private.example.invalid/router/v1/", models: [] },
310+
},
311+
},
312+
},
313+
} as never);
314+
315+
expect(model?.baseUrl).toBe("https://private.example.invalid/router/v1");
316+
});
317+
318+
it("preserves the canonical official destination for dynamically resolved default models", async () => {
319+
const provider = await registerSingleProviderPlugin(openrouterPlugin);
320+
const model = provider.resolveDynamicModel?.({
321+
provider: "openrouter",
322+
modelId: "openrouter/auto",
323+
modelRegistry: { find: vi.fn(() => null) },
324+
providerConfig: { baseUrl: "https://openrouter.ai/v1///" },
325+
} as never);
326+
327+
expect(model?.baseUrl).toBe("https://openrouter.ai/api/v1");
328+
});
329+
330+
it("forwards configured proxy destination and headers to both usage requests", async () => {
331+
const provider = await registerSingleProviderPlugin(openrouterPlugin);
332+
const fetchFn = vi.fn<typeof fetch>(async () => Response.json({ data: { usage: 1 } }));
333+
334+
await provider.fetchUsageSnapshot?.({
335+
config: {
336+
models: {
337+
providers: {
338+
openrouter: {
339+
baseUrl: "https://private.example.invalid/router/v1///",
340+
request: { headers: { "X-Private-Proxy-Tenant": "synthetic-tenant" } },
341+
models: [],
342+
},
343+
},
344+
},
345+
},
346+
env: {},
347+
provider: "openrouter",
348+
token: "synthetic-private-proxy-key",
349+
timeoutMs: 5000,
350+
fetchFn: fetchFn as unknown as typeof fetch,
351+
});
352+
353+
expect(fetchFn.mock.calls.map(([url]) => url)).toEqual([
354+
"https://private.example.invalid/router/v1/credits",
355+
"https://private.example.invalid/router/v1/key",
356+
]);
357+
for (const [, options] of fetchFn.mock.calls) {
358+
expect(new Headers(options?.headers).get("x-private-proxy-tenant")).toBe("synthetic-tenant");
359+
}
360+
});
361+
362+
it("does not start authenticated catalog discovery when no credential exists", async () => {
363+
const provider = await registerSingleProviderPlugin(openrouterPlugin);
364+
const catalogSpy = vi.spyOn(openRouterCatalog, "buildOpenrouterLiveProvider");
365+
366+
try {
367+
await expect(
368+
provider.catalog?.run({
369+
config: {
370+
models: {
371+
providers: {
372+
openrouter: { baseUrl: "https://private.example.invalid/v1", models: [] },
373+
},
374+
},
375+
},
376+
resolveProviderApiKey: () => ({}),
377+
} as never),
378+
).resolves.toBeNull();
379+
expect(catalogSpy).not.toHaveBeenCalled();
380+
} finally {
381+
catalogSpy.mockRestore();
382+
}
383+
});
384+
255385
it("normalizes OpenRouter API ids before capability loading and lookup", async () => {
256386
getOpenRouterModelCapabilitiesMock.mockReset();
257387
loadOpenRouterModelCapabilitiesMock.mockClear();

‎extensions/openrouter/index.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
buildOpenrouterProvider,
3333
isOpenRouterProxyReasoningUnsupportedModel,
3434
normalizeOpenRouterBaseUrl,
35-
OPENROUTER_BASE_URL,
35+
resolveOpenRouterApiBaseUrl,
3636
} from "./provider-catalog.js";
3737
import { resolveOpenRouterExtraParamsForTransport } from "./provider-routing.js";
3838
import { buildOpenRouterSpeechProvider } from "./speech-provider.js";
@@ -237,7 +237,9 @@ export default defineSingleProviderPluginEntry({
237237
name: capabilities?.name ?? ctx.modelId,
238238
api: "openai-completions",
239239
provider: PROVIDER_ID,
240-
baseUrl: OPENROUTER_BASE_URL,
240+
baseUrl: resolveOpenRouterApiBaseUrl(
241+
ctx.providerConfig?.baseUrl ?? ctx.config?.models?.providers?.openrouter?.baseUrl,
242+
),
241243
reasoning:
242244
(capabilities?.reasoning ?? false) &&
243245
!isOpenRouterProxyReasoningUnsupportedModel(ctx.modelId),
@@ -288,10 +290,13 @@ export default defineSingleProviderPluginEntry({
288290
if (!apiKey) {
289291
return null;
290292
}
293+
const providerConfig = ctx.config.models?.providers?.openrouter;
291294
return {
292295
provider: await buildOpenrouterLiveProvider({
293296
apiKey,
294297
discoveryApiKey: auth.discoveryApiKey,
298+
baseUrl: providerConfig?.baseUrl,
299+
request: providerConfig?.request,
295300
}),
296301
};
297302
},
@@ -340,6 +345,8 @@ export default defineSingleProviderPluginEntry({
340345
fetchUsageSnapshot: async (ctx) =>
341346
await fetchOpenRouterUsage({
342347
token: ctx.token,
348+
baseUrl: ctx.config.models?.providers?.openrouter?.baseUrl,
349+
request: ctx.config.models?.providers?.openrouter?.request,
343350
timeoutMs: ctx.timeoutMs,
344351
fetchFn: ctx.fetchFn,
345352
}),

0 commit comments

Comments
 (0)