Hook
Block edits to secret files
A PreToolUse hook that denies any read or write to .env, credentials, and key files — a guardrail against leaking secrets into context or commits.
Install to
settings.json → hooks.PreToolUse {
"hooks": {
"PreToolUse": [
{
"matcher": "Read|Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path // empty'); case \"$f\" in *.env|*.env.*|*credentials*|*.pem|*.key|*id_rsa*|*.p12) printf '{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Blocked: secret/credential file. Edit it yourself outside the session.\"}}' ;; esac"
}
]
}
]
}
}{
"hooks": {
"PreToolUse": [
{
"matcher": "Read|Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path // empty'); case \"$f\" in *.env|*.env.*|*credentials*|*.pem|*.key|*id_rsa*|*.p12) printf '{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Blocked: secret/credential file. Edit it yourself outside the session.\"}}' ;; esac"
}
]
}
]
}
} How to install
- Open your settings file:
~/.claude/settings.json(user),.claude/settings.json(project, shared), or.claude/settings.local.json(project, private). - Merge the
hooksblock into it. If ahookskey already exists, add this event to it rather than replacing the whole object. - Hooks live-reload — no restart needed in most cases.
- Verify: run
/debugto confirm the hook is registered, then trigger the event (e.g. edit a file) and watch it fire. - This hook relies on
jqreading the event JSON from stdin — make surejqis installed.