A short script reads the tool call JSON, checks the target path against patterns such as .env, package-lock.json and .git/, and exits with code 2 plus a reason on stderr. The agent gets the reason as feedback and the edit never runs. You can write and test one after reading.
Read in this order
- OfficialBlock edits to protected files (Claude Code hooks guide)A worked script that matches protected paths and blocks the edit with exit code 2.