|
| 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" | jq -r '.tool_name // ""' 2>/dev/null || true)" |
| 18 | + |
| 19 | +# Only validate Bash commands — allow everything else through. |
| 20 | +# If TOOL_NAME is empty (parse failure), fall through to check the command anyway (fail closed). |
| 21 | +if [[ -n "$TOOL_NAME" && "$TOOL_NAME" != "Bash" ]]; then |
| 22 | + exit 0 |
| 23 | +fi |
| 24 | + |
| 25 | +COMMAND="$(printf '%s' "$INPUT" | jq -r '.tool_input.command // ""' 2>/dev/null || true)" |
| 26 | + |
| 27 | +if [[ -z "$COMMAND" ]]; then |
| 28 | + exit 0 |
| 29 | +fi |
| 30 | + |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# 2. Blocked patterns — glob-style matching |
| 33 | +# Each pattern is checked against the full command string (case-insensitive). |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | +BLOCKED_PATTERNS=( |
| 36 | + # AWS — Destructive operations |
| 37 | + "aws * delete*" |
| 38 | + "aws * remove*" |
| 39 | + "aws * destroy*" |
| 40 | + "aws * terminate*" |
| 41 | + "aws * deregister*" |
| 42 | + "aws * purge*" |
| 43 | + "aws s3 rm *" |
| 44 | + "aws s3 rb *" |
| 45 | + "aws cloudformation delete-stack*" |
| 46 | + |
| 47 | + # AWS — Provisioning & scaling |
| 48 | + "aws ec2 run-instances*" |
| 49 | + "aws ec2 stop-instances*" |
| 50 | + "aws autoscaling set-desired-capacity*" |
| 51 | + "aws autoscaling update-auto-scaling-group*" |
| 52 | + "aws application-autoscaling *" |
| 53 | + "aws ecs update-service*" |
| 54 | + |
| 55 | + # AWS — IAM |
| 56 | + "aws iam delete*" |
| 57 | + "aws iam create*" |
| 58 | + "aws iam put*" |
| 59 | + "aws iam attach*" |
| 60 | + |
| 61 | + # Kubernetes — Mutations |
| 62 | + "kubectl delete *" |
| 63 | + "kubectl apply *" |
| 64 | + "kubectl create *" |
| 65 | + "kubectl patch *" |
| 66 | + "kubectl scale *" |
| 67 | + "kubectl rollout *" |
| 68 | + "kubectl drain *" |
| 69 | + "kubectl cordon *" |
| 70 | + "kubectl exec *" |
| 71 | + "kubectl edit *" |
| 72 | + |
| 73 | + # Terraform / IaC |
| 74 | + "terraform destroy*" |
| 75 | + "terraform apply*" |
| 76 | + "terraform import *" |
| 77 | + "terraform state rm *" |
| 78 | + "terraform taint *" |
| 79 | + |
| 80 | + # Helm |
| 81 | + "helm install *" |
| 82 | + "helm upgrade *" |
| 83 | + "helm delete *" |
| 84 | + "helm uninstall *" |
| 85 | + "helm rollback *" |
| 86 | + |
| 87 | + # Git — Destructive |
| 88 | + "git push --force*" |
| 89 | + "git push -f *" |
| 90 | + "git push --force-with-lease*" |
| 91 | + "git push origin main*" |
| 92 | + "git push origin master*" |
| 93 | + "git reset --hard*" |
| 94 | + "git clean -f*" |
| 95 | + |
| 96 | + # File system — Destructive |
| 97 | + "rm -rf /" |
| 98 | + "rm -rf /*" |
| 99 | + "rm -rf ~" |
| 100 | + "rm -rf ~/*" |
| 101 | + "rm -rf ..*" |
| 102 | + "mkfs.*" |
| 103 | + "dd if=*/dev/*" |
| 104 | + |
| 105 | + # Credential access via cat/less/head/tail |
| 106 | + "cat */.aws/*" |
| 107 | + "cat */.kube/*" |
| 108 | + "cat */.ssh/*" |
| 109 | + "cat *.env" |
| 110 | + "cat *.env.*" |
| 111 | + "cat */secrets/*" |
| 112 | + "cat *credentials*" |
| 113 | + "less */.aws/*" |
| 114 | + "less */.ssh/*" |
| 115 | + "head */.aws/*" |
| 116 | + "head */.ssh/*" |
| 117 | + "tail */.aws/*" |
| 118 | + "tail */.ssh/*" |
| 119 | + |
| 120 | + # Network & remote access |
| 121 | + "ssh *" |
| 122 | + "scp *" |
| 123 | + "curl *|*sh" |
| 124 | + "curl *|*bash" |
| 125 | + "wget *|*sh" |
| 126 | + "wget *|*bash" |
| 127 | + |
| 128 | + # Privilege escalation |
| 129 | + "sudo *" |
| 130 | + "sudo" |
| 131 | + "chmod 777 *" |
| 132 | + "chown root *" |
| 133 | +) |
| 134 | + |
| 135 | +# --------------------------------------------------------------------------- |
| 136 | +# 3. Check function — matches a single sub-command against all patterns |
| 137 | +# --------------------------------------------------------------------------- |
| 138 | +check_command() { |
| 139 | + local cmd="$1" |
| 140 | + # Trim leading/trailing whitespace using parameter expansion |
| 141 | + cmd="${cmd#"${cmd%%[![:space:]]*}"}" |
| 142 | + cmd="${cmd%"${cmd##*[![:space:]]}"}" |
| 143 | + |
| 144 | + if [[ -z "$cmd" ]]; then |
| 145 | + return 0 |
| 146 | + fi |
| 147 | + |
| 148 | + # Convert to lowercase using parameter expansion |
| 149 | + local cmd_lower="${cmd,,}" |
| 150 | + |
| 151 | + for pattern in "${BLOCKED_PATTERNS[@]}"; do |
| 152 | + local pattern_lower="${pattern,,}" |
| 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 newlines using parameter expansion |
| 168 | +# Order matters: replace && and || before | to avoid double-splitting || |
| 169 | +NORMALIZED="${COMMAND//&&/$'\n'}" |
| 170 | +NORMALIZED="${NORMALIZED//||/$'\n'}" |
| 171 | +NORMALIZED="${NORMALIZED//;/$'\n'}" |
| 172 | +NORMALIZED="${NORMALIZED//|/$'\n'}" |
| 173 | + |
| 174 | +while IFS= read -r subcmd; do |
| 175 | + check_command "$subcmd" |
| 176 | +done <<< "$NORMALIZED" |
| 177 | + |
| 178 | +# If we reach here, the command is allowed |
| 179 | +exit 0 |
0 commit comments