---
title: "Block dangerous actions with hooks · Agent Wranglers"
url: https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break/blocking-hooks
updated: 2026-10-20
licence: "All rights reserved"
---

# Block dangerous actions with hooks

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [Rules the agent cannot break](https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break)
- Rule: Use the agent's hooks or permission rules to block actions that must never happen, instead of trusting instructions.
- How: Start with the agent's deny rules in its settings file, then add a hook for anything a rule cannot express. Claude Code, for example, blocks a call when a `PreToolUse` hook exits with code 2. Block force pushes, deleting folders, editing applied migrations, reading secret files and `git commit --no-verify`. Commit the settings so the whole team gets them. Text patterns miss rewritten commands, such as `git -C . push --force`. A file rule may not cover a shell `cat` of the same file. Treat them as a tripwire, and keep the [sandbox](https://agentwranglers.se/playbook/ai-assisted-development/safety-and-permissions/sandbox) as the boundary.
- Check: Ask the agent to run each blocked command, such as `git push --force` and reading `.env`. The rule or hook stops every one, and the agent reports the reason.

Example:

```json
{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Edit(./migrations/applied/**)", "Bash(git push --force *)"],
    "ask": ["Bash(git push *)", "Bash(rm *)"]
  },
  "hooks": {
    "PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": ".claude/hooks/block-dangerous.sh" }] }]
  }
}
```

```bash
#!/bin/bash
# .claude/hooks/block-dangerous.sh, run before every shell command
command=$(jq -r '.tool_input.command')
if [[ "$command" =~ (push.*\ (--force|-f)|--no-verify|rm\ -rf\ /|DROP\ TABLE) ]]; then
  echo "Blocked by project policy: $command" >&2
  exit 2
fi
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers ai development playbook: https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break/blocking-hooks`.

- Previous: [Use strict types and linters](https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break/strict-tooling)
- Next: [Protect the checks themselves](https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break/protect-the-checks)
- [Rules the agent cannot break](https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break)
