Skip to content

Commit 1a297f5

Browse files
juliusmarmingecodex
andcommitted
Harden Claude text generation tool isolation
Refresh PR #4062 with defense-in-depth tool restrictions and a focused regression test for the empty --tools argument. Co-authored-by: codex <[email protected]>
1 parent 3de2da0 commit 1a297f5

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎apps/server/src/textGeneration/ClaudeTextGeneration.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ function makeFakeClaudeBinary(dir: string) {
4545
" exit 3",
4646
" fi",
4747
"fi",
48+
'if [ -n "$T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS" ]; then',
49+
' previous_arg=""',
50+
' empty_tools_found=""',
51+
' for arg in "$@"; do',
52+
' if [ "$previous_arg" = "--tools" ]; then',
53+
' [ -z "$arg" ] || {',
54+
' printf "%s\\n" "--tools was not followed by an empty argument" >&2',
55+
" exit 6",
56+
" }",
57+
' empty_tools_found="yes"',
58+
" break",
59+
" fi",
60+
' previous_arg="$arg"',
61+
" done",
62+
' [ "$empty_tools_found" = "yes" ] || {',
63+
' printf "%s\\n" "missing --tools empty argument" >&2',
64+
" exit 7",
65+
" }",
66+
"fi",
4867
'if [ -n "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" ]; then',
4968
' printf "%s" "$stdin_content" | grep -F -- "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" >/dev/null || {',
5069
' printf "%s\\n" "stdin missing expected content" >&2',
@@ -75,6 +94,7 @@ function withFakeClaudeEnv<A, E, R>(
7594
stderr?: string;
7695
argsMustContain?: string;
7796
argsMustNotContain?: string;
97+
requireEmptyTools?: boolean;
7898
stdinMustContain?: string;
7999
configDirMustBe?: string;
80100
claudeConfig?: Partial<ClaudeSettings>;
@@ -91,6 +111,7 @@ function withFakeClaudeEnv<A, E, R>(
91111
const previousStderr = process.env.T3_FAKE_CLAUDE_STDERR;
92112
const previousArgsMustContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN;
93113
const previousArgsMustNotContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
114+
const previousRequireEmptyTools = process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS;
94115
const previousStdinMustContain = process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
95116
const previousConfigDirMustBe = process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE;
96117

@@ -123,6 +144,12 @@ function withFakeClaudeEnv<A, E, R>(
123144
delete process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
124145
}
125146

147+
if (input.requireEmptyTools === true) {
148+
process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS = "1";
149+
} else {
150+
delete process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS;
151+
}
152+
126153
if (input.stdinMustContain !== undefined) {
127154
process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN = input.stdinMustContain;
128155
} else {
@@ -169,6 +196,12 @@ function withFakeClaudeEnv<A, E, R>(
169196
process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN = previousArgsMustNotContain;
170197
}
171198

199+
if (previousRequireEmptyTools === undefined) {
200+
delete process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS;
201+
} else {
202+
process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS = previousRequireEmptyTools;
203+
}
204+
172205
if (previousStdinMustContain === undefined) {
173206
delete process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
174207
} else {
@@ -190,6 +223,35 @@ function withFakeClaudeEnv<A, E, R>(
190223
}
191224

192225
it.layer(ClaudeTextGenerationTestLayer)("ClaudeTextGeneration", (it) => {
226+
it.effect("disables all Claude tools for headless text generation", () =>
227+
withFakeClaudeEnv(
228+
{
229+
output: JSON.stringify({
230+
structured_output: {
231+
title: "No tools available",
232+
},
233+
}),
234+
argsMustContain:
235+
"--permission-mode dontAsk --disallowedTools Agent,Bash,Edit,Glob,Grep,Monitor,NotebookEdit,PowerShell,Read,Skill,WebFetch,WebSearch,Workflow,Write --strict-mcp-config --tools",
236+
argsMustNotContain: "--dangerously-skip-permissions",
237+
requireEmptyTools: true,
238+
},
239+
(textGeneration) =>
240+
Effect.gen(function* () {
241+
const generated = yield* textGeneration.generateThreadTitle({
242+
cwd: process.cwd(),
243+
message: "Name this thread without using tools.",
244+
modelSelection: {
245+
instanceId: ProviderInstanceId.make("claudeAgent"),
246+
model: "claude-haiku-4-5",
247+
},
248+
});
249+
250+
expect(generated.title).toBe("No tools available");
251+
}),
252+
),
253+
);
254+
193255
it.effect("forwards Claude thinking settings for Haiku without passing effort", () =>
194256
withFakeClaudeEnv(
195257
{

‎apps/server/src/textGeneration/ClaudeTextGeneration.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ import {
4646
import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts";
4747

4848
const CLAUDE_TIMEOUT_MS = 180_000;
49+
const CLAUDE_TEXT_GENERATION_DISALLOWED_TOOLS = [
50+
"Agent",
51+
"Bash",
52+
"Edit",
53+
"Glob",
54+
"Grep",
55+
"Monitor",
56+
"NotebookEdit",
57+
"PowerShell",
58+
"Read",
59+
"Skill",
60+
"WebFetch",
61+
"WebSearch",
62+
"Workflow",
63+
"Write",
64+
].join(",");
4965

5066
/**
5167
* Schema for the wrapper JSON returned by `claude -p --output-format json`.
@@ -169,6 +185,14 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
169185
resolveClaudeApiModelId(modelSelection),
170186
...(cliEffort ? ["--effort", cliEffort] : []),
171187
...(settingsJson ? ["--settings", settingsJson] : []),
188+
// `--tools ""` is the primary tool-surface restriction. Older Claude
189+
// versions treated an empty value as omitted, so pair it with a
190+
// deny-by-default mode and an explicit legacy built-in denylist.
191+
"--permission-mode",
192+
"dontAsk",
193+
"--disallowedTools",
194+
CLAUDE_TEXT_GENERATION_DISALLOWED_TOOLS,
195+
"--strict-mcp-config",
172196
"--tools",
173197
"",
174198
],

0 commit comments

Comments
 (0)