HOOK · deterministic-policy
PreToolUse hook: block production config edits
Hard-denies any Edit or Write to Terraform, CI workflow, dotenv, and Spring production-profile paths, with the reason fed back to Claude.
✓ verified 2026-06-10 · stack: claude-code, java, terraform · source
A PreToolUse hook that fires on every Edit and Write, parses the target
path from the JSON the hook receives on stdin, and exits 2 when the path
matches a protected pattern. Exit 2 blocks the tool call before it runs and
delivers the stderr text to Claude as the reason, so the model can explain
the block and route around it instead of retrying blindly.
#!/bin/bashINPUT=$(cat)FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
PROTECTED_PATTERNS=( "application-prod" "application-staging" "/terraform/" "/.github/workflows/" "/pom.xml" ".env" "Makefile")
for pattern in "${PROTECTED_PATTERNS[@]}"; do if [[ "$FILE_PATH" == *"$pattern"* ]]; then echo "Blocked: '$FILE_PATH' matches protected pattern '$pattern'. Request human review for changes to production configuration." >&2 exit 2 fidone
exit 0{ "hooks": { "PreToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "\"${CLAUDE_PROJECT_DIR}\"/.claude/hooks/block-prod-config-edits.sh" } ] } ] }}Make the script executable (chmod +x .claude/hooks/block-prod-config-edits.sh)
and commit both files. ${CLAUDE_PROJECT_DIR} resolves to the project root, so
the hook works regardless of the session’s working directory.
What it does
- Blocks file modifications via the
EditandWritetools to Spring production/staging profiles, Terraform, GitHub workflow definitions,pom.xml, dotenv files, and Makefiles. - Exit 2 blocks before the side effect and overrides any matching allow rule; the stderr message is what Claude sees. Note that any JSON printed on stdout is ignored when exiting 2: this script deliberately uses the exit-code channel only.
- Substring matching is intentionally broad:
application-prodalso catchesapplication-production.ymland copies in test fixtures. Tune the pattern list to your repo before committing.
Failure modes
- It only sees
EditandWrite. A Bash command (sed -i,tee,mv) can still modify the files. Pair the hook with permission deny rules on the same paths, and the sandbox for Bash containment. See the cautious profile. - It is repo-trust. The script lives in the repo and runs with your user’s
OS permissions; anyone with write access can edit or delete it. For
organisation-grade enforcement, replicate it in managed settings (ideally as
an inline command) and set
allowManagedHooksOnlywhere appropriate. jqmust be on PATH. Ifjqis missing the script exits non-zero but not 2, which Claude Code treats as a non-blocking error: the edit proceeds. If that is unacceptable, inline the parse or assertcommand -v jqandexit 2on failure.- Substring false positives. A legitimate file like
docs/Makefile-notes.mdmatchesMakefile. Broaden or anchor patterns deliberately; an over-eager block is visible and cheap, a missed block is not.
Verified against the hooks guide’s “block edits to protected files” reference pattern and the current hook input/exit-code schema, 2026-06-10.