Claude Code Safety Hooks

Four Claude Code hooks that deny the commands you never want an agent to run, ask before the risky ones, keep it out of your secrets, and make it fix a key it just wrote into a file.

Claude Code Safety Hooks cover

What each hook blocks

Four tested Claude Code hooks, packaged as a plugin, that stop the mistakes an agent makes when it moves fast:

HookEventWhat it does
guard_bash.pyPreToolUse BashDenies rm -rf of /, ~, *, . or a top-level dir, force-push or delete of main/master/production/release/*, curl … | sh, chmod -R 777, writes to disk devices, fork bombs, env-var exfiltration. Asks before git reset --hard, git clean -fdx, discarding changes, branch -D, DROP/TRUNCATE through a DB client, printing .env, terraform destroy, kubectl delete ns, aws s3 rm --recursive, npm publish. Checks every part of a && b; c, $( ) and backticks, strips sudo/FOO=1 prefixes.
protect_files.pyPreToolUse Edit|Write|MultiEdit|NotebookEditDenies writes to .env* (not .env.example), keys and certs, .git/, secrets/, *.tfstate, .npmrc/.pypirc/.netrc. Asks before editing lockfiles, CI workflows and Claude/hook settings.
secret_scan.pyPostToolUse Edit|Write|MultiEditIf the file now contains a live-looking AWS, GitHub, Stripe live, Slack, Google, OpenAI or Anthropic key or a private key block, exits 2 so Claude sees it and moves it to an env var. Values are masked in the message.
audit_log.pyPostToolUse *Appends one redacted JSON line per tool call to .claude/logs/tool-audit.jsonl (time, session, agent, tool, command or path).

Every rule has a code (git_reset_hard, pipe_to_shell, …) shown in the reason, so you can switch one off per project.

What you get

The screenshot is real output from the kit’s own commands. Tested from a fresh download of this exact zip: ./run_tests.sh ran 17 tests OK on Python 3.13 and 3.10.

Real terminal output from Claude Code Safety Hooks: demo and passing tests

Install

As a plugin (recommended): add this folder to a plugin marketplace you use, or point Claude Code at it, then enable youraitools-safety-hooks (the plugin's name; Claude Code reserves names starting with claude-). Hooks load from hooks/hooks.json with ${CLAUDE_PLUGIN_ROOT} paths.

Per project, without plugins: copy hooks/ to .claude/hooks/ in your repo and merge settings.example.json into .claude/settings.json. Run /hooks in Claude Code to confirm they are listed.

Requires python3 on PATH (3.9+). No packages, no network.

Try it before you install

python3 tools/try_hooks.py                      # sample commands and files, with the decision for each
python3 tools/try_hooks.py "git push -f origin main" "rm -rf ./build"
./run_tests.sh                                  # 17 test groups, ~90 cases

Configure (optional) .claude/safety-hooks.json

{
  "allow": ["git_reset_hard", "package_publish"],
  "protected_branches": ["main", "release/*"],
  "protected_paths": ["migrations/*", "infra/prod/*"],
  "allow_paths": [".npmrc"],
  "ask_paths": ["docker-compose.prod.yml"],
  "scan_skip": ["fixtures/*"],
  "audit_log": true
}

A broken config file is ignored (the defaults stay on) and the reason goes to stderr.

How decisions work

PreToolUse hooks print permissionDecision: "deny" or "ask" with a reason and exit 0; anything else is silent, so Claude Code's normal permission flow applies (a hook staying silent never approves anything). The secret scan exits 2 after the write, which shows Claude the message so it fixes the file.

Honest limits

  • Pattern-based: it catches the common destructive forms, not every possible way to delete data. Obfuscated commands (eval "$(echo cm0gLXJm | base64 -d)") get through. Keep Claude Code's permission rules on; hooks are a second layer.
  • If a hook can't start (no python3), Claude Code treats it as a non-blocking error and the action goes ahead. On v2.1.295+ you can add "onFailure": "block" to each hook in hooks/hooks.json to fail closed.
  • The secret scan looks for known key formats; it doesn't find passwords or custom tokens, and it runs after the write (the file on disk has the key until Claude fixes it).
  • Windows: needs python3 on PATH in Git Bash; untested in PowerShell-only setups.
  • Not a security audit.

Format and license

Zip, 17 KB, v1.0.1, 14 files, plugin youraitools-safety-hooks. You may use and modify this kit for your own work and for clients you serve, including paid client work. Not affiliated with or endorsed by Anthropic.

FAQ

Does it replace Claude Code permissions?
No; it's a second layer on top of them.
Will it slow Claude down?
Each hook is a short Python script (tens of milliseconds).
Windows?
Works where python3 is on PATH in Git Bash.
How do I get the files?
Right after payment, Stripe’s confirmation page shows your download link.
Refunds?
Email hello@youraitools.co within 14 days if it doesn’t work and we can’t fix it.
Can I pay with crypto?
Yes. Pay the same price in USDC or USDT on Base, Ethereum, Arbitrum, Optimism, Polygon or Solana from the "Pay with crypto" button, with no account or KYC. We check the payment on-chain and show your download links as soon as it confirms.

Get Claude Code Safety Hooks

Tested Claude Code hooks as a plugin: block rm -rf and force-push to main, protect .env and lockfiles, catch API keys in files, audit log. $19 once.

Pairs well with

DownloadFree

Claude Code Hooks Starter (Free)

Free Claude Code hooks examples: SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Notification and Stop, plus a local tester that checks your hook's output.

View Claude Code Hooks Starter (Free)
Download$12

Cursor Rules Pack

24 Cursor rules (.mdc) for Next.js, TypeScript, React, Python, FastAPI, Django, Go, Rust and more. Detects your stack, installs in one command, lints your rules. $12 once.

View Cursor Rules Pack
DownloadFree

Cursor Rules Starter (Free)

Free Cursor rules examples in .mdc format: agent workflow, commits, testing, security, code review. Includes a linter that shows why a rule isn't applying.

View Cursor Rules Starter (Free)

Related