Hook chặn lệnh shell nguy hiểm
Trước khi Claude chạy một lệnh Bash, hook kiểm tra lệnh đó và chặn các mẫu nguy hiểm như xóa đệ quy ở thư mục gốc, force push, chạy script tải từ mạng hay xóa bảng database. Claude nhận lý do bị chặn để đổi cách làm.
Đã thử với Claude Code 2.1.329
.claude/hooks/block-dangerous.sh
#!/bin/bash
# Blocks risky Bash commands before they run. Exit 2 = block; stderr goes back to Claude.
cmd=$(jq -r '.tool_input.command // empty')
[ -z "$cmd" ] && exit 0
deny() {
echo "Blocked by .claude/hooks/block-dangerous.sh: $1. Ask the user to run it themselves if it is really needed." >&2
exit 2
}
echo "$cmd" | grep -Eq 'rm[[:space:]]+-[a-zA-Z]*r[a-zA-Z]*f?[a-zA-Z]*[[:space:]]+(/|~|\$HOME)([[:space:]]|$)' && deny "recursive delete of / or the home folder"
# --force-with-lease is the safe variant and stays allowed.
echo "$cmd" | grep -Eq 'git[[:space:]]+push.*([[:space:]]--force([[:space:]]|$)|[[:space:]]-f([[:space:]]|$)|[[:space:]]\+[^[:space:]])' && deny "force push"
echo "$cmd" | grep -Eq '(curl|wget)[^|]*\|[[:space:]]*(sudo[[:space:]]+)?(ba|z)?sh' && deny "piping a download into a shell"
echo "$cmd" | grep -Eiq 'drop[[:space:]]+(table|database)' && deny "dropping a table or database"
echo "$cmd" | grep -Eq 'git[[:space:]]+reset[[:space:]]+--hard' && deny "git reset --hard discards work"
exit 0
.claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous.sh"
}
]
}
]
}
}
Lưu ý
- Cần cài jq. Chạy `chmod +x .claude/hooks/block-dangerous.sh` một lần.
- Hook chỉ là một lớp an toàn, không bắt được mọi cách viết lệnh. Với những gì tuyệt đối không được làm, thêm quy tắc `deny` trong permissions.
- Sửa danh sách mẫu trong script cho hợp với dự án của bạn.