Building a Code Runner with Piston
How a website can safely run a stranger's code: the danger, the idea of a sandbox, and the one tiny footgun that quietly breaks everything.
By Daniel Chiwuzoh ยท
A stranger hands you an envelope
Imagine you run a small office, and a stranger walks in, hands you a sealed envelope, and says: "Inside are instructions. Please follow them exactly, then tell me what happened."
You don't know this person. You could read the instructions in advance, sure, but you have thousands of strangers a day and no time to vet each one. The instructions might say "add these two numbers." Or they might say "open the filing cabinet, photocopy every customer's bank details, and mail them to this address." Or, more mundane but just as ruinous: "stand here counting to infinity and never stop working."
Following a stranger's instructions, sight unseen, is exactly what a website does when it offers to run your code. And on my apex interviewer, that is precisely the feature I wanted: you type a solution into the browser, hit Run Tests, and the site executes your program against some example cases and tells you whether it passed.
The moment I decided to build that, I had signed up to follow sealed envelopes from strangers. So the entire engineering problem becomes a single question: how do you follow a stranger's instructions without letting them burn your office down?
Why running code is so dangerous
Let me make the danger concrete, because it is easy to wave at and hard to feel.
A program isn't a passive document like a photo. It is a list of actions, and by default those actions run with the same powers as the program that launched them. If your website's program can read the database, then code your website runs can, by default, also try to read the database. If your server can reach the open internet, so can the stranger's code. The envelope's instructions inherit the keys to your building.
Untrusted code, given the run of the machine, can do roughly four nasty things. It can read things it shouldn't, like your secrets, your other users' data, the keys to everything else. It can break things, so delete files, corrupt the database. It can reach out and use your server as a launchpad to attack someone else, or phone home with whatever it stole. And it can simply refuse to stop: an infinite loop or a memory hog that ties up the machine until nothing else works, no malice required, just a bug.
So I can't just run the envelope's contents in my office. I need a special room.
The special room: a sandbox
The idea that solves this is a sandbox, and the name is well chosen. A child's sandbox lets them dig, build, and knock things down freely, but the mess stays in the box. The sand doesn't get into the house.
A software sandbox is a deliberately sealed room where I can run a stranger's code while guaranteeing four things about that room:
It's locked from the outside. The code can't reach my database, my secrets, or my other programs. The room has no doors into the rest of the building.
It has no phone. The code can't reach the internet, so it can't call out to steal or attack. (No network access at all.)
It has a timer on the door. If the code runs too long, the room is emptied by force. The infinite loop gets a hard stop.
It has limited air. The code gets a fixed ration of memory. It can't suffocate the whole machine by demanding everything.
Build a room with those four properties, drop the stranger's instructions inside, let them do whatever they like, read the result through a slot in the door, then burn the room to ash and build a fresh one for the next stranger. That is how you safely run code you have never seen.

Now, I could try to build such a room myself, from scratch. This would be a deep and treacherous project, because the history of computing is littered with sandboxes that turned out to have a hidden window someone climbed out of. Happily, someone has already built a very good room and given it away for free.
Piston: a room someone already built
Piston is an open-source code-execution engine. Its entire reason to exist is to be that sealed room. You hand it a chunk of code, a language to run it in, and some input to feed the program. It runs the code under exactly the restrictions above (strict time limits, strict memory limits, no network) and hands you back what the program printed, plus whether it succeeded, failed, or got killed for running too long.
My website never runs the stranger's code itself. It is the receptionist. It takes the envelope and slides it through a slot to Piston, which does the dangerous part in its sealed room and slides the result back. My code's job is just to ask the question politely and interpret the answer.
That asking is an ordinary web request. My app bundles up the code into a small message ("here is the language, here is the program, here is the input it should read, and here is how long you are allowed to take") and sends it to Piston's /execute address. Piston runs it and replies with the program's output. My code then compares that output to the expected answer and decides: pass or fail.
I added one more guard rail on my own side, and it is a small lesson in not trusting any single safety net. Piston enforces its own time limit inside the room. But what if Piston itself hangs, or the network to it drops? My request would wait forever, and a waiting web request is its own kind of stuck machine. So I set a second timer on my side, deliberately a little longer than Piston's. Long enough that a healthy Piston always gets to return its own clean timeout result first, but short enough that a truly dead Piston doesn't leave me hanging forever. Two timers, the outer one only ever firing if the inner one failed to. Belt, and a slightly looser belt over the belt. I hope at this point you are not confused ๐.
The footgun: a version number that has to match exactly
Here is the part I most want you to remember, because it is the kind of bug that produces no smoke, no alarm, just a quiet, baffling failure.
A programming language isn't one fixed thing. It comes in versions (Python 3.10, Python 3.11, and so on) and they differ in small ways. To get reproducible results, where the same submission always behaves the same, I pin an exact version for each language. My code keeps a little table: Python means exactly 3.10.0, JavaScript means exactly 18.15.0, Java means exactly 15.0.2, and so on for the six languages I support. When my app talks to Piston, it doesn't say "run this in Python." It says "run this in Python 3.10.0, precisely."
Now here is the trap. Piston ships completely empty. Out of the box it has no languages installed at all, not even one. You install the ones you want, at the exact versions you want, after it is running. And here is the sharp edge: when my app asks for "Python 3.10.0" and that exact version isn't installed, Piston does not helpfully fall back to whatever Python it happens to have. It just refuses. The request fails. That language is simply broken, with an error that points at nothing obvious.
So there are now two lists of version numbers that must agree: the table inside my app that says what to ask for, and the set of versions actually installed inside Piston. If those two lists drift apart by even a single digit, so my app asks for 3.10.0 but the server only has 3.10.1, that language quietly stops working. Nothing catches fire. It just fails, and you sit there staring at it ๐.

I keep the two lists glued together with intent and comments. The app's version table carries a note pointing straight at the install step, and the install step carries a note pointing straight back, each saying in effect: if you change me, change the other one or you will break a language. When I set up a fresh Piston, I run a one-time installer that walks through that exact list (Python 3.10.0, Node 18.15.0, TypeScript 5.0.3, Java 15.0.2, the C++ compiler at 10.2.0, Go 1.16.2) and installs each, waiting first for Piston to be ready to listen. It is safe to run again, it just confirms what is already there. But the two lists must stay in lockstep, and that fragile little agreement is the single thing most likely to bite whoever runs this next, including future me.
Letting strangers in the front door, safely
There was one last wrinkle. The website that uses this code runner lives on a different host than Piston because of security reasons. So Piston has to be reachable over the public internet for my app to reach it at all.
That made me nervous, and it should make you nervous too. I have just spent this whole article explaining that Piston runs untrusted code, and now I am putting its front door on the public street, where anyone can knock? Couldn't a passer-by just start shoving their own envelopes through the slot, using my carefully-built room (and my electricity, and my memory) to run whatever they want, all day long?
The clean answer would be to only accept requests from my app's address and slam the door on everyone else. But my app runs on a serverless platform whose outgoing address changes constantly, so there is no single fixed knock to recognize. An address-based lock would lock out my own app along with the strangers.
So instead I put a password on the door. Every request to Piston has to carry the right credentials, a username and a long random token, and anything arriving without them is turned away before it reaches the room at all. This is plain HTTP Basic authentication, the oldest password scheme on the web, and here it is exactly enough: my app knows the password, nobody else does, and the front door checks it on every single knock. The password check is handled right at the entrance, by the same front-door web server that provides the automatic HTTPS padlock, so an unauthenticated request never even gets near the engine. Knock without the password and you get a polite "no" and nothing else. I hope you are still with me ๐ฅฑ.
And there is one more layer underneath, which is really the foundation the whole thing rests on. Because the code inside genuinely is dangerous, I keep Piston physically apart from anything precious. It runs in a heavily-privileged container, the kind that, to do its sandboxing job, has to give up some of the host's own protective armor, and a container with its armor off is not something you ever want sitting next to your production database and your real customers' data. If a stranger ever did manage to climb out of the sealed room, I want them to land in an empty parking lot, not in the vault. So Piston lives on a throwaway machine that holds nothing valuable: no customer data, no production secrets, nothing worth stealing. The sandbox is strong. But I build as though, one day, it won't be.

The whole idea, in one breath
Running a stranger's code is following a sealed envelope's instructions sight unseen: useful, and genuinely dangerous. The defense is a sandbox, which is a sealed room, locked from the outside, no phone, a timer on the door, limited air. Rather than build that room myself I rented Piston, a ready-made one, and my app just slides envelopes through the slot. The one quiet footgun is that the version I ask for and the version that is installed have to match to the last digit, or a language breaks with no warning. And because the room can never be trusted completely, I gate its public door with a password and keep it standing well away from anything I would hate to lose.
Strangers hand me envelopes all day now. They follow their instructions in a room with no windows, on a machine that holds nothing worth taking, and then the room is swept clean for the next one. That is the whole trick, and once you see it as a room rather than a magic spell, it stops being scary and starts being just careful.
The simple believeth every word: but the prudent man looketh well to his going. - Proverbs 14:15