A drop-in Python PreToolUse hook that intercepts every Bash / Write / Edit call Claude Code wants to make, scans it against 12 destructive patterns and protected paths, and returns permissionDecision: "deny" — so the command never reaches your shell.
Agent frameworks ship approve-all toggles and "yolo mode". I built this after an agent tried to run a recursive delete against a mounted volume during a refactor. Prompts are not a safety boundary — a deterministic hook that runs outside the model is.
Exit code 0 + JSON decision. The command is rejected before a single byte hits your shell — not logged after the fact.
Stdlib only (json/re/sys). No venv, no daemon, no network calls. Works on Linux and macOS with Python 3.8+.
Every block is appended to .claude/guard-blocks.log with timestamp + rule name, so you can tune false positives instead of guessing.
guard.json lets you whitelist paths, downgrade rules to warn-only, or add your own regexes without editing source.
| Pattern | Rule | Decision |
|---|---|---|
rm -rf /, rm -rf ~, rm -rf * | recursive-root-delete | deny |
chmod 777 on system paths | world-writable-perms | deny |
curl ... | sh · wget ... | bash | remote-code-pipe | deny |
dd of=/dev/sda, mkfs, fdisk | raw-device-write | deny |
:(){ :|:& };: | fork-bomb | deny |
Writes to /etc/passwd, /etc/shadow, ~/.ssh/, .env | protected-path-write | deny |
git push --force to main/master | destructive-git | deny |
shutdown, reboot, init 0 | host-power | ask |
# 1. drop the hook somewhere stable
mkdir -p .claude/hooks && cp pretool_guard.py .claude/hooks/
# 2. register it as a PreToolUse hook in .claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|Write|Edit|MultiEdit",
"hooks": [
{ "type": "command", "command": "python3 .claude/hooks/pretool_guard.py" }
]
}
]
}
}
# 3. verify it actually denies — the test suite ships with the kit
$ echo '{"tool_name":"Bash","tool_input":{"command":"rm -rf /"}}' | python3 .claude/hooks/pretool_guard.py
{"hookSpecificOutput": {"permissionDecision": "deny",
"permissionDecisionReason": "blocked by rule: recursive-root-delete"}}
$ python3 -m pytest -q test_pretool_guard.py
34 passed in 0.11s
$(...), path traversal, symlink tricks)deny / ask / warn / offOne-time payment, delivered instantly. Pay with PayPal, card via PayPal, or crypto (Base / USDC).
Pay $19 with PayPal →