An AI agent that writes code needs somewhere to run it, and that code is untrusted. How we isolate it on our own servers: a service that holds nothing, no network in or out, a separate user per job, checks on every file it returns, and tests that run inside the real container.
In September I wrote about three security boundaries for hosting untrusted, AI-generated HTML. That one was about the browser. A page nobody reviewed runs in the browser of someone who is signed in to our platform, and the work was making sure it can't act as them. At the end I said the part where our assistant writes those pages on its own hadn't shipped yet.
This one is about the server. Sooner or later an agent stops only writing text and starts writing programs, and a program has to run somewhere. Ours does that now, and we run what it writes on our own servers. This is how we isolate that code, how we check the isolation, and what it cost us.
The line is where instructions stop working
The line is where instructions stop working

The line is where instructions stop working. Above it, any input can tell the model what to write. Below it, the kernel and the server decide what the code can do.

Why the code is untrusted

Our example is document generation. The agent writes a Node.js script against pptxgenjs, and the script builds a .pptx. The safe alternative was a fixed set of templates the model only fills with data, where nothing the model writes ever runs. We chose code, because a script can compose whatever the content needs. But nothing below depends on PowerPoint. The same applies to any agent that runs what it writes: data analysis, file conversion, charts, tests.
The model doesn't write that code from nothing. It reads the conversation, files people uploaded, results from the web. Any of those can carry instructions. So the script is untrusted in the same way an uploaded file is, and for the same reason: we don't know who effectively wrote it.
There was a second reason to run code, and in the end it mattered more. An agent that can't see its result works blind: text runs off the edge, boxes land on top of each other. So every run comes back with pictures of what it produced, the model looks, fixes what it sees, and runs again. We do the same for HTML pages: every page the agent writes is opened in headless Chromium at a phone's width, and the model gets pictures of it and the errors from its scripts. That loop only works if running code is cheap and safe to do many times in a row.
Pictures come back in the tool result
Pictures come back in the tool result

Pictures come back in the tool result. RubyLLM sends them the way each vendor takes them, so the same loop runs on all three.

Why not the provider's code execution

All the major model vendors offer some form of this: code the model writes runs in their container. It would have been the fastest route, and we said no to it early.
We run AI work on chains of models from three vendors, with a fallback from one to the next. A feature built on one vendor's container works only on that vendor, and goes down with it. There is also the content itself: the code works on documents people gave us, and we didn't want those executing in a third party's environment.
Files the code needs follow the same rule. When a script needs fonts or images, the model never gets them. It gets a list of what it may use, each file named by a reference like asset://bm_7f3a with a short note, and it writes that reference into the code. Our app swaps the reference for the real file only on the way into the sandbox, so the files go from our storage to our container and nowhere else.
So the execution environment is ours, and any model that can call a tool can drive it. The tool sends the code, our service runs it, and the pictures come back in the tool result. The tools don't know which vendor is on the other end.

What a hostile script would go after

In the HTML article the target was the reader's session. Here there is no session in the room, and the list is different:
  • The network. A script that can open a connection can send whatever it was given (the documents it works on, the files it was given) anywhere it likes. Or it can reach services next to it that were never meant to be reachable.
  • The next run. Builds for different clients go through the same service. Anything a run leaves behind, a file, a process, shared memory, could be read by the run after it.
  • The service itself. It runs other people's code all day. If a script could take it down, every build waiting in line would fail with it.
  • The person who downloads the file. A generated file, a .pptx for one, can point at things outside itself, and PowerPoint fetches them when it opens the file.

A service that holds nothing

The sandbox is a small HTTP service of its own, built from a Debian image with the runtimes the code needs and a headless browser to take pictures of the result. It has two working endpoints and a health check. One takes a script and the files it may use, and answers with the file it built and a picture of every page, or with what stopped it. The other takes an HTML page and answers with pictures of it top to bottom and a list of problems.
What it doesn't have matters more than what it does. No credentials, no database, no state between runs, no published port, no route from the public proxy. We deploy it with Kamal as an accessory next to the job workers, and the app reaches it over Docker's network by container name. If a script got full control of the service, it would find nothing worth stealing in it.
A full escape into the server finds nothing to take
A full escape into the server finds nothing to take

A full escape into the server finds nothing to take. The secrets live in the app, on the other side of one HTTP call.

Boundary one: nothing goes out

The container starts as root, and the first thing it does is close the door:
iptables -F OUTPUT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -P OUTPUT DROP
iptables -I OUTPUT 1 -d 127.0.0.11 -j DROP   # Docker's embedded DNS
Loopback stays open, and so do the replies to connections that came in, since that's how the app gets its answer. Everything else is dropped. The last line is easy to miss: Docker's DNS resolver lives on a loopback address inside the container, so without it a script could still resolve names, and DNS is a channel on its own. Loopback has one more rule on top: the job's user is refused on the service's own port, so a script can't send requests to the service that is running it. Inbound works the same way in reverse: new connections reach only the service's port, so a port a job opens while it runs can't be reached from anything next to the container.
The same rules are set for IPv6. If the container isn't allowed to set its firewall, it refuses to start. A sandbox that comes up without its firewall looks exactly like one that has it, so we'd rather it didn't come up.
Pages get the same treatment inside the browser. Every request Chromium makes is answered by the sandbox itself, with the files the page was given, or refused. Windows the page opens and forms it submits go nowhere either, and WebRTC, which opens UDP sockets of its own, is kept behind the same dead end. An alert or print dialog is dismissed, so a page can't hold the line by waiting for a click.

Boundary two: the job is not the server

The server stays root, because it has to keep the firewall and kill whatever a job leaves behind. The job doesn't. Each script, the check of the file it wrote, LibreOffice and the Poppler tools all run as a separate unprivileged user that has no capabilities and can't gain any.
The container itself runs like this:
docker run \
  --cap-drop ALL \
  --cap-add NET_ADMIN --cap-add SETUID --cap-add SETGID --cap-add KILL \
  --cap-add DAC_OVERRIDE --cap-add CHOWN --cap-add FOWNER \
  --security-opt no-new-privileges \
  --read-only --tmpfs /work:size=1g,exec --tmpfs /tmp:size=256m \
  --memory 2g --pids-limit 512 \
  artifact-sandbox
Every capability is there for one reason: NET_ADMIN for the firewall at start, SETUID and SETGID to start a job as the job user, KILL to stop that user's processes, the last three so the server can hand a job its directory and then read and delete what it wrote. The filesystem is read-only apart from two tmpfs mounts: 1 GB for runs and 256 MB for /tmp. The script sees six environment variables and nothing else.
Runs go one at a time. After every step of a job (the script, the check, LibreOffice, the browser) the server kills every process the job user owns. When the run ends, it also deletes the run's directory, empties /tmp, /dev/shm and /dev/mqueue, and removes the user's System V IPC objects. A script that forks a process into the background and exits gets that process killed along with everything else.
The server never trusts what the script left. It reads the output with O_NOFOLLOW and only if it's a regular file, so an output file that is a symlink to something else is refused, not read. Unpacking and checking the archive happens as the job user too, never in the server's own process. There are limits on everything: 60 seconds for the script, 20 MB for the output, 30 pages, 40 MB for all the pictures of one run together. LibreOffice renders in a directory created after the script is dead, from bytes the server holds, so the script can't plant a profile or a font for it.

What comes out is checked too

A sandbox is usually described by what it keeps in. The file it hands back deserves the same attention, because a third person opens it later, on their own machine.
Take a .pptx as the example. It is a zip of XML files, and a relationship inside it can point outside the file. PowerPoint fetches such targets when the file opens: a picture by URL tells its host who opened the file and when, and a picture on a network share can make the reader's machine try to sign in to that share. So the check refuses any file that points outside itself, except a web or mail hyperlink the reader chooses to click. The check also refuses anything in the file that runs code: OLE objects, packaged files, ActiveX controls, macros. A native chart keeps its own spreadsheet inside the file, and that one is allowed. A file that holds the same part twice is refused outright, because one program may read the first copy and another the last, and so is a file with anything before or after the archive. When fonts are put inside the file after the check, the result is checked again. A page follows the same rule from the other side: it loads nothing from outside.
A few smaller rules came from the same way of looking at it. Sizes are capped and counted as the answer is read, so one build can't make the app hold hundreds of megabytes. A build whose caller hung up is stopped at once instead of keeping the line busy. A caller takes a place in the queue before its request body is read, and when the 8 places are taken, the next one is turned away. And the log line of a run says how many problems it had, never what they said, because a problem can quote the script, and the script can quote the documents it was given.

Testing the walls where they stand

The service has an ordinary test suite that runs on a developer's machine. That suite can't test the sandbox itself: outside the container there is no firewall, no switch to the job user, no cleanup between runs as root.
So the same suite has a second mode. It builds the image, runs it with the production flags next to a neighbouring container, and checks from inside a job that it has no privileges and an environment of six names, can't reach the neighbour over TCP, UDP or IPv6, can't resolve names, can't reach the service's own port, can't be reached on a port it opens, can't read the server's process or files, can write only where it should, and leaves nothing for the next run: no files, no processes, no listeners, no shared memory. A page and the windows it opens are checked the same way. The last check starts the container without the right to set its firewall and expects it to refuse. The CI workflow that publishes the sandbox image runs this mode first, on the same source, and pushes only if it passes. It's slow and needs Docker, so it runs where the image is built, not with the app's own tests.
Same suite, second mode
Same suite, second mode

Same suite, second mode. Slow and needs Docker, so it runs where the image is built, not with the app's tests.
Before writing this I ran it again, and then attacked a fresh container by hand on top of it: HTTPS and raw TCP to the internet, DNS through Docker's resolver and through a public one, a symlink in place of the output, a script that never ends, a file with a picture on a remote host and one on a network share, a malformed request to the service, eleven callers at once, a job listening for connections from next door, a page opening a WebRTC connection, files with an OLE object, with macros, and with a part written twice. Every one ended the way it should: a timeout or a refusal, the endless script stopped at 60 seconds, every one of those files refused at the check, the service still answering, nine callers in line and two turned away. Where a case wasn't covered yet, it got a fix and a test before this went out.

What looking at its own work changed

The security work was the cost. Here is what we got for it.
On the same request, a page written blind kept its risk table as a table, and on a phone the main column ran off the screen inside a scrolling box. No automatic check flags that, because technically nothing is wrong. The page the model had looked at was rebuilt as cards.
It isn't free. A change to a page used to take a few seconds and now takes about a minute, because every change is opened and looked at again, a one-line edit included. I had a quick path that skipped the look for small edits, and we took it out: what a change did is only known after you see it. A run gets at most three looks at a page, so a model can't spend its budget looking, and the budget itself went from 4 minutes to 7. Every picture stays in the model's context, so a long document revised many times costs real tokens. The 30-page ceiling is also a cost ceiling.
Inline scripts in a page are checked with node --check, which parses them without running them. Running them is what the browser in the sandbox is for.

The trade-offs

Runs go one at a time. Two people asking for a build at once wait for each other, and the app gives a build 5 minutes before it calls the sandbox unavailable. For now that's fine. If people start waiting, the plan is several job users running in parallel.
Without the sandbox, nothing gets made. If it's down, the run fails and the assistant says in the chat that it couldn't build the artifact. We'd rather say so than save a page nobody looked at.
It's a hardened container, not a virtual machine. For code that holds no secrets and can reach nothing, that's the trade we chose, and we wrote down when we'd revisit it. When we do, there are two directions. gVisor runs the same container on a kernel of its own in user space, needs nothing new from the host, and costs little extra memory. Docker Sandboxes, which Docker built this year for coding agents, gives every run a microVM with its own kernel and a network that is closed by default. It needs KVM on the host, and a VM per run brings its own kernel and Docker daemon, so it would want a host of its own rather than a place next to the app. Either one would sit under the same service without changing what it does.

If you're about to let an agent run code

Before you pick a provider's hosted container, check whether the feature has to survive that provider being down. Ours did, and that settled it in a day.
Treat the script the way you'd treat an uploaded file, because it is one. The model wrote it after reading things you don't control.
Start from what the service holds, not from what it blocks. A sandbox with no credentials, no database and no state is one where a full escape into the service finds nothing. Then block the network, including the DNS resolver and IPv6, and make the service refuse to start when it can't.
Look at every output format as something a third person will open. The run is only half of it.
And run your isolation tests where the isolation is. A green suite that runs as a normal user outside the container tells you nothing about the firewall.
Happy Coding!
Share: