How Claude Code worksPart 5
Permissions and safety
On this page
In short: A new colleague does not get every key on day one. Some doors open with a key (allow), some need a knock so you can say yes or no (ask), and some stay locked for everyone (deny). The mode decides what happens when no rule matches. The sandbox is the walls of the room: it limits what any command can reach, whatever the command says.
Part 4 connected your colleague to other departments. This part decides what they may touch once they are there. The project in the examples, the Acme storefront, is made up.
Keys, knocks and locked doors
Think of it like this: On the first day you give a new colleague the key to the workshop. They knock before going into the client’s office. The safe stays locked, and nobody hands out that key.
Claude Code works the same way. Before Claude takes an action, such as running a command, editing a file or calling a tool, it checks three lists in your settings:
- deny: the locked doors. The action is refused.
- ask: knock first. You see the action and say yes or no.
- allow: a key. The action runs without a question.
The order matters, and it is always the same: deny first, then ask, then allow. A locked door wins over any key, however specific the key is.
Example: With Bash(git *) in allow and Bash(git push *) in ask, git status runs at once but git push still knocks. The ask rule is checked before the allow rule.
Under the hood: how rules are written
- A rule is a tool name, with an optional pattern in brackets:
Bash(npm test *),Read(./.env),Edit(./src/**),WebFetch(domain:docs.example.com). - MCP tools are named
mcp__<server>__<tool>, as in Part 4. - A bare filename such as
Read(.env)matches that file at any depth under the project. - Read and Edit deny rules cover Claude’s own file tools and the file commands it recognises, like
catandsed. They do not cover a script that opens files by itself. For that, use the sandbox. - Simple read-only commands like
ls,cat,grepandgit diffnever prompt. Add an ask or deny rule if you want one of them to. - Rules can live in user, project, local and managed settings. A deny at any level cannot be undone by an allow at another level. Managed settings, set by an organisation, sit above everything.
Modes: how careful the whole visit is
Think of it like this: Rules are the keys. The mode is the house rule for doors that have no key yet: ask me, look but do not touch, or go ahead.
Press Shift+Tab to move between modes, or set defaultMode in your settings so every session starts the same way.
Asks the first time for each kind of action. Every door needs your nod until you hand over a key.
Looks around and reads, but does not touch your files. A walk-through before the work starts.
Edits files and makes folders in the project without asking. Keys to the workshop, not to the front door.
No routine prompts. A second checker reviews risky actions against what you asked for.
Anything that would prompt is refused. Only pre-approved keys work. Good for unattended runs.
Skips prompts. Only inside a throwaway container or VM, never on your laptop.
Important: Bypass mode skips the prompts, including for writes to .git and .claude. Use it only in a container or VM that holds nothing you would miss.
Three layers that work together
Rules, hooks and the sandbox each catch something the others miss. Use them together.
Your allow, ask and deny lists. They match the name of an action, such as Bash(git push *).
A script that looks at each action before it runs and can block it with your own logic (Part 3).
The operating system limits which folders and websites commands can reach, whatever the command says.
Example: A deny rule Read(./.env) stops Claude from opening the file. But if Claude runs node print-config.js and that script reads .env itself, the rule never sees it. The sandbox does: the script cannot reach paths outside what the sandbox allows.
Under the hood: the sandbox and hooks
- Run
/sandboxto turn it on. It applies to shell commands and every process they start, and limits both the filesystem and the network. - With the sandbox on, sandboxed commands can run without a prompt, because the walls do the job the prompt was doing.
- Paths and domains from your permission rules are merged into the sandbox settings, so
WebFetch(domain:...)rules can widen or narrow where commands may connect. - A PreToolUse hook (Part 3) runs before the permission check and can block with exit code 2. It cannot open a door a deny rule has locked.
- The sandbox matters most for prompt injection: even if hidden instructions fool Claude, the walls still hold.
When to give a key, and when to lock the door
- The action is routine and reversible: running tests, the linter, the build.
- You have approved it ten times this week and never said no.
- It only reads, like
git diffor a search.
- It touches secrets:
.env, keys, credentials. - It cannot be undone: deleting data, force-pushing, dropping a table.
- It publishes: pushing, deploying, posting, sending email. Make those ask.
The Acme storefront, step by step
Here is one project’s settings built up in order. All of it lives in .claude/settings.json, so the whole team gets the same keys.
- 1 · Start in plan mode for the Acme storefront
A new colleague walks the building before they get keys. Plan mode reads and proposes; nothing changes.
"permissions": { "defaultMode": "plan" } - 2 · Lock the secret doors
These stay shut for everyone, however a later rule is written.
"deny": [ "Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)", "Bash(rm -rf *)" ]
- 3 · Knock before anything leaves the building
Pushing and deploying always ask, even after a key for
gitis handed out."ask": [ "Bash(git push *)", "Bash(npm run deploy *)", "mcp__tracker__update_issue" ]
- 4 · Hand out keys for the routine work
The commands you approve all day stop interrupting you.
"allow": [ "Bash(npm test *)", "Bash(npm run lint)", "Bash(npm run build)", "Bash(git diff *)" ]
- 5 · Add walls for shell commands
Rules match what a command says. A script can still open files by itself, so the sandbox limits what any command can reach.
/sandbox
- 6 · Commit it, keep your own keys local
The team shares
.claude/settings.jsonin git. Personal extras go in.claude/settings.local.json, which stays out of git.
Where this fits in the bigger picture
Think of it like this: A trusted colleague is trusted because the boundaries are clear, not because there are none.
This series is building a setup that works the way you work, on any project. Safety is what lets you hand more over. When the dangerous doors are locked and the publishing doors knock, you can give keys for everything else and stop babysitting. From Part 7 the written spec decides what gets built; these rules decide what Claude may do while building it.
Which one fits?
- I keep approving the same safe command: Give it a keyAdd it to
allow, for exampleBash(npm test *). Or choose “Yes, and don't ask again” at the prompt. - Claude must never read a file: A deny rule, plus the sandbox
Read(./.env)indenyblocks Claude's file tools and the file commands it recognises. The sandbox also blocks scripts that open the file themselves. - It is fine, but I want to see it first: An ask rulePut it in
ask. An ask rule wins over a broader allow rule, sogit pushstill knocks aftergithas a key. - I am starting on a codebase I do not know: Plan modeShift+Tab until it says Plan, or set
defaultMode. Claude reads and proposes, you approve, then it works. - The rule depends on what the command does: A PreToolUse hookA script that reads the full command and blocks it with exit code 2. Deny rules still apply on top.
- I want to skip every prompt: Only in a throwaway boxBypass mode belongs in a container or VM with nothing to lose. On your laptop, use keys for routine work instead.
Your setup so far
- Part 1 · where it worksYou know which machine does what, and which folders Claude can reach.
- Part 2 · what it remembersOne task per notebook; photocopies in their own folder; helpers for noisy side jobs.
- Part 3 · how it behavesFridge rules for what is always true, recipe cards for procedures, alarms for musts, specialists for checks.
- Part 4 · what it can reachPhone lines to your tracker and tools, checked before trusted; your setup packed as one toolbox.
- Part 5 · what it may doKeys for routine work, a knock for anything that publishes, locked doors for secrets.
- NextPart 6 · context and cost
Three rules of thumb
Write the deny list before the allow list. A locked door holds whatever keys come later.
Pushes, deploys, messages and tracker writes ask. Everything that stays on your desk can have a key.
Rules read the command text. The sandbox limits what any command can actually reach.
QuizYour settings have Bash(git *) in allow and Bash(git push *) in ask. Claude wants to run git push origin main. What happens?
Show the answer
Answer: Claude Code asks you first
Rules are checked deny, then ask, then allow. The ask rule matches before the allow rule is reached, so you get a knock on the door.
Words in this post:
- Permission rule: a line in settings that says an action is allowed, needs asking, or is denied.
- Mode: the default behaviour for actions no rule covers. Switch with Shift+Tab.
- Plan mode: Claude reads and proposes, but does not change your files.
- Sandbox: limits from the operating system on what commands can read, write and connect to.
- Prompt injection: hidden instructions in content Claude reads, trying to steer it.
Facts here were checked against the Claude Code documentation on permissions and sandboxing in November 2026. These features change often, so the docs are the final word for your version.
Next, Part 6: Context and cost is about keeping your colleague’s desk clear, so it stays sharp and does not waste effort.
Setting up Claude Code or AI agents for your team?
I help teams build this: agents, RAG search and Claude Code setups that hold up in production.
Series
How Claude Code works
- How Claude Code worksWhere Claude runs
- How Claude Code worksSessions and forks
- How Claude Code worksSkills, hooks and subagents
- How Claude Code worksMCP servers and plugins
- How Claude Code worksPermissions and safetyyou are here
- How Claude Code worksContext and cost
- How Claude Code worksSpecs as the source of truth
- How Claude Code worksContract first
- How Claude Code worksVerify against the spec
- How Claude Code worksHandoffs and memory
- How Claude Code worksYour setup as a kit
- How Claude Code worksRolling it out