Created
May 27, 2026 19:17
-
-
Save alanalvestech/0cd5cf84d2491517ae0da1c4d1e1df65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Preciso configurar um hook de segurança no Claude Code que bloqueia comandos | |
| destrutivos antes de executar. | |
| Faz o seguinte: | |
| 1. Cria o arquivo `~/.claude/scripts/safety-check.sh` com este conteúdo exato: | |
| #!/usr/bin/env bash | |
| # Bloqueia operações destrutivas antes de executar (Bash, Edit, Write) | |
| input=$(cat) | |
| tool=$(echo "$input" | python3 -c "import sys,json; d=json.load(sys.stdin); | |
| print(d.get('tool_name',''))" 2>/dev/null) | |
| block() { | |
| echo "🚫 BLOQUEADO pelo safety-check: $1" >&2 | |
| echo "Se tiver certeza, execute manualmente no terminal." >&2 | |
| exit 2 | |
| } | |
| if [ "$tool" = "Bash" ]; then | |
| cmd=$(echo "$input" | python3 -c "import sys,json; d=json.load(sys.stdin); | |
| print(d.get('tool_input',{}).get('command',''))" 2>/dev/null) | |
| is_remote_ssh=false | |
| if echo "$cmd" | grep -qE "^ssh "; then | |
| is_remote_ssh=true | |
| fi | |
| DANGEROUS_PATTERNS=( | |
| "rm -[rf]+ ~" | |
| "rm -[rf]+ \$HOME" | |
| "> /dev/sda" | |
| "dd if=/dev/zero" | |
| "mkfs\." | |
| ":\(\)\{ :\|:& \};:" | |
| "chmod -R 777 /" | |
| "chown -R .* /" | |
| "git reset --hard HEAD~[0-9]" | |
| "git clean -fdx?" | |
| "git push.*--force" | |
| "git push.*-f " | |
| "DROP (TABLE|DATABASE|SCHEMA)" | |
| "TRUNCATE TABLE" | |
| ) | |
| LOCAL_ONLY_PATTERNS=( | |
| "curl.+\|.*(bash|sh)" | |
| "wget.+\|.*(bash|sh)" | |
| ) | |
| for pattern in "${DANGEROUS_PATTERNS[@]}"; do | |
| if echo "$cmd" | grep -qiE "$pattern"; then | |
| block "comando Bash perigoso detectado.\nComando: $cmd\nPadrão: $pattern" | |
| fi | |
| done | |
| project_dir=$(pwd) | |
| if echo "$cmd" | grep -qE "rm -[rf]{1,2} /"; then | |
| target=$(echo "$cmd" | grep -oE "rm -[rf]{1,2} (/[^ ;|&]+)" | grep -oE "/[^ | |
| ;|&]+") | |
| if [ -n "$target" ] && [[ "$target" != "$project_dir"* ]]; then | |
| block "rm com caminho absoluto fora do projeto ($project_dir).\nAlvo: $target" | |
| fi | |
| fi | |
| if [ "$is_remote_ssh" = false ]; then | |
| for pattern in "${LOCAL_ONLY_PATTERNS[@]}"; do | |
| if echo "$cmd" | grep -qiE "$pattern"; then | |
| block "comando Bash perigoso detectado.\nComando: $cmd\nPadrão: $pattern" | |
| fi | |
| done | |
| fi | |
| fi | |
| if [ "$tool" = "Edit" ] || [ "$tool" = "Write" ]; then | |
| path=$(echo "$input" | python3 -c "import sys,json; d=json.load(sys.stdin); | |
| print(d.get('tool_input',{}).get('file_path',''))" 2>/dev/null) | |
| SENSITIVE_PATHS=( | |
| "$HOME/.ssh" | |
| "$HOME/.gnupg" | |
| "$HOME/.netrc" | |
| "$HOME/.npmrc" | |
| "$HOME/.pypirc" | |
| "/etc/passwd" | |
| "/etc/shadow" | |
| "/etc/sudoers" | |
| ) | |
| for sensitive in "${SENSITIVE_PATHS[@]}"; do | |
| if [[ "$path" == "$sensitive"* ]]; then | |
| block "tentativa de editar caminho sensível: $path" | |
| fi | |
| done | |
| fi | |
| exit 0 | |
| 2. Dá permissão de execução: `chmod +x ~/.claude/scripts/safety-check.sh` | |
| 3. Adiciona o hook no `~/.claude/settings.json` — se o arquivo já existir, merge o | |
| bloco de hooks; se não existir, cria com este conteúdo: | |
| { | |
| "hooks": { | |
| "PreToolUse": [ | |
| { | |
| "matcher": "", | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "~/.claude/scripts/safety-check.sh" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| } | |
| Depois confirma que o hook está ativo rodando: `claude config list` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment