Skip to content

Claude Code Complete Guide

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

  1. Implementation of automated security check functionality
  2. Completion of safe auto-approval mode configuration
  3. 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

SymptomCauseSolution
Legitimate commands blockedRules too strictRelax conditions in security-rules.json
Hooks not workingPermission configuration issueExecute chmod +x security-check.py
Check processing slowToo many rule validationsImplement 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
### 2. Logging and Alerts
import logging
logging.basicConfig(filename='security.log', level=logging.WARNING)
logging.warning(f'Blocked command: {command}')
### 3. Emergency Stop Functionality
{
  "emergency_stop": {
    "keyword": "EMERGENCY_STOP",
    "action": "kill_all_processes"
  }
}

Next Steps