Compare commits

...
Author SHA1 Message Date
Julius Marmingeandcodex 1a297f5c2d 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]>
2026-07-20 17:11:30 +02:00
Denys 3de2da09bd fix: disallow JSON generation in Claude Code from running any tools 2026-07-20 17:02:47 +02:00
2 changed files with 88 additions and 1 deletions

No files matched your search

@@ -45,6 +45,25 @@ function makeFakeClaudeBinary(dir: string) {
" exit 3",
" fi",
"fi",
'if [ -n "$T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS" ]; then',
' previous_arg=""',
' empty_tools_found=""',
' for arg in "$@"; do',
' if [ "$previous_arg" = "--tools" ]; then',
' [ -z "$arg" ] || {',
' printf "%s\\n" "--tools was not followed by an empty argument" >&2',
" exit 6",
" }",
' empty_tools_found="yes"',
" break",
" fi",
' previous_arg="$arg"',
" done",
' [ "$empty_tools_found" = "yes" ] || {',
' printf "%s\\n" "missing --tools empty argument" >&2',
" exit 7",
" }",
"fi",
'if [ -n "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" ]; then',
' printf "%s" "$stdin_content" | grep -F -- "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" >/dev/null || {',
' printf "%s\\n" "stdin missing expected content" >&2',
@@ -75,6 +94,7 @@ function withFakeClaudeEnv<A, E, R>(
stderr?: string;
argsMustContain?: string;
argsMustNotContain?: string;
requireEmptyTools?: boolean;
stdinMustContain?: string;
configDirMustBe?: string;
claudeConfig?: Partial<ClaudeSettings>;
@@ -91,6 +111,7 @@ function withFakeClaudeEnv<A, E, R>(
const previousStderr = process.env.T3_FAKE_CLAUDE_STDERR;
const previousArgsMustContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN;
const previousArgsMustNotContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
const previousRequireEmptyTools = process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS;
const previousStdinMustContain = process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
const previousConfigDirMustBe = process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE;
@@ -123,6 +144,12 @@ function withFakeClaudeEnv<A, E, R>(
delete process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
}
if (input.requireEmptyTools === true) {
process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS = "1";
} else {
delete process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS;
}
if (input.stdinMustContain !== undefined) {
process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN = input.stdinMustContain;
} else {
@@ -169,6 +196,12 @@ function withFakeClaudeEnv<A, E, R>(
process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN = previousArgsMustNotContain;
}
if (previousRequireEmptyTools === undefined) {
delete process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS;
} else {
process.env.T3_FAKE_CLAUDE_REQUIRE_EMPTY_TOOLS = previousRequireEmptyTools;
}
if (previousStdinMustContain === undefined) {
delete process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
} else {
@@ -190,6 +223,35 @@ function withFakeClaudeEnv<A, E, R>(
}
it.layer(ClaudeTextGenerationTestLayer)("ClaudeTextGeneration", (it) => {
it.effect("disables all Claude tools for headless text generation", () =>
withFakeClaudeEnv(
{
output: JSON.stringify({
structured_output: {
title: "No tools available",
},
}),
argsMustContain:
"--permission-mode dontAsk --disallowedTools Agent,Bash,Edit,Glob,Grep,Monitor,NotebookEdit,PowerShell,Read,Skill,WebFetch,WebSearch,Workflow,Write --strict-mcp-config --tools",
argsMustNotContain: "--dangerously-skip-permissions",
requireEmptyTools: true,
},
(textGeneration) =>
Effect.gen(function* () {
const generated = yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Name this thread without using tools.",
modelSelection: {
instanceId: ProviderInstanceId.make("claudeAgent"),
model: "claude-haiku-4-5",
},
});
expect(generated.title).toBe("No tools available");
}),
),
);
it.effect("forwards Claude thinking settings for Haiku without passing effort", () =>
withFakeClaudeEnv(
{
@@ -46,6 +46,22 @@ import {
import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts";
const CLAUDE_TIMEOUT_MS = 180_000;
const CLAUDE_TEXT_GENERATION_DISALLOWED_TOOLS = [
"Agent",
"Bash",
"Edit",
"Glob",
"Grep",
"Monitor",
"NotebookEdit",
"PowerShell",
"Read",
"Skill",
"WebFetch",
"WebSearch",
"Workflow",
"Write",
].join(",");
/**
* Schema for the wrapper JSON returned by `claude -p --output-format json`.
@@ -169,7 +185,16 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
resolveClaudeApiModelId(modelSelection),
...(cliEffort ? ["--effort", cliEffort] : []),
...(settingsJson ? ["--settings", settingsJson] : []),
"--dangerously-skip-permissions",
// `--tools ""` is the primary tool-surface restriction. Older Claude
// versions treated an empty value as omitted, so pair it with a
// deny-by-default mode and an explicit legacy built-in denylist.
"--permission-mode",
"dontAsk",
"--disallowedTools",
CLAUDE_TEXT_GENERATION_DISALLOWED_TOOLS,
"--strict-mcp-config",
"--tools",
"",
],
{ env: claudeEnvironment },
);