$ recombobulate _
home / tips / block-dangerous-commands-before-they-run-with-pretooluse-hooks
0

Block Dangerous Commands Before They Run with PreToolUse Hooks

bagwaa @bagwaa · Mar 26, 2026 · Configuration
block-dangerous-commands-before-they-run-with-pretooluse-hooks

The PreToolUse hook fires before every tool call. Because it supports blocking via exit code 2, you can use it to intercept commands that should never run automatically, adding a programmable safety layer on top of standard permission rules.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "bash ~/.claude/hooks/check-dangerous.sh"
          }
        ]
      }
    ]
  }
}

The hook script receives the tool input as JSON on stdin. Inspect the command and exit with code 2 to block it:

#!/usr/bin/env bash
input=$(cat)
cmd=$(echo "$input" | jq -r '.command // empty')
if echo "$cmd" | grep -qE '(rm -rf /|DROP TABLE|git push --force)'; then
  echo '{"decision":"block","reason":"Blocked dangerous command"}'
  exit 2
fi

This is more granular than static permission rules. You can apply context-aware logic: block git push --force to main but allow it to feature branches, or block destructive commands outside a specific working directory.

The matcher field uses regex, so "Bash" matches all shell calls, "Edit|Write" matches file-modifying tools, and "mcp__.*" matches every MCP tool call.

Add this config to ~/.claude/settings.json for a global safety net, or to .claude/settings.json for project-specific rules.

A PreToolUse hook is a programmable checkpoint that runs before every tool call, giving you fine-grained control over what Claude can actually do.


via Claude Code Hooks

~/recombobulate $ tip --comments --count=0

Log in to leave a comment.

~/recombobulate $ tip --related --limit=3
0
Switch to the Stable Update Channel with autoUpdatesChannel

The autoUpdatesChannel setting pins Claude Code to a stable release track that skips versions with major regressions.

bagwaa @bagwaa · 2 hours ago
0
Set Claude's Response Language with the language Setting

The language setting makes Claude respond in your preferred language by default, across every session and project.

bagwaa @bagwaa · 2 hours ago
0
Customize or Remove Claude's Git Attribution with the attribution Setting

The attribution setting lets you customize or completely remove Claude's Co-Authored-By trailer from git commits and pull requests.

bagwaa @bagwaa · 2 hours ago