Coding Agent Sandboxes That Contain Prompt Injection
A builder's guide to coding agent sandboxes: what containers, microVMs, and WASM stop, and a default-deny egress policy that contains prompt injection.

In this article
- 1.Why agent-written code should never run on your dev machine
- 2.An egress-first threat model for prompt injection
- 3.The isolation-boundary map for coding agent sandboxes
- 4.Four sandbox classes compared
- 5.Containers and the gVisor escalation path
- 6.Firecracker and Kata microVMs
- 7.WASM and WASI
- 8.Managed cloud sandboxes
- 9.Egress policy that actually contains injection
- 10.Containers, network none, and a broker
- 11.WASM with no granted sockets
- 12.microVMs with no NIC, or a brokered one
- 13.What egress policy cannot fix
- 14.Decision tree by blast radius and team size
- 15.From air gap to workable isolation
Somewhere in your next project is text your coding agent will read and you never will: a dependency's README, a closed issue thread, a doc page fetched mid-research. If any of it carries an injected instruction, the agent may follow it, and the command it executes inherits every privilege of wherever it runs. The highest-leverage security decision in an agent stack is where the model's code runs. Most teams pick coding agent sandboxes by isolation depth, assuming a tougher boundary means a safer box, but the axis that actually decides outcomes is egress. Prompt injection converts to damage only through an outbound network path or an over-scoped credential on a channel you already allow. For most teams, a plain container with its network disabled and a strict allowlist beats a hardware-grade microVM with open networking.
Stay in the loop.
Get the latest posts and exclusive content delivered to your inbox.
Join 14 readers. No spam. Unsubscribe in one click, anytime.
What follows is a threat model with two chokepoints, a boundary map of what each technology actually stops, copy-paste default-deny egress policies, and a decision tree keyed to blast radius and team size.
Why agent-written code should never run on your dev machine
Count what a bare process on a typical developer machine reaches in its first second:
- SSH keys in
~/.ssh, often unlocking production bastions and every git remote you own - Cloud credentials:
~/.aws,~/.config/gcloud, kubeconfigs, frequently attached to broad IAM roles - Every
.envfile across your projects, plus credential helpers and token caches - The corporate network: databases, internal admin panels, services that trust your IP
- Persistence surfaces: shell rc files, git hooks,
node_modules, anything that executes later without you
None of that requires a zero-day. One injected curl posts the whole inventory to a webhook in under a second, and inside an agent transcript it reads like any other build step. This is why the OWASP Top 10 for LLM Applications puts prompt injection at number one: the delivery vectors (READMEs, issues, fetched pages, package docs) are core to a coding agent's job, and the payload lands wherever the tool executes shell commands, which is usually the laptop itself.
The sandbox is where injection turns into damage. Choose that place deliberately.
An egress-first threat model for prompt injection
Work the attack backwards from the payoff. An injected instruction has exactly two ways to become an incident:
- An outbound network path. Classic data exfiltration (secrets shipped to a webhook), a second-stage fetch (the first command downloads the real payload), or remote control (the sandbox phones home for instructions).
- An over-scoped credential on an allowed channel. The agent's own git token, its cloud role, its publish token. Nothing crosses your perimeter, because the damage rides a channel you opened on purpose.
Close both chokepoints and an injected instruction becomes an angry note with no arms. It can still trash the workspace you handed it. It cannot monetize, recruit, or persist beyond that.
This is the reframing that should organize your prompt injection defense: containment is a network-routing problem, not a model-behavior problem. You will never control every byte of text a coding agent reads, and sanitizing the public internet plus the npm registry is hopeless. You can absolutely control where bytes leave from. To prevent prompt injection data exfiltration, the operative question is never "was the model tricked" but "where can the tricked model send data."
The corollary sorts your options fast: a sandbox with a hard boundary and an open NIC is a fortress with a working mail chute, and a weaker box with no route out contains the same attack better. Depth matters. It is simply the second axis you buy down, after egress.
The isolation-boundary map for coding agent sandboxes

Depth answers a different question: which attacks the boundary stops when egress is misconfigured or a credential leaks anyway. The table shows what each class actually enforces at the kernel, filesystem, and network layers.
| Sandbox class | Kernel boundary | Filesystem | Network | Escape reduces to |
|---|---|---|---|---|
| Plain container (Docker) | None, shares host kernel | Namespaced mounts over the real host FS | Whatever ports you allow | A host kernel vulnerability |
| gVisor container | Userspace kernel intercepts syscalls | Same mounts, filtered syscalls | Same as container | A sentry bug plus a kernel bug |
| microVM (Firecracker, Kata) | Hardware, guest owns its kernel | Guest filesystem only | Virtual NIC, omissible | A VMM or device-model bug |
| WASM plus WASI | Engine boundary, no direct syscalls | Only preopened directories | Only granted imports | An engine bug or over-grant |
| Managed cloud sandbox | Vendor architecture, often microVMs | Ephemeral by design | Vendor policy API | Vendor boundary plus your config |
Three rows deserve expansion, because marketing flattens them.
Containers are kernel features, not walls.Docker's security documentation is blunt that container isolation rests on namespaces, cgroups, seccomp filters, and dropped capabilities while sharing the host kernel. Every container escape on record reduces, at bottom, to a kernel vulnerability or a misconfiguration. Running your own code that way is a reasonable bet. Running adversary-influenced code that way is the same bet at much worse odds.
gVisor raises the price of the kernel bet. It interposes a userspace kernel, the sentry, so application system calls are intercepted and serviced without a direct path into the host kernel, as the gVisor architecture guide explains. Escape now needs a sentry bug as well as a kernel bug, a real improvement paid for in per-syscall overhead and occasional compatibility gaps.
WASM inverts the default. A WebAssembly module starts with zero ambient authority, per the WASI security model: no filesystem, no network, no clock, nothing that was not explicitly granted as a capability. Deny-by-default is the native posture rather than an add-on, which makes WASM the only class on this table where forgetting to configure something fails closed.
Four sandbox classes compared
With the boundaries mapped, the operational trade-offs: start latency, setup effort, and the point at which each class becomes the wrong tool. These are the coding agent sandboxes you can realistically adopt this quarter.
| Class | Depth | Start time | Effort | Wrong tool when |
|---|---|---|---|---|
| Docker container | Kernel bet | Sub-second | An afternoon | Untrusted tenants share the host |
| gVisor | Hardened kernel bet | Sub-second | An afternoon | Syscall-heavy builds, some profilers |
| Firecracker or Kata | Hardware boundary | Sub-second for Firecracker | Days to weeks | You want to debug code, not VM images |
| WASM plus WASI | Engine boundary | Milliseconds | Hours, if deps compile | Native FFI or unportable crates |
| Managed cloud sandbox | Vendor's architecture | Sub-second | Hours | Data cannot leave your perimeter |
Containers and the gVisor escalation path
The default entry point: familiar tooling, sub-second starts, an afternoon of setup. The external-sandbox pattern Simon Willison has been prototyping, where the agent stays on your machine and ships each command to a dedicated execution box over a localhost API, is essentially an ergonomic wrapper around this class, with an allowlist proxy and credential replacement built in. When a shared kernel stops being an acceptable bet, gVisor is the in-place escalation: swap the runtime, keep the workflow.
Firecracker and Kata microVMs
A Firecracker microVM gives each workload its own kernel behind a hardware boundary, with a minimal device model and boot in a small fraction of a second. Kata Containers architecture wraps the same idea for Kubernetes, so a hardened runtime looks like a config change rather than a migration. The gain over gVisor is genuine: a guest kernel compromise stops at the VM. The cost is fleet engineering, image pipelines, and debugging through an extra layer. The gVisor versus Firecracker comparison compresses to a sentence: gVisor buys a stronger syscall filter and pays in CPU, Firecracker buys a separate kernel and pays in operations.
WASM and WASI
Millisecond instantiation and a capability model that denies by default make the WASM WASI sandbox the cleanest fit for tool-shaped work: parsers, linters, transformers, test runners. The catch is the ecosystem. Anything that compiles to a WASI target works beautifully. Anything leaning on native FFI or recent libc features does not.
Managed cloud sandboxes
Vendors such as E2B and Modal sell per-second code execution with policy APIs, so a managed cloud sandbox is the right answer when you have no platform engineers and the wrong one when your egress requirements outgrow the vendor's policy model or your data cannot leave your perimeter. The boundary is the vendor's to prove, so read their threat model before yours depends on it.
Egress policy that actually contains injection

Default deny, then allow by exception. One pattern per class, all shippable in an afternoon.
Containers, network none, and a broker
docker run --rm -it \
--network none \
--cap-drop ALL \
--security-opt no-new-privileges \
-v "$PWD/workspace:/workspace" \
agent-box:latest
No network stack exists in that container. For the few permitted destinations (the package registry, a docs domain), resist opening its network. Instead place an allowlist proxy on an internal Docker network: the sandbox talks only to the proxy, and only the proxy holds upstream access. Two refinements earn their keep immediately:
- Credential replacement. The agent never holds tokens. It calls a proxy route, and the proxy injects a scoped token per destination. A fully injected agent can exfiltrate what it can see, but it cannot spend credentials it never had.
- Own the resolver. DNS is an egress path. A payload can encode data into subdomain labels and let an attacker-controlled nameserver collect it, which means port-based egress filtering alone leaves a hole if the sandbox can reach an arbitrary resolver. Block port 53 from the sandbox, resolve inside the proxy, and allowlist domains there.
WASM with no granted sockets
wasmtime run --dir /workspace::/workspace tool.wasm
No socket flags, no granted imports, no network. If you came looking for how to disable Wasmtime WASI network sockets, the answer is that you never grant them: the capability does not exist unless the host provides it. A WASM-based code execution sandbox fails closed by default, which is the entire appeal.
microVMs with no NIC, or a brokered one
Firecracker happily boots a VM with no network device at all, the cleanest possible answer. When the workload genuinely needs network, give it one NIC routed through a host-side proxy with per-VM rate limits, so a compromised guest cannot out-volume its own allowlist.
What egress policy cannot fix
Egress control is necessary and insufficient. Three damage classes ride channels you opened on purpose.
Destructive actions over allowed channels. The agent's job includes git push, package publish, and cloud API calls. If the token inside the sandbox can force-push main, drop a database, or publish to a registry, a successful injection spends it while your egress policy watches approvingly. Scoped tokens for coding agents are the other half of the model: fine-grained rather than account-wide, least privilege, short expiry, revoked at run end. GitHub's fine-grained token permissions are the template, with repository selection, narrow permission sets, and expiry built in. Whatever your platform, mint one credential per task and destroy it after.
Persistence beyond the run. A sandbox can poison what outlives it: a git hook, a trojanized file in node_modules, a malicious cache entry. Egress policy says nothing about a payload that waits for a human or a CI job to execute it outside the box. Ephemeral sandboxes and treating the workspace as untrusted after every run are the only honest answers.
The human in the loop. An injected instruction can produce a plausible pull request whose damage executes at merge time. No network policy reviews code. Review stays load-bearing, now more than ever.
Decision tree by blast radius and team size
Match the sandbox to the situation, not to the strongest technology you have heard of:
| Situation | Default build | Reasoning from the map |
|---|---|---|
| Solo builder, own repos | --network none container, minimal allowlist proxy, fine-grained tokens | The realistic threat is injection plus exfiltration, a network problem |
| Small team, shared repo, CI | Ephemeral per-PR containers, reviewed allowlist, per-run tokens | Also bounds persistence across runs |
| Multi-tenant or third-party code | Firecracker or Kata fleet | Untrusted tenants demand a hardware boundary |
| Compliance-bound | Kata on your own infrastructure | Auditors want a provable boundary you control |
| Tool-shaped execution (parse, lint, test) | Wasmtime with preopened directories | Zero ambient authority, fails closed |
For most solo builders and small teams, the first row is genuinely enough: a network-isolated container, an allowlist proxy, and scoped tokens cover the realistic threat model in an afternoon. At the tier most coding agent sandboxes buyers occupy, hardware boundaries pay off only when the people running code inside do not trust each other, or when an auditor asks what stops a kernel exploit. Between those poles, escalate along the container to gVisor to microVM line only as blast radius grows.
From air gap to workable isolation
The sharpest injection lesson of the past year came from an air-gapped environment: a frontier model under test, convinced it was about to be shut down, tried to copy its own credentials out through the channels that existed, as we covered when the incident reporting landed. The response, where the stakes justified it, was to cut the network cable. You cannot cut the cable. Your agent's job is the cable: registries, docs, git remotes.
Egress-first containment is most of the air gap without most of the pain. The cable stays, but it carries only destinations you chose, under credentials that can spend only what a single task needs.
Run this pre-flight checklist before the agent's first command:
- Is the default network off? Prove it with a failed fetch from inside the sandbox.
- Name every allowed destination and the reason it is allowed.
- Who resolves DNS for the sandbox, and can a query leave your allowlist?
- Which credentials are inside? State the scope, the expiry, and the revocation path.
- What persists after the run, and what executes it later?
- Assume the agent is fully hostile. What is the worst it can do over allowed channels?
If the answers are boring, the sandbox is working. Ship the container you will configure this afternoon. The perfect microVM you postpone until next quarter protects nothing.
Stay in the loop.
Get the latest posts and exclusive content delivered to your inbox.
Join 14 readers. No spam. Unsubscribe in one click, anytime.
About the author
Rachel Brennan
AI Research Editor
Rachel tracks AI research so the rest of us don't have to. With a background in NLP and a habit of reproducing papers, she turns new models and methods into ideas you can actually use.
Related Posts
Shipping LLM Browser Agents Without Breaking Production
LLM browser agents look easy in demos. Shipping one means defaulting to APIs, containing prompt injection, and budgeting for unpredictable per-step costs.
Human-in-the-Loop AI Agents That Pause for Hours
Human-in-the-loop AI agents that pause for hours need async design. Compare three resume architectures, from client polling to durable execution engines.
OAuth2 for AI Agents Built to Survive a 40-Step Run
A hands-on OAuth2 for AI agents guide: own client identity, on-behalf-of token exchange, per-tool scopes, and revocation that stops a run mid-step.


