Stop Claude Code from deleting a critical path
Wire DefenseClaw into Claude Code, collect representative observe-mode traffic, then safely verify CMD-RM-RF against a path that can't be deleted.
I want to observe Claude Code and block recursive force deletion against critical system paths.
Run Claude Code under observe mode first, then switch to action mode so the bundled CMD-RM-RF rule (CRITICAL) blocks a recursive force delete of / or a top-level directory such as /etc, /var, /home, /usr or /opt. The rule does not flag every rm -rf: a path below those directories (/var/tmp/build), a relative path or a home subdirectory is not matched.
Before you start
- Claude Code is installed.
- DefenseClaw is installed (Install).
Outcome
By the end of this story, you'll have:
- Claude Code wired to DefenseClaw through its native hooks.
- Observe-mode alerts and audit rows from normal Claude Code use.
- An
actionmode policy that blocksrm -rfagainst/and protected top-level directories with the CRITICALCMD-RM-RFrule. - An audit row showing the block, with the rule ID for the next reviewer.
Wire Claude Code (observability only)
Wire Claude Code. It starts in observe mode.
defenseclaw setup claude-codeDefenseClaw registers the hook events for your Claude Code version (28 events, or 29 from 2.1.219 where DirectoryAdded is observation only) and records each hook call in its audit log. Host setup turns Claude Code's native OpenTelemetry export off: ~/.claude/settings.json gets CLAUDE_CODE_ENABLE_TELEMETRY=0 and none for the OTEL_LOGS_EXPORTER, OTEL_METRICS_EXPORTER and OTEL_TRACES_EXPORTER entries, so on a host install DefenseClaw sees Claude Code through its hooks only. Only an isolated OpenShell sandbox keeps native logs and metrics. See the Claude Code connector.
Watch the audit log fill up
Use Claude Code normally for a while, then read what was recorded.
defenseclaw alerts --limit 10
defenseclaw tuiInspect the rule pack
Look at the rule.
grep -n -A10 'id: CMD-RM-RF' ~/.defenseclaw/policies/guardrail/default/rules/commands.yamlIn PowerShell:
Select-String -Path "$HOME\.defenseclaw\policies\guardrail\default\rules\commands.yaml" -Pattern 'id: CMD-RM-RF' -Context 0,10114: - id: CMD-RM-RF
…
121- title: "Recursive force delete from critical root path"
122- severity: CRITICALTurn on blocking
Turn on blocking for Claude Code (Turn on blocking has the detail).
defenseclaw guardrail mode action --connector claudecodeTrigger the rule
Ask Claude Code to run rm -rf /proc. /proc is a kernel pseudo-filesystem, so the exercise stays safe even if blocking were off. The hook sends the command to the gateway, the gateway matches CMD-RM-RF, and Claude Code gets a deny before the shell starts:
DefenseClaw policy blocked this action (rule CMD-RM-RF: Recursive force delete from critical root path). Do not retry it in another form.Many models refuse this command themselves. Then no tool call is made and there is nothing to block; use the test rule in the quickstart instead.
Confirm in the audit log
defenseclaw alerts --limit 5
defenseclaw-gateway audit export --connector claudecode --since 30m \
| jq -c 'select(.action == "connector-hook") | {timestamp, action: .structured.action, rule_ids: .structured.rule_ids}'In PowerShell:
defenseclaw-gateway audit export --connector claudecode --since 30m |
ForEach-Object { $_ | ConvertFrom-Json } |
Where-Object { $_.action -eq 'connector-hook' } |
ForEach-Object { [pscustomobject]@{ timestamp = $_.timestamp; action = $_.structured.action; rule_ids = $_.structured.rule_ids } }{"timestamp":"2026-10-21T06:59:40.323651948Z","action":"block","rule_ids":["CMD-RM-RF"]}Each hook call writes one connector-hook row. action is the answer Claude Code received.
Why this works the way it does
List view for small screens. Use the expand button to open the drawing.
- Claude CodeBash: rm -rf /proc
- callsPreToolUse hook
- PreToolUse hookruns before the tool
- asksGateway
- Gatewaymatches CMD-RM-RF (CRITICAL)
- recordsAudit log
- checksGuardrail mode?
- Guardrail mode?
- observeObserve: allowed
- actionAction: denied
- Audit log~/.defenseclaw/audit.db
- Action: deniedthe shell never starts
- Observe: allowedlogged as would-block
Variations
Undo
defenseclaw guardrail mode observe --connector claudecodeNext
Stories
Step-by-step walkthroughs for common goals — block a destructive command on Claude Code, catch a prompt injection on Codex, stop a secret upload from Cursor, approve risky tool calls, run the local observability stack, and add a connector without losing audit history.
Catch a prompt injection on Codex
Verify Codex prompt enforcement with the bundled TRUST-IGNORE-PREVIOUS rule, then add the optional judge for ambiguous and semantic cases.