Short answer
Use three layers. Deny rules in .claude/settings.json, such as Bash(rm *), stop the usual spellings. A PreToolUse hook reads the full command and can refuse it with exit code 2. The sandbox limits what any command can reach, however it is written. Text matching alone can be bypassed, so keep a commit or backup before risky work.
Claude Code is most useful when it can run commands. That is also how it deletes files, force-pushes over a teammate's work or drops a table. Claude Code gives you three tools against this: deny rules, hooks and the sandbox. Each catches something the others miss, so this guide covers all three, with a hook we tested.
Layer 1: deny and ask rules
In .claude/settings.json, list commands Claude Code must never run, and commands it must ask you about first. Claude Code checks deny first, then ask, then allow, and the first match wins, so a deny beats a broader allow.
{
"permissions": {
"deny": [
"Bash(rm -rf *)",
"Bash(git push --force *)",
"Bash(git reset --hard *)"
],
"ask": [
"Bash(rm *)",
"Bash(git push *)"
]
}
}The limit is that these rules match the text of the command. Claude Code's own permissions page says Bash rules aren't a security boundary, and gives examples: a Bash(rm *) rule stops rm -rf build/ but not /bin/rm -rf build/ or bash -c 'rm -rf build/'. Rules catch the usual spelling. They don't catch every spelling.
Layer 2: a PreToolUse hook that reads the whole command
A hook is your own script that Claude Code runs before each tool call. It gets the call as JSON on standard input, including the full shell command, and can refuse it by exiting with code 2. The message it prints goes back to Claude as the reason. Save this as .claude/hooks/block_rm.py:
#!/usr/bin/env python3
"""Claude Code PreToolUse hook: refuse shell commands that delete or force-push."""
import json, re, sys
BLOCK = [
(r"\brm\s+(-[a-zA-Z]*[rf][a-zA-Z]*\s+)+", "recursive or forced rm"),
(r"\bgit\s+push\b.*(--force|-f\b)", "force push"),
(r"\bgit\s+reset\s+--hard\b", "git reset --hard"),
(r"\b(DROP|TRUNCATE)\s+(TABLE|DATABASE)\b", "drops a table or database"),
]
event = json.load(sys.stdin)
command = event.get("tool_input", {}).get("command", "")
for pattern, why in BLOCK:
if re.search(pattern, command, re.IGNORECASE):
print(f"Blocked by hook: {why}. Ask the user to run it themselves.", file=sys.stderr)
sys.exit(2) # exit code 2 = refuse this tool call; stderr goes back to Claude
sys.exit(0)And register it in .claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "python3 .claude/hooks/block_rm.py" }
]
}
]
}
}We tested the script on 5 December 2026 by feeding it ten commands. It refused rm -rf build/, rm -r -f node_modules, /bin/rm -rf ~, bash -c 'rm -rf /', git push --force origin main and a DROP TABLE, and let through rm notes.txt, ls -la and a normal git push. It also let through find . -delete, which deletes just as well. A hook reads more than a rule does, but it is still matching text.
Layer 3: the sandbox
Claude Code's sandbox limits which files and network addresses commands can reach, whatever the command says. Its docs point to it for "filesystem and network enforcement that doesn't depend on the command text". This is the layer that catches the spelling you didn't think of. Use it for anything that runs without you watching.
And keep a way back
- Commit before you let it loose. Claude Code's rewind doesn't bring back files that only a shell command changed. Git does.
- Don't give it production credentials. A command that can't reach the production database can't drop it.
- Be careful with skip-permissions modes. They exist for sandboxed, throwaway environments, not your laptop with your real files on it.
Doing it with SteerPlane
Rules in each developer's settings file drift apart, and nobody sees what was refused. SteerPlane's Claude Code hooks check every tool call against rules kept on one server, so the whole team shares them and every call and refusal is recorded. From version 1.2.1:
pip install "steerplane[cli]" export STEERPLANE_API_URL=http://localhost:8000 # your SteerPlane server export STEERPLANE_API_KEY=sk_sp_your_steerplane_key steerplane claude-code install --scope user # every project on this machine
The rules are policies whose name starts with claude-code. Deny, allow and approval patterns match the tool name or the tool with its argument, for example Bash(*rm -rf*), Write(*.env) or Bash(git push*). An approval pattern makes Claude Code ask you. If SteerPlane can't be reached, the tool call is refused rather than let through. The same hooks can also hold each session to a spending limit. We tested them with real Claude Code on 1 December 2026.
One thing to know. SteerPlane's patterns also match command text, the same way Claude Code's own rules do, and they are case-sensitive. They give you one shared set of rules and a record of every call. They don't replace the sandbox.
A checklist before you let Claude Code run commands
- Deny rules for commands that must never run, ask rules for the risky ones.
- A PreToolUse hook for patterns rules can't express.
- The sandbox on for unattended work.
- Work committed in git before a long session.
- No production credentials in the environment Claude Code runs in.
Questions people ask
Can Claude Code delete my files?
Yes, if it is allowed to run shell commands. Claude Code's rewind does not bring back files that only shell commands changed, so keep work committed in git before letting it run commands.
How do I block a command in Claude Code?
Add it to permissions.deny in .claude/settings.json, for example "Bash(git push *)". Deny is checked before ask and allow, so it wins over a broader allow.
Are Claude Code deny rules a security boundary?
No. Claude Code's own docs say Bash rules match the usual way a command is written and give /bin/rm -rf build/ and bash -c 'rm -rf build/' as examples that a Bash(rm *) rule does not stop. Use the sandbox for enforcement that doesn't depend on the command text.