Claude Code · PreToolUse hook

One rm -rf / away from a dead machine.
Block it before the tool runs.

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.

Buy the kit — $19
One-time · instant delivery · MIT license · 34 unit tests included

Why this exists

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.

Runs pre-execution

Exit code 0 + JSON decision. The command is rejected before a single byte hits your shell — not logged after the fact.

Zero dependencies

Stdlib only (json/re/sys). No venv, no daemon, no network calls. Works on Linux and macOS with Python 3.8+.

Audit trail

Every block is appended to .claude/guard-blocks.log with timestamp + rule name, so you can tune false positives instead of guessing.

Config-driven

guard.json lets you whitelist paths, downgrade rules to warn-only, or add your own regexes without editing source.

What it blocks

PatternRuleDecision
rm -rf /, rm -rf ~, rm -rf *recursive-root-deletedeny
chmod 777 on system pathsworld-writable-permsdeny
curl ... | sh · wget ... | bashremote-code-pipedeny
dd of=/dev/sda, mkfs, fdiskraw-device-writedeny
:(){ :|:& };:fork-bombdeny
Writes to /etc/passwd, /etc/shadow, ~/.ssh/, .envprotected-path-writedeny
git push --force to main/masterdestructive-gitdeny
shutdown, reboot, init 0host-powerask

Install (2 minutes)

# 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

What's in the download

Get the Safety Guard Kit — $19

One-time payment, delivered instantly. Pay with PayPal, card via PayPal, or crypto (Base / USDC).

Pay $19 with PayPal →
Prefer crypto? Note your order code BOX-5341f4d1b674 in the payment memo so delivery can be matched.
Questions before buying? Reply to any of my published articles on dev.to and I'll answer.