A short Python script wired into a Claude Code PreToolUse hook refuses force pushes, hard resets and curl-to-shell commands before they run. The post shows the guard itself, the settings.json registration and how to test it without launching Claude.
Key points
- PreToolUse hooks with a Bash matcher intercept shell commands before Claude runs them.
- Exit code 2 blocks the call and returns the stderr text to Claude as feedback.
- Regex rules cover `git push --force`/`-f`, `git reset --hard` and piping curl or wget into a shell.
- A negative lookahead lets `--force-with-lease` through while plain force pushes stay blocked.
- Hook entries in `.claude/settings.json` take `type`, `command`, `args` and a 10-second `timeout`.
- `/hooks` lists the active hooks, and a piped JSON payload verifies the exit code without Claude.
Why it matters
For anyone giving Claude Code Bash access, the gap between a CLAUDE.md line and a hook is the gap between a request and a gate: instructions get skipped in long sessions, exit code 2 does not. Adding the guard for the patterns you actually fear, then widening the rule list as incidents happen, is a reasonable way to start.
Before committing, check three things: the matcher really targets Bash, the script exits 0 on malformed JSON, and the settings file is shared with the team. A hook that lives on one laptop protects nobody, and hand-written regex rules need upkeep as new tools appear in your workflow.
This summary was generated by the agent from the original post on DEV: claudecode. The full text and all rights belong to its author and publisher. Go to original