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 action mode policy that blocks rm -rf against / and protected top-level directories with the CRITICAL CMD-RM-RF rule.
  • 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-code

DefenseClaw 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 tui

Inspect the rule pack

Look at the rule.

grep -n -A10 'id: CMD-RM-RF' ~/.defenseclaw/policies/guardrail/default/rules/commands.yaml

In PowerShell:

Select-String -Path "$HOME\.defenseclaw\policies\guardrail\default\rules\commands.yaml" -Pattern 'id: CMD-RM-RF' -Context 0,10
114:  - id: CMD-RM-RF
…
121-    title: "Recursive force delete from critical root path"
122-    severity: CRITICAL

Turn on blocking

Turn on blocking for Claude Code (Turn on blocking has the detail).

defenseclaw guardrail mode action --connector claudecode

Trigger 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

calls
asks
records
checks
observe
action
Claude CodeBash: rm -rf /proc
PreToolUse hookruns before the tool
Gatewaymatches CMD-RM-RF (CRITICAL)
Audit log~/.defenseclaw/audit.db
Guardrail mode?
Observe: allowedlogged as would-block
Action: deniedthe shell never starts

List view for small screens. Use the expand button to open the drawing.

  1. Claude CodeBash: rm -rf /proc
    • callsPreToolUse hook
  2. PreToolUse hookruns before the tool
    • asksGateway
  3. Gatewaymatches CMD-RM-RF (CRITICAL)
    • recordsAudit log
    • checksGuardrail mode?
  4. Guardrail mode?
    • observeObserve: allowed
    • actionAction: denied
  5. Audit log~/.defenseclaw/audit.db
  6. Action: deniedthe shell never starts
  7. Observe: allowedlogged as would-block
The gateway makes the decision, not Claude Code. The PreToolUse hook only carries the command and the answer. The same CMD-RM-RF finding is recorded in both modes. Only action mode turns it into a deny, and a CRITICAL finding never becomes an approval prompt.

Variations

Undo

defenseclaw guardrail mode observe --connector claudecode

Next