Don't let Claude Code read your secrets: why you need to set up sandboxing today
Claude Code reads your files, runs commands, and edits your code. By default, nothing stops a hacked package or a booby-trapped README from using that against you. Here are the four things to do about it, what each one protects you from, and what it costs.
It was a usual day of watching Claude write code for me. Then the lines appearing on the terminal suddenly put me in panic mode. Claude Code was fiddling with production APIs without my approval. I had only asked for help with a failing API integration. It found credentials in my environment, used them to call the live endpoints, and pasted the responses into our chat. Helpful? Sure. But now those credentials sit in my Claude conversation history. Forever.
Updated November 2026. A lot has changed since April. Auto mode is now the default, and the sandbox is much easier to live with. Updated article with latest security advice.
The short version
Here are the four things you should do now to make sure you are safe, what each one saves you from, and what it costs you. The rest of the article explains why.
Do this | If you don't | What it costs you |
|---|---|---|
1. Stay in auto mode, not yolo mode | Nothing checks Claude's own risky actions | Nothing. It's the default |
2. Turn on the sandbox | A hacked package can read your SSH and AWS keys and send them anywhere | Some commands fail until you allow a domain |
3. Block your secret folders | Even with the sandbox on, commands can still read | Almost nothing |
4. Keep production keys off your laptop | Anything Claude reads ends up in your chat history, like mine did | Setting up separate dev keys |
Steps 2 and 3 are one config. Paste it into ~/.claude/settings.json and it covers every project:
// ~/.claude/settings.json
{
"permissions": {
"deny": [
"Read(**/.env*)",
"Read(~/.ssh/**)",
"Read(~/.aws/**)"
]
},
"sandbox": {
"enabled": true,
"excludedCommands": ["git push *", "git pull *", "git fetch *"],
"filesystem": {
"denyRead": ["~/.ssh", "~/.aws", "~/.gnupg", "~/.config/gcloud", "~/.kube"]
}
}
}To check it worked, type /sandbox in Claude Code and open the configuration tab.
What can actually go wrong
There are three different ways this goes wrong, and each needs a different fix.
Claude does something dumb. It force-pushes, deploys, or runs a script from the internet. Auto mode is built for this. That's step 1.
Someone else's code turns on you. A package you install was hacked, or a README Claude reads tries to steer it. Auto mode mostly misses this, because it judges the command, not what the code behind it does. The sandbox catches it. That's steps 2 and 3.
Claude reads a secret. Once Claude reads a file, its contents are in your chat. That's what happened to me, and no setting fully fixes it. That's step 4.
Step 1: Stay in auto mode
Instead of asking you about every action, a second AI model checks each one before it runs. It's the default in the terminal now. The docs say yolo mode (--dangerously-skip-permissions) is only for containers and VMs. If you used yolo mode to get away from the prompts like I did, auto mode is the better deal.
Reads and edits in your project
âš That includes reading your .env
Everything else
Usually lets through
✓ installing packages
âš calling an API with your keys
Blocks
✗ curl | bash, force push
✗ sending your data elsewhere
If you don't: in yolo mode, nothing checks what Claude does. Only the sandbox and your deny rules are left.
What it costs: a prompt now and then. If auto mode blocks something 3 times in a row, or 20 times in a session, it goes back to asking you.
But know its limits. By default, auto mode allows "reading .env and sending credentials to their matching API". It cares about where your secrets go, not whether Claude sees them. And it has two blind spots:
Poisoned packages. Installing packages from your lock file is allowed. If one of them was hacked, like axios was, its install script runs, and auto mode never sees what that script does. All it saw was
npm install.Edit, then run. Edits inside your project run without any check. Say a booby-trapped README tricks Claude into slipping a line into a test file. The next
npm testlooks harmless, but it runs that line.
Auto mode makes a judgment call. It's not a wall. For a wall, you need the sandbox.
Step 2: Turn on the sandbox
The sandbox limits which files and websites every Bash command can reach, including any programs those commands start. Your operating system enforces it: Seatbelt on MacOS, bubblewrap on Linux and WSL2. So it holds even when Claude gets tricked. Native Windows has no sandbox, so use WSL2 there.
Start with the smallest circle. The bigger the circle, the less a sandboxed command can do. Claude's own Read, Edit and WebFetch tools aren't in the onion. They follow your permission rules.
Writes are locked down well. A bad install script can't add a git hook, edit your ~/.zshrc, or change Claude's own settings. And it can only connect to domains you've allowed.
In the screenshot above, I ask Claude Code to run composer update (PHP's package manager). Yolo mode is on. Without the sandbox, Claude would just run it. With the sandbox on, Claude first asks me to allow the domain.
If you don't: every command Claude runs can read your whole machine and connect to any server. If a hacked package runs its install script, nothing stops it from reading your SSH keys and sending them anywhere. To be fair, the same is true when you run npm install yourself. Claude just does it far more often.
What it costs: some commands fail the first time. When a command needs a new domain, you get asked to allow it. In auto mode, the second AI handles most of those requests for you. Keeping that list up to date by hand is why I used to turn the sandbox off, and it's mostly gone now. For the rest, see the fixes under When something breaks below.
Step 3: Block your secret folders
The sandbox has one big gap: by default, commands can still read almost your whole machine, including ~/.ssh and ~/.aws/credentials. You have to block those yourself. The config does it with two lists, because they cover different things:
`denyRead` stops Bash commands from reading your secret folders. Add any others you have.
`permissions.deny` stops Claude's own Read tool, which the sandbox doesn't cover, from opening them and your
.envfiles. Deny rules work in every mode, even yolo mode.
If you don't: commands can read your keys, even if they can't send them far. And Claude can read them straight into your chat.
What it costs: almost nothing. The one catch is git over SSH, which needs ~/.ssh. That's why the config runs git push, git pull and git fetch outside the sandbox. Other commands still can't edit .git/hooks or .git/config, so nothing can sneak into what runs when you push.
Step 4: Keep production keys off your laptop
There's one thing no setting can fix: your project's .env. The sandbox gives commands full access to your project folder. The deny rule stops Claude's Read tool, but a grep or a Python script can still read the file. And you can't block it, because your app needs it every time Claude runs your tests. The same goes for secrets you export in your .zshrc: every command Claude runs gets a copy.
So the fix is boring. Use separate dev keys with as few permissions as possible. Then whatever Claude sees can't hurt anything that matters.
If you don't: sooner or later, Claude reads a real key, and it stays in your chat history.
What it costs: setting up dev keys once. This alone would have saved me from the mess at the top of this post.
When something breaks
Most problems have an official fix now:
Dev servers on MacOS: add
"network": { "allowLocalBinding": true }to thesandboxblock so they can use localhost. On Linux, exclude the command instead.Docker: add the exact commands you need to
excludedCommands, likedocker compose *. Never all ofdocker. An excluded command runs with no sandbox at all, and Docker can mount any folder on your machine.TLS errors on MacOS: if
gh,gcloudorterraformfail, setsandbox.enableWeakerNetworkIsolationto true, or exclude the tool.Browser logins on MacOS: if
openorosascriptfail, setsandbox.allowAppleEventsto true in your user settings, or excludeopen *.jest: run it with
--no-watchman.Database clients: most database drivers skip the proxy, so they can't connect anywhere. Exclude them or run them yourself.
Keep both lists short: excluded commands and allowed domains. The sandbox only checks the domain name, and a broad entry like github.com lets traffic reach a site where anyone can create a repo or a gist. That's a way out for your data.
Optional extras
The four steps are enough for most people. These are for when you want more.
Close the escape hatch. When a command fails inside the sandbox, Claude can retry it outside, and in auto mode the second AI decides whether that's OK. So the sandbox is a wall with a door. Add "allowUnsandboxedCommands": false to the sandbox block to remove the door. Worth it if you want a real wall and can live with more commands just failing.
Let Claude use a token without seeing it. In mask mode, commands only see a fake placeholder, and the sandbox proxy swaps in the real value on requests to hosts you list. Worth it if Claude often needs a token like GH_TOKEN. The catch: it needs tlsTerminate, which lets the proxy open all your HTTPS traffic.
// ~/.claude/settings.json
{
"sandbox": {
"network": {
"tlsTerminate": {},
"allowedDomains": ["api.github.com"]
},
"credentials": {
"envVars": [
{ "name": "GH_TOKEN", "mode": "mask", "injectHosts": ["api.github.com"] }
]
}
}
}Claude Code only reads mask settings from your user settings, never from a repo's .claude/settings.json. Otherwise a repo you cloned could send your tokens to its own server.
Strip secrets from the environment. Set CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1, and Claude Code removes anything that looks like a credential from every command it runs. Worth it if you can't stop exporting secrets in your shell. The catch: it leaves GitHub tokens alone, and it can break tools that need those variables, like a dev server that reads DATABASE_URL.
Go all the way. Run Claude in a dev container, so it can't see your machine at all. Worth it if you work with anything sensitive. That's also the only place where yolo mode belongs.
The bigger picture
Think of the sandbox as basic hygiene. Knowing what I know now, I'd never run Claude Code on my own machine without it. Just recently, axios got poisoned, a package with millions of installs. No one saw that coming. We should all be ready for the next one.
Stay safe, and enjoy coding with LLMs! 🎉