Sub topic 12
Running the agent in a sandbox
This chapter assumes you know the agent tool loop, the idea of indirect prompt injection, and basic Docker commands. Earlier chapters showed that you cannot reliably stop a model from following injected text. A sandbox takes a different approach. It limits what an injected instruction can do once the model follows it.
What a sandbox does
A sandbox is an isolated environment where the agent's commands run. The agent can still be fooled, but the damage stays inside the boundary. A good sandbox restricts four things:
- Filesystem: the agent sees only the project, not your home directory, SSH keys, or cloud credentials.
- Network: outbound connections are blocked or limited.
- Privileges: the process runs without root or extra Linux capabilities.
- Lifetime: the environment is thrown away after the task.
This weakens the lethal trifecta from the previous chapters. If the sandbox holds no private data and has no outbound channel, an injection has nothing to steal and nowhere to send it.
A minimal container sandbox
This command runs the test suite with no network, no extra capabilities, and a read-only root filesystem:
docker run --rm \
--network none \
--cap-drop ALL \
--read-only \
--tmpfs /tmp \
-v "$PWD":/work \
-w /work \
python:3.12-slim \
python -m pytest -qEach flag has a job:
| Flag | Effect |
|---|---|
| `--network none` | No outbound channel for exfiltration |
| `--cap-drop ALL` | Removes Linux capabilities the process rarely needs |
| `--read-only` | The image cannot be modified |
| `--tmpfs /tmp` | Gives scratch space that disappears on exit |
| `-v "$PWD":/work` | Exposes only the project directory |
The -v mount is still writable. A hijacked agent can change or delete project files. Use a git branch or a copy, and review the diff afterward. You can add :ro to the mount when the task only reads files.
Common mistakes
- Mounting too much. Mounting
$HOMEor/var/run/docker.sockhands the agent your secrets or control of the host. Mount only what the task needs. - Passing secrets as environment variables. Anything in the container's environment is readable by injected commands. Leave tokens out of the sandbox, or use short-lived, narrowly scoped ones.
- Leaving the network open for convenience. Installing dependencies needs network access. Do that in a separate step before the agent starts, or use an egress allowlist (covered in a later chapter).
- Treating a container as a perfect wall. Containers share the host kernel, so a kernel bug can allow an escape. For hostile code, use a virtual machine or a microVM.
- Sandboxing only the shell. If the agent's file-editing or MCP tools run outside the sandbox, they bypass it.
Exercise
A teammate runs an agent in a container with --network none, but mounts their home directory at /home/dev and passes GITHUB_TOKEN as an environment variable. What is still risky, and what would you change?
Show answer
With no network, the agent cannot send data out directly. But the home directory and the token are still exposed. Injected commands could read SSH keys, copy secrets into project files that later get committed, or damage files in the home directory. Mount only the project directory, remove the token from the environment, and review the diff before committing.