From 6adb49dab61afe7ee9010ed4594f1ca0cd257f52 Mon Sep 17 00:00:00 2001 From: John Doe <123843277+scuttletoby@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:08:21 +0100 Subject: [PATCH 1/2] fix(#946): builtinTools "none" guard passes only mcp__ tools; boot test catches a dropped threadRoles lookup The guard no longer allows a call because hook_event_name is not "PreToolUse": whatever the event name says (missing, empty, another event), only an mcp__ tool passes. ThreadRoleTargets.layer makes a "ThreadRoleTargets.make" span, and the real-server boot test lists it with the fork's other layer spans, so a server that loses it from layerRuntimeServices fails that test. Co-Authored-By: Claude Opus 5.5 --- .../src/fork/claudeMcpProfile.adapter.test.ts | 39 +++++++++++++++++++ apps/server/src/fork/claudeMcpProfile.ts | 6 +-- .../server/src/fork/threadRoleTargetsLayer.ts | 5 ++- apps/server/src/holdWork.boot.test.ts | 3 ++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/apps/server/src/fork/claudeMcpProfile.adapter.test.ts b/apps/server/src/fork/claudeMcpProfile.adapter.test.ts index 9ba603d5d1de3..50711002a446b 100644 --- a/apps/server/src/fork/claudeMcpProfile.adapter.test.ts +++ b/apps/server/src/fork/claudeMcpProfile.adapter.test.ts @@ -461,6 +461,45 @@ describe("ClaudeAdapterV2 MCP profile", () => { }), ); + it.effect( + 'the builtinTools "none" guard passes only MCP tools, whatever the event name says (#946)', + () => + Effect.gen(function* () { + const bare = { servers: "all", t3Tools: "all", builtinTools: "none" } as const; + const { opened } = yield* runTurns({ mcpProfile: "bare", mcpProfiles: { bare } }, []); + const decision = (answer: unknown) => + (answer as { hookSpecificOutput?: { permissionDecision?: string } } | undefined) + ?.hookSpecificOutput?.permissionDecision; + const call = (tool: string, eventName: unknown, omitEvent = false) => ({ + ...(omitEvent ? {} : { hook_event_name: eventName }), + tool_name: tool, + tool_input: {}, + tool_use_id: "toolu_profile", + session_id: NATIVE_THREAD, + transcript_path: "/tmp/transcript.jsonl", + cwd: "/workspace", + }); + const events: ReadonlyArray<[label: string, eventName: unknown, omit: boolean]> = [ + ["missing", undefined, true], + ["undefined", undefined, false], + ["empty", "", false], + ["PostToolUse", "PostToolUse", false], + ["lower case", "pretooluse", false], + ["not a string", 7, false], + ]; + for (const [label, eventName, omit] of events) { + // The same call made again gets the same answer: nothing is remembered between calls. + for (const attempt of [1, 2]) { + const [refused] = yield* preToolUse(opened[0], "Bash", call("Bash", eventName, omit)); + assert.strictEqual(decision(refused), "deny", `Bash, event ${label}, try ${attempt}`); + const mcpTool = "mcp__t3-code__t3_thread_list"; + const [allowed] = yield* preToolUse(opened[0], mcpTool, call(mcpTool, eventName, omit)); + assert.deepStrictEqual(allowed, {}, `${mcpTool}, event ${label}, try ${attempt}`); + } + } + }), + ); + it.effect("refuses the turn, without opening Claude, when the profile is missing", () => Effect.gen(function* () { const { opened, refusal } = yield* runTurns({ mcpProfile: "lean" }, []); diff --git a/apps/server/src/fork/claudeMcpProfile.ts b/apps/server/src/fork/claudeMcpProfile.ts index b7abc1c6b47d0..d32caab38c46a 100644 --- a/apps/server/src/fork/claudeMcpProfile.ts +++ b/apps/server/src/fork/claudeMcpProfile.ts @@ -137,14 +137,14 @@ export function claudeMcpProfilePlan( /** * Refuses a call to any tool but an MCP server's; hooks run in every mode and in sub-agents. Fails * closed: a tool name that is not a string, or any error while checking, refuses the call (a hook - * that throws would count as a non-blocking error and let the call through). + * that throws would count as a non-blocking error and let the call through). It never trusts + * `hook_event_name`: whatever it says (missing, empty, another event), only an `mcp__` tool passes. */ const claudeBuiltinToolsGuard = (name: string): HookCallback => async (input) => { try { - if (input.hook_event_name !== "PreToolUse") return {}; - const toolName: unknown = input.tool_name; + const toolName: unknown = (input as { tool_name?: unknown }).tool_name; if (typeof toolName === "string" && toolName.startsWith("mcp__")) return {}; const shown = typeof toolName === "string" ? `the built-in tool "${toolName}"` : "a tool"; return builtinToolDenial( diff --git a/apps/server/src/fork/threadRoleTargetsLayer.ts b/apps/server/src/fork/threadRoleTargetsLayer.ts index 290168d8c9a3f..867aeba988d04 100644 --- a/apps/server/src/fork/threadRoleTargetsLayer.ts +++ b/apps/server/src/fork/threadRoleTargetsLayer.ts @@ -1,7 +1,8 @@ /** * Fork (calm sidebar): {@link ThreadRoleTargets} read from upstream's orchestrator, for the * `threadRoles` save check (threadRoleTargets.ts). Kept apart from that file so the settings file - * never imports orchestration code. Wired in forkLayers.ts (`layerRuntimeServices`). + * never imports orchestration code. Wired in forkLayers.ts (`layerRuntimeServices`); its make span + * shows holdWork.boot.test.ts the real server builds it. */ import { isListedThreadLineage, type ThreadId } from "@t3tools/contracts"; import { Effect, Layer } from "effect"; @@ -26,5 +27,5 @@ export const layer = Layer.effect( }; }), }), - ), + ).pipe(Effect.withSpan("ThreadRoleTargets.make")), ); diff --git a/apps/server/src/holdWork.boot.test.ts b/apps/server/src/holdWork.boot.test.ts index 54e1de70b9469..b5a5c7227dc08 100644 --- a/apps/server/src/holdWork.boot.test.ts +++ b/apps/server/src/holdWork.boot.test.ts @@ -394,6 +394,9 @@ const FORK_LAYER_SPANS = [ "AgentSlotRoutes.scheduledRunsGated", // The record of who saved each scheduled task, which needs the server's state folder. "ScheduledTaskMcpProfiles.stored", + // The `threadRoles` save check's thread lookup in `ForkLayers.layerRuntimeServices`. Without it + // the app refuses every new role ("cannot be checked here") and nothing else fails. + "ThreadRoleTargets.make", ]; const missingForkLayers = (spans: ReadonlySet) => From 3209d17674c20db6a92c20eb662764a91e974cba Mon Sep 17 00:00:00 2001 From: John Doe <123843277+scuttletoby@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:25:58 +0100 Subject: [PATCH 2/2] fix(#946): server-list MCP guard no longer allows a call because of its event name The server-list guard returned allow when hook_event_name was not "PreToolUse", so with servers ["gh"] and a missing or other event name a call like mcp__x__Bash (server x, not listed) passed. The guard is registered only under PreToolUse, so the check is dropped, as it was for the builtinTools "none" guard. The event-name test loop is shared by both guards: for a missing, undefined, empty, "PostToolUse", "pretooluse" or numeric event name, each call made twice, a left-out server's tool is refused and a listed server's tool passes. Co-Authored-By: Claude Opus 5.5 --- .../src/fork/claudeMcpProfile.adapter.test.ts | 102 ++++++++++++------ apps/server/src/fork/claudeMcpProfile.ts | 14 +-- 2 files changed, 80 insertions(+), 36 deletions(-) diff --git a/apps/server/src/fork/claudeMcpProfile.adapter.test.ts b/apps/server/src/fork/claudeMcpProfile.adapter.test.ts index 50711002a446b..6b359f05e7374 100644 --- a/apps/server/src/fork/claudeMcpProfile.adapter.test.ts +++ b/apps/server/src/fork/claudeMcpProfile.adapter.test.ts @@ -248,6 +248,65 @@ const preToolUse = ( return answers; }); +/** Hook input for `tool` whose `hook_event_name` is `eventName`, or left out when `omitEvent`. */ +const eventCall = (tool: string, eventName: unknown, omitEvent: boolean) => ({ + ...(omitEvent ? {} : { hook_event_name: eventName }), + tool_name: tool, + tool_input: {}, + tool_use_id: "toolu_profile", + session_id: NATIVE_THREAD, + transcript_path: "/tmp/transcript.jsonl", + cwd: "/workspace", +}); + +/** Event names a guard must not trust: missing, undefined, empty, another event, wrong case, a number. */ +const UNTRUSTED_EVENTS: ReadonlyArray<[label: string, eventName: unknown, omit: boolean]> = [ + ["missing", undefined, true], + ["undefined", undefined, false], + ["empty", "", false], + ["PostToolUse", "PostToolUse", false], + ["lower case", "pretooluse", false], + ["not a string", 7, false], +]; + +const decision = (answer: unknown) => + (answer as { hookSpecificOutput?: { permissionDecision?: string } } | undefined) + ?.hookSpecificOutput?.permissionDecision; + +/** + * Under every untrusted event name, `refusedTool` is refused and `allowedTool` passes. Each call is + * made twice: the same call made again gets the same answer, as nothing is remembered between calls. + */ +const expectPerEventName = ( + options: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined, + refusedTool: string, + allowedTool: string, +) => + Effect.gen(function* () { + for (const [label, eventName, omit] of UNTRUSTED_EVENTS) { + for (const attempt of [1, 2]) { + const answers = yield* preToolUse( + options, + refusedTool, + eventCall(refusedTool, eventName, omit), + ); + assert.isTrue( + answers.some((answer) => decision(answer) === "deny"), + `${refusedTool}, event ${label}, try ${attempt}: expected a deny`, + ); + const passed = yield* preToolUse( + options, + allowedTool, + eventCall(allowedTool, eventName, omit), + ); + assert.isNotEmpty(passed, `${allowedTool}, event ${label}, try ${attempt}: no hook ran`); + for (const answer of passed) { + assert.deepStrictEqual(answer, {}, `${allowedTool}, event ${label}, try ${attempt}`); + } + } + } + }); + describe("ClaudeAdapterV2 MCP profile", () => { it.effect("with no profile set, opens Claude as before and never checks its servers", () => Effect.gen(function* () { @@ -467,36 +526,19 @@ describe("ClaudeAdapterV2 MCP profile", () => { Effect.gen(function* () { const bare = { servers: "all", t3Tools: "all", builtinTools: "none" } as const; const { opened } = yield* runTurns({ mcpProfile: "bare", mcpProfiles: { bare } }, []); - const decision = (answer: unknown) => - (answer as { hookSpecificOutput?: { permissionDecision?: string } } | undefined) - ?.hookSpecificOutput?.permissionDecision; - const call = (tool: string, eventName: unknown, omitEvent = false) => ({ - ...(omitEvent ? {} : { hook_event_name: eventName }), - tool_name: tool, - tool_input: {}, - tool_use_id: "toolu_profile", - session_id: NATIVE_THREAD, - transcript_path: "/tmp/transcript.jsonl", - cwd: "/workspace", - }); - const events: ReadonlyArray<[label: string, eventName: unknown, omit: boolean]> = [ - ["missing", undefined, true], - ["undefined", undefined, false], - ["empty", "", false], - ["PostToolUse", "PostToolUse", false], - ["lower case", "pretooluse", false], - ["not a string", 7, false], - ]; - for (const [label, eventName, omit] of events) { - // The same call made again gets the same answer: nothing is remembered between calls. - for (const attempt of [1, 2]) { - const [refused] = yield* preToolUse(opened[0], "Bash", call("Bash", eventName, omit)); - assert.strictEqual(decision(refused), "deny", `Bash, event ${label}, try ${attempt}`); - const mcpTool = "mcp__t3-code__t3_thread_list"; - const [allowed] = yield* preToolUse(opened[0], mcpTool, call(mcpTool, eventName, omit)); - assert.deepStrictEqual(allowed, {}, `${mcpTool}, event ${label}, try ${attempt}`); - } - } + yield* expectPerEventName(opened[0], "Bash", "mcp__t3-code__t3_thread_list"); + }), + ); + + it.effect( + "the server-list guard refuses a left-out server, whatever the event name says (#946)", + () => + Effect.gen(function* () { + const gh = { servers: ["gh"], t3Tools: "all" } as const; + const { opened } = yield* runTurns({ mcpProfile: "gh", mcpProfiles: { gh } }, [ + { name: "gh", status: "connected" }, + ]); + yield* expectPerEventName(opened[0], "mcp__x__Bash", "mcp__gh__list_issues"); }), ); diff --git a/apps/server/src/fork/claudeMcpProfile.ts b/apps/server/src/fork/claudeMcpProfile.ts index d32caab38c46a..3e12feda63f9d 100644 --- a/apps/server/src/fork/claudeMcpProfile.ts +++ b/apps/server/src/fork/claudeMcpProfile.ts @@ -10,6 +10,7 @@ import { type HookCallbackMatcher, type HookEvent, type McpServerStatus, + type PreToolUseHookInput, type Settings as ClaudeSettings, } from "@anthropic-ai/claude-agent-sdk"; import { @@ -263,10 +264,10 @@ export function claudeMcpCallOutsideProfile( } /** - * Refuses, at the moment of the call, a tool call to an MCP server the profile leaves out. The - * options keep such servers from loading; this covers what loads anyway (a managed policy that - * drops T3's allowlist, an agent file's servers, a server added mid-session). Hooks run in every - * permission mode and inside sub-agents. + * Refuses, at the call, a tool of an MCP server the profile leaves out but that loaded anyway (a + * managed policy that drops T3's allowlist, an agent file's servers, one added mid-session). Hooks + * run in every permission mode and in sub-agents. It never trusts `hook_event_name`: whatever it + * says (missing, empty, another event), a left-out server's tool is refused. */ const claudeMcpProfileGuard = ( @@ -275,8 +276,9 @@ const claudeMcpProfileGuard = t3ServerNames: ReadonlyArray, ): HookCallback => async (input) => { - if (input.hook_event_name !== "PreToolUse") return {}; - const server = claudeMcpCallOutsideProfile(servers, t3ServerNames, input); + // Registered only under PreToolUse, so the input is read as one whatever its event name. + const call = input as PreToolUseHookInput; + const server = claudeMcpCallOutsideProfile(servers, t3ServerNames, call); if (server === undefined) return {}; return { hookSpecificOutput: {