|
| 1 | +#!/usr/bin/env bash |
| 2 | +# validate-bash-command.sh — Pre-execution hook (Layer 3 guardrail) |
| 3 | +# |
| 4 | +# Claude Code invokes this hook before every Bash tool call. |
| 5 | +# Input: JSON on stdin {"tool_name":"Bash","tool_input":{"command":"..."}} |
| 6 | +# Output: exit 0 to allow, exit 2 with JSON {"error":"..."} to block. |
| 7 | +# |
| 8 | +# IMPORTANT: Do not modify, disable, or bypass this hook. |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# 1. Read hook input from stdin and extract the command |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | +INPUT="$(cat)" |
| 16 | + |
| 17 | +TOOL_NAME="$(printf '%s' "$INPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('tool_name',''))" 2>/dev/null || true)" |
| 18 | + |
| 19 | +# Only validate Bash commands — allow everything else through |
| 20 | +if [[ "$TOOL_NAME" != "Bash" ]]; then |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +COMMAND="$(printf '%s' "$INPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('tool_input',{}).get('command',''))" 2>/dev/null || true)" |
| 25 | + |
| 26 | +if [[ -z "$COMMAND" ]]; then |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +# 2. Blocked patterns — glob-style matching |
| 32 | +# Each pattern is checked against the full command string (case-insensitive). |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | +BLOCKED_PATTERNS=( |
| 35 | + # AWS — Destructive operations |
| 36 | + "aws * delete*" |
| 37 | + "aws * remove*" |
| 38 | + "aws * destroy*" |
| 39 | + "aws * terminate*" |
| 40 | + "aws * deregister*" |
| 41 | + "aws * purge*" |
| 42 | + "aws s3 rm *" |
| 43 | + "aws s3 rb *" |
| 44 | + "aws cloudformation delete-stack*" |
| 45 | + |
| 46 | + # AWS — Provisioning & scaling |
| 47 | + "aws ec2 run-instances*" |
| 48 | + "aws ec2 stop-instances*" |
| 49 | + "aws autoscaling set-desired-capacity*" |
| 50 | + "aws autoscaling update-auto-scaling-group*" |
| 51 | + "aws application-autoscaling *" |
| 52 | + "aws ecs update-service*" |
| 53 | + |
| 54 | + # AWS — IAM |
| 55 | + "aws iam delete*" |
| 56 | + "aws iam create*" |
| 57 | + "aws iam put*" |
| 58 | + "aws iam attach*" |
| 59 | + |
| 60 | + # Kubernetes — Mutations |
| 61 | + "kubectl delete *" |
| 62 | + "kubectl apply *" |
| 63 | + "kubectl create *" |
| 64 | + "kubectl patch *" |
| 65 | + "kubectl scale *" |
| 66 | + "kubectl rollout *" |
| 67 | + "kubectl drain *" |
| 68 | + "kubectl cordon *" |
| 69 | + "kubectl exec *" |
| 70 | + "kubectl edit *" |
| 71 | + |
| 72 | + # Terraform / IaC |
| 73 | + "terraform destroy*" |
| 74 | + "terraform apply*" |
| 75 | + "terraform import *" |
| 76 | + "terraform state rm *" |
| 77 | + "terraform taint *" |
| 78 | + |
| 79 | + # Helm |
| 80 | + "helm install *" |
| 81 | + "helm upgrade *" |
| 82 | + "helm delete *" |
| 83 | + "helm uninstall *" |
| 84 | + "helm rollback *" |
| 85 | + |
| 86 | + # Git — Destructive |
| 87 | + "git push --force*" |
| 88 | + "git push -f *" |
| 89 | + "git push --force-with-lease*" |
| 90 | + "git push origin main*" |
| 91 | + "git push origin master*" |
| 92 | + "git reset --hard*" |
| 93 | + "git clean -f*" |
| 94 | + |
| 95 | + # File system — Destructive |
| 96 | + "rm -rf /" |
| 97 | + "rm -rf /*" |
| 98 | + "rm -rf ~" |
| 99 | + "rm -rf ~/*" |
| 100 | + "rm -rf ..*" |
| 101 | + "mkfs.*" |
| 102 | + "dd if=*/dev/*" |
| 103 | + |
| 104 | + # Credential access via cat/less/head/tail |
| 105 | + "cat */.aws/*" |
| 106 | + "cat */.kube/*" |
| 107 | + "cat */.ssh/*" |
| 108 | + "cat *.env" |
| 109 | + "cat *.env.*" |
| 110 | + "cat */secrets/*" |
| 111 | + "cat *credentials*" |
| 112 | + "less */.aws/*" |
| 113 | + "less */.ssh/*" |
| 114 | + "head */.aws/*" |
| 115 | + "head */.ssh/*" |
| 116 | + "tail */.aws/*" |
| 117 | + "tail */.ssh/*" |
| 118 | + |
| 119 | + # Network & remote access |
| 120 | + "ssh *" |
| 121 | + "scp *" |
| 122 | + "curl *|*sh" |
| 123 | + "curl *|*bash" |
| 124 | + "wget *|*sh" |
| 125 | + "wget *|*bash" |
| 126 | + |
| 127 | + # Privilege escalation |
| 128 | + "sudo *" |
| 129 | + "sudo" |
| 130 | + "chmod 777 *" |
| 131 | + "chown root *" |
| 132 | +) |
| 133 | + |
| 134 | +# --------------------------------------------------------------------------- |
| 135 | +# 3. Check function — matches a single sub-command against all patterns |
| 136 | +# --------------------------------------------------------------------------- |
| 137 | +check_command() { |
| 138 | + local cmd="$1" |
| 139 | + # Trim leading/trailing whitespace |
| 140 | + cmd="$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')" |
| 141 | + |
| 142 | + if [[ -z "$cmd" ]]; then |
| 143 | + return 0 |
| 144 | + fi |
| 145 | + |
| 146 | + # Convert to lowercase for case-insensitive matching |
| 147 | + local cmd_lower |
| 148 | + cmd_lower="$(echo "$cmd" | tr '[:upper:]' '[:lower:]')" |
| 149 | + |
| 150 | + for pattern in "${BLOCKED_PATTERNS[@]}"; do |
| 151 | + local pattern_lower |
| 152 | + pattern_lower="$(echo "$pattern" | tr '[:upper:]' '[:lower:]')" |
| 153 | + |
| 154 | + # shellcheck disable=SC2254 |
| 155 | + if [[ "$cmd_lower" == $pattern_lower ]]; then |
| 156 | + printf '{"error":"BLOCKED by guardrail hook: command matches blocked pattern: %s"}\n' "$pattern" >&2 |
| 157 | + exit 2 |
| 158 | + fi |
| 159 | + done |
| 160 | + |
| 161 | + return 0 |
| 162 | +} |
| 163 | + |
| 164 | +# --------------------------------------------------------------------------- |
| 165 | +# 4. Split on pipes and command chains, then check each sub-command |
| 166 | +# --------------------------------------------------------------------------- |
| 167 | +# Replace common chain operators with a delimiter |
| 168 | +NORMALIZED="$(echo "$COMMAND" | sed 's/&&/\n/g; s/||/\n/g; s/;/\n/g; s/|/\n/g')" |
| 169 | + |
| 170 | +while IFS= read -r subcmd; do |
| 171 | + check_command "$subcmd" |
| 172 | +done <<< "$NORMALIZED" |
| 173 | + |
| 174 | +# If we reach here, the command is allowed |
| 175 | +exit 0 |
0 commit comments