Block Dangerous Bash Commands: PreToolUse Hook With Tests

A SKILL.md that installs a Claude Code PreToolUse hook blocking rm -rf, force pushes, hard resets and DROP TABLE, plus a test script that proves each pattern blocks or allows.

Skill prompt

Prompt

Installs to .agents/skills/block-dangerous-bash-commands-pretooluse-hook-with-tests/

mkdir -p .agents/skills/block-dangerous-bash-commands-pretooluse-hook-with-tests && curl -sL "https://prompts-gpt.com/prompts/block-dangerous-bash-commands-pretooluse-hook-with-tests/raw" -o .agents/skills/block-dangerous-bash-commands-pretooluse-hook-with-tests/SKILL.md
SKILL.md
Preview
---
name: block-dangerous-bash-commands-pretooluse-hook-with-tests
description: "A SKILL.md that installs a Claude Code PreToolUse hook blocking rm -rf, force pushes, hard resets and DROP TABLE, plus a test script that proves each pattern blocks or allows."
---

# Block Dangerous Bash Commands: PreToolUse Hook With Tests

A SKILL.md that installs a Claude Code PreToolUse hook blocking rm -rf, force pushes, hard resets and DROP TABLE, plus a test script that proves each pattern blocks or allows.

## Prompt

```
---
name: destructive-command-guard
description: Installs and tests a Claude Code PreToolUse hook that blocks destructive shell commands (rm -rf, git push --force, git reset --hard, git clean -f, DROP or TRUNCATE) before they run. Use when someone asks to "block rm -rf", "stop Claude force pushing", "add a safety hook" or "test my PreToolUse hook".
---

# Destructive command guard

Exit codes for PreToolUse command hooks: 0 means no decision (normal permission rules apply), 2 blocks the call and shows stderr to Claude, any other code is a non-blocking error and the call still runs. So the guard must use exit 2, including when it cannot read its input.

## Steps

1. Check `jq` is installed. Read `.claude/settings.json` and list any PreToolUse hooks already there.
2. Write `.claude/hooks/blocked-patterns.txt`, one extended regex per line:
   - `rm +-[a-zA-Z]*([rR][a-zA-Z]*f|f[a-zA-Z]*[rR])`
   - `git +push .*(--force( |$)|-f( |$))`
   - `git +reset +--hard`
   - `git +clean +-[a-zA-Z]*f`
   - `(drop|truncate) +(table|database|schema)`
3. Write `.claude/hooks/destructive-guard.sh` and make it executable:

```bash
#!/usr/bin/env bash
command -v jq >/dev/null || exit 2
cmd=$(jq -r '.tool_input.command // ""') || exit 2
list="$CLAUDE_PROJECT_DIR/.claude/hooks/blocked-patterns.txt"
[ -r "$list" ] || exit 2
while IFS= read -r p; do
  [ -z "$p" ] && continue
  if printf '%s' "$cmd" | grep -Eiq -- "$p"; then
    echo "Blocked by destructive-command guard (pattern: $p). Explain why it is needed and ask the user to run it." >&2
    exit 2
  fi
done < "$list"
exit 0
```

4. Register it in `.claude/settings.json` under `hooks`, `PreToolUse`, with matcher `Bash` and a `command` hook pointing at the script. Merge with existing hooks; never overwrite them.
5. Write `.claude/hooks/guard-cases.tsv`, one case per line: `block` or `allow`, a tab, a command. Include each pattern, and near-misses that must pass: `rm -r build`, `git push --force-with-lease`, `git reset --soft HEAD~1`, `grep -r drop src`.
6. Write `.claude/hooks/test-guard.sh` that sets `CLAUDE_PROJECT_DIR` to `git rev-parse --show-toplevel` when it is unset (it is only set for real hook runs), pipes each command through the hook, building the input with `jq -n --arg c "$cmd" '.tool_input.command = $c'`, and compares the exit code (2 for block, 0 for allow). Print PASS or FAIL per case and exit 1 on any failure. Run it.

## Output

The files written, the settings change as a diff, and the test table with a final pass count.

## Stop and ask

- A test fails after two fixes to a pattern.
- The user wants patterns that would block their normal workflow (deploy scripts, migrations).
```

## Usage Notes

Save as .claude/skills/destructive-command-guard/SKILL.md and ask Claude Code to "add a safety hook". For which commands to block, approve or log, write the policy first with Checkable Guardrail Rules for a Coding Agent. Needs jq.

Example output

Example for: a Node.js repo with no existing hooks, run in Claude Code.

Files written

  • .claude/hooks/blocked-patterns.txt (5 patterns)
  • .claude/hooks/destructive-guard.sh (executable)
  • .claude/hooks/guard-cases.tsv (13 cases)
  • .claude/hooks/test-guard.sh (executable)

Settings change

+  "hooks": {
+    "PreToolUse": [
+      {
+        "matcher": "Bash",
+        "hooks": [
+          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/destructive-guard.sh" }
+        ]
+      }
+    ]
+  }

Tests

ExpectCommandExitResult
blockrm -rf node_modules dist2PASS
blockrm -fr ~/projects2PASS
blockgit push origin main --force2PASS
blockgit push -f2PASS
blockgit reset --hard HEAD~32PASS
blockgit clean -fdx2PASS
blockpsql -c "DROP TABLE users"2PASS
blockpsql -c "truncate table orders"2PASS
allowrm -r build0PASS
allowgit push --force-with-lease0PASS
allowgit reset --soft HEAD~10PASS
allowgrep -r drop src0PASS
allownpm test0PASS

13 of 13 passed.

Note: a regex guard stops accidents, not a determined bypass (for example a command written to a script file first). Keep sandboxing and permission rules as well.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • It fails closed: a missing jq, unreadable input or missing pattern file exits 2, because any other non-zero code lets the command run.
  • Near-miss test cases such as --force-with-lease and rm -r build catch patterns that would block normal work.
  • Patterns live in a text file, so you can add one and rerun the tests without touching the script.

Save as .claude/skills/destructive-command-guard/SKILL.md and ask Claude Code to "add a safety hook". For which commands to block, approve or log, write the policy first with Checkable Guardrail Rules for a Coding Agent. Needs jq.

Common edits

  • Add patterns for your stack, such as kubectl delete, terraform destroy or aws s3 rm --recursive, with a matching block case in the tests.
  • Swap exit 2 for a JSON permissionDecision of ask when you want a confirmation prompt instead of a hard block.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

More in Claude Code

Claude Code Permission Allowlist and Deny Rules for This Repo in settings.json

Has Claude Code read your repo's scripts and CI, then propose allow, ask and deny permission rules with a reason for each, split into shared and local settings files, plus commands to test them.

Package Skills, Hooks and MCP Into a Claude Code Plugin

A SKILL.md that turns the skills, subagents, hooks and MCP servers in your .claude folder into an installable Claude Code plugin with a marketplace file, then validates and test-installs it.

Create Selective Pre-Commit Hooks for Claude Code

This prompt generates a bash script that triggers specific tests and typechecks only for packages modified in your current git branch.

Coding

Batch Rename Files in a Folder with a Script You Can Preview First

You get a PowerShell or Bash rename script that shows every old-to-new name before changing anything, writes an undo log, and includes plain run steps.

Writing

Write a Blog Intro That Hooks Readers in Under 100 Words

You get three blog intros under 100 words each, built on a problem, a surprising fact from your notes, and a short scene, each checked for a payoff by sentence three.