A text-pattern guard is not a security boundary
A guard that refuses a word in the text of a command judges a spelling, not an effect: any spelling that avoids that word gets through, and even a list of allowed commands matched by exact equality lets through the code that an allowed command reads from a file the agent can edit.
A guard refuses any command that contains the word rm. Three ways of writing the same deletion get through it, and the following demonstration checks this in a throwaway folder.
The guard and its four trials
mkdir -p demo-guard && cd demo-guard
touch target1.txt target2.txt target3.txt target4.txt
cat > guard.sh << 'EOF'
#!/bin/bash
cmd="$1"
if echo "$cmd" | grep -qw 'rm'; then
echo "REFUSED: forbidden pattern detected in the typed command"
exit 1
fi
echo "ALLOWED: no forbidden pattern found, running"
eval "$cmd"
EOF
chmod +x guard.sh
./guard.sh "rm target1.txt"
./guard.sh "r'm' target2.txt"
./guard.sh 'A=r; B=m; $A$B target3.txt'
ENC=$(printf 'rm target4.txt' | base64)
./guard.sh "echo $ENC | base64 -d | bash"
ls -1
REFUSED: forbidden pattern detected in the typed command
ALLOWED: no forbidden pattern found, running
ALLOWED: no forbidden pattern found, running
ALLOWED: no forbidden pattern found, running
guard.sh
target1.txt
The first trial is refused and target1.txt survives. The second splits the word with apostrophes, which the interpreter removes. The third assembles the word from two one-letter variables. The fourth encodes the command in base64, which base64 -d | bash decodes then runs.
Why fixing case by case closes nothing
Forbidding the apostrophe, variables or base64 closes three doors, not the spellings that come next. The Claude Code permissions page says this of its own rules: a Bash(rm *) deny rule stops rm -rf build/ but lets /bin/rm -rf build/ through, and is not a security boundary (see a read rule).
Exact equality shrinks the surface without closing it
A list of allowed commands holds up better when it demands exact string equality and admits no launcher program, that is, no program that runs another command passed to it: bash, sh -c, eval, find -exec, xargs. It then closes the rewrites. It leaves open the code that an allowed command reads elsewhere. Git runs the program named by the repository's core.fsmonitor setting when it refreshes its index, during a git status.
mkdir -p demo-fsmonitor && cd demo-fsmonitor
git init -q
git config core.fsmonitor 'echo RUN_BY_GIT_STATUS > witness.txt; false'
git status > /dev/null 2>&1
cat witness.txt
RUN_BY_GIT_STATUS
A git status identical to the allowed entry ran what an agent can write into .git/config. The other route no longer inspects the text: the sandbox, an isolation the operating system imposes on shell commands and their child processes, bounds files and network whatever the command that launched the code. It runs on macOS, Linux and WSL2, not on native Windows.
The sandbox does not replace the permission prompt. Since version 2.1.325 of the changelog, a sandboxed Bash command follows the normal permission flow by default; sandbox.autoAllowBashIfSandboxed: true restores running without prompts.
A guard that is too strict also refuses wrongly
A guard that refuses anything resembling a write teaches people to get around it: it blocks a redirection to a new file, a heredoc made of pure data, a grep whose pattern quotes a dangerous command. Fix it with narrow exemptions and a test bench that keeps a control: a known gap detected, every refusal held. Do not turn on sandbox.allowNestedUserNamespaces: it lets sandboxed commands on Linux get around the sandbox's file protections.
Measuring the guard, watching what it depends on
Log every refusal and what follows it: a refusal lifted by replaying the identical command avoided nothing. Prefer a guard limited to the destructive effect, deletion or overwriting, rather than a word in the text.
A guard that relies on a Claude Code flag breaks silently if an update renames it. A sentinel, run from a SessionStart hook, prints the flags missing from claude --help. Its control, a nonexistent flag, must be reported, otherwise the detector is broken.
One more fix against an exact-equality list
Added: also refuse the lone apostrophe. Next bypass: two pasted variables, still allowed.
Allowed: git status Refused: bash -c 'git status' Still open: the program that core.fsmonitor names in .git/config
Three bypasses, what the guard sees, what runs
| Bypass | What the guard sees | What actually runs |
|---|---|---|
| Split by apostrophes, r'm' | The letters r and m separated by an apostrophe, no whole word rm | The deletion, once the interpreter has removed the apostrophes |
| Variable concatenation, $A$B | Two one-letter variables, never r and m side by side in the text | The deletion, once the variables are assembled at run time |
| Base64 encoding | A string of characters with no visible link to the forbidden word | The deletion, once decoded then passed to the interpreter |
An administrator writes a guard that refuses to run any command containing the word rm, to stop an automated script from deleting files. He tests it with the command rm notes.txt, which is refused. He then deploys the guard on his production system.
Write in one sentence what this situation establishes, and in one sentence what it does not establish.
What this establishes: The test establishes that the guard refused the command rm notes.txt.
What this does not establish: It does not establish what it does with another command, whether it spells out rm in full or obtains the same deletion through apostrophes, variables or an encoding, no other command having been tested.
The three most common miscalibrations
- Too broad The deployed guard stops the automated script from deleting files in production, since the deletion test was refused.
- Too narrow The test establishes that the guard was run on a command, and nothing about the answer it gave.
- Beside the point The test shows that the automated script targeted by the guard really does run file deletions.
- A guard that looks for a word in the text of a command judges a spelling, not the effect the interpreter will produce.
- Each bypass that gets fixed leaves open the spellings the fix did not foresee.
- A list allowed by exact equality, with no launcher program, closes the rewrites of a command but not the code that command reads in its configuration.
- The sandbox bounds files and network through the operating system, whatever the command that launched the code, and its Bash commands go through the permission prompt unless you set autoAllowBashIfSandboxed.
- A guard that is too strict refuses wrongly and teaches people to get around it: fix it with narrow exemptions, measure it by logging its refusals, and a flag sentinel, tested on a nonexistent flag, reports the silent failure.
Replay the core.fsmonitor demonstration in a new folder and check that witness.txt contains the expected line. Then write a guard of a few lines that refuses a command containing a word of your choice, test it with that word written as is then split by apostrophes, and note which of the two trials it lets through.
Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.
- Claude Code, configuring permissions, Bash rule limits section, consulted on 2026-09-28 accessed on 2026-09-28
- Claude Code, sandboxing, consulted on 2026-09-28 accessed on 2026-09-28
- Git, git config documentation, core.fsmonitor entry, consulted on 2026-09-28 accessed on 2026-09-28
- Claude Code changelog, versions 2.1.305 and 2.1.309, deniedMcpServers and sandbox accessed on 2026-10-24
- Claude Code, command-line reference: flags checked by the sentinel accessed on 2026-11-16
- Claude Code changelog, version 2.1.325, sandboxed Bash commands and permission flow accessed on 2026-11-16