How do you stop Claude Code from deleting files or pushing to main?
Use layers. Deny rules in .claude/settings.json stop the usual forms of rm -rf and git push. A PreToolUse hook reads each command and blocks it with exit code 2. Deny rules keep secret files out of reach, a Stop hook gates done on tests, and branch protection on the git host is the backstop.
What do permission rules stop?
Rules in .claude/settings.json are checked deny first, then ask, then allow, and an allow rule cannot carve an exception out of a deny. A deny on Bash(rm -rf *) or Bash(git push *) stops the form Claude usually writes. Anthropic is plain about the limit: a Bash rule matches the command text, so git -C . push or bash -c 'rm ...' gets past it. Rules are the first layer, not the wall.
How does a hook block a command?
A PreToolUse hook runs before each tool call and gets the call as JSON on stdin. Exit 0 means no objection; the normal permission flow continues. Exit 2 blocks the call, and what you print to stderr becomes Claude's feedback. Most other codes, exit 1 included, let the call through, which is the classic mistake. This one refuses pushes to main and force pushes:
#!/bin/bash
cmd=$(jq -r '.tool_input.command // empty')
if echo "$cmd" | grep -qE 'git .*push.*([ :+](main|master)( |$)|--force|-f( |$))'; then
echo "Blocked by policy: no push to main, no force push. Push a branch and open a PR." >&2
exit 2
fi
exit 0
Register it under PreToolUse with matcher Bash in .claude/settings.json. Then test it: pipe a sample payload into the script and check for exit 2. It still matches text, so a plain git push while on main slips through.
How do you keep secret files out of reach?
Deny Read(./.env) and Read(./.env.*). Those rules also cover cat, head and redirections, but not a script that opens the file itself. Our secret-file guard runs on Bash, Read, Edit, Write, Grep and Glob, matches .env files, keys and credential paths, and blocks with exit 2. A false block costs one retry; a missed secret costs a rotation.
How do you stop work being called done untested?
A Stop hook fires when Claude finishes responding, and exit 2 keeps it working. Have it run the tests and block on a failure. Check the stop_hook_active field so it does not loop; Claude Code overrides a Stop hook that blocks eight times running. We also send back any pull request that says tests pass without naming the command.
What is the final backstop?
Branch protection on the git host. On GitHub a protection rule blocks force pushes and branch deletion by default, and can require reviews, passing checks and a list of who may push. That holds whatever happens on the laptop. Our pushes to main are refused twice, by a hook and by the server.
Are hooks a security boundary?
No. They match what Claude writes, and command hooks run with your full user permissions. For enforcement that does not depend on command text, Anthropic points to the sandbox: off by default, on with /sandbox, OS-level limits on files and network for shell commands on macOS, Linux and WSL2. File tools, MCP servers and hooks run outside it, and reads include ~/.ssh unless you deny them. The docs also note that without managed settings or a Team or Enterprise login, a plugin mod can approve a call your hook blocked. We set all five layers up in our Claude Code setup.
Questions owners ask
Is a line in CLAUDE.md enough?
No. Anthropic says Claude treats CLAUDE.md as context, not enforced configuration. Write the rule there so Claude knows why, and enforce it with a deny rule or a hook.
Does bypass permissions mode skip my hooks?
No. Per the docs, a PreToolUse hook that denies a call blocks it even in bypassPermissions mode.
What should Claude do when a hook blocks it?
Say it was blocked by policy and stop. Our CLAUDE.md and our hook messages both say never to reach the file another way, and we test that by asking.
Sources
- Anthropic, Claude Code docs: configure permissions (deny, ask, allow order; what a Bash rule does not match)
- Anthropic, Claude Code docs: automate actions with hooks (exit codes, Stop hooks, hooks and permission modes)
- Anthropic, Claude Code docs: hooks reference, security considerations
- Anthropic, Claude Code docs: sandboxing for shell commands (off by default, macOS and Linux)
- GitHub Docs: about protected branches (force pushes and deletion blocked by default, required reviews and checks)