Code Sandbox
Running candidate code is the one place where untrusted input becomes a process. Two iron rules:
- It must not inherit the host's secrets;
- It must not be able to ask for unlimited time.
Execution flow
Why "clear env + redirect HOME" isn't isolation
Pointing HOME at a temp dir and clearing environment variables is not isolation: a program can still read absolute paths (the profile databases hold interview history, tokens, personal notes) and still open a socket. So OS-level isolation is required.
macOS seatbelt: a targeted profile
On macOS the kernel sandbox (sandbox-exec) is available and cheap. A targeted profile is far safer than a blanket one — interpreters keep working while only the data directory and network are denied:
(version 1)
(allow default)
(deny network*)
(deny file-read* (subpath "<dataDir>"))
(deny file-write* (subpath "<dataDir>"))A few key details:
- Real paths: macOS temp dirs live under a symlink (
/var → /private/var). Seatbelt matches the real path, so denying only the written form protects nothing —protectedPaths()expands viarealpathSync. - Inline
-p, not a shared profile file: a world-readable file in tmp could be swapped between write and exec (TOCTOU), silently downgrading the sandbox. So the profile text is inlined into the arguments. - Switches:
IFG_SANDBOX=offdisables it (debugging);IFG_SANDBOX=requiremakes a missing mechanism a hard error instead of a silent downgrade.
Platform matrix
This is the only platform-dependent layer — the codebase's single process.platform branch. Everything else (process group, cleaned environment, scratch HOME, hidden tests kept out of the candidate directory) holds on every platform:
| Layer | macOS | Linux | Windows |
|---|---|---|---|
| Separate process group + timeout/output caps | ✅ | ✅ | ✅ |
Environment cleared · HOME pointed at a scratch dir | ✅ | ✅ | ✅ |
| Hidden tests never in the candidate cwd | ✅ | ✅ | ✅ |
| Kernel: no network + no profile-dir reads | ✅ seatbelt | ❌ | ❌ |
Where there is no seatbelt, sandboxIsolation() returns {enabled:false, reason:'no seatbelt on this platform (Linux: run under bwrap)'} and both the UI and doctor surface it — the downgrade is visible, it never pretends isolation succeeded. Set IFG_SANDBOX=require if a missing mechanism should be fatal instead.
Child environment
Only the minimal variables needed to start an interpreter are inherited; everything else is dropped — otherwise the program inherits DEEPSEEK_API_KEY and every other secret the host was started with:
keep: PATH · LANG · LC_ALL · TZ · TMPDIR
HOME = temp dir (~/.aws, ~/.ssh unreachable)
TMPDIR = temp dir
NODE_OPTIONS = --max-old-space-size=512
PYTHONIOENCODING = utf-8 · PYTHONDONTWRITEBYTECODE = 1Time & output caps (hardcoded)
| Limit | Value |
|---|---|
Single-run cap MAX_RUN_MS | 10 s (clamped regardless of request; default 3 s, min 500 ms) |
Output cap MAX_OUTPUT_BYTES | 16 MB |
| Compile budget | May exceed the run budget (g++/javac on template-heavy code), but candidate programs stay under MAX_RUN_MS |
Process group: timeout kills grandchildren too
detached: true puts the child in its own process group. On timeout, process.kill(-pid, 'SIGKILL') targets the whole group — spawnSync alone would leave grandchildren running.
Async variant: never blocks the loop
runSandboxedAsync uses spawn rather than spawnSync: the latter blocks the whole host until timeout — one slow program freezes every other request. The async variant keeps the same sandbox, kills the whole process group on timeout, and never blocks the event loop.
Output truncation does Buffer.concat then a single subarray cut, so multi-byte UTF-8 is never split and output stays strictly under the cap.
Four languages & Pyodide
- Languages:
python/javascript/cpp/java; cpp and java are stdin-only (ACM style, read stdin / print output). - Question modes:
function(LeetCode function, args passed),class(e.g. LRU cache, ops sequence),stdin. - Pyodide (browser Python) runs in an isolated subprocess (
pyodide-worker.mjs, 512 MB); the js bridge can't touch the host — it's not eval'd in the main process.
Quota & pool
/code/quota and /code/pool expose the run quota and available runtime pool; when generating questions, Question Smith can only pick from locally available runtimes, so it never produces a question this machine can't run.