# Rule, hook or sandbox: which one actually stops your agent

> Claude Code has four guard layers and none is a full wall on its own. Here is what each one stops.

Source: https://fullbytedeveloper.com/blog/guard-layers
Published here: 2026-11-11
Tested: 2026-11-08, Claude Code hooks with jq

You told your agent never to delete files. It still can. Claude Code has four guard layers, and none is a full wall on its own. Here is what each one stops (docs checked Nov 8).

## Layer 1: CLAUDE.md

A line in CLAUDE.md shapes what Claude tries to do. Claude Code enforces nothing from it [1].

## Layer 2: Deny rules

A deny rule like `Bash(rm *)` stops `rm -rf build/`. It does not stop `/bin/rm -rf build/` or `bash -c 'rm -rf build/'` [1].

Rules are checked deny, then ask, then allow. The first match wins.

```json title=".claude/settings.json"
{
  "permissions": {
    "deny": ["Bash(rm *)"]
  }
}
```

## Layer 3: PreToolUse hook

A hook reads the full command. Exit 2 blocks the call before permission rules are checked, even when an allow rule matches [2]. It blocks only what your script matches.

```bash title=".claude/hooks/block-rm.sh"
#!/bin/bash
cmd=$(jq -r '.tool_input.command')
if grep -qw rm <<<"$cmd"; then
  echo "Blocked: $cmd" >&2
  exit 2
fi
```

Make it executable (`chmod +x`) and register it in settings [2]:

```json title=".claude/settings.json"
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh",
            "args": []
          }
        ]
      }
    ]
  }
}
```

**Warning:** needs `jq` installed. Without it the script blocks nothing.

In my test (Nov 8): the hook returned exit 2 for `rm`, `/bin/rm` and `bash -c 'rm'`, and exit 0 for `find build -delete`. A hook is as good as your match.

## Layer 4: Sandbox

The sandbox is an OS boundary for shell commands (Bash, PowerShell, Monitor) and every process they start [3]. Type `/sandbox` in a session to turn it on. Works on macOS, Linux and WSL2. On native Windows, commands run unsandboxed.

**Warning:** by default the sandbox still lets commands write and delete inside the project folder, and sandboxed commands run without a prompt. List paths to protect in `sandbox.filesystem.denyWrite` [3].

Read, Edit, WebFetch, MCP servers and hooks run outside the sandbox [3].

## Use all four

Each layer catches what the others miss. CLAUDE.md steers intent, deny rules block named patterns, a hook inspects the full command, and the sandbox walls off the machine.

Send this to the friend who told their agent never to delete files and assumes it listens.

## Sources

1. [Claude Code docs: permissions](https://code.claude.com/docs/en/permissions)
2. [Claude Code docs: hooks](https://code.claude.com/docs/en/hooks)
3. [Claude Code docs: sandboxing](https://code.claude.com/docs/en/sandboxing)
