Skip to content

Guide

Claude Code hooks: block dangerous commands

Short answer: Add a PreToolUse hook in Claude Code’s settings that reads the command from its input and exits with code 2 to block it. Simple word lists miss tricky commands and block harmless ones, which is the gap Antelog fills.

How hooks work

Before each tool call, Claude Code runs your hook and passes the call as JSON on standard input. Exit code 0 lets it run. Exit code 2 blocks it, and whatever you print to standard error is shown to Claude.

1. Register the hook

In ~/.claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Bash",
        "hooks": [{ "type": "command", "command": "~/.claude/block.sh" }] }
    ]
  }
}

2. Write the check

~/.claude/block.sh (needs jq; run chmod +x on it):

#!/bin/sh
cmd=$(jq -r '.tool_input.command // ""')
case "$cmd" in
  *"rm -rf /"*|*"rm -rf ~"*|*"push --force"*)
    echo "Blocked: this command could delete work." >&2
    exit 2 ;;
esac
exit 0

Where simple hooks fall short

  • They miss variations: rm -fr, a deleting Python script, or a cloud CLI deleting a database.
  • They block harmless text: a commit message or a file that only mentions a dangerous word.
  • They fail open: if the script crashes, the command may run.

A ready-made hook

Antelog is a tested hook that reads commands and the scripts they run, uses AI checks for unclear cases, blocks the command if the guard itself crashes, and keeps a record of every action. It installs with one command.

Install Antelog

Free for developers. On macOS and Linux:

$ curl -fsSL https://antelog.dev/install.sh | sh

Windows and other tools: see the docs.

More guides