What you will be able to do
- Write permission rules for an agent starting from deny, and spot patterns that match more than intended
- Explain why command-pattern rules alone cannot contain a determined command, and where the sandbox takes over
- Configure sandbox filesystem and network boundaries and know which processes and tools they do not cover
- Keep credential files and secret environment variables out of an agent's reach
1.Least privilege: writing rules from deny
Every guardrail on the prompt side can eventually be beaten by a clever enough input. Least privilege controls the damage when that happens: an agent holding only the tools, paths and credentials its task needs leaves an injected instruction very little to work with. The same principle applies in reverse to the agent's own mistakes.
In Claude Code this is written as permission rules. Allow rules run a tool without asking, ask rules prompt every time, and deny rules block the tool outright (Permission rules: allow, ask and deny). For least privilege, start from deny and open only what the job needs. A build agent needs its build and commit commands, never a push to the remote, and a team that uses no MCP servers can block every one of them with a single mcp__* deny entry.
{
"permissions": {
"allow": [
"Bash(npm run *)",
"Bash(git commit *)"
],
"deny": [
"Bash(git push *)"
]
}
}Wildcards widen a rule further than they appear to. The * stands for any text in its position, so Bash(git * main) matches every git subcommand, including git -c core.fsmonitor=<script> diff main, and -c makes git run a program you name. A single space changes the match: Bash(ls *) does not match lsof, but Bash(ls*) does. When you write an allow rule, check what else the wildcard admits.
Checkpoint 1 of 6· Fill the gap
This build agent should be able to build and commit, but must never push to the remote. Which key completes the rule set?
{
"permissions": {
"allow": [
"Bash(npm run *)",
"Bash(git commit *)"
],
" ? ": [
"Bash(git push *)"
]
}
}Least privilege starts from deny. A deny rule blocks the tool outright. An ask rule would still let the push run once someone approves the prompt.
Source: code.claude.comCheckpoint 2 of 6· Check yourself
To let an agent run git commands against the main branch without prompting, an admin adds the allow rule Bash(git * main). What does this rule actually admit?
The * stands for any text in its position, so it matches every git subcommand and option. That includes -c, which can make git run an arbitrary program. Check what else a wildcard admits before you allow it.
“In Bash(git * main), it stands in for the subcommand, so Claude Code matches every git subcommand and every option before it.”Source: code.claude.com
2.Where command-pattern rules stop protecting
| Rule | Stops | Doesn't stop |
|---|---|---|
| Bash(curl *) | curl https://example.com | /usr/bin/curl https://example.com, sh -c 'curl https://example.com' |
| Bash(rm *) | rm -rf build/ | /bin/rm -rf build/, bash -c 'rm -rf build/' |
| Bash(git push *) | git push origin main | git -C . push origin main, git 'push' origin main |
Pattern rules are a good way to express intent and a weak way to contain a command that is trying to get out. Treat them as the first layer. The sandbox in the next section enforces filesystem and network limits on every sandboxed command however it is spelled, and explicit deny rules are still respected inside it. Behind both preventive layers, audit logging is the detective control: it records what the agent actually did, so a bypass that gets through is found and the rule tightened (Audit events and their routing).
Checkpoint 3 of 6· Check yourself
A team relies on a single deny rule, Bash(git push *), to keep its agent from pushing to the remote. Which of these commands still runs, because the rule never matches it?
Bash rules match the literal command text. git -C . push starts with different text, so the pattern does not catch it. The two git push commands do match, because * stands for any text. This is why pattern rules are only the first layer, with the sandbox and audit logging underneath.
“Bash(git push *) | git push origin main | git -C . push origin main”Source: code.claude.com
3.Sandboxing the filesystem and network
Claude Code's built-in sandbox restricts which files and network hosts its shell commands can reach. It uses Seatbelt on macOS and bubblewrap on Linux and WSL2. The defaults are tight for writes and network and loose for reads and environment variables, which tells you where to configure.
| Access | Default | Change it with |
|---|---|---|
| Writes | Working directory, a per-user temp directory and added directories | filesystem.allowWrite, filesystem.denyWrite |
| Reads | Most of the machine, including ~/.ssh and ~/.aws/credentials | filesystem.denyRead, credentials |
| Network | Only through a proxy that checks allowed domains, which start empty | network.allowedDomains, network.deniedDomains |
| Environment variables | Inherited from Claude Code, including secrets | credentials, CLAUDE_CODE_SUBPROCESS_ENV_SCRUB |
Read access is where least privilege usually has to be added by hand. Denying the home directory and allowing only the project directory gives the agent the code it needs and nothing beside it. Deny and allow combine predictably: a narrower allow re-opens part of a denied region, while a deny inside a wider allow holds. allowRead: ["~/"] with denyRead: ["~/**/.env"] keeps every .env blocked.
{
"sandbox": {
"enabled": true,
"filesystem": {
"denyRead": ["~/"],
"allowRead": ["."]
}
}
}Know where the boundary ends. The built-in Read, Edit, Write, WebFetch and WebSearch tools follow permission rules, not sandbox settings. Command hooks, local MCP servers and helper commands run with your full access. Commands that match excludedCommands run unsandboxed, and Claude can ask to retry a failed command outside the sandbox. A matching allow rule such as Bash(curl *) silently approves that retry. To keep retries visible, add an ask rule for Bash(dangerouslyDisableSandbox:true), or set allowUnsandboxedCommands to false. Repositories cannot weaken the boundary from inside either: project settings cannot switch filesystem isolation off.
Checkpoint 4 of 6· Fill the gap
Sandboxed Bash commands should read the project's code but nothing else in the home directory, including ~/.ssh. Which key completes the sandbox settings?
{
"sandbox": {
"enabled": true,
"filesystem": {
" ? ": ["~/"],
"allowRead": ["."]
}
}
}By default, reads reach most of the machine, so read restrictions have to be added by hand. A denyRead on the home directory, plus a narrower allowRead for the working directory, opens the project again and keeps the rest blocked. denyWrite would only limit writes, and those are already tight by default.
Source: code.claude.comCheckpoint 5 of 6· Check yourself
An admin enables the sandbox and adds ~/.aws/credentials to filesystem.denyRead. Claude then calls its built-in Read tool on that file. What happens?
Sandbox settings limit only the shell commands Claude runs through Bash. Built-in Read, Edit, Write, WebFetch and WebSearch follow permission rules, so blocking them takes a separate deny rule.
“A denyRead entry doesn't stop the Read tool, and allowedDomains doesn't limit WebFetch”Source: code.claude.com
Sources3
4.Keeping secrets out of the agent's reach
A secret the agent can read is a secret an injected instruction can try to send somewhere. By default, sandboxed commands can still read credential files such as ~/.ssh, and they inherit every environment variable Claude Code has. The credentials block closes both routes: files entries block credential paths, and envVars entries keep named tokens out of command environments.
{
"sandbox": {
"enabled": true,
"credentials": {
"files": [
{ "path": "~/.aws/credentials", "mode": "deny" },
{ "path": "~/.ssh", "mode": "deny" }
],
"envVars": [
{ "name": "GITHUB_TOKEN", "mode": "deny" },
{ "name": "NPM_TOKEN", "mode": "deny" }
]
}
}
}Where the configuration lives decides whether it applies. Claude Code ignores credentials entries in project or local settings, so a cloned repository cannot rewrite them. Put them in user or managed settings. When managed settings configure sandbox.filesystem or a credentials file deny, only managed settings can change that key, which keeps an administrator's restrictions in force on every machine.
The layers fail independently, and that matters when someone turns filesystem isolation off. File deny blocks stop being enforced, because the filesystem layer applies them. Environment-variable deny and mask entries keep working, because scrubbing does not depend on that layer. Setting CLAUDE_CODE_SUBPROCESS_ENV_SCRUB keeps filesystem isolation on regardless of any filesystem.disabled setting, including one in managed settings.
Checkpoint 6 of 6· Fill the gap
GITHUB_TOKEN and NPM_TOKEN are set in Claude Code's environment, and sandboxed commands must not inherit them. Which key completes this credentials block?
{
"sandbox": {
"enabled": true,
"credentials": {
"files": [
{ "path": "~/.aws/credentials", "mode": "deny" },
{ "path": "~/.ssh", "mode": "deny" }
],
" ? ": [
{ "name": "GITHUB_TOKEN", "mode": "deny" },
{ "name": "NPM_TOKEN", "mode": "deny" }
]
}
}
}Sandboxed commands inherit every environment variable by default, so a token needs an envVars deny entry to stay out of their environment. A files entry or denyRead only blocks paths on disk. Env-var scrubbing also keeps working when filesystem isolation is turned off.
Source: code.claude.comSources3
Practise it for real
Turn on the Claude Code sandbox with no unsandboxed fallback and confirm that it blocks writes outside the project and direct network access.
1.Start Claude Code with claude --settings '{"sandbox": {"enabled": true, "allowUnsandboxedCommands": false}}'. On Linux or WSL2, install bubblewrap and socat first.
Why: allowUnsandboxedCommands set to false removes the route where a failed command is retried outside the sandbox.
You should see: The session starts; /sandbox shows the resolved sandbox settings under Config.
2.Ask Claude to run touch ~/sandbox-probe.
Why: Your home directory is outside the default write boundary.
You should see: The command fails with Operation not permitted on macOS, or Read-only file system on Linux and WSL2.
3.Ask Claude to run curl --noproxy '*' https://example.com.
Why: Sandboxed commands have no direct route out; traffic must go through the proxy that checks allowed domains.
You should see: The command fails with Could not resolve host.
4.Add the credentials block that denies ~/.aws/credentials, ~/.ssh, GITHUB_TOKEN and NPM_TOKEN to your user settings, not the project's .claude/settings.json.
Why: Claude Code applies no credentials entries from project or local settings.
You should see: Sandboxed commands can no longer read those files or see those variables.
Stuck? Get a nudge
If a command you rely on breaks, add the narrowest allowWrite path or allowedDomains entry it needs rather than excluding it from the sandbox.
Sources
(3)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://platform.claude.com/docs/en/test-and-evaluate/strengthen-guardrails/mitigate-jailbreaksOfficial docs
“Apply the principle of least privilege so that a successful injection can do minimal damage”
↩︎ Least privilege: writing rules from deny - 2.https://code.claude.com/docs/en/permissionsOfficial docs
“Deny rules prevent Claude Code from using the specified tool.”
↩︎ Least privilege: writing rules from deny“That includes -c, which makes git run a program you name.”
↩︎ Least privilege: writing rules from deny“Bash(ls*) has no space, so it matches lsof too.”
↩︎ Least privilege: writing rules from deny“git -C . push origin main, git -c push.default=current push origin main, git 'push' origin main”
↩︎ Where command-pattern rules stop protecting“/bin/rm -rf build/, bash -c 'rm -rf build/'”
↩︎ Exam trap 1“In Bash(git * main), it stands in for the subcommand, so Claude Code matches every git subcommand and every option before it.”
↩︎ Checkpoint“The value is compared against the literal input Claude sends, before any normalization.”
↩︎ Prediction“Bash(git push *) | git push origin main | git -C . push origin main”
↩︎ Checkpoint - 3.https://code.claude.com/docs/en/sandboxingOfficial docs
“Explicit deny rules are always respected”
↩︎ Where command-pattern rules stop protecting“Connections go through a proxy on your machine that checks each host against your allowed domains, which start empty.”
↩︎ Sandboxing the filesystem and network“a broad allow can’t silently re-expose a secret”
↩︎ Sandboxing the filesystem and network“helper commands such as your status line command and apiKeyHelper run with your full access”
↩︎ Sandboxing the filesystem and network“it also approves the retry, so the command runs outside the sandbox with no prompt”
↩︎ Sandboxing the filesystem and network“so a checked-out project can’t switch filesystem isolation off.”
↩︎ Sandboxing the filesystem and network“Inherited from Claude Code, including any secrets in its environment”
↩︎ Keeping secrets out of the agent's reach“Project or local settings: Claude Code applies none of their credentials entries.”
↩︎ Keeping secrets out of the agent's reach“Environment variable scrubbing is independent of the filesystem layer”
↩︎ Keeping secrets out of the agent's reach“A denyRead entry doesn’t stop the Read tool, and allowedDomains doesn’t limit WebFetch”
↩︎ Exam trap 2“A denyRead entry doesn't stop the Read tool, and allowedDomains doesn't limit WebFetch”
↩︎ Checkpoint