Run untrusted code safely in hardware-isolated sandboxes
Running user-submitted code is inherently risky. Users might (intentionally or accidentally) submit malicious code, fork bombs, or resource hogs. BoxLite lets you execute arbitrary code in hardware-isolated micro-VMs where even kernel exploits can't escape to your host. Each submission runs in a fresh, isolated environment.
LeetCode-style platforms that evaluate user solutions
Interactive environments like CodePen, Replit, or JSFiddle
Coding bootcamps and tutorial sites with runnable examples
Code execution APIs for third-party integrations
When accepting code from untrusted sources, you need to defend against:
:(){ :|:& };: - crashes systems via process exhaustionimport boxlite
async def run_user_code(code: str, language: str) -> dict:
async with boxlite.SimpleBox(
image=get_image(language),
memory_mb=256, # Limit memory
cpus=1, # Limit CPU
) as box:
try:
result = await asyncio.wait_for(
box.exec("python", "-c", code),
timeout=10.0 # 10 second timeout
)
return {
"success": result.exit_code == 0,
"output": result.stdout,
"error": result.stderr
}
except asyncio.TimeoutError:
return {"success": False, "error": "Execution timed out"} BoxLite provides multiple layers of protection:
| Attack | Protection |
|---|---|
| Fork bombs | Process limits + cgroup constraints inside VM |
| Memory exhaustion | Hard memory limit per sandbox |
| Infinite loops | Configurable execution timeout |
| Network attacks | Isolated network (or disabled entirely) |
| Container escapes | VM boundary (separate kernel) |
| Host filesystem access | No host mounts by default |
BoxLite runs any OCI/Docker image, so you can support any language:
LANGUAGE_IMAGES = {
"python": "python:3.12-slim",
"javascript": "node:20-slim",
"rust": "rust:1.75-slim",
"go": "golang:1.21-alpine",
"java": "openjdk:21-slim",
"cpp": "gcc:13",
}