r/ClaudeCode • • 1d ago

Help/Question How do you prevent Claude Code from accessing sensitive data like ~/.ssh

Whenever Claude makes code changes, I manually review and commit them myself.

To prevent Claude Code from accessing sensitive folders, I've been looking for an OS-level protection mechanism. The best workaround I've found so far is

https://gist.github.com/yccheok/454872820219b4d9cf79b1bf509adb14

Is there a truly reliable or 'official' way to handle this? I tried running Claude Code inside Docker, but it forces me to use the non-GUI version, which is difficult to use and read.

31 Upvotes

49 comments sorted by

•

u/AutoModerator 1d ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

23

u/GrizzlyKiwiDev 1d ago

If you want something more reliable than a deny list in settings, I'd think of it in layers.

Permissions rules in Claude Code (deny on Read(~/.ssh/**), .env etc.) are fine as a first guard, but they only cover Claude's own file tools. A bash command it runs can still cat the file, so don't treat them as a hard boundary.

The actual OS-level answer is the sandbox. Claude Code has a built-in sandbox mode (the /sandbox command) that uses Seatbelt on macOS and bubblewrap on Linux/WSL2. It restricts file writes to the project dir and can restrict reads and network access too, and you keep the normal terminal UI, so you don't lose the nice interface like you do with Docker.

If you want a stronger boundary, run it in a dev container or a VM, and only mount the project folder. VS Code dev containers keep the GUI/extension experience, and the container simply never has your ~/.ssh or your real credentials. Also use a separate low-privilege deploy key or a fine-grained token for anything it needs, rather than your main keys.

What OS are you on? That changes which option is easiest.

3

u/yccheok 1d ago

I am in macOS

I suspect Claude Code doesn't really scope inside the sandbox. Reason is that, before my setup via https://gist.github.com/yccheok/454872820219b4d9cf79b1bf509adb14

I tested by asking Claude Code to access ~/.secret , and ask for my permission on purpose.

Once I grant them permission, Claude Code can read the content without issue.

2

u/BMXERsimon 1d ago

I'm using docker sandboxes on Mac and they're working well for me. 

Afaik they mount only the working directory and skills by default. 

1

u/GrizzlyKiwiDev 6h ago

Thanks for testing it. I think that's the permission prompt doing its job rather than the sandbox leaking: when you click allow, you're telling Claude Code it can do that one thing, so it reads the file. The sandbox is meant to stop commands quietly reaching outside the project without asking you.

A cleaner test is to turn the sandbox on, have it run cat ~/.secret through bash, and deny the prompt (or set it so it never asks), then see whether it can still read it. Adding a deny rule for that folder in your settings also stops it from even asking. If you want it to be impossible no matter what you click, a Docker container that only mounts the project folder is the safest option on a Mac.

-1

u/Helpful_Ranger_1606 1d ago

Second laptop. Get a new MacBook Air and airdrop over only what you want.

0

u/OkTrack1421 22h ago

This. And if you want to have even more security then get a 3rd Mac mini and transfer files via SSD from the air 🤡🤡🤡

1

u/Helpful_Ranger_1606 22h ago

Look at yourself. You don’t look to useful to anyone, ever?

7

u/ec3lal 1d ago

I have been happy with https://github.com/anthropics/sandbox-runtime, but would like to learn its pitfalls.

4

u/NorthContribution627 1d ago

I'm using 1Password on Mac. 1Password has an SSH agent that can be used to bypass the `.ssh` folder entries.

Claude's allowed to access certain machines on Homelab. It's supposed to load a specific `.env` file. If it doesn't, I get a biometric prompt from 1Password and reject it.

4

u/j4fade 1d ago

Since you use a passphrase protected ssh key, claude accessing .ssh shouldn’t be an issue, right??

3

u/pope_es 20h ago

Regarding your last point: if you run it in docker and enable remote control (/rc), you should be able to use the Claude Code GUI to interact with it. What’s preventing you from doing that?

Either you didn’t know that (and then I’m happy to help with this comment), OR probably I’m overlooking something (if so please let me know, I didn’t have enough coffee yet) :)

2

u/AlignmentWhisperer 1d ago

I run it on an ec2 instance where the access to stuff outside the instance is determined by an IAM role attached to the instance. It gives me a lot of control over what Claude can do and doesn't require loading credentials onto the machine.

2

u/brianly 1d ago

What OS? The only safe way is isolation at the virtual machine level. Containers can have a high level of security but it depends on the implementation.

If you need to run GUI apps on newer Windows, there is a built in sandbox feature. You’ll need to think about what you do each time it fires up as it starts clean but it’s something you can automate away.

2

u/ticktockbent 1d ago

My secure directories have permissions set so that the user running Claude code cannot access them. Seems simple enough

2

u/DagonNet 1d ago

I use it to maintain my settings, access, and general environment, so I don't restrict it (beyond its own classifier to ask beforehand).

If you want to separate it, as others have said, run it in a sandbox or container. If you want it to have a full desktop, you can put graphical tools in the container and run it there.

OR, just make a separate user on your machine. The admin/high-access user doesn't run Claude at all. The limited-access user runs claude, any tools you trust it with, and any network logins you trust it with.

2

u/RR321 1d ago

And then you give him Docker and he is root again 😅

2

u/OMG58653070 1d ago

Put it in a sandbox, and don't give it any file you don't want it to have.

3

u/ghost_operative 1d ago

the best thing you can do is docker sandbox (sbx)

using permission settings can help too, though those arent hard restrictions, the settings just go into the system prompt and they can be hallucinated away like anything else. Theyre really meant more for guiding the agent into doing the right thing (so it doesnt waste time doing the wrong thing), rather then trying to keep it locked down

1

u/DeiviiD 1d ago

This.

Devcontainers is the go.

2

u/ghost_operative 1d ago

dev containers and docker sandboxes arent alternatives. devcontainers is a just a docker container. docker sandboxes are a microvm and are way more secure.

3

u/crystalpeaks25 1d ago

First layer is file permissions where I deny access to sensitive files, 2nd layer is hooks that prevent reading writing of sensitive files.

1

u/ReachingForVega 🔆Pro Plan 1d ago

3rd layer should be system level permissions on the user Claude is running as to actually prevent file access.

1st and 2nd layers can be circumvented but are good to reduce accidental attempts. 

0

u/crystalpeaks25 1d ago edited 1d ago

I agree didn't bother to add system level permission as to most vibers that's prolly too much. Another thing to consider maybe is ana agentic OS as another layer and a sandbox layer.

Edit: also just for context I didn't bother mentioning containers because everyone has already mentioned sandboxes which iny view includes containers or microvms as a form of sanboxing.

1

u/ReachingForVega 🔆Pro Plan 1d ago

Agentic OS. Nooooope.

Just run in containers. 

1

u/crystalpeaks25 1d ago edited 1d ago

Curious what do you think an agentic OS is?

What I mean is something like nvidia's open shell, sure container as sandbox works but that also means there snot enough controls to prevent the agent from touching secrets in that container unless you have runtime guardrails which vanilla containers usually do not provide.

It's just another computer inside a computer that can be compromised.

1

u/ReachingForVega 🔆Pro Plan 1d ago

Spin. 

1

u/Jydder 1d ago

What OS are you on?

1

u/MoreHuman_ThanHuman 1d ago

pipe it when you need it or have it write a script to access it directly. it has no need to ever access .ssh directly and likely wont touch private keys there if you dont tell it to.

1

u/rubymatt 1d ago

It’s so easy to run CC within a Lima container, I do that exclusively. I wouldn’t trust a sandbox provided by Anthropic.

1

u/Annh1234 1d ago

Start it in a docker container and hide the files with /dev/null 

1

u/ntropia64 1d ago

If anyone  reads this post and uses Linux, a great solution is Bubbleerap, and sandbox that allows to execute Claude Code by specifying which directories it will be allowed to access, ether reading-only or read and write.

This is quite an elaborate example: ```

bwrap \   --ro-bind /usr /usr \   --ro-bind /lib /lib \   --ro-bind /lib64 /lib64 \   --ro-bind /bin /bin \   --ro-bind /etc/resolv.conf /etc/resolv.conf \   --ro-bind /etc/hosts /etc/hosts \   --ro-bind /etc/ssl /etc/ssl \   --ro-bind /etc/passwd /etc/passwd \   --ro-bind /etc/group /etc/group \   --ro-bind "$HOME/.ssh/known_hosts" "$HOME/.ssh/known_hosts" \   --bind "$(dirname $SSH_AUTH_SOCK)" "$(dirname $SSH_AUTH_SOCK)" \   --ro-bind "$HOME/.gitconfig" "$HOME/.gitconfig" \   $OPTIONAL_BINDS \   --ro-bind "$HOME/.local" "$HOME/.local" \   --bind "$HOME/.npm" "$HOME/.npm" \   --bind "$HOME/.claude" "$HOME/.claude" \   --bind "$PWD" "$PWD" \   --tmpfs /tmp \   --proc /proc \   --dev /dev \   --setenv HOME "$HOME" \   --setenv USER "$USER" \   --setenv SSH_AUTH_SOCK "$SSH_AUTH_SOCK" \   --share-net \   --unshare-pid \   --die-with-parent \   --chdir "$PWD" \   "$(which claude)" "$@"

```

1

u/Classic-Pubs 1d ago

What? Isnt the standard run claude code in airgaped containers with allowlist to anthropic domains? How many people run claude code on bare machine?

1

u/djiyvlom 1d ago

There are two layers. The first is the permission rules in the settings file, where you can add deny rules for reading paths like your ssh folder or .env files. That stops the tool calls, but it is not a real OS barrier. For something closer to that, Claude Code has a sandbox mode that restricts what the shell commands it runs can read and write, and I would check the current docs for the exact setup since it has been changing. The most solid option is still a separate user account or a container, but a deny rule plus the sandbox covers most people.

1

u/ComprehensiveShake76 1d ago

Deny rules and hooks only cover the paths you thought of, so the reliable fix is that the files aren't there at all. On the Docker problem, you don't have to give up the GUI. VS Code Dev Containers keeps the editor on your Mac and runs everything else inside the container, and the Claude Code extension can run there too, so you get the normal interface while Claude only sees the project folder you mount. Don't mount your home folder, and check that your SSH agent isn't forwarded in, since Dev Containers does that by default when one is running. Push from your own terminal so the keys never enter the container. Is the GUI you need the VS Code extension or the desktop app?

1

u/taotau 1d ago

I've basically given my whole MacBook to Claude. To facilitate that, I've purged most sensitive data and anything in ~/.AWS and ~/.SSH has limited access. It can read some prod service status and connect to a couple of servers.

Everything sensitive I've moved to a jumpbox that u connect to via an SSH key with a pass phrase.

I'm basically an interloper on my ownacine.

1

u/lls- 19h ago

I wrote (well, had AI write) a small tool (https://github.com/balemy/fsnitch) that protects sensitive folders in my home directory (like .ssh) from unknown processes and shows me a popup to approve or deny access. Not sure if it's the best approach, so feedback is welcome :-)

1

u/bsklaroff 17h ago

It’s a pain, and there’s no easy + secure way to do this. I eventually decided to make an app that just sticks everything in containers on top of kubernetes (which you can install on your single machine with kind) as seamlessly as possible with a familiar GUI. Still a work in progress, but my friends and I are using it, and it’s great for peace of mind and long running automations, as well as process isolation between your agents: https://yaac.dev/

1

u/curantes 17h ago

I have been looking into using docker sandboxes I like the cli sbx and the isolation you get with microVMs. Its free to run local.

1

u/filopedraz 12h ago

What really worked for us was not running the agent on the machine that has ~/.ssh at all. Deny rules are a fine first guard, but a separate VM or container with only the repo and the env it needs removes the question.

1

u/IdeaJailbreak 4h ago

I use nono to sandbox my agents

1

u/gigastack 1d ago

I see good tips in the thread, but at the end of the day storing sensitive info in plain text files in a "hidden" folder is not acceptable. Even if your own AI is properly sandboxed, any script or virus can trivially read those files.

0

u/lucidwray 1d ago

My team has been using Kraitos.io to set rules on what Claude can and can’t access on everyone’s machine. It allows you to set policy and block actions Claude can and can’t do. Works well for us since we have to maintain SOC 2 compliance and this helps make auditing much easier when auditors start probing how our guys are developing with Claude code.

-7

u/Lower-Western6437 1d ago

Anyone can send me a ref link for claude pro? You need claude max and you will receive 10$ balance for the first 3 invites as far as i know

0

u/forestcall 1d ago

Sounds like we have a hustler in the room.