~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
- Check: T3 is complete, or your project is open in VS Code with
.mcp.jsonpresent (T4 adds to the same.claude/settings.jsonfrom T1/T3). - Check: Claude Code extension is active in VS Code and you can start a new session.
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
- 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)
(Optional) Add personal guardrails to ~/.claude/settings.json.
These apply across all projects on your machine, not just signup-api.
Paste the content below, then:
- File does not exist yet: save it as
~/.claude/settings.json. - File already exists: open
~/.claude/settings.jsonand merge thedenyandaskentries into your existingpermissionsblock.
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*)"
]
}
}
rm -rf * destructive shell pattern.
git push or git reset --hard variant.
~/.claude/settings.json. Every project you open on this machine will inherit these rules.
/status shows “User settings” in the Setting sources row.
Docs: [1]
Add project deny rules to .claude/settings.json.
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.jsonand merge thepermissionsblock 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 *)"
]
}
}
Read(./.env) blocks the project’s .env file; Bash(rm -rf *) blocks that specific destructive pattern.
git push or git reset --hard.
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__*).
.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).
.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]
Start a new session and verify the deny rule blocks a read.
- Click + New session in the Claude Code extension tab in VS Code.
- If your project does not have a
.envfile, create one with one line (e.g.SECRET=test). - 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.
.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.
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).
.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.
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 ."
}
]
}
]
}
}
- Update
.claude/settings.jsonwith the hook block above. - Start a new session: click + New session in the Claude Code extension tab.
- Run
/hooksin the chat — confirm the PostToolUse hook appears. - Ask Claude to make a small intentionally messy edit:
In main.py, add an extra blank line after the imports, then stop. - After Claude’s edit, check: did ruff format automatically clean it up?
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]
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.
/permissions shows your deny and ask rules active.
Your own project
(Optional) Add personal guardrails to ~/.claude/settings.json.
These apply across all projects on your machine. Add deny rules for credential paths that matter in your context.
- File does not exist yet: create
~/.claude/settings.jsonwith the structure below. - File already exists: open
~/.claude/settings.jsonand add deny rules for your credential paths.
Adapt the paths below to match the credentials you want to protect on this machine:
File contents — not a command:
{
"permissions": {
"deny": [
"Read(~/.ssh/**)",
"Read(~/.aws/**)",
"Read(~/.git-credentials)",
"Read(~/.npmrc)"
],
"ask": [
"Bash(git push --force*)"
]
}
}
git push --force variant.
~/.claude/settings.json. Every project you open on this machine will inherit these rules.
/status shows “User settings” in the Setting sources row.
Docs: [1]
Add project deny rules to .claude/settings.json.
- File does not exist yet: create
.claude/settings.json(create the.claude/folder first if needed) with the structure below. - File already exists: open
.claude/settings.jsonand merge thepermissionsblock into it.
Adapt the rules for your project — replace or extend the deny and ask patterns shown:
File contents — not a command:
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"deny": [
"Read(**/.env)",
"Bash(rm -rf *)"
],
"ask": [
"Bash(git push*)",
"Bash(git reset --hard*)"
]
}
}
.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).
.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]
Start a new session and verify the deny rule blocks a read.
- Click + New session in the Claude Code extension tab in VS Code.
- If your project does not have a
.envfile, create one with one line (e.g.SECRET=test). - 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.
.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.
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).
.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.
Add a PostToolUse hook for automatic enforcement.
A hook runs automatically after every file edit — no approval needed once configured. Adapt the formatter command to your stack.
Update .claude/settings.json to include the hooks block below. Replace the formatter command with the one for your project.
File contents — not a command:
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"deny": [
"Read(**/.env)",
"Bash(rm -rf *)"
],
"ask": [
"Bash(git push*)",
"Bash(git reset --hard*)"
]
},
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "cd \"$CLAUDE_PROJECT_DIR\" && <your-formatter-command>"
}
]
}
]
}
}
Replace <your-formatter-command> with your stack’s formatter, e.g. npx prettier --write . for Node.js/TypeScript, uv run ruff format . for Python (uv), or black . for Python (pip). Keep cd "$CLAUDE_PROJECT_DIR" so the command always runs from the project root.
- Update
.claude/settings.jsonwith the hook block above. - Start a new session: click + New session in the Claude Code extension tab.
- Run
/hooksin the chat — confirm the PostToolUse hook appears. - Ask Claude to make a small intentionally messy edit:
In main.py, add an extra blank line after the imports, then stop. - After Claude’s edit, check: did your formatter automatically clean it up?
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]
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.
/permissions shows your deny and ask rules active.
/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,/statusto 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/. /skillslists 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/sandboxin 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