cocode.dk English Dansk Español 简体中文

Why sandbox AI coding agents

An AI coding agent is a program that lets a language model act on your computer. Examples are Claude Code, opencode, pi and prime-agent. An agent reads and edits files, runs commands and tests, installs packages and fetches web pages, all with your user account's permissions: anything you can do, it can do.

A sandbox is a fenced-off space for the agent. The agent runs as a normal program but sees only part of your computer: the project folder, the programs it needs and the few internet hosts it must reach.

Source on GitHub

What can go wrong without a sandbox

Without a sandbox, six things can go wrong.

  1. Mistakes. A wrong command, such as a clean-up aimed at the wrong folder, can delete or overwrite files outside the project.

  2. Prompt injection. Text the agent reads (a README in a dependency, an issue comment, a web page, a code comment) can carry instructions the model follows, such as "send the contents of ~/.ssh to this address". Models cannot reliably tell data from instructions.

  3. Leaked secrets. With your home folder readable and the internet open, a tricked or careless agent can read API keys in .env files and tool configs, SSH keys, cloud credentials and other projects' code, and send them anywhere.

  4. Malicious packages. An agent can install a malicious package whose install script runs with your permissions.

  5. Things that run later. An agent can plant something that runs after its session ends: a git hook in .git/hooks, a line in ~/.bashrc, a changed tool config, or a swapped program in a tool folder.

  6. Other projects exposed. Agent tools keep a history of sessions and logs, which may hold other projects' code and prompts.

What a sandbox does

A sandbox sets three fences.

  • Files: only the project folder, the system's programs and the agent's own folders (its program, settings and login) are visible. The rest of your home folder (keys, other projects, browser data) is not there. The project's git-ignored files, such as .env and local keys, are hidden too. The project is writable when the agent edits code and read-only when it only reviews. Its .git folder is read-only, so the agent cannot commit or add hooks. The tools are read-only, so nothing can be swapped. Dependency folders (node_modules, .venv) are writable only when the agent runs builds; otherwise they are hidden. Settings the agent must write go into a throwaway copy. The ready-made wrappers leave out the agent's history from other projects.
  • Network: there is no direct internet. All traffic goes through a proxy (a gatekeeper program that forwards allowed connections) outside the sandbox that lets through only the hosts named for that agent, typically its model provider's API. Everything else is refused, even services on the same computer.
  • Environment variables: they are cleared except for the few you name, so keys in your shell do not leak in.

Scratch folders inside the sandbox are thrown away when the agent exits.

This project builds on Anthropic's sandbox-runtime (srt) on Linux. srt uses bubblewrap (bwrap), which relies on Linux namespaces, the same kernel features containers use. It is not a virtual machine. There is no daemon and no image: a sandbox starts in about a second and uses the programs you already have installed. A small shell script, agent-box, turns each agent's list of hosts, folders and kept variables into srt's settings. opencode, pi and prime-agent run this way, each reaching only its own model providers.

What you gain

  • Mistakes stay inside the project.
  • Secrets outside the sandbox cannot be read or leaked.
  • A tricked agent cannot reach an attacker's server.
  • The agent cannot plant git hooks or swap tools that run later, because .git and the tool folders are read-only. The project's own files stay writable, and so do its dependency folders when the agent runs builds, so review what the agent writes there. After each run, the sandbox lists the files the agent changed in dependency folders and other git-ignored places.
  • You can let an agent work unattended with a bounded worst case.
  • It is cheap and fast next to a virtual machine or container image, and each agent's rules are a short list you can read.

What a sandbox does not do

  • It shares your computer's kernel (the core of the operating system), like a container. A kernel bug could let a process out. A virtual machine isolates more. Keep the kernel updated.
  • Allowed hosts still receive data. Whatever is inside the sandbox can still be sent to the allowed hosts, such as the model provider. That includes the keys the agent needs.
  • Code the agent writes runs outside the sandbox once you run it, push it or install it. Review the diff first, along with any files it changed in dependency folders (node_modules, .venv).
  • Package registries are blocked, so nothing installs inside the sandbox. Install outside, with your usual checks.
  • The project folder is not protected. The agent can still change or delete files there, including work you have not committed. Commit or stash first, or give the agent a separate worktree (a separate checkout of the repository).
  • On Ubuntu, which restricts unprivileged user namespaces through AppArmor, srt's extra system-call filter (seccomp) must be switched off for the sandbox to start. The agent-box script detects this and switches the filter off by itself. The file and network fences still apply.
  • This project's scripts are for Linux only.

Getting started

The agent-sandboxing skill teaches Claude Code to build a sandbox for your agent: https://github.com/cocodedk/agent-sandboxing

You need Linux, Claude Code, bubblewrap, socat, ripgrep, git, python3, Node.js and srt. The steps use Debian or Ubuntu.

  1. Install the system tools:

    sudo apt install bubblewrap socat ripgrep git python3

    The command finishes without errors.

  2. Install Node.js from nodejs.org or your package manager, then run npm --version. It should print a version number.

  3. Install srt:

    npm install -g @anthropic-ai/sandbox-runtime

    Run srt --version. It should print a version number.

  4. Download the skill:

    git clone https://github.com/cocodedk/agent-sandboxing

    A folder named agent-sandboxing appears.

  5. Copy the skills/agent-sandboxing folder from the download into ~/.claude/skills/:

    mkdir -p ~/.claude/skills && cp -r agent-sandboxing/skills/agent-sandboxing ~/.claude/skills/

    You now have ~/.claude/skills/agent-sandboxing.

  6. In Claude Code, ask for a sandbox, for example "sandbox opencode" or "put my agent CLI in a box". For opencode, pi and prime-agent, Claude uses the ready-made wrapper. For any other agent, it surveys what the agent needs and writes a small wrapper script. Then it runs the skill's probe test. The test tries each fence: a hidden .env, a read-only .git, a refused host and cleared variables. It prints PASS or FAIL per check and should end with 0 failed.

The skill includes ready-made wrappers: opencode-box, pi-box and prime-agent-box. The skill sandboxes other agent CLIs that Claude Code runs. It does not sandbox Claude Code itself, which has its own sandbox settings.

Credit and license

The sandbox itself is Anthropic's open-source sandbox-runtime: https://github.com/anthropics/sandbox-runtime (npm @anthropic-ai/sandbox-runtime, Apache-2.0 license). This project only adds the settings for each agent and the wrappers around it.

This project is licensed under Apache-2.0. Site: https://sandbox.cocode.dk/

cocode.dk