Claude Code Security Configuration Guide
Key point
This guide outlines the limitations of Claude Code's permission rules and demonstrates how to protect .env files using PreToolUse hooks.
Details
An incident occurred where a user's file was deleted after granting Claude Code shell access and a .env file containing production credentials, then selecting "Don't ask again," which allowed the permission rules to be bypassed.
Existing deny rules like Bash(rm .env*) match command text from left to right, so they fail to block commands that include paths, such as rm -f ./apps/api/.env.production. Additionally, they cannot prevent file truncation via methods other than rm (e.g., echo "" > .env) or scenarios involving content rather than file paths (e.g., AWS keys).
To address this, we propose a two-layer security configuration.
1. Configure Precise Deny Rules
Use deny rules within the permissions section of settings.json. Deny rules apply across all permission modes, including bypassPermissions, ensuring files remain protected even if the user skips permission checks.
- Explicitly specify path patterns such as
Read(./.env)andRead(./**/.env.*). Readdeny rules also blockEditandWriteoperations for those paths and apply to Bash file commands recognized by Claude Code (such ascat).
2. Utilize PreToolUse Hooks
Install a PreToolUse hook that receives and validates tool calls as JSON before permission rules are evaluated.
- The hook can veto tool calls by returning
exit 2, taking precedence overallowrules. - This is effective for blocking real-world incident scenarios missed by permission rules, such as content-based risks rather than path-based ones.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.