~10 min

T4 · Guardrails

Layer your guardrails: project deny rules, an enforcement hook, and optional personal settings.

Knowledge refresher — settings, hooks, and what they actually cover

Five layers of protection — weakest first. Only the last two hold when the model is wrong or has been manipulated:

CLAUDE.md Guidance Claude reads and usually follows Doesn’t hold
Permission rules A filter on the commands Claude asks to run Doesn’t hold
PreToolUse hooks Your script decides, with your full permissions Partly
OS sandbox Limits the running process, whatever the command Holds: shell only
Container or VM Isolates everything Claude Code runs Holds

In this task you set up the first three rows. Use them — but don’t present them as a security boundary. OS sandbox and containers are in Appendix 3 with docs links.

  • Permission rules (deny/ask) — enforced by Claude Code, not the model. A Bash rule, as written, isn’t a security boundary around the program — a script run with approval bypasses it. — Settings files and precedence
  • Hooks — your script runs with your full user permissions. More reliable than deny rules because they fire on the tool event, not a command name match. — Hooks
  • OS sandbox and containers — the only layers that hold regardless of what the model chose to run. Beyond the scope of today’s hands-on. — Sandboxing

Before you start

T4 can run independently — you just need a project open in VS Code. If your environment is not ready, follow along and complete it afterwards.


Steps

Which path is yours?
  • Option 1: Sample project (signup-api) — ready-made settings are shown as copy-paste blocks at each step — review and use them as-is.
  • Option 2: Your own project — adapt the patterns shown to your stack and credentials.

Sample project (signup-api)

1

(Optional) Add personal guardrails to ~/.claude/settings.json.

These apply across all projects on your machine, not just signup-api.

~/ ← your home directory
└── .claude/
    └── settings.json ← create or update this

Paste the content below, then:

  • File does not exist yet: save it as ~/.claude/settings.json.
  • File already exists: open ~/.claude/settings.json and merge the deny and ask entries into your existing permissions block.

File contents — not a command:

{
  "$schema": "https://json.schemastore.org/claude-code-settings.json",
  "permissions": {
    "deny": [
      "Read(~/.ssh/**)",
      "Read(~/.aws/**)",
      "Read(~/.config/gh/**)",
      "Read(~/.git-credentials)",
      "Read(~/.npmrc)",
      "Read(~/.pypirc)",
      "Read(~/.docker/config.json)",
      "Read(~/.kube/**)",
      "Bash(rm -rf *)"
    ],
    "ask": [
      "Bash(git push *)",
      "Bash(git push --force*)",
      "Bash(git reset --hard*)"
    ]
  }
}
deny Highlighted orange — credential paths — blocks Claude’s Read tool on SSH keys, AWS credentials, GitHub CLI config, git credentials, npm, PyPI, Docker, and Kubernetes configs, plus the rm -rf * destructive shell pattern.
ask Highlighted teal — push and reset confirmations — Claude pauses and shows you the command before running any git push or git reset --hard variant.
User-level — applies to ALL projects on this machine
This writes to ~/.claude/settings.json. Every project you open on this machine will inherit these rules.
Done when
/status shows “User settings” in the Setting sources row.

Docs: [1]

2

Add project deny rules to .claude/settings.json.

signup-api/ ← VS Code workspace root
└── .claude/
    └── settings.json ← create or update this

Paste the content below, then:

  • File does not exist yet: save it as .claude/settings.json (create the .claude/ folder first if needed).
  • File already exists: open .claude/settings.json and merge the permissions block into it, or ask Claude to merge it for you.

File contents — not a command:

{
  "$schema": "https://json.schemastore.org/claude-code-settings.json",
  "permissions": {
    "allow": [
      "Bash(uv run pytest *)",
      "Bash(uv run ruff check *)",
      "Bash(uv run ruff format *)",
      "Bash(git status)",
      "Bash(git diff *)",
      "mcp__project-helper__*"
    ],
    "ask": [
      "Bash(git push *)",
      "Bash(git reset --hard *)"
    ],
    "deny": [
      "Read(./.env)",
      "Bash(rm -rf *)"
    ]
  }
}
deny Highlighted orange — env file + destructive shell — Read(./.env) blocks the project’s .env file; Bash(rm -rf *) blocks that specific destructive pattern.
ask Highlighted teal — irreversible git operations — Claude pauses and shows you the command before running any git push or git reset --hard.
allow — pre-approved commands that run without a prompt: test runner (uv run pytest), linter/formatter (uv run ruff), read-only git (git status, git diff), and all tools on the project-helper MCP server from T3 (mcp__project-helper__*).
Project-level — committed to the repo
.claude/settings.json is committed to the repo. These rules apply to everyone who opens this project.

Open .claude/settings.json in VS Code and confirm it parses as valid JSON (no red squiggles, no syntax errors in the editor status bar).

Done when
.claude/settings.json exists and looks correct.

Permission rules filter the commands Claude asks to run. A script you approve can still bypass them — you’ll see this in the next step.

Docs: [1]

3

Start a new session and verify the deny rule blocks a read.

  1. Click + New session in the Claude Code extension tab in VS Code.
  2. If your project does not have a .env file, create one with one line (e.g. SECRET=test).
  3. In the Claude Code chat, ask:
Use the Read tool to open .env

Expected: Claude’s Read tool is denied — you should see a permission denial, not the file contents.

Done when
Claude shows a denial message for the Read tool and .env contents are not revealed.

The deny rule stopped Claude’s Read tool. But it only covers that one tool — Step 4 shows what still gets through.

4

See what the deny rule does not cover.

This is an intentional teaching moment. The deny rule blocks Claude’s Read tool — not every way a file can be opened.

In the Claude Code chat, ask:

Run: python -c "print(open('.env').read())"

Approve the Bash command when prompted. Result: the script reads .env despite the deny rule.

The deny rule covered Claude’s Read tool. Bash with your approval can still read files. This is not a failure — your approval is part of the control. For a stronger boundary, use a hook (next step) or OS sandboxing (see Appendix 3).

Done when
You saw the script read .env despite the deny rule. The limitation is understood.

This is the boundary: permission rules protect Claude’s tools, not the shell. Hooks go further.

5

Add a PostToolUse hook for automatic enforcement.

A hook runs automatically after every file edit — no approval needed once configured. This example auto-formats Python files with ruff after every edit Claude makes.

Update .claude/settings.json to include the full content below, which combines the permissions from Step 2 with the hook.

File contents — not a command:

{
  "$schema": "https://json.schemastore.org/claude-code-settings.json",
  "permissions": {
    "allow": [
      "Bash(uv run pytest *)",
      "Bash(uv run ruff check *)",
      "Bash(uv run ruff format *)",
      "Bash(git status)",
      "Bash(git diff *)",
      "mcp__project-helper__*"
    ],
    "ask": [
      "Bash(git push *)",
      "Bash(git reset --hard *)"
    ],
    "deny": [
      "Read(./.env)",
      "Bash(rm -rf *)"
    ]
  },
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "cd \"$CLAUDE_PROJECT_DIR\" && uv run ruff format ."
          }
        ]
      }
    ]
  }
}
deny Highlighted orange — same deny rules from Step 2.
ask Highlighted teal — same ask rules from Step 2.
  1. Update .claude/settings.json with the hook block above.
  2. Start a new session: click + New session in the Claude Code extension tab.
  3. Run /hooks in the chat — confirm the PostToolUse hook appears.
  4. Ask Claude to make a small intentionally messy edit:
    In main.py, add an extra blank line after the imports, then stop.
  5. After Claude’s edit, check: did ruff format automatically clean it up?
Done when
After Claude’s edit, the file is formatted correctly without you asking — the hook ran.

If the hook didn’t run, check that .claude/settings.json has both the permissions and hooks blocks merged correctly, then start a new session.

The hook fires on the tool event regardless of what Claude asked. It’s more reliable than a deny rule because your script decides, with your full permissions. For complete isolation, see Appendix 3.

Docs: [2]

6

Run /permissions and /status to see the full picture.

These read-only commands show you the effective rule set and which settings files loaded — useful any time you want to understand what guardrails are active.

Done when
/permissions shows your deny and ask rules active.
Look around — read-only, ~1 min
/permissions   — effective allow / ask / deny rules and which file each came from
/hooks         — loaded hooks grouped by event
/status        — which settings files are active and their precedence

No changes needed. These commands are always safe to run.

After this task, you should be able to

  • Add project-level deny and ask rules and understand what they protect against (and what they don’t)
  • Configure a PostToolUse hook and verify it fires
  • Read /permissions, /hooks, /status to understand the active guardrail state

To build on this

  • Other useful global settings — default model, pre-approved git commands, WebFetch controls → Appendix 1
  • Team skills and slash commands — reusable instruction sets invoked on demand → Appendix 2
  • Sandboxing — complete isolation for sensitive work → Appendix 3

Appendix

1 — Other useful global settings

Things people commonly add to ~/.claude/settings.json:

  • Default model: "model": "claude-sonnet-4-5"
  • Pre-approve safe read-only git commands: "allow": ["Bash(git status)", "Bash(git diff *)", "Bash(git log *)"]
  • WebFetch domain allow/deny: "allow": ["WebFetch(domain:docs.python.org)"]

Docs: [1]

2 — Team skills and slash commands

Skills are reusable instruction sets you invoke on demand with a slash command — they load only when you ask for them, keeping every session lean.

  • Project skills live in .claude/skills/.
  • /skills lists what’s loaded in the current session.
  • Built-in slash commands you’ve already seen: /init, /doctor, /memory, /mcp, /hooks, /permissions, /status.
  • Custom skills extend this with team-specific patterns — a code review checklist, a PR format template, a release checklist.

If time allows: create .claude/skills/house-checks/SKILL.md in your project (create the folders if needed) with the content below, then invoke it with /house-checks.

File contents — not a command:

---
name: house-checks
description: Verify code changes before reporting a task as done. Use after editing code in this project.
---

# House checks

1. Run the checks: `uv run ruff check .`, `uv run ruff format --check .` and `uv run pytest -q`.
   If the project uses other commands, use the ones in its README or `pyproject.toml`.
2. Read the changes: `git diff`
3. Confirm no test was weakened. Look for removed or loosened assertions, new `skip` or `xfail` markers, deleted tests, and expected values changed without a reason in the task.
4. Report PASS or FAIL for each check, with evidence (the command and its key output lines). Never report the task as done while a check is failing.

Docs: [3]

3 — Sandboxing — complete isolation

For the most sensitive work, deny rules and hooks are not enough. Options in increasing isolation order:

  • Claude Code OS sandbox — run with claude --sandbox (macOS and Linux). Restricts filesystem and network access at the OS level. In VS Code, run /sandbox in the Claude Code chat to toggle sandbox mode for the current session and check its status.
  • Docker container — run Claude Code inside a container where the blast radius is limited to the container.
  • VM or ephemeral cloud environment — for the highest assurance, run in a fresh VM or cloud development environment that is discarded after the session.

Docs: [4]


References

  1. Settings files and precedence
  2. Hooks
  3. Slash commands and skills
  4. Security and trust