Claude Code Security Auto-Check Configuration - Implementing Safe Auto-Approval Mode¶
Target Audience
- Intermediate developers who want to use Claude Code's auto-approval settings but have security concerns
Key Points¶
- Implementation of automated security check functionality
- Completion of safe auto-approval mode configuration
- Automatic blocking of dangerous command execution
The Core Problem¶
While auto-approval settings dramatically improve development efficiency, they pose risks of malicious code execution and file deletion. Without proper security check mechanisms, they can trigger critical incidents in production environments.
Solution¶
Step 1: Create Security Rules Configuration File¶
Define security rules to be checked by Hooks.
{
"forbidden_commands": [
"rm -rf", "sudo rm", "git reset --hard",
"DROP TABLE", "DELETE FROM", "> /dev/null"
],
"forbidden_paths": [
"/etc/", "/usr/", "/var/log/",
"~/.ssh/", "~/.aws/"
]
}
Step 2: Configure pre-execution-check Hooks¶
Automate Claude Code's pre-execution checks.
{
"hooks": {
"PreToolUse": [
{
"name": "Security Check Hook",
"condition": {"tool_name": "Bash"},
"hooks": [
{"type": "command", "command": "python security-check.py {command}"}
]
}
]
}
}
Step 3: Implement Security Check Script¶
Add validation functionality to automatically detect and block dangerous commands.
import sys, json, re
def check_security(command):
with open('security-rules.json', 'r') as f:
rules = json.load(f)
for forbidden in rules['forbidden_commands']:
if forbidden in command:
print(f"BLOCKED: {forbidden}")
return False
return True
if __name__ == "__main__":
if not check_security(sys.argv[1]):
sys.exit(1)
Common Issues and Solutions¶
| Symptom | Cause | Solution |
|---|---|---|
| Legitimate commands blocked | Rules too strict | Relax conditions in security-rules.json |
| Hooks not working | Permission configuration issue | Execute chmod +x security-check.py |
| Check processing slow | Too many rule validations | Implement optimization with regex |
Advanced Configuration (For Advanced Users - Click to Expand)
## Advanced Security Settings ### 1. File Access Monitoring# Real-time monitoring with inotify-tools
inotifywait -m /important/files/ -e modify,delete
import logging
logging.basicConfig(filename='security.log', level=logging.WARNING)
logging.warning(f'Blocked command: {command}')
{
"emergency_stop": {
"keyword": "EMERGENCY_STOP",
"action": "kill_all_processes"
}
}
Next Steps¶
- Claude Code Hooks Complete Guide - Learn more advanced automation configurations
- Claude Code Enterprise Deployment - Security management for team development