Engineering a Secure Execution Environment for AI-Generated Artifacts

Engineering a Secure Execution Environment for AI-Generated Artifacts

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.It does now. And a month later it went one step further: the agent writes programs, and we run them on our own servers. This is how we set that up, how we check it, and what it cost us. 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 agent writes a program People kept taking what our assistant produced and hand-formatting it into slides. So we let the assistant build an editable PowerPoint deck from a chat, on the client's brand, and change it when asked. The spec proposed a fixed library of slide layouts: the model fills a JSON structure, our code turns it into slides. It's the safe option, since nothing the model writes ever runs. We went the other way. The model writes a Node.js script against pptxgenjs, and the script builds the .pptx. What people ask for isn't a fixed set of deck types, and a script can compose whatever the content needs, with real text boxes, tables and native charts that stay editable in PowerPoint. The price is obvious. Code a model wrote has to run somewhere. And the model doesn't write it from nothing: it reads the chat, the client's uploaded documents, search results from the web. Any of those can carry instructions. So the script is untrusted in the same way the HTML was, and for the same reason. We don't know who effectively wrote it. There was a second reason for running code, and in the end it mattered more than decks. A model that writes slides blind produces text running off the edge and boxes stacked on top of each other. So every build comes back with a picture of each slide, the model looks, fixes what it sees, and builds again. Normally nothing is saved until it says the slides are clean. If time runs out, the last deck that built is kept, and the assistant says its last fixes weren't checked. Once that worked for decks, we did the same for HTML pages: every page the agent writes is opened in headless Chromium at a phone's width (up to three times a run), and the model gets pictures of it and the errors from its scripts. More on what that changed below. 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 three model vendors we use offer some form of this: code the model writes runs in their environment. It would have been the fastest route, and we said no to it early. Two of the three run Python only, and our deck library is Node, but that wasn't the main reason. Our product runs its 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. We had already decided that every AI feature has to work on all three. There is also the content itself: a deck is built from a client's strategy documents, and we didn't want those executing in a third party's environment. The brand is the other half of that. A branded deck needs the brand's files: logos, and fonts the client has a license for. In a vendor's container those files would have to be uploaded next to the script before it could use them. In ours 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 its size and a note on where the brand uses it, and it writes that reference into the script or the page. Our app swaps the reference for the real file only on the way into the sandbox, so the files themselves go from our storage to our container and nowhere else. The provider does see the slides when the model looks at them, logo included, and that is the point of looking. What it sees is a picture of the slide, not the vector logo or the font files behind it. So the execution environment is ours, and any model that can call a tool can drive it. The tool sends the script, our service runs it, and the pictures come back in the tool result. RubyLLM passes them the way each provider accepts them, inside the result for Claude and Gemini, as a user message right after it for OpenAI. The tools don't know any of that. 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 client's content, the brand files) 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 .pptx 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 Node 22, LibreOffice, Poppler and Chromium. It has two working endpoints and a health check. One takes a script and the brand kit's files and answers with the .pptx, a picture of every slide and each slide's text, 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. 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 brand 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 a deck.pptx 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 deck, 30 slides, 40 MB for all the pictures of one deck 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. A .pptx is a zip of XML files, and a relationship inside it can point outside the file. PowerPoint fetches such targets when the deck opens: a picture by URL tells its host who opened the deck 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 deck 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 deck, 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 the brand fonts are put inside the deck 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 client. 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. 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 deck, a script that never ends, a deck 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, decks 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 decks 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. For decks, the first real run (four slides, one chart, two builds) took about 80 seconds, and the second build was the model fixing what it saw in the first. 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 deck revised many times costs real tokens. The 30-slide 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 decks 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!

Original Source

Read the full article at Hackernoon →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.