Live-infrastructure commands were pre-approved in a local allowlist. Deploys, cluster mutations and SSH now always prompt, and a PreToolUse hook catches the same commands when they are wrapped.
42 lines
1.6 KiB
Bash
Executable File
42 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# PreToolUse hook for Bash: forces a permission prompt before any command that
|
|
# changes live infrastructure. The ask rules in .claude/settings.json match the
|
|
# command text as Claude usually writes it; this catches the same commands when
|
|
# they are wrapped (sh -c '...', env VAR=x ..., absolute paths).
|
|
set -euo pipefail
|
|
|
|
input=$(cat)
|
|
cmd=$(jq -r '.tool_input.command // empty' <<<"$input")
|
|
cwd=$(jq -r '.cwd // empty' <<<"$input")
|
|
|
|
[ -z "$cmd" ] && exit 0
|
|
|
|
# A subcommand must follow whitespace, so flags like `preview --refresh` don't match.
|
|
# It may be followed by a closing quote or paren when the command is wrapped.
|
|
end="([[:space:]\"')]|\$)"
|
|
pulumi_re="pulumi[[:space:]]+([^|;&]*[[:space:]])?(up|destroy|refresh|import|state|cancel)$end"
|
|
kubectl_re="kubectl[[:space:]]+([^|;&]*[[:space:]])?(apply|delete|patch|edit|replace|create|drain|cordon|uncordon|taint|scale|rollout|exec|annotate|label|set|cp)$end"
|
|
ssh_re='(^|[^[:alnum:]_./-])(ssh|scp)[[:space:]]'
|
|
|
|
what=""
|
|
if [[ $cmd =~ $pulumi_re ]]; then
|
|
what="pulumi ${BASH_REMATCH[2]}"
|
|
elif [[ $cmd =~ $kubectl_re ]]; then
|
|
what="kubectl ${BASH_REMATCH[2]}"
|
|
elif [[ $cmd =~ $ssh_re ]]; then
|
|
what="${BASH_REMATCH[2]} to a node"
|
|
fi
|
|
|
|
[ -z "$what" ] && exit 0
|
|
|
|
stack=$(grep -oE '0[1-5]-[a-z0-9-]+' <<<"$cmd $cwd" | head -n 1 || true)
|
|
reason="Live infrastructure: '$what'${stack:+ in $stack}. Approve only if you want Claude to run this instead of running it yourself."
|
|
|
|
jq -n --arg reason "$reason" '{
|
|
hookSpecificOutput: {
|
|
hookEventName: "PreToolUse",
|
|
permissionDecision: "ask",
|
|
permissionDecisionReason: $reason
|
|
}
|
|
}'
|