Skip to content

Commit faaa78d

Browse files
authored
fix(responses): reject malformed selectors at the authorization gate (lidge-jun#2501)
Refusing to rewrite a malformed selector was not enough. When its name is already the flattened wire name — {type:"function", namespace:1, name:"collaboration__safe"} — returning it unchanged leaves an exact match against the alias map, so the alias arms anyway and an upstream call is restored into the namespace. Both selector shapes reach it. The check belongs at the authorization gate, not only in the rewriter: a malformed namespace makes the whole selector untrustworthy regardless of which name it carries. The unqualified shape is untouched, since an absent namespace is a deliberate, legitimate selector. Also states the POSIX permission precondition in the troubleshooting page, which described only the content half of the skip rule. Tests cover both malformed-with-wire-name shapes and keep a positive control on the unqualified selector so the fix cannot pass by being deny-all. 186 pass across namespace-tool-compat, responses-parser, openai-responses-passthrough, and the write-amplification suite. tsc --noEmit clean.
1 parent 43227ac commit faaa78d

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎docs-site/src/content/docs/troubleshooting/disk-usage-temp-files.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ about two seconds after a change, and once it is near the 24 MB bound the wait
5858
stretches to at most thirty seconds. A cache that has only just grown therefore
5959
still takes the short wait once — the longer cadence applies from the write after
6060
it. A flush is skipped only when this process already wrote the same bytes to the
61-
same file and that file still matches on disk, so a fresh process rewrites an
62-
identical snapshot once, and a file changed underneath the proxy is repaired rather
63-
than left alone.
61+
same file, that file still matches on disk, and — outside Windows — its mode is
62+
still owner-only. A fresh process rewrites an identical snapshot once, and a file
63+
whose contents or permissions changed underneath the proxy is rewritten through the
64+
hardening path rather than left alone.
6465

6566
Together these keep the write rate roughly flat as the cache grows, instead of
6667
re-serializing and replacing the whole file every two seconds.

‎src/responses/namespace-tool-compat.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,27 @@ function rewriteToolList(
217217
* the failure this layer exists to prevent, and this layer's own response restoration is what put
218218
* the key on the item.
219219
*/
220+
/**
221+
* A selector's `namespace` is either absent — meaning "unqualified, resolve the bare
222+
* name" — or a string naming the group. A present-but-non-string value is neither, and
223+
* a malformed selector must not authorize anything: not through the unqualified
224+
* fallback, and not by happening to carry an already-flattened wire name, which would
225+
* otherwise match the alias map exactly and arm it anyway.
226+
*/
227+
function hasMalformedNamespace(value: Record<string, unknown>): boolean {
228+
return "namespace" in value && typeof value.namespace !== "string";
229+
}
230+
220231
function rewriteNamedSelector(
221232
value: unknown,
222233
plan: NamespaceRewritePlan,
223234
bareFallback: boolean,
224235
): unknown {
225236
if (!isPlainObject(value) || typeof value.name !== "string") return value;
226-
// An ABSENT namespace is an unqualified selector and may fall back to the bare
227-
// name. A PRESENT but non-string one is malformed, and must not take that path:
228-
// treating it as absent lets `{type:"function", namespace:1, name:"safe"}` resolve
229-
// to a namespace wire name, which then authorizes an alias the caller never named
230-
// in any well-formed way. Fail closed and hand the selector back untouched.
231-
if ("namespace" in value && typeof value.namespace !== "string") return value;
237+
// Malformed: hand it back untouched so no rewrite occurs. Authorization rejects it
238+
// separately — returning it unchanged is not by itself enough, because the name it
239+
// carries may already BE a wire name.
240+
if (hasMalformedNamespace(value)) return value;
232241
const namespace = value.namespace;
233242
if (typeof namespace !== "string") {
234243
if (!bareFallback) return value;
@@ -276,6 +285,9 @@ function authorizedAliases(
276285
(toolChoice.type === "function" || toolChoice.type === "custom")
277286
&& typeof toolChoice.name === "string"
278287
) {
288+
// A malformed namespace makes the whole selector untrustworthy, even when its
289+
// name is already a flattened wire name that would match the alias map exactly.
290+
if (hasMalformedNamespace(toolChoice)) return new Map();
279291
authorized = new Map([[toolChoice.name, toolChoice.type]]);
280292
} else if (toolChoice.type === "allowed_tools" && Array.isArray(toolChoice.tools)) {
281293
authorized = new Map();
@@ -290,6 +302,7 @@ function authorizedAliases(
290302
if (!isPlainObject(tool)) continue;
291303
if (tool.type !== "function" && tool.type !== "custom") continue;
292304
if (typeof tool.name !== "string") continue;
305+
if (hasMalformedNamespace(tool)) continue;
293306
authorized.set(tool.name, tool.type);
294307
}
295308
} else {

‎tests/namespace-tool-compat.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,35 @@ describe("Responses namespace tool compatibility", () => {
299299
expect(aliases.size).toBe(0);
300300
});
301301

302+
// Refusing to REWRITE a malformed selector is not enough on its own. If its name is
303+
// already the flattened wire name, it matches the alias map exactly and arms it
304+
// anyway — so authorization has to reject the selector itself, whatever name it
305+
// carries. Both selector shapes, because a caller can write either.
306+
test("a malformed selector already using the flattened wire name authorizes nothing", () => {
307+
const forced = rewriteRoutedNamespaceToolsForUpstream({
308+
tools: namespaceTools,
309+
tool_choice: { type: "function", namespace: 1, name: wireName },
310+
});
311+
expect(forced.aliases.size).toBe(0);
312+
expect(restoreRoutedNamespaceCalls({ type: "function_call", name: wireName }, forced.aliases).changed).toBe(false);
313+
314+
const allowed = rewriteRoutedNamespaceToolsForUpstream({
315+
tools: namespaceTools,
316+
tool_choice: { type: "allowed_tools", mode: "required", tools: [{ type: "function", namespace: 1, name: wireName }] },
317+
});
318+
expect(allowed.aliases.size).toBe(0);
319+
});
320+
321+
test("an unqualified selector using the wire name still authorizes", () => {
322+
// The legitimate shape this must not break: no namespace field at all, naming
323+
// the flattened tool directly.
324+
const { aliases } = rewriteRoutedNamespaceToolsForUpstream({
325+
tools: namespaceTools,
326+
tool_choice: { type: "function", name: wireName },
327+
});
328+
expect(aliases.get(wireName)).toBeDefined();
329+
});
330+
302331
test("a correctly qualified selector still authorizes, so this is not deny-all", () => {
303332
const { aliases } = rewriteRoutedNamespaceToolsForUpstream({
304333
tools: namespaceTools,

0 commit comments

Comments
 (0)