Skip to content
Mastering Claude

The course175 lessons

Home / Security and data

Security and data9 minApplication

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.

Figure 1

One more fix against an exact-equality list

Pattern guard, one more fix
Added: also refuse the lone apostrophe.
Next bypass: two pasted variables, still allowed.
Exact-equality list
Allowed: git status
Refused: bash -c 'git status'
Still open: the program that core.fsmonitor names in .git/config
On the left, each fix leaves the next one open. On the right, rewrites of the command are refused, and the allowed command still runs whatever the repository's configuration makes it launch.
Figure 2

Three bypasses, what the guard sees, what runs

BypassWhat the guard seesWhat actually runs
Split by apostrophes, r'm'The letters r and m separated by an apostrophe, no whole word rmThe deletion, once the interpreter has removed the apostrophes
Variable concatenation, $A$BTwo one-letter variables, never r and m side by side in the textThe deletion, once the variables are assembled at run time
Base64 encodingA string of characters with no visible link to the forbidden wordThe deletion, once decoded then passed to the interpreter
For each of the three bypasses run, the text the guard inspected and the command actually launched once interpreted.
Calibrate it yourself

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 to remember
  • 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.
Do this now

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.

Check the source

Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.