Claude Code Sandbox

Claude Code sandbox, Cybrec blog cover
Author

Author

Piotr Kaluzny

Head Instructor

Dec 2, 2026

A permission rule in Claude Code matches the text of a command. That leaves a gap: a deny rule on .env stops cat .env, but not a Python script that opens the file by itself, because the command never names the file. The Claude Code sandbox closes that gap. It doesn't read commands at all. The operating system limits what every shell command, and every program it starts, can reach. This post covers what the sandbox enforces, how to turn it on and test it, how it works with your permission rules, and the escape hatch you should close.

What the sandbox enforces

The sandbox wraps the shell commands Claude runs: Bash, PowerShell, and Monitor commands, and every process they start. On macOS it uses the built-in Seatbelt, and on Linux and WSL2 it uses bubblewrap. On native Windows, commands run unsandboxed, so use WSL2 there.

By default, the limits are:

  • Writes: only the working directory, a temporary folder, and directories you've added
  • Reads: most of the machine, including credential files such as ~/.ssh and ~/.aws/credentials, unless you deny them
  • Network: no direct route out; connections go through a proxy that lets through only the hosts you allow, and the list starts empty

The reads default surprises people. The sandbox is mainly about where commands can write and what they can connect to. To keep a file out of reach, you still need a deny rule for it.

Some things run outside the sandbox: Claude's own file and web tools (Read, Edit, Write, WebFetch), hooks, and MCP servers. Those are controlled by permission rules, not by the sandbox.

Turn it on

Run /sandbox in Claude Code. On the Mode tab, choose one of two sandbox modes:

  • Sandbox commands, with auto-allow: commands that run inside the sandbox are approved without a prompt, because the sandbox contains them
  • Sandbox commands, with regular permissions: every command still goes through your normal permission flow, sandboxed or not

Claude Code saves the choice in the project's .claude/settings.local.json. That's also where you add the hosts commands may reach:

{
  "permissions": {
    "deny": ["Read(./.env)"]
  },
  "sandbox": {
    "network": {
      "allowedDomains": ["raw.githubusercontent.com"]
    }
  }
}

When a command tries to reach a host that isn't on the list, what happens depends on the permission mode. In Manual mode you get a prompt to allow it, and in dontAsk mode it's refused. On Linux and WSL2, the sandbox also needs the bubblewrap and socat packages; /sandbox shows a Dependencies tab when they're missing.

Check that it works

Don't assume the sandbox is on. Ask Claude to run two commands that must fail:

touch ~/sandbox-probe
curl --noproxy '*' https://example.com

The first must fail with "Read-only file system" on Linux and WSL2, or "Operation not permitted" on macOS, because your home folder is outside the write limits. The second must fail with "Could not resolve host", because skipping the proxy leaves the command no way out. If the touch succeeds, the sandbox isn't running: check /sandbox and delete the file.

Ask Claude to run them, rather than typing them yourself after !. Commands you type at the ! prompt usually run outside the sandbox, so they don't test it.

It borrows your permission rules

With the sandbox on, Claude Code copies part of your permission rules into it. Read and Edit rules become file limits for commands, and WebFetch domain rules become network limits. Your Read deny on .env, written for Claude's file tools, now also stops every program from opening the file.

This is what closes the gap from the start of the post. In our tests, a Python tool that loads a token from .env on its own printed the token with the sandbox off, even with the deny rule in place. With the sandbox on and the same rule, the same command failed with "Permission denied: '.env'". No new rule was needed.

The borrowing only goes one way. Settings you put in the sandbox itself, such as allowedDomains, never limit Claude's own file and web tools.

Close the escape hatch

When a command fails because of the sandbox, Claude can retry it outside the sandbox. In Manual mode, that retry shows a separate prompt, so you decide. But if an allow rule matches the command, the rule approves the retry too, and the command runs unsandboxed without asking.

We hit this in our own setup. The project allowed Bash(python3 *), so when we asked Claude to run the same Python tool outside the sandbox, it did, with no prompt, and the token came back.

Turn the escape hatch off with strict sandbox mode: in /sandbox, on the Overrides tab, or with "allowUnsandboxedCommands": false in the sandbox settings. Claude Code then ignores requests to leave the sandbox. If a tool genuinely can't work sandboxed, list it in excludedCommands deliberately instead of leaving the door open for everything.

Sandbox auto-allow is not auto mode

The two names are close, but they're separate features. Auto-allow approves commands because the sandbox contains them, and it works in any permission mode, including Manual. Auto mode approves actions because a classifier judged them safe. You can combine them, with one difference to know: in auto mode, a host that isn't on your list is refused, unless the command names the host and the classifier approves it for that one command.

For limits that never change from day to day, pair the sandbox in auto-allow mode with Manual mode and strict sandbox mode. Content-specific ask rules, such as Bash(git push *), still prompt even for sandboxed commands.

Conclusion

The Claude Code sandbox limits what shell commands can write and reach, enforced by the operating system rather than by the text of a command. Turn it on with /sandbox, allow only the hosts your tools need, and prove it works with the touch and curl checks. It borrows your Read, Edit, and WebFetch rules, so a deny on a secrets file finally covers every program. Then close the escape hatch with strict sandbox mode, or a broad allow rule will quietly let commands out. Together with Claude Code permissions, it gives you both halves: what may run, and what it can reach. For how these fit with auto mode and managed settings, see Claude Code security.

Relevant Training

The sandbox, permission rules, auto mode, and managed settings are covered in Claude Code Agent Security, part of Cybrec's Claude AI training program, with hands-on tasks in an AI Lab.

0 Comments

Claude Code sandbox, Cybrec blog cover
Author

Author

Piotr Kaluzny

Head Instructor

Dec 2, 2026