Skip to content

Commit e8b36b0

Browse files
committed
fix(web-search): tell the operator when a bridge endpoint is refused
A provider keyed under a custom name, say "my-ollama", pointing at http://127.0.0.1:11434/api/web_search armed before the destination check and disarms after it, because only the registry ids (ollama, vllm, lm-studio, litellm) are local by default. Two properties combined to make that invisible: config load never runs providerWebSearchBridgeConfigError, so the block loads cleanly, and the plan-time refusal returns undefined by design so the key stays unspent. The operator's web search stopped working with no signal at all. resolveOllamaWebSearchEndpoint now warns once per provider and endpoint when the refusal is a destination decision, naming the two remedies: set allowPrivateNetwork, or key the provider under its registry id. The planner runs per request, so the warning is deduplicated and the dedupe set is bounded. The destination URL is never logged and the provider key goes through redactSecretString, since a provider key is caller-controlled. Adds the coverage the review named as missing: a metadata endpoint survives validateConfigCandidate intact and is then refused at plan time, which pins the load-path behavior the whole argument rests on rather than simulating it.
1 parent bcc1b3e commit e8b36b0

2 files changed

Lines changed: 150 additions & 4 deletions

File tree

‎src/web-search/passthrough-bridge.ts‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,52 @@ import {
6565
xaiSearchOptionsFromConfig,
6666
} from "./sidecar-providers";
6767
import { providerDestinationConfigError } from "../lib/destination-policy";
68+
import { redactSecretString } from "../lib/redact";
6869

6970
/** Canonical Ollama Cloud origin. The only origin the "ollama" backend derives on its own. */
7071
export const OLLAMA_CLOUD_ORIGIN = "https://ollama.com";
7172
const OLLAMA_WEB_SEARCH_PATH = "/api/web_search";
7273

74+
/**
75+
* Providers already warned about a destination-refused bridge endpoint. The planner runs per
76+
* request, so without this a refused endpoint would warn on every turn. Keyed on provider plus
77+
* endpoint so that editing the config warns again; the key itself is never logged.
78+
*/
79+
const warnedRefusedBridgeEndpoints = new Set<string>();
80+
/** Bound the dedupe set so a pathological config cannot grow it without limit. */
81+
const MAX_WARNED_REFUSED_ENDPOINTS = 64;
82+
83+
/**
84+
* A refused endpoint disarms the bridge, and the refusal itself has to stay silent at the point of
85+
* use -- returning undefined is what keeps the key unspent. But silence alone made a real
86+
* configuration fail invisibly: a provider keyed under a CUSTOM name (say "my-ollama") pointing at
87+
* a loopback endpoint used to arm, and the destination policy now refuses it because only the
88+
* registry ids are local by default. The config file never reaches
89+
* "providerWebSearchBridgeConfigError", so nothing else would tell the operator. One warning per
90+
* provider and endpoint gives them the remedy without leaking the destination: the URL is
91+
* deliberately omitted and the provider name is redacted, because a provider key is
92+
* caller-controlled and can be token-shaped.
93+
*/
94+
function warnRefusedBridgeEndpointOnce(providerName: string, endpoint: string): void {
95+
const key = providerName + "\u0000" + endpoint;
96+
if (warnedRefusedBridgeEndpoints.has(key)) return;
97+
if (warnedRefusedBridgeEndpoints.size >= MAX_WARNED_REFUSED_ENDPOINTS) {
98+
warnedRefusedBridgeEndpoints.clear();
99+
}
100+
warnedRefusedBridgeEndpoints.add(key);
101+
console.warn(
102+
"[web-search] provider " + JSON.stringify(redactSecretString(providerName))
103+
+ " webSearchBridge.endpoint was refused by destination policy, so the bridge stays disarmed."
104+
+ " Set allowPrivateNetwork:true for an intentionally local endpoint, or key the provider under"
105+
+ " its registry id (ollama, vllm, lm-studio, litellm).",
106+
);
107+
}
108+
109+
/** Test seam: the dedupe is process-wide, so a test that asserts the warning must reset it. */
110+
export function resetRefusedBridgeEndpointWarningsForTests(): void {
111+
warnedRefusedBridgeEndpoints.clear();
112+
}
113+
73114
const DEFAULT_BRIDGE_MAX_SEARCHES = 3;
74115
const DEFAULT_BRIDGE_TIMEOUT_MS = 60_000;
75116
/** Queries honored from one call's "queries" array; the rest are ignored rather than billed. */
@@ -131,8 +172,10 @@ function originOf(value: string | undefined): string | undefined {
131172
* registry entry that is local by definition, so a local Ollama on 127.0.0.1 keeps working. This
132173
* is the ONLY reader of "webSearchBridge.endpoint" in the tree, which is what lets it act as the
133174
* authorization boundary for a config file the operator edited by hand -- that path never reaches
134-
* "providerWebSearchBridgeConfigError". The refusal is deliberately silent: config-time is where
135-
* an operator is told why, and a value that survives file load simply cannot be spent.
175+
* "providerWebSearchBridgeConfigError", so a value that survives file load simply cannot be spent.
176+
* The refusal returns undefined rather than an error, because disarming is what keeps the key
177+
* unspent -- but it is not silent: see warnRefusedBridgeEndpointOnce for why a custom-named local
178+
* provider has to be told, once, that its endpoint was refused and how to re-authorize it.
136179
*/
137180
export function resolveOllamaWebSearchEndpoint(
138181
providerName: string,
@@ -144,7 +187,10 @@ export function resolveOllamaWebSearchEndpoint(
144187
if (providerDestinationConfigError(providerName, {
145188
baseUrl: configured,
146189
allowPrivateNetwork: provider.allowPrivateNetwork,
147-
})) return undefined;
190+
})) {
191+
warnRefusedBridgeEndpointOnce(providerName, configured);
192+
return undefined;
193+
}
148194
return configured;
149195
}
150196
return originOf(provider.baseUrl) === OLLAMA_CLOUD_ORIGIN

‎tests/web-search/web-search-passthrough-bridge.test.ts‎

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import {
1515
planPassthroughWebSearchBridge,
1616
resolveOllamaWebSearchEndpoint,
1717
resolvePassthroughWebSearchBridgeAuth,
18+
resetRefusedBridgeEndpointWarningsForTests,
1819
shouldResolveOpenAiPassthroughWebSearchBridge,
1920
sidecarSettingsForBridge,
2021
WEB_SEARCH_BRIDGE_ERROR_CODE,
2122
WEB_SEARCH_BRIDGE_MIXED_TOOLS_ERROR_CODE,
2223
type PassthroughWebSearchBridgePlan,
2324
} from "../../src/web-search/passthrough-bridge";
24-
import { providerWebSearchBridgeConfigError } from "../../src/config";
25+
import { providerWebSearchBridgeConfigError, validateConfigCandidate } from "../../src/config";
2526
import { mapOllamaSearchResponse } from "../../src/web-search/ollama-executor";
2627
import { UNDECLARED_TOOL_CALL_ERROR_CODE } from "../../src/server/responses-undeclared-tool-guard";
2728
import { handleResponses } from "../../src/server/responses";
@@ -418,6 +419,105 @@ describe("webSearchBridge.endpoint destination policy", () => {
418419
});
419420
});
420421

422+
// The refusal disarms the bridge without an error, which is what keeps the key unspent. That
423+
// silence broke a real configuration: a provider keyed under a CUSTOM name pointing at loopback
424+
// used to arm, and only the registry ids are local by default. The operator has to be told once.
425+
describe("a refused endpoint tells the operator once", () => {
426+
const gateway = { baseUrl: "https://gateway.example/v1" };
427+
428+
function captureWarnings(run: () => void): string[] {
429+
const lines: string[] = [];
430+
const saved = console.warn;
431+
console.warn = (...args: unknown[]) => { lines.push(args.map(String).join(" ")); };
432+
try {
433+
run();
434+
} finally {
435+
console.warn = saved;
436+
}
437+
return lines;
438+
}
439+
440+
test("a custom-named local provider is warned, with the remedy and without the endpoint", () => {
441+
resetRefusedBridgeEndpointWarningsForTests();
442+
const provider = providerFixture(
443+
{ enabled: true, backend: "ollama", endpoint: "http://127.0.0.1:11434/api/web_search" },
444+
gateway,
445+
);
446+
const warnings = captureWarnings(() => {
447+
expect(resolveOllamaWebSearchEndpoint("my-ollama", provider)).toBeUndefined();
448+
});
449+
expect(warnings).toHaveLength(1);
450+
expect(warnings[0]).toContain("my-ollama");
451+
expect(warnings[0]).toContain("allowPrivateNetwork");
452+
// The destination itself never reaches the log.
453+
expect(warnings[0]).not.toContain("127.0.0.1");
454+
expect(warnings[0]).not.toContain("/api/web_search");
455+
});
456+
457+
test("the same refusal does not warn again on every later request", () => {
458+
resetRefusedBridgeEndpointWarningsForTests();
459+
const provider = providerFixture(
460+
{ enabled: true, backend: "ollama", endpoint: "http://10.0.0.5/api/web_search" },
461+
gateway,
462+
);
463+
const warnings = captureWarnings(() => {
464+
for (let i = 0; i < 5; i += 1) {
465+
expect(planPassthroughWebSearchBridge(parsedFixture(), provider, {
466+
providerName: "local-llm",
467+
isPassthrough: true,
468+
stream: true,
469+
})).toBeUndefined();
470+
}
471+
});
472+
expect(warnings).toHaveLength(1);
473+
});
474+
475+
test("an accepted endpoint is not warned about", () => {
476+
resetRefusedBridgeEndpointWarningsForTests();
477+
const provider = providerFixture(
478+
{ enabled: true, backend: "ollama", endpoint: "http://127.0.0.1:11434/api/web_search" },
479+
gateway,
480+
);
481+
const warnings = captureWarnings(() => {
482+
expect(resolveOllamaWebSearchEndpoint("ollama", provider)).toBe("http://127.0.0.1:11434/api/web_search");
483+
});
484+
expect(warnings).toEqual([]);
485+
});
486+
});
487+
488+
// The blocker this policy exists for: config load does NOT run providerWebSearchBridgeConfigError,
489+
// so a metadata endpoint reaches running config intact. Plan time is what refuses to spend it.
490+
describe("a metadata endpoint survives config load and is refused at plan time", () => {
491+
test("configSchema accepts the block and the planner still disarms", () => {
492+
const result = validateConfigCandidate({
493+
port: 0,
494+
defaultProvider: "gateway",
495+
providers: {
496+
gateway: {
497+
adapter: "openai-responses",
498+
baseUrl: "https://gateway.example/v1",
499+
authMode: "key",
500+
apiKey: "fixture-key",
501+
webSearchBridge: {
502+
enabled: true,
503+
backend: "ollama",
504+
endpoint: "http://169.254.169.254/latest/meta-data",
505+
},
506+
},
507+
},
508+
});
509+
expect(result.ok).toBe(true);
510+
const loaded = (result as { ok: true; config: OcxConfig }).config.providers.gateway!;
511+
// It really did survive validation, untouched.
512+
expect(loaded.webSearchBridge?.endpoint).toBe("http://169.254.169.254/latest/meta-data");
513+
expect(planPassthroughWebSearchBridge(parsedFixture(), loaded, {
514+
providerName: "gateway",
515+
isPassthrough: true,
516+
stream: true,
517+
})).toBeUndefined();
518+
});
519+
});
520+
421521
describe("providerWebSearchBridgeConfigError endpoint destination policy", () => {
422522
test("names webSearchBridge.endpoint rather than baseUrl in a metadata refusal", () => {
423523
const value = { enabled: true, backend: "ollama", endpoint: "http://169.254.169.254/latest/meta-data" };

0 commit comments

Comments
 (0)