← sources

My hook blocked Claude from editing migrations. Claude used echo >> instead.

source: reddit (reddit:ClaudeCode)published 2026-11-17 13:15ingested 2026-11-18 02:42

retained text

I had a PreToolUse hook that stopped Claude Code from editing anything in `db/migrations/`. It worked. Claude tried Edit, got exit 2, read my message, and said fine.

Then it wrote the same change with Bash:

    echo "ALTER TABLE ..." >> db/migrations/004_add_index.sql

Nothing was hiding it. My hook only watched Edit and Write. Claude had a task, one road was closed, and it took the other one.

What I changed:

* **The path guard now checks Bash commands too.** It looks for `>`, `>>`, `tee`, `sed -i`, `rm`, `mv`, `cp` and `truncate` aimed at a protected path. It’s still not perfect, since a script can write files. But it closes the obvious door.
* **I test hooks like code.** Each hook gets fake JSON on stdin and a check on the exit code. For example: `rm -rf /` should exit 2, and `rm -rf ./dist` should exit 0. My first version also blocked `./dist`. I only found that out because of a test.
* **A Stop hook runs the tests before Claude says “done”.** If the tests fail, Claude gets the error and keeps working. After 3 blocked stops in a row it gives up and hands back to me, so it can’t loop forever.

The lesson: a hook only blocks the tool it’s attached to. If a rule matters, ask how else Claude could do the same thing, and write a test for that too.

Has anyone else had Claude find a way around one of their hooks? What did it use?

document fetched 2026-11-18 00:44 · canonical https://reddit.com/r/ClaudeCode/comments/1xyk5mb/my_hook_blocked_claude_from_editing_migrations