Dev.to Security 🔐 Cybersecurity 👁 2 📖 2 min read

7 mistakes that make Claude Code hooks fail silently

Claude Code hooks are the only rules an agent can't forget in a long session. But a hook that never fires fails silently: no error, no warning, just an agent doing the thing you thought you'd blocked. Here are seven mist

Claude Code hooks are the only rules an agent can't forget in a long session. But a hook that never fires fails silently: no error, no warning, just an agent doing the thing you thought you'd blocked. Here are seven mistakes I see most in .claude/settings.json, and one in .mcp.json.

1. A lower-case matcher

{ "matcher": "bash", "hooks": [...] }

Tool names are case-sensitive. bash matches nothing; it has to be Bash. Same for Edit, Write, Read.

2. The hook object without its wrapper

"PostToolUse": [{ "type": "command", "command": "npx prettier --write" }]

Each event takes an array of groups, and each group has its own hooks array:

"PostToolUse": [
  { "matcher": "Edit|Write|MultiEdit",
    "hooks": [{ "type": "command", "command": "npx prettier --write" }] }
]

3. Matching Edit but not Write

A guard on "matcher": "Edit" protects existing files, but the agent can still create a brand-new file with Write or change several at once with MultiEdit. Use Edit|Write|MultiEdit.

4. A relative script path

"command": ".claude/hooks/protect-files.sh"

The hook runs from the current working directory, and the agent changes directories. Anchor it:

"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh"

The quotes matter if the path has a space.

5. exit 1 in a PreToolUse hook

Exit code 1 is a non-blocking error: the tool call still runs. To block it and send your message back to the agent, write to stderr and exit 2.

echo "Blocked: .env files are off limits" >&2
exit 2

6. Allowing all of Bash

"permissions": { "allow": ["Bash"] }

That approves every shell command, rm -rf and curl | sh included. Allow specific commands instead, like "Bash(npm run test:*)", and leave the rest to ask.

7. No deny rule for .env

Nothing stops the agent from reading your secrets into its context unless you say so:

"permissions": { "deny": ["Read(./.env)", "Read(./.env.*)"] }

Bonus: literal tokens in .mcp.json

.mcp.json is meant to be committed. Put "GITHUB_TOKEN": "${GITHUB_TOKEN}" there, not the token itself, and pin npx packages to a version.

Check yours in 10 seconds

I turned these checks into a free browser tool: AgentLint. Paste your CLAUDE.md, settings.json or .mcp.json and it lists what's wrong and how to fix it. Nothing is uploaded.

For tested hooks to copy, the Claude Code Playbook repo has a .env guard, a dangerous-command blocker and a formatter hook, with a test script.

Which of these has bitten you?

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.