Sub topic 10
Sandboxing what the agent can touch
This chapter assumes you have read the chapter on permissions and approval modes. You should be comfortable with the shell, with file permissions, and with the idea that an agent runs commands on your behalf.
Approvals and sandboxes are different controls
An approval is a question the tool asks you: "May I run this command?" A sandbox is a boundary the operating system enforces: "This process cannot write outside these folders, whatever it tries."
The two controls answer different questions.
- Approvals decide whether a human sees an action before it happens.
- A sandbox decides what the action can reach once it runs.
Approvals depend on you reading each prompt carefully. After fifty prompts, people tend to approve without reading. A sandbox does not tire. It limits the damage of a command you approved by mistake, and of a command the model produced after reading a malicious file.
What a sandbox restricts
A sandbox usually limits two resources.
- Filesystem access. The agent may read and write inside the project directory, and perhaps a temporary directory. Writes to your home directory, shell profile, or SSH keys are blocked.
- Network access. Outbound connections are blocked or limited to an allowed list. This stops a compromised session from sending your files to a remote server.
Sandboxes apply to the commands the agent starts, not only to its own file edits. If the agent runs npm test, that process and everything it spawns inherit the limits.
Sandbox modes in Codex CLI
Codex CLI exposes the sandbox as a setting separate from the approval policy. The configuration from the earlier chapter sets both.
# ~/.codex/config.toml
approval_policy = "on-request"
sandbox_mode = "workspace-write"Codex documents three sandbox modes.
| Mode | File access | Typical use |
|---|---|---|
| `read-only` | Read files, no writes | Exploring an unfamiliar repository, code review |
| `workspace-write` | Read anywhere, write in the workspace | Everyday development |
| `danger-full-access` | No filesystem limits | Only inside an already isolated machine |
In workspace-write, network access is off by default for commands the agent runs. Tests that download packages will fail until you allow network access or install dependencies first. The exact option names can change between releases, so check the current Codex documentation before relying on them.
You can combine the two controls to match risk:
read-onlywithon-requestapprovals for reading code you do not trust.workspace-writewithon-requestfor normal feature work.- Avoid pairing
danger-full-accesswith approvals turned off unless the machine itself is disposable.
Sandboxing in Claude Code
Claude Code starts from its permission rules. By default it can read files and asks before editing files or running commands. Its settings file lists allowed and denied tools.
{
"permissions": {
"allow": [
"Bash(npm run test:*)"
],
"deny": [
"Read(./.env)"
]
}
}These rules are checked by the tool itself. They are a policy layer, and they are not the same as an operating system boundary. A deny rule on Read(./.env) blocks Claude's own file-read tool. A shell command that reads the file may be handled differently, so do not treat tool rules as a complete wall.
Claude Code also offers an optional sandboxed mode for its shell commands. It uses operating system features to restrict filesystem and network access for those commands. Check the current documentation for how to enable it on your platform. When it is on, shell commands that stay inside the limits can run with fewer prompts, because the boundary is doing the protecting.
Sandboxing in Copilot CLI
Copilot CLI works mainly through trust and approval. It asks you to trust a directory before working in it, and it asks before running tools. Its sandboxing features differ from the other two tools and change quickly. Read its current documentation before assuming it gives you an operating system boundary. If you need a hard limit and the tool does not provide one, apply isolation yourself, as described next.
Isolating with containers and virtual machines
You can add your own boundary under any of the three tools. A container or virtual machine gives you isolation that does not depend on the tool's own implementation.
docker run --rm -it \
-v "$PWD":/workspace \
-w /workspace \
node:22 bashInside this container, install the agent and run it. The agent sees only the mounted project and the container's filesystem. Your home directory, SSH keys, and cloud credentials on the host are not visible.
Practical points:
- Mount only the project. Do not mount your home directory or the Docker socket. Mounting the socket gives the container control of the host.
- Pass credentials deliberately. Give the agent its own API key, and use a token with narrow scope for git or cloud access.
- A disposable environment, such as a throwaway virtual machine or a CI job, makes broader permissions much less risky.
- Network rules matter as much as file rules. A container with full network access can still send mounted files elsewhere.
Common errors and gotchas
- Treating permission rules as a sandbox. Tool-level allow and deny rules guide the agent. They can miss paths the agent reaches through a shell command.
- A command fails with a permission or network error. The sandbox may be blocking it. Read the error. Do not switch to full access as the first fix. Widen only what the task needs, such as one extra writable directory.
- Allowing broad command patterns. A rule like
Bash(npm run test:*)is narrow. A rule that allowsBash(*)removes the point of having rules. - Secrets inside the workspace. Anything the agent can read, it can include in a model request. Keep
.envfiles and keys outside the mounted workspace, or deny reads of them. - Assuming the sandbox covers external tools. Tools you connect through MCP run as separate processes with their own access. The sandbox on the agent's shell may not constrain them. The chapter on MCP covers this.
Choosing a setting for a task
| Task | Suggested setup |
|---|---|
| Read and explain a repository you just cloned | Read-only mode, network off |
| Implement a feature in your own project | Workspace-write, network off, approvals for new commands |
| Run an agent in CI | Disposable runner, narrow tokens, no interactive approvals |
| Work on code you do not trust | Container or virtual machine with no host credentials |
Exercise
The agent's test run fails with "network unreachable" when it tries to download a package, while the sandbox mode is workspace-write. What is the most likely cause, and what is a safer fix than switching to full access?
Show answer
Workspace-write blocks outbound network access for commands by default, so the download is stopped by the sandbox. A safer fix is to install dependencies yourself before the session (for example with npm ci) or to enable network access only for this task. Full access removes the filesystem limits as well, which the task does not need.
Exercise
Your Claude Code settings deny Read(./.env). Why is it still unsafe to keep production secrets in that file while running the agent on your host machine?
Show answer
The deny rule is a tool-level policy, so it blocks that tool's read action but is not an operating system boundary. A shell command the agent runs could still read the file. Keep secrets outside the workspace, or run the agent in a container that does not contain them.