Hook

Hook that blocks dangerous shell commands

Before Claude runs a Bash command, the hook checks it and blocks dangerous patterns such as recursive deletes at the root, force pushes, piping a download into a shell or dropping database tables. Claude is told why so it can change course.

Tested with 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"
          }
        ]
      }
    ]
  }
}

Notes

  • Needs jq. Run `chmod +x .claude/hooks/block-dangerous.sh` once.
  • A hook is one safety layer and cannot catch every way to write a command. For things that must never happen, add `deny` rules in permissions as well.
  • Adjust the pattern list in the script to your project.

Related lessons

Type to search the reference, templates and lessons.