01The short version
A coding agent is a program in which an AI model reads files, runs commands and changes code. On a Mac, the agent starts with the rights of your user account. Thus it can read each file and use each key that your account can reach. A key is a secret text that opens a service.
This guide puts limits in macOS, in settings files and in separate accounts, where the agent cannot change them.
If this agent obeys an attacker at this moment, what can it read, what can it change, and where can it send data?
The central assumption is: assume a breach. Plan as if the agent already obeys an attacker. A web page, a README file or a package can contain a sentence that the agent obeys as an instruction. The name of this attack is prompt injection. Plan also as if the computer already runs malicious software. Then make sure that the damage stays small.
- Prepare the computer. Turn on updates, disk encryption, the screen lock and the firewall.
- Separate agent work from personal data. Give the agent its own macOS account and one projects folder.
- Turn on the sandbox. A sandbox is an enclosed work area that limits the files and the websites of the agent.
- Use a stronger boundary for unattended work. Use a virtual machine (a second computer that runs as software) or a cloud session.
- Protect keys and secrets. Keep them in a password manager and on a hardware security key, a small device that holds a secret.
- Control what the agent installs and loads. Wait before you install new package versions.
- Send the settings from one place. A team uses settings that a developer cannot override.
02Prepare the computer
| Setting | Menu path in System Settings | What to do |
|---|---|---|
| Updates | General > Software Update | Click the Info button next to Automatic Updates. Turn on "Install macOS updates" and "Install Security Responses and system files". |
| Disk encryption | Privacy & Security > FileVault | Turn on FileVault. Store the recovery key in the password manager, not on this Mac. |
| Screen lock | Lock Screen | Set "Require password after screen saver begins or display is turned off" to the shortest time in the list. |
| Firewall | Network > Firewall | Turn on Firewall. Click Options and turn on "Enable stealth mode". |
| Sharing services | General > Sharing | Turn off each service that you do not use, such as Remote Login, Screen Sharing and File Sharing. |
The paths are from the Mac User Guide updates; FileVault; lock screen; firewall; sharing.
A Mac with Apple silicon encrypts its data automatically. FileVault, the disk encryption of macOS, adds one condition: nobody can decrypt the data without your login password Apple. Thus a thief cannot read the files of a Mac that is off.
The firewall blocks connections that come in from the network. It does not limit the connections that the agent opens. Remote Login lets a different computer open a command line on your Mac with SSH (Secure Shell).
An administrator can add users, install applications and change settings. A standard user can change only its own settings Apple. Only an administrator can use sudo, the command that runs a program with system rights Apple. Thus an agent in a standard account cannot change system settings.
To add an account, select System Settings > Users & Groups and click Add User. Keep one account of the type Administrator for administration only. Do the daily work in an account of the type Standard.
03Separate agent work from personal data
The agent can reach all that its account holds, such as your mail, your photos and your browser sessions. A separate computer or a virtual machine for agent work gives the strongest separation. A separate macOS user account is the practical minimum. It has its own home folder, its own browser data and its own keychain, the macOS store for passwords.
In Users & Groups, create an account of the type Standard for agent work. In this account, open Terminal and try to list your personal home folder.
ls /Users/yourname
The answer must be "Permission denied". If the command shows files, run the command below in your personal account. It lets only your account open your home folder.
chmod 700 ~
Make one projects folder in the agent account, for example ~/agent-work. Clone into it only the repositories that the agent works on. Start the agent in the folder of one project. Do not start it in the home folder or in Downloads. With the sandbox on, the start folder is the area that commands can write to. Agents also read instruction files from it. Codex reads each AGENTS.md file from the project root down to the current folder Codex. A reported attack took control of coding agents with an AGENTS.md file in the Downloads folder BleepingComputer.
Software that controls a browser profile can act as you on each website where you are signed in. If you do not use a separate account, use a separate browser profile for agent work. Sign in only to the accounts that the work needs. In Chrome, select Profile and then "Add Chrome profile" Google.
04Turn on the sandbox
The sandbox is a boundary that the operating system puts around the commands of the agent. On macOS, Claude Code and Codex use Seatbelt, the sandbox framework that is part of macOS. You do not install more software Claude Code; Codex.
In Claude Code, the sandbox is off by default. When it is on, a command can write only to the start folder and to a temporary folder. It can read most of the Mac, including key files such as ~/.ssh, unless a rule blocks the path. It has no direct connection to the network. A proxy (a program between the command and the network) compares each host with a list of permitted domains. The list starts empty Claude Code.
Put the content below in your user settings file, ~/.claude/settings.json Claude Code.
{
"sandbox": {
"enabled": true,
"failIfUnavailable": true,
"allowUnsandboxedCommands": false,
"network": { "allowedDomains": ["github.com", "registry.npmjs.org"] },
"filesystem": { "denyRead": ["~/.ssh", "~/.aws"] }
},
"permissions": {
"disableBypassPermissionsMode": "disable",
"deny": ["Read(~/.ssh/**)", "Read(~/.aws/**)", "Read(./.env)", "Read(./.env.*)"]
}
}
The first three lines turn the sandbox on, stop Claude Code if the sandbox cannot start, and block a second try outside the sandbox. allowedDomains lists the hosts that commands can reach. denyRead blocks commands from the folders that hold keys. The deny rules block the file tools of Claude Code from the same folders and from .env files, which frequently hold keys. disableBypassPermissionsMode blocks the mode that skips all permission questions.
Codex turns its sandbox on by default. A command can write only to the start folder and to temporary folders, and it has no network access Codex. Keep these defaults. In ~/.codex/config.toml, their names are sandbox_mode = "workspace-write" and approval_policy = "on-request".
Anthropic writes that the sandbox "is not a complete isolation boundary" Claude Code. Three limits are important.
- File tools and web tools. The sandbox of Claude Code covers shell commands only. The tools Read, Edit and WebFetch obey the permission rules, so the
denyrules are necessary. - Helper programs. An MCP (Model Context Protocol) server is a connector program that gives the agent more tools. A hook is a script that the agent software runs at a fixed event. Local MCP servers and hooks run outside the sandbox with all rights of your account.
- Permitted hosts. The proxy examines only the name of the host, not the content. Each host on the list is a possible exit for data, so keep the list short.
05Use a stronger boundary for unattended work
Unattended work is work in which nobody watches the steps of the agent. Anthropic writes that the sandbox for commands alone is "not sufficient for fully unattended runs" Claude Code.
| Option | How it works | Limit |
|---|---|---|
| Docker Sandboxes | The agent runs in a microVM, a small virtual machine with its own operating system core. It sees only the project folder. | The project folder stays writable. |
| A virtual machine | A full second operating system runs on the Mac. | More setup. Share only the project folder with it. |
| Cloud sessions | The agent runs in a virtual machine of the vendor. | The code leaves the Mac. |
Docker Sandboxes run on a Mac with Apple silicon and macOS 14 or later. They do not need Docker Desktop, and the local use is free Docker install; Docker. A proxy on the Mac adds the stored credentials to the requests, so the secret values do not enter the virtual machine. The network policy blocks each destination that it does not permit Docker security.
brew trust docker/tap
brew install docker/tap/sbx
sbx login
sbx run claude ~/agent-work/my-project
The first two commands install the tool sbx with Homebrew, a package manager for macOS. sbx login signs you in to Docker. sbx run starts Claude Code in a new microVM with that one folder. Write codex in place of claude for Codex.
In a cloud session of Claude Code, the GitHub credentials stay on the servers of Anthropic and do not enter the virtual machine Claude Code.
Some flags turn off all permission questions: --dangerously-skip-permissions in Claude Code, and --dangerously-bypass-approvals-and-sandbox or --yolo in Codex. Docker Sandboxes start the agents with these flags inside the microVM, where the virtual machine is the boundary Docker for Claude Code; Docker for Codex. Do not use these flags on the Mac itself. In August 2025, malicious versions of the package Nx started the AI coding tools of developers with exactly these flags Snyk.
06Protect keys and secrets
The agent can send each secret that it can read. Thus keep each secret out of the files and the variables that the agent reaches.
- Password manager and its command-line tool. Keep each key in a password manager. With the 1Password tool
op, a.envfile holds only references to secrets. The commandop run --env-file=./app.env -- npm startgives the real values to that one process only 1Password. Useop runfor the one command that needs the secret, not for the agent session. - Hardware security key. This is a small USB device. It makes its secret inside the device, and software cannot copy the secret out. Each use needs a touch. Thus malicious software that reads all your files gets no credential that works later or on a different computer. The key also answers only to the genuine website, so a false sign-in page gets nothing. One limit stays: malicious software can use a session that is already open. Register two keys for the code host (for example GitHub) and for the password manager.
- SSH key on the hardware key. The command
ssh-keygen -t ed25519-skmakes an SSH key whose private part stays in the hardware key GitHub. The file in~/.sshis only a reference. Each push needs a touch, so the agent cannot push without you. The SSH software that comes with macOS cannot make or use these keys. Runbrew install opensshand put the Homebrew folder first in yourPATHYubico. In the sandbox of Claude Code,git pushto an SSH address can fail on macOS Claude Code. Let the agent commit, and push from your own terminal. - The macOS keychain. Claude Code keeps its login in the keychain Claude Code. The command
security list-keychainsshows that the login keychain is a file in the Library folder of one account. Plan as if each program in the agent account can ask the keychain of that account. - No secrets in
.envfiles. Adenyrule stops the file tools, but a script can still open the file Claude Code. Move the values to the password manager and keep only references in the file. - Git credentials. Give the agent its own credential for the code host. On GitHub, use a fine-grained personal access token. You limit this key to the named repositories and give it an expiry date GitHub.
07Control what the agent installs and loads
Set a waiting period before the package manager installs a new release nesbitt.io. The period gives the package registry time to find and remove a malicious version.
npm config set min-release-age 7
This command tells npm to install only versions that are more than 7 days old npm. For pnpm, write minimumReleaseAge: 10080 in pnpm-workspace.yaml. The unit is minutes, and the documented default is 1440 (one day) pnpm. For uv, write exclude-newer = "1 week" in ~/.config/uv/uv.toml uv; uv files.
Fix the version of each MCP server. A start command such as npx -y some-server gets the newest version at each start. Write the exact version after the name, as in some-server@1.4.2. Claude Code asks for approval before it uses a server from the project file .mcp.json Claude Code.
Review the files that steer the agent. Claude Code reads CLAUDE.md at the start of each session, and it can read AGENTS.md Claude Code. The file .mcp.json starts programs, and .claude/settings.json can contain hooks. Read these files before you start an agent in a repository that you did not write.
Editor extensions run code in your account. Visual Studio Code has a Restricted Mode for folders that you do not trust. It turns off agents, tasks and many extensions, but it cannot stop a malicious extension Visual Studio Code.
08Settings for a team
Claude Code has managed settings for a team. No user file, project file or command-line option can override them Claude Code. A team of one person can use them too.
| Method | Where the settings are | Use it when |
|---|---|---|
| A managed settings file | /Library/Application Support/ClaudeCode/managed-settings.json on each Mac. |
You have no device management. |
| Settings from the organisation console | claude.ai, Organization settings > Claude Code > Managed settings. | You have a Claude Team or Enterprise plan. |
| A configuration profile | The preference domain com.anthropic.claudecode. Device management software sends it. |
You manage the Macs centrally. |
For the file method, save the JSON example from this guide as managed-settings.json. Then run these commands in the administrator account.
sudo mkdir -p "/Library/Application Support/ClaudeCode"
sudo cp managed-settings.json "/Library/Application Support/ClaudeCode/"
The commands create the system folder and copy the file into it. A developer who is an administrator can edit this file, so the standard account is part of the control. /status then shows "Enterprise managed settings (file)".
With the console method, Claude Code gets the settings at each start and each hour Claude Code.
Add the keys allowManagedPermissionRulesOnly, allowManagedMcpServersOnly and allowManagedHooksOnly. Then only the managed rules, MCP servers and hooks apply. For Codex, the file /etc/codex/requirements.toml sets limits that a user cannot override Codex.
For light device management, Apple Business is a free service with device management included Apple. Anthropic publishes profile templates for Jamf, Iru and Intune.
09Things to avoid
- Do not start an agent in the home folder or in Downloads.
- Do not use a flag that skips the permission questions on the Mac itself.
- Do not keep keys in
.envfiles, in notes, in screenshots or in the shell history. - Do not give the agent your personal access token or your administrator account.
10If something suspicious occurs
Do not clean the Mac first.
- Stop the agent and stop work on the Mac.
- Disconnect the Mac from the network. Turn off Wi-Fi and remove the network cable.
- Keep the Mac on and locked. Do not delete files or logs.
- Use a different trusted device for the next steps.
- Cancel each credential that the agent account could reach: tokens of the code host, API keys and SSH keys.
- End the open sessions of the code host, the AI provider and the password manager.
- Read the activity log of the code host. Look for unknown pushes, new keys and new tokens.
- If you cannot exclude malicious software, erase the Mac and install macOS again. Then create new keys.
11Monthly self-check
- macOS is up to date, and the automatic updates are on.
- FileVault and the firewall are on, and the sharing services that I do not use are off.
ls /Users/yournamein the agent account answers "Permission denied"./statusin Claude Code shows the settings file, and the sandbox is on.- No project folder has a
.envfile with real secrets. - Each token of the agent has an expiry date and only the named repositories.
- The code host shows no unknown SSH keys, tokens or sessions.
- Each MCP server has a fixed version, and I know each editor extension.
12Sources
- Anthropic, Claude Code documentation, "Sandboxing": code.claude.com
- Anthropic, Claude Code documentation, "Deploy managed settings": code.claude.com
- OpenAI, Codex documentation, "Agent approvals and security": learn.chatgpt.com
- GitHub, "Generating a new SSH key for a hardware security key": docs.github.com
- Yubico, "Securing SSH with FIDO2": developers.yubico.com