Agent Wranglers

AID-13Recommended

Block dangerous actions with hooks

Use the agent's hooks or permission rules to block actions that must never happen, instead of trusting instructions.

How to do it

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 as the boundary. For cloud accounts, Block dangerous actions for the whole organization applies the same idea.

Examples

JSON example

{
  "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" }] }]
  }
}

Shell example

#!/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

How to 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.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-assisted-development/rules-the-agent-cannot-break/blocking-hooks">Agent Wranglers AI-assisted development playbook</a>. In code you write, add a comment next to it with the same link.