dangerous-cmd-warn.sh 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # hooks/dangerous-cmd-warn.sh
  3. # PreToolUse hook - warns before destructive or irreversible commands
  4. # Matcher: Bash
  5. #
  6. # Configuration in .claude/settings.json:
  7. # {
  8. # "hooks": {
  9. # "PreToolUse": [{
  10. # "matcher": "Bash",
  11. # "hooks": ["bash hooks/dangerous-cmd-warn.sh $TOOL_INPUT"]
  12. # }]
  13. # }
  14. # }
  15. #
  16. # Exit codes:
  17. # 0 = allow (safe or not matched)
  18. # 2 = block with message (dangerous command detected)
  19. INPUT="$1"
  20. if [[ -z "$INPUT" ]]; then
  21. exit 0
  22. fi
  23. # -------------------------------------------------------------------
  24. # Dangerous patterns and their risk descriptions
  25. # -------------------------------------------------------------------
  26. declare -A PATTERNS
  27. # Git destructive operations
  28. PATTERNS["git\s+push\s+.*--force"]="Force push can overwrite remote history and lose others' commits"
  29. PATTERNS["git\s+push\s+-f\b"]="Force push can overwrite remote history and lose others' commits"
  30. PATTERNS["git\s+reset\s+--hard"]="Hard reset discards all uncommitted changes permanently"
  31. PATTERNS["git\s+clean\s+-f"]="git clean -f permanently deletes untracked files"
  32. PATTERNS["git\s+checkout\s+--\s+\."]="Discards all unstaged changes in working directory"
  33. PATTERNS["git\s+branch\s+-D"]="Force-deletes a branch even if not fully merged"
  34. PATTERNS["git\s+stash\s+drop"]="Permanently removes a stash entry"
  35. PATTERNS["git\s+rebase\s+.*--force"]="Forced rebase can rewrite shared history"
  36. # File system destructive operations
  37. PATTERNS["rm\s+-rf\s+/"]="Recursive force delete from root - catastrophic data loss"
  38. PATTERNS["rm\s+-rf\s+~"]="Recursive force delete of home directory"
  39. PATTERNS["rm\s+-rf\s+\\."]="Recursive force delete of current directory"
  40. PATTERNS["rm\s+-rf\s+\*"]="Recursive force delete with glob - likely unintended"
  41. PATTERNS["rmdir\s+/"]="Attempting to remove root directory"
  42. PATTERNS["> /dev/sda"]="Direct write to block device - destroys filesystem"
  43. PATTERNS["mkfs\\."]="Formatting a filesystem destroys all data"
  44. PATTERNS["dd\s+.*of=/dev/"]="Direct disk write - can destroy data"
  45. # Database destructive operations
  46. PATTERNS["DROP\s+DATABASE"]="Drops entire database - all data lost"
  47. PATTERNS["DROP\s+TABLE"]="Drops table and all its data permanently"
  48. PATTERNS["DROP\s+SCHEMA"]="Drops schema and all contained objects"
  49. PATTERNS["TRUNCATE\s+TABLE"]="Removes all rows without logging - cannot rollback"
  50. PATTERNS["DELETE\s+FROM\s+\w+\s*;"]="DELETE without WHERE clause removes all rows"
  51. PATTERNS["UPDATE\s+\w+\s+SET\s+.*(?!WHERE)"]="UPDATE without WHERE clause modifies all rows"
  52. # Process/system operations
  53. PATTERNS["kill\s+-9\s+1\b"]="Killing PID 1 (init/systemd) crashes the system"
  54. PATTERNS["killall\s+-9"]="Force-kills all matching processes without cleanup"
  55. PATTERNS["chmod\s+-R\s+777"]="World-writable recursive permissions - security risk"
  56. PATTERNS["chown\s+-R\s+.*\s+/"]="Recursive ownership change from root"
  57. # Container operations
  58. PATTERNS["docker\s+system\s+prune\s+-a"]="Removes ALL unused Docker data (images, containers, volumes)"
  59. PATTERNS["docker\s+volume\s+prune"]="Removes all unused Docker volumes (data loss)"
  60. PATTERNS["kubectl\s+delete\s+namespace"]="Deletes entire Kubernetes namespace and all resources"
  61. PATTERNS["kubectl\s+delete\s+.*--all"]="Deletes all resources of a type"
  62. # Package/dependency operations
  63. PATTERNS["npm\s+cache\s+clean\s+--force"]="Clears entire npm cache"
  64. PATTERNS["pip\s+install\s+--force-reinstall"]="Force reinstalls all packages"
  65. # Environment/secrets
  66. PATTERNS["printenv"]="Prints all environment variables (may contain secrets)"
  67. PATTERNS["env\s*$"]="Prints all environment variables (may contain secrets)"
  68. PATTERNS["cat\s+.*\\.env"]="Displaying .env file may expose secrets"
  69. # -------------------------------------------------------------------
  70. # Check each pattern
  71. # -------------------------------------------------------------------
  72. for pattern in "${!PATTERNS[@]}"; do
  73. if echo "$INPUT" | grep -qEi "$pattern"; then
  74. echo "WARNING: Potentially dangerous command detected"
  75. echo "Pattern: $pattern"
  76. echo "Risk: ${PATTERNS[$pattern]}"
  77. echo ""
  78. echo "The command has been blocked. If you're certain this is safe,"
  79. echo "ask the user to confirm before proceeding."
  80. exit 2
  81. fi
  82. done
  83. exit 0