Skip to content

Commit b66ec3d

Browse files
committed
fix(responses): fail closed when a routed provider calls an undeclared tool
The bridged paths already refuse a tool call the request never declared — `declaredToolNames` in src/bridge.ts turns it into a 502 naming the tool. The native Responses passthrough had no equivalent, so the same call was relayed verbatim. Codex then received a top-level `function_call(name=apply_patch)` for which it has no handler: under code mode `apply_patch` exists only as a nested `tools.apply_patch(...)` helper declared inside the `exec` description, never as a wire tool. The turn surfaced as a bare `aborted` with the target file unchanged, no `custom_tool_call_output`, and no error anywhere to explain it. Ground truth for the guard is the OUTBOUND body rather than the parsed internal tool list. The passthrough forwards wire shapes the internal list flattens or renames — namespaced MCP groups, `additional_tools` items carried inside `input`, the routed custom-tool rewrite — so only the wire names can be compared against what the provider echoes back. Namespaced tools are accepted under either coordinate system, since Codex routes MCP calls by an explicit `namespace` field. Scope: - Only client-executed items are checked (`function_call`, `custom_tool_call`). Hosted calls are run upstream or carry no name. - Forward auth is the canonical ChatGPT backend speaking Codex's own protocol rather than a routed provider, so it stays unguarded — the same line the routed custom-tool and image-gen rewrites already draw. - An unreadable or empty catalog disables the guard rather than failing every turn. - Both the streaming relay and the bounded-JSON answer are covered. Fixes #1700
1 parent 9db1676 commit b66ec3d

4 files changed

Lines changed: 546 additions & 18 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { namespacedToolName } from "../types";
2+
import { sseDataPayload, type SseBlockRewrite } from "./sse-payload-rewrite";
3+
4+
/**
5+
* Item types the CLIENT executes by name. Hosted calls (`web_search_call`,
6+
* `image_generation_call`, `local_shell_call`, `tool_search_call`, …) are run upstream or
7+
* carry no tool name, so they are never matched against the request catalog.
8+
*/
9+
const CLIENT_EXECUTED_CALL_TYPES = new Set(["function_call", "custom_tool_call"]);
10+
11+
/** An upstream-supplied name reaches the error message; keep it bounded. */
12+
const MAX_REPORTED_NAME_CHARS = 100;
13+
14+
export const UNDECLARED_TOOL_CALL_ERROR_CODE = "undeclared_tool_call";
15+
16+
function isPlainObject(value: unknown): value is Record<string, unknown> {
17+
return !!value && typeof value === "object" && !Array.isArray(value);
18+
}
19+
20+
function addWireToolName(names: Set<string>, tool: unknown, namespace?: string): void {
21+
if (!isPlainObject(tool) || typeof tool.name !== "string" || tool.name.length === 0) return;
22+
names.add(tool.name);
23+
// Codex routes MCP calls by an explicit `namespace` field, so the same tool is reachable
24+
// as a bare inner name or as the flattened form; accept both rather than guess which
25+
// coordinate system this provider echoes back.
26+
if (namespace) names.add(namespacedToolName(namespace, tool.name));
27+
}
28+
29+
function addWireToolSpecs(names: Set<string>, specs: unknown): void {
30+
if (!Array.isArray(specs)) return;
31+
for (const spec of specs) {
32+
if (!isPlainObject(spec)) continue;
33+
if (spec.type === "namespace" && Array.isArray(spec.tools)) {
34+
const namespace = typeof spec.name === "string" ? spec.name : undefined;
35+
for (const inner of spec.tools) addWireToolName(names, inner, namespace);
36+
continue;
37+
}
38+
addWireToolName(names, spec);
39+
}
40+
}
41+
42+
/**
43+
* Tool names the OUTBOUND Responses body actually declared.
44+
*
45+
* This reads the body that goes upstream rather than the parsed internal tool list: the
46+
* passthrough forwards wire shapes (namespaced MCP groups, `additional_tools` items carried
47+
* inside `input`, routed custom-tool rewrites) that the internal list flattens or renames, and
48+
* only the wire names can be compared against what the provider echoes back.
49+
*/
50+
export function collectDeclaredWireToolNames(body: unknown): Set<string> {
51+
const names = new Set<string>();
52+
if (!isPlainObject(body)) return names;
53+
addWireToolSpecs(names, body.tools);
54+
if (Array.isArray(body.input)) {
55+
for (const item of body.input) {
56+
if (isPlainObject(item) && item.type === "additional_tools") addWireToolSpecs(names, item.tools);
57+
}
58+
}
59+
return names;
60+
}
61+
62+
function undeclaredNameInItem(item: unknown, declared: ReadonlySet<string>): string | undefined {
63+
if (!isPlainObject(item)) return undefined;
64+
if (typeof item.type !== "string" || !CLIENT_EXECUTED_CALL_TYPES.has(item.type)) return undefined;
65+
const name = item.name;
66+
if (typeof name !== "string" || name.length === 0) return undefined;
67+
if (declared.has(name)) return undefined;
68+
if (typeof item.namespace === "string" && declared.has(namespacedToolName(item.namespace, name))) {
69+
return undefined;
70+
}
71+
return name;
72+
}
73+
74+
/** First undeclared client tool named by a Responses SSE payload, or undefined. */
75+
export function undeclaredToolCallName(
76+
payload: unknown,
77+
declared: ReadonlySet<string>,
78+
): string | undefined {
79+
if (!isPlainObject(payload)) return undefined;
80+
if (payload.type === "response.output_item.added" || payload.type === "response.output_item.done") {
81+
return undeclaredNameInItem(payload.item, declared);
82+
}
83+
// Sparse gateways skip incremental items and only ever ship the terminal snapshot.
84+
if (payload.type === "response.completed" || payload.type === "response.incomplete") {
85+
return undeclaredToolCallNameInResponse(payload.response, declared);
86+
}
87+
return undefined;
88+
}
89+
90+
/** First undeclared client tool in a Responses object's `output` array, or undefined. */
91+
export function undeclaredToolCallNameInResponse(
92+
response: unknown,
93+
declared: ReadonlySet<string>,
94+
): string | undefined {
95+
if (!isPlainObject(response) || !Array.isArray(response.output)) return undefined;
96+
for (const item of response.output) {
97+
const name = undeclaredNameInItem(item, declared);
98+
if (name !== undefined) return name;
99+
}
100+
return undefined;
101+
}
102+
103+
export function undeclaredToolCallMessage(name: string): string {
104+
const reported = name.slice(0, MAX_REPORTED_NAME_CHARS);
105+
return `routed provider emitted undeclared client tool "${reported}"; only request-declared tools may be called`;
106+
}
107+
108+
function failedBlocks(name: string, newline: string): readonly string[] {
109+
const failure = {
110+
type: "upstream_error",
111+
code: UNDECLARED_TOOL_CALL_ERROR_CODE,
112+
message: undeclaredToolCallMessage(name),
113+
};
114+
const payload = JSON.stringify({
115+
type: "response.failed",
116+
response: { status: "failed", error: failure, last_error: failure },
117+
});
118+
return [`event: response.failed${newline}data: ${payload}`, "data: [DONE]"];
119+
}
120+
121+
/**
122+
* Fail closed when a routed provider calls a tool the request never declared (#1700).
123+
*
124+
* The bridged paths already refuse such a call (`declaredToolNames` in src/bridge.ts), but the
125+
* native Responses passthrough relayed it verbatim: Codex received a `function_call` for a tool
126+
* it has no top-level handler for — `apply_patch`, which under code mode exists only as a nested
127+
* `tools.apply_patch(...)` helper inside `exec` — and the turn surfaced as a bare `aborted` with
128+
* no output and no explanation. Replacing the offending event with an explicit `response.failed`
129+
* turns that silent dead end into a compatibility error naming the tool.
130+
*
131+
* Everything after the trip is dropped so a later `response.completed` cannot contradict the
132+
* terminal already sent. Non-JSON and non-item blocks pass through untouched.
133+
*/
134+
export function createUndeclaredToolCallGuardBlockRewrite(
135+
declared: ReadonlySet<string>,
136+
): SseBlockRewrite {
137+
let tripped = false;
138+
return (block: string) => {
139+
if (tripped) return [];
140+
const payload = sseDataPayload(block);
141+
if (payload === null || payload === "[DONE]") return [block];
142+
let parsed: unknown;
143+
try {
144+
parsed = JSON.parse(payload);
145+
} catch {
146+
return [block];
147+
}
148+
const name = undeclaredToolCallName(parsed, declared);
149+
if (name === undefined) return [block];
150+
tripped = true;
151+
return failedBlocks(name, block.includes("\r\n") ? "\r\n" : "\n");
152+
};
153+
}

‎src/server/responses/core.ts‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ import {
237237
} from "../sse-payload-rewrite";
238238
import { restoreRoutedCustomCallsInJson } from "../../responses/custom-tool-compat";
239239
import { createRoutedCustomToolRestoreBlockRewrite } from "../responses-custom-tool-repair";
240+
import {
241+
collectDeclaredWireToolNames,
242+
createUndeclaredToolCallGuardBlockRewrite,
243+
undeclaredToolCallMessage,
244+
undeclaredToolCallNameInResponse,
245+
} from "../responses-undeclared-tool-guard";
240246
import { createGithubCopilotResponsesBlockRewrite } from "../github-copilot-responses-repair";
241247
import { responsesJsonToSseStream } from "../responses-json-events";
242248
import { guardTerminalEventStream } from "./terminal-guard";
@@ -2267,6 +2273,25 @@ async function handleResponsesInner(
22672273
if (toolBridgeMaps.freeformToolNames.has(name)) routedCustomToolNames.add(name);
22682274
}
22692275
}
2276+
// #1700: the bridged paths refuse a call to a tool the request never declared
2277+
// (`declaredToolNames`, src/bridge.ts). The passthrough had no equivalent, so a routed
2278+
// provider's top-level `apply_patch` — which under Codex code mode exists only as a nested
2279+
// `tools.apply_patch(...)` helper inside `exec`, never as a wire tool — reached Codex as a
2280+
// call it cannot execute, and the turn showed a bare `aborted` with the file untouched.
2281+
// Forward auth is the canonical ChatGPT backend speaking Codex's own protocol rather than a
2282+
// routed provider, so it keeps passing through unguarded, as it does for the rewrites above.
2283+
// An empty catalog disables the guard: passthrough sends the raw body upstream, so a request
2284+
// whose tools this proxy could not read is not a catalog worth policing.
2285+
const outboundRequestBody = (() => {
2286+
try {
2287+
return JSON.parse(request.body) as unknown;
2288+
} catch {
2289+
return undefined;
2290+
}
2291+
})();
2292+
const declaredWireToolNames = route.provider.authMode === "forward"
2293+
? new Set<string>()
2294+
: collectDeclaredWireToolNames(outboundRequestBody);
22702295
recordAdapterReasoning(logCtx, request);
22712296
const actualHostKey = upstreamHostHealthKey(
22722297
route.providerName,
@@ -2654,13 +2679,6 @@ async function handleResponsesInner(
26542679
// injection at the block level, after payload rewrites. Defaults come
26552680
// from the finalized OUTBOUND body — the normalized internal tool shapes
26562681
// are not the Responses wire shapes the snapshot must mirror.
2657-
const snapshotDefaultsRequest = (() => {
2658-
try {
2659-
return JSON.parse(request.body) as unknown;
2660-
} catch {
2661-
return undefined;
2662-
}
2663-
})();
26642682
const blockRewrites = [
26652683
payloadRewrites.length > 0
26662684
? payloadRewriteAsBlockRewrite(composeSsePayloadRewrites(...payloadRewrites))
@@ -2672,7 +2690,12 @@ async function handleResponsesInner(
26722690
? createGithubCopilotResponsesBlockRewrite(translatorBudget)
26732691
: undefined,
26742692
snapshotRepairEnabled
2675-
? createResponsesSnapshotBlockRewrite(snapshotDefaultsRequest, translatorBudget)
2693+
? createResponsesSnapshotBlockRewrite(outboundRequestBody, translatorBudget)
2694+
: undefined,
2695+
// Last: every rewrite above can still rename or reshape a call item, so the guard must
2696+
// compare the names the client will actually receive against the declared catalog.
2697+
declaredWireToolNames.size > 0
2698+
? createUndeclaredToolCallGuardBlockRewrite(declaredWireToolNames)
26762699
: undefined,
26772700
].filter((rewrite): rewrite is NonNullable<typeof rewrite> => rewrite !== undefined);
26782701
const clientBlockRewrite = blockRewrites.length > 0
@@ -2862,20 +2885,27 @@ async function handleResponsesInner(
28622885
restoreImageGenCallsInJson(text, imageGenCallAliases),
28632886
routedCustomToolNames,
28642887
);
2865-
const repaired = (() => {
2866-
if (!hasResponsesSnapshotRepair(route.provider.responsesSnapshotRepair)) return restored;
2867-
let outbound: unknown;
2868-
try {
2869-
outbound = JSON.parse(request.body);
2870-
} catch {
2871-
outbound = undefined;
2872-
}
2873-
return repairResponsesSnapshotJson(restored, outbound);
2874-
})();
2888+
const repaired = hasResponsesSnapshotRepair(route.provider.responsesSnapshotRepair)
2889+
? repairResponsesSnapshotJson(restored, outboundRequestBody)
2890+
: restored;
28752891
return parsed._responseModelId !== undefined && parsed._responseModelId !== parsed.modelId
28762892
? rewriteResponsesModelJson(repaired, parsed._responseModelId)
28772893
: repaired;
28782894
})();
2895+
// #1700: same fail-closed policy as the SSE relay above. Both the plain JSON answer and
2896+
// the reframed-SSE branch below are built from this body, so one check covers them.
2897+
if (declaredWireToolNames.size > 0) {
2898+
const undeclared = (() => {
2899+
try {
2900+
return undeclaredToolCallNameInResponse(JSON.parse(clientJson), declaredWireToolNames);
2901+
} catch {
2902+
return undefined;
2903+
}
2904+
})();
2905+
if (undeclared !== undefined) {
2906+
return formatErrorResponse(502, "upstream_error", undeclaredToolCallMessage(undeclared));
2907+
}
2908+
}
28792909
// #875: the transport-neutral reliability policy forced a bounded JSON
28802910
// upstream for a client that asked for SSE. Reframe the completed JSON
28812911
// as the canonical terminal SSE sequence (created → output_item.done →

‎tests/responses-custom-tool-repair.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ describe("routed Responses custom-tool compatibility", () => {
742742
tools: Array<Record<string, unknown>>;
743743
toolChoice?: unknown;
744744
metadata?: unknown;
745+
/** The upstream call names a tool this request never declared at all (#1700). */
746+
undeclared?: boolean;
745747
}> = [
746748
{
747749
name: "streaming none",
@@ -770,6 +772,7 @@ describe("routed Responses custom-tool compatibility", () => {
770772
stream: false,
771773
tools: [ordinaryTool],
772774
metadata: { nested: { type: "custom", name: "exec" } },
775+
undeclared: true,
773776
},
774777
];
775778

@@ -823,6 +826,14 @@ describe("routed Responses custom-tool compatibility", () => {
823826
expect(clientSse).toContain("response.function_call_arguments.done");
824827
expect(clientSse).not.toContain("custom_tool_call");
825828
expect(clientSse).not.toContain("ctc_exec");
829+
} else if (policyCase.undeclared) {
830+
// #1700: this request's catalog holds only `ordinary` — a metadata blob that merely
831+
// looks like a tool declaration declares nothing — so a call to `exec` is refused
832+
// instead of relayed. The restore contract still holds either way: it never became
833+
// a custom_tool_call.
834+
expect(response.status).toBe(502);
835+
const body = await response.json() as { error: { message: string } };
836+
expect(body.error.message).toContain('undeclared client tool "exec"');
826837
} else {
827838
const body = await response.json() as { output: Array<Record<string, unknown>> };
828839
expect(body.output[0]).toEqual(upstreamItem);

0 commit comments

Comments
 (0)