100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
AI Guardrails & Safety Engineering
30 minadvanced

Sandboxing Code Execution

A coding agent that runs `exec()` on model-generated Python directly inside the same process serving user requests has handed an attacker who successfully manipulates that agent the same privileges the entire application runs with — the same filesystem access, the same network access, the same environment variables holding production credentials. Every guardrail built in this course up to this point tries to prevent a malicious or hallucinated instruction from reaching code execution in the first place; sandboxing is the layer that assumes, correctly, that eventually one will get through anyway, and asks what the blast radius is once it does.

A sandbox is an execution environment deliberately stripped of everything the code running inside it does not need: no access to the host filesystem beyond an explicitly mounted, isolated directory, no network access unless explicitly granted, no ability to see or affect other processes on the host, and a hard ceiling on CPU, memory, and wall-clock time so a runaway or deliberately resource-exhausting script cannot take down anything beyond its own container. This is least privilege from the previous lesson applied specifically to the one capability that is hardest to bound after the fact — arbitrary code execution — because unlike a scoped API token, a line of running code can attempt literally anything the underlying operating system permits unless something outside the process itself prevents it.

This lesson builds the actual containment primitives a platform engineer would reach for — namespaces and seccomp for process- and syscall-level isolation, gVisor-class sandboxes for a stronger boundary than a container alone provides, read-only mounts, dropped Linux capabilities, and no-network-by-default — not as abstract security theory but as the specific mechanisms that make "the agent's code ran, and it could not have done anything beyond what we explicitly allowed" an engineering guarantee rather than a hope.

Analogy🏏Cricket
🏏 Think of it like cricket: a stadium hosting a rowdy local derby does not simply hope every spectator behaves — it builds physical containment that makes a pitch invasion structurally difficult regardless of any individual spectator's intent: a moat-style perimeter track between the stands and the boundary rope, a fence at a height and angle that resists climbing, and stewards positioned specifically at the shortest, most tempting run to the pitch. None of that containment stops a person from wanting to invade the pitch — it stops the attempt from succeeding even when someone genuinely tries, which is a fundamentally different and more reliable guarantee than trusting crowd behavior alone. A ground that relies only on "we trust our fans, we announced the rules over the PA system" discovers, the first time a genuinely determined fan tries, that a rule with no physical enforcement behind it was never actually a boundary. Sandboxing agent code execution works on the identical logic: it does not try to guarantee the code will never attempt something dangerous — that is what the earlier guardrail lessons in this course are for — it guarantees that even a genuinely successful attempt, a piece of code that really does try to read every file on the host or open a connection to an external server, physically cannot succeed, because the containment around it makes the attempt structurally impossible rather than merely discouraged. The insight is that sandboxing: it is the layer that holds even when every earlier layer of trust has already failed, the same way a stadium's physical containment holds even for the one spectator every announcement failed to deter.
Lesson 14 of 35
0% complete