✻Founding-member pricing: every lesson for $19/mo. The first 3 lessons are free. Start lesson 1 →
Unit 3 · Configure Claude · Lesson 15 of 40

Permission Rules & Sandboxing

Write precise allow, ask and deny permission rules with Claude Code's rule syntax, and use OS-level sandboxing to restrict files and network access.

ACADEMY11 min+72 XP4-question quiz

🎯 By the end of this lesson you'll be able to

  • Read and write permission rule syntax
  • Order allow, ask and deny rules correctly
  • Avoid common wildcard pitfalls
  • Use /sandbox for OS-level isolation

Rules: fine-grained control on top of modes

Modes set the overall dial. Rules carve out precise exceptions: always allow pnpm test, always ask before git push, never read .env.

.claude/settings.json
{
  "permissions": {
    "allow": [
      "Bash(pnpm test *)",
      "Bash(git status *)",
      "Bash(git diff *)",
      "WebFetch(domain:docs.stripe.com)"
    ],
    "ask": ["Bash(git push *)", "Bash(npm publish *)"],
    "deny": ["Read(./.env)", "Read(./.env.*)", "Edit(./migrations/applied/**)", "Bash(rm -rf *)"]
  }
}

Rule syntax cheat sheet

RuleMatches
BashEvery shell command
Bash(npm run test *)Commands starting with npm run test
Read(./.env)Reading .env in the project root
Edit(./src/**)Edits anywhere under src/
WebFetch(domain:example.com)Fetching pages from that domain
mcp__github__*Every tool from the github MCP server
Skill(deploy) / Agent(Explore)A specific skill or subagent type
!
The space before * matters

Bash(git diff *) matches git diff followed by arguments. Bash(git diff*) also matches git diff-index and anything else that starts with those letters. Include the space unless you really mean a prefix.

How rules combine

  • Deny beats ask beats allow. If anything denies, it's denied.
  • Rules from all settings scopes are combined.
  • Deny rules hold in every permission mode.
◎Which list does it belong in?pick the best fit
pnpm test and pnpm lint, run dozens of times a day.
Safe, frequent and read-mostly. Pre-approve it.
git push to the remote.
It's outward-facing, so confirm each time.
Reading .env or the secrets/ folder.
Claude never needs to read raw secrets.
terraform apply against production.
Far too dangerous to approve from an agent session.

Managing rules interactively

/permissions lists every active rule, shows which settings file it came from, and lets you add or remove rules. When you choose "Yes, don't ask again" at a prompt, Claude Code writes the matching allow rule for you.

✓
Cut prompts safely

If you're approving the same safe command over and over, add an allow rule for exactly that command pattern. Allow Bash(pnpm test *), not Bash.

Sandboxing: limits enforced by the OS

Rules decide whether a command runs. Sandboxing limits what a command can touch once it's running. With /sandbox enabled, Bash commands run inside an OS-level sandbox that restricts:

  • Filesystem: writes are limited to your working directories.
  • Network: outbound connections are limited to hosts you approve.

That means even if a command is allowed, it can't scribble outside your project or phone home to an unapproved server. Sandboxing and rules work best together: rules for intent, sandbox for blast radius.

⌨Type the command+15 XP

Write a permission rule that blocks reading the .env file in the project root

❯

Check your understanding

?Lesson quiz10 XP per correct answer · +25 for a perfect score
QUESTION 1 OF 4

Which rule pre-approves only test commands that start with pnpm test?

Why: Tool name, then a command pattern. The space before * limits it to arguments.
QUESTION 2 OF 4

What's the order of precedence for rules?

Why: Deny always wins, then ask, then allow.
QUESTION 3 OF 4

What does sandboxing add on top of permission rules?

Why: Sandboxing restricts what an allowed command can actually touch.
QUESTION 4 OF 4

Why is Bash(git diff*) riskier than Bash(git diff *)?

Why: The space marks the end of the command word.
0 / 4 correct
Academy lesson

Unlock lesson 15 and the next 25

You've finished the free lessons. Join the Academy for $19/month to unlock every lesson, quiz, terminal simulation and puzzle, plus the final exam and your certificate of completion.

  • All 40 lessons across 9 units
  • 165+ quiz questions
  • Final exam + certificate
  • New lessons as Claude Code evolves
  • XP, streaks and badges
  • Cancel anytime

Already a member? Log in to continue.

Key takeaways

  • Rules carve precise exceptions on top of modes: allow, ask, deny.
  • Deny beats ask beats allow, in every mode.
  • Mind the space before * in Bash patterns.
  • Sandboxing limits blast radius at the OS level. Use it with rules.

Nice work. Lesson 15 done?

Mark it complete to earn +72 XP, keep your streak alive and unlock badges.

Written by Curtis Cornell from Anthropic's public Claude Code documentation. Last reviewed November 14, 2026. Claude Code changes fast; if something here differs from your version, the official docs win.

Start lesson 1 free →