Rule, hook or sandbox: which one actually stops your agent
Claude Code has four guard layers and none is a full wall on its own. Here is what each one stops.
Published here , tested on Claude Code hooks with jq · 3 sources · Resume reading
You told your agent never to delete files. It still can. Claude Code has four guard layers, and none is a full wall on its own. Here is what each one stops (docs checked Nov 8).
Layer 1: CLAUDE.md
A line in CLAUDE.md shapes what Claude tries to do. Claude Code enforces nothing from it [1].
Layer 2: Deny rules
A deny rule like Bash(rm *) stops rm -rf build/. It does not stop /bin/rm -rf build/ or bash -c 'rm -rf build/' [1].
Rules are checked deny, then ask, then allow. The first match wins.
{
"permissions": {
"deny": ["Bash(rm *)"]
}
}Layer 3: PreToolUse hook
A hook reads the full command. Exit 2 blocks the call before permission rules are checked, even when an allow rule matches [2]. It blocks only what your script matches.
#!/bin/bash
cmd=$(jq -r '.tool_input.command')
if grep -qw rm <<<"$cmd"; then
echo "Blocked: $cmd" >&2
exit 2
fiMake it executable (chmod +x) and register it in settings [2]:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh",
"args": []
}
]
}
]
}
}Warning: needs jq installed. Without it the script blocks nothing.
In my test (Nov 8): the hook returned exit 2 for rm, /bin/rm and bash -c 'rm', and exit 0 for find build -delete. A hook is as good as your match.
Layer 4: Sandbox
The sandbox is an OS boundary for shell commands (Bash, PowerShell, Monitor) and every process they start [3]. Type /sandbox in a session to turn it on. Works on macOS, Linux and WSL2. On native Windows, commands run unsandboxed.
Warning: by default the sandbox still lets commands write and delete inside the project folder, and sandboxed commands run without a prompt. List paths to protect in sandbox.filesystem.denyWrite [3].
Read, Edit, WebFetch, MCP servers and hooks run outside the sandbox [3].
Use all four
Each layer catches what the others miss. CLAUDE.md steers intent, deny rules block named patterns, a hook inspects the full command, and the sandbox walls off the machine.
Send this to the friend who told their agent never to delete files and assumes it listens.
Keep this guide
Filed under Claude Code, Security
Questions about this guide
Does CLAUDE.md enforce rules?
No. CLAUDE.md shapes what Claude tries to do. Claude Code enforces nothing from it.
Does the sandbox protect everything?
No. It covers shell commands only. Read, Edit, WebFetch, MCP servers and hooks run outside it.
Sources
- Claude Code docs: permissionscode.claude.com
- Claude Code docs: hookscode.claude.com
- Claude Code docs: sandboxingcode.claude.com