Agentic Systems · Personal experiment
•
8 min read

Sandboxing coding agents on my own machine

  • Seatbelt
  • bubblewrap
  • Landlock
  • seccomp
  • gVisor
  • Prompt injection

When you give a model a shell or a run_python tool, whatever it writes runs on your machine, as you. Your SSH keys, cloud credentials and .env files are one cat away, and nothing in the tool-calling protocol limits what pytest or pip install can do. Any limit has to come from your harness.

What a sandbox is for

The risk I worry about most is Simon Willison’s lethal trifecta. If an agent can see private data, reads untrusted content like a GitHub issue or a web page, and has some way to send data out, it can be talked into leaking that data.

Prompt injection is still unsolved at the model layer. The paper “The Attacker Moves Second” (October 2025) broke twelve published defences with adaptive attacks, most of them at over 90% success. So the job of a sandbox is to take away one of those three.

Filesystem isolation and a clean environment take away the private data, and blocking the network takes away the way out. Leaking doesn’t need a raw socket either. A DNS lookup for SECRET.attacker.example is enough, and CVE-2025-55284 was exactly that, with Claude Code before v1.0.4 auto-approving ping, nslookup and dig.

Why approval prompts fall short

Approval prompts and Python-level guards don’t hold up well on their own. Anthropic’s numbers for Claude Code show how quickly people stop reading prompts.

MeasureNumber
Permission prompts users approve (March 2026)93%
Permission prompts users approve (August 2026)97%
Planted dangerous command caught, 1,053 testers13.6%
Catch rate early in a sessionabout 17%
Catch rate after 50 or more promptsabout 5%

13.6%! The catch rate also falls about 3x over a session, from about 17% to about 5%. A classifier that reviews each action is one more advisory layer. Claude Code’s auto-mode classifier had a 0.4% false-positive rate and a 17% false-negative rate, so it misses about one bad action in six.

In-process guards like audit hooks are easy to get around too. CPython’s own docs say sys.addaudithook is “not suitable for implementing a ‘sandbox’”, and native code or a plain os.stat slips past it.

With a careful subprocess runner and no OS wrapper, the probe read a planted secret with NO trouble at all, then wrote outside the workspace, opened a TCP connection and resolved DNS. A sandbox also means fewer prompts. Anthropic says sandboxing cut Claude Code’s permission prompts by 84% internally.

Seatbelt on macOS

So the boundary has to come from the OS. On macOS that’s Seatbelt, run through sandbox-exec. It has been marked deprecated since roughly 2017, yet Codex, Claude Code and Chrome all rely on it. Here is the profile and how to run it.

;; agent.sb (macOS 26.5.2, Apple silicon)
(version 1)
(deny default)
(allow process-exec)
(allow process-fork)
(allow signal (target same-sandbox))
(allow sysctl-read)
(allow file-read*)                        ; simple but broad: ~/.ssh stays readable
(deny file-read* (subpath (param "SECRETS")))
(allow file-write*
  (subpath (param "WORKSPACE"))
  (literal "/dev/null"))
;; no (allow network*) rule, so all network access is denied, DNS included.
;; no (allow mach-lookup): a broad mach-lookup lets code ask launchd to start
;; processes outside the sandbox (for example via `open`).
WS=$(cd workspace && pwd -P)      # resolved real paths only
SECRETS=$(cd ~/.ssh && pwd -P)    # a directory to hide; add one deny rule per location
cd "$WS" && sandbox-exec -f ../agent.sb -D WORKSPACE="$WS" -D SECRETS="$SECRETS" python3 main.py

Seatbelt matches resolved paths, which is why those pwd -P calls matter. A workspace passed as /tmp/... silently fails its own write rule, because the kernel sees /private/tmp/.... For proper read isolation, list the readable paths instead of denying a few.

Bubblewrap on Linux

On Linux the main tool is bubblewrap, which builds a fresh namespace view without root. This recipe works unchanged on Debian 12 (kernel 6.1) and Ubuntu 26.04 (kernel 7.0).

WS="$(realpath ./workspace)"
bwrap \
  --ro-bind / / \
  --tmpfs "$HOME" \
  --bind "$WS" "$WS" \
  --tmpfs /tmp \
  --dev /dev \
  --proc /proc \
  --unshare-all \
  --die-with-parent \
  --new-session \
  --cap-drop ALL \
  --clearenv \
  --setenv PATH /usr/local/bin:/usr/bin:/bin \
  --setenv HOME "$WS" \
  --chdir "$WS" \
  -- python3 main.py

The empty tmpfs over $HOME hides ~/.ssh and ~/.aws, and --unshare-all leaves only loopback.

On Ubuntu 24.04 (kernel 6.8) it fails with bwrap: setting up uid map: Permission denied, because of an AppArmor restriction. Fix A below is the one to use.

# Fix A (preferred, scoped to /usr/bin/bwrap)
sudo apt install apparmor-profiles
sudo install -m 0644 /usr/share/apparmor/extra-profiles/bwrap-userns-restrict /etc/apparmor.d/
sudo apparmor_parser -r /etc/apparmor.d/bwrap-userns-restrict
# Fix B (host-wide; weakens every process)
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0

Landlock, Docker and gVisor

Landlock is an extra Linux layer that lets a process restrict itself without root. Its network rules cover TCP from kernel 6.7, but UDP rules only arrived in kernel 7.2, so on most kernels DNS still gets through.

For stronger isolation there’s a hardened Docker container, which shares the host kernel. Docker’s default seccomp profile blocks about 44 of 300-plus syscalls, so the flags below matter more than the image.

docker run --rm -i \
  --network none \
  --read-only \
  --tmpfs /tmp:rw,noexec,nosuid,size=64m \
  --mount type=bind,src="$PWD/workspace",dst=/workspace \
  -w /workspace \
  --user "$(id -u):$(id -g)" \
  --cap-drop ALL \
  --security-opt no-new-privileges \
  --pids-limit 128 \
  --memory 512m --memory-swap 512m \
  --cpus 1 \
  --ulimit nofile=256:256 \
  --init \
  python:3.13-slim \
  timeout 60 python /workspace/main.py

With these flags, a fork test stopped at about 125 processes, and a 700 MB allocation under the 512 MB limit was killed with SIGKILL.

Adding --runtime=runsc puts gVisor, a user-space kernel, in between. The same flags work, though in one test its 512 MB memory limit killed the WHOLE sandbox instead of returning an error.

Never mount docker.sock and never pass your API keys in with -e.

How the options compare

Here is how the options compare. Startup times are rough orders of magnitude, except Firecracker’s documented figure.

OptionIsolation strengthStartupSetup costWhat breaks
Approval rulesNone (advisory)InstantLowPrompt fatigue; forbid-lists are easy to sidestep
In-process Python guardsVery weakInstantLowstat, native code, introspection
Subprocess hygieneResources onlyMillisecondsLowsetsid escapes killpg; absolute RLIMIT_AS on macOS
SeatbeltStrong for mistakes and injectionMillisecondsMediumUnresolved paths silently miss; reads open by default
bubblewrap + seccompStrong for mistakes and injectionMillisecondsMediumUbuntu 24.04 AppArmor; fails inside default Docker
LandlockFilesystem plus TCPMillisecondsMediumUDP/DNS open below kernel 7.2; pre-existing threads before ABI 8
Hardened Docker (runc)Similar to an OS sandbox; shared kernelSub-secondMediumnoexec /tmp breaks some pip builds
gVisorStronger: separate syscall surfaceSub-secondMediumMemory budget; different errno values
microVM (Firecracker, Kata)StrongestUnder 125 ms (Firecracker)High, or buy itNot tested locally

Firecracker documents a boot time under 125 ms with under 5 MiB of memory overhead.

Network and secrets

For the network, the sandbox gets none of its own and can only talk to a proxy on the host that enforces a domain allowlist.

Secrets never go inside the sandbox. The proxy attaches the real token on the way out, only for allowed hosts.

What I’d use

On my own laptop I’d use Seatbelt with an explicit list of readable paths on macOS, or the bwrap recipe plus a seccomp filter on Linux, with the network going through a host proxy in both cases. For code I don’t trust at all, I’d use gVisor, a microVM or a hosted sandbox.

Whatever you pick, run a small probe script inside it that tries to read a fake secret, write outside the workspace and reach the network. Make the harness refuse to run if the sandbox binary is missing.

Every option here had at least one surprise on some platform, so it’s worth checking on your own setup.