DEV Community

Jack Napier
Jack Napier

Posted on

Claude Code Hooks: 3 Examples That Stop the Agent From Breaking Things

Claude Code is great until it runs git reset --hard on an afternoon of work, or "helpfully" edits your .env. Hooks fix that. They're shell commands Claude Code runs at fixed points (before a tool call, after an edit, when it stops), and they can block, allow or ask.

Here are three hooks I put in every project. All of them are free on GitHub (link at the end).

How a hook works in 30 seconds

Hooks live in .claude/settings.json. Each one gets JSON on stdin describing what Claude is about to do. For a PreToolUse hook, you answer with a decision:

{"hookSpecificOutput": {"hookEventName": "PreToolUse",
  "permissionDecision": "deny",
  "permissionDecisionReason": "why"}}
Enter fullscreen mode Exit fullscreen mode

deny blocks the call and shows Claude the reason. ask hands the decision to you. Exit 0 with no output lets it through.

1. Block dangerous shell commands

A PreToolUse hook on the Bash tool. It checks the command against two lists of regexes:

  • deny: rm -rf / or ~, force push, git reset --hard, git clean -f, mkfs, dd of=/dev/…
  • ask: git push, sudo, curl … | sh, DROP TABLE, package publishing
{"matcher": "Bash", "hooks": [{"type": "command",
  "command": "python \"${CLAUDE_PROJECT_DIR}/.claude/hooks/block_dangerous_bash.py\""}]}
Enter fullscreen mode Exit fullscreen mode

Test it without Claude:

echo '{"tool_input":{"command":"rm -rf /"}}' | python .claude/hooks/block_dangerous_bash.py
Enter fullscreen mode Exit fullscreen mode

2. Protect files Claude should never edit

A PreToolUse hook on Edit|Write|MultiEdit. It denies any write to .env*, *.pem, *.key, credentials files, lockfiles and .git/. You can add your own globs in .claude/protected.txt, one per line.

Lockfiles are on the list on purpose: when Claude edits package-lock.json by hand, you get installs that work on your machine and nowhere else.

3. Auto-format after every edit

A PostToolUse hook on Edit|Write|MultiEdit. It looks at the file extension and runs the formatter if it's installed: prettier for JS/TS/CSS/JSON/MD, ruff or black for Python, gofmt, rustfmt. No formatter installed means it quietly does nothing.

The point isn't style. Claude reads the formatted file on its next turn, so diffs stay small and it stops "fixing" whitespace.

Hooks vs skills

People mix these up. A skill is instructions Claude chooses to load when a task matches its description. A hook is code that always runs, whether Claude wants it or not. Use skills for how to do a task well, hooks for rules that must never be broken.

Get them

All three hooks, plus three skills (clean commits, PR descriptions, codebase explainer), are MIT on GitHub: https://github.com/MrNapier89/claude-code-skills-hooks

Copy the .claude/ folder into your repo and they work.

If you want more, I sell a 10-hook pack with a secret guard on prompts, "run the tests before you say done", lint feedback, an audit log and desktop notifications, plus a guide. Disclosure: that's my product.

Top comments (3)

Collapse
 
ky_agents profile image
Ky • • Edited

Nice read, thanks. I am a fan of the topic covering .env and lockfiles, not just rm -rf.

At one point in my project I noticed the agent doesn't have to edit .env to do damage. Being able to read it is enough of a problem. A Read(./.env) rule in permissions.deny helps a bit here. It doesn't cover grep -r or Python/Node scripts that open files themselves, but for that a good old sandbox with denyRead does the trick. By default the sandbox can still read ~/.ssh and ~/.aws, so those are worth adding too. (sandboxing docs).

Just a heads up for readers. Always read the scripts before copying a .claude/ folder from GitHub. Hooks run with your user permissions, so better safe than sorry.

On another note, have you tried combining hooks with the sandbox?

Collapse
 
mrnapier profile image
Jack Napier •

Thanks Ky, great additions! The read-vs-edit point is a really good one. I focused mostly on writes, but being able to just read .env is arguably the bigger risk. Good call on ~/.ssh and ~/.aws too, I'll add those to the post.

Haven't combined them properly yet, but it's next on my list. The way I see it, they cover different layers: a PreToolUse hook catches obvious patterns early and can tell the agent why it was blocked. The sandbox is the hard OS-level backstop for things the hook can't see, like a script opening .env itself. Have you found a setup that works well for you?

Collapse
 
ky_agents profile image
Ky •

Agreed on hooks vs sandbox. I have my sandbox set to refuse to start if it can't run (failIfUnavailable), otherwise Claude Code silently runs without it. Then deny rules for ~/.ssh, ~/.aws and .env, plus a short list of allowed hosts. Lastly, a fake key as an alarm, in case something goes looking.

To be honest, I don't go heavy on hooks. For sure they are useful, but also not hard to get around. Still working on my dream setup, but I'm getting there.

Happy building!