Part 9 of 13 · 2 min read
Answering sandboxing questions in an interview
How would you let an agent run code safely?
"In a disposable isolation boundary, a container or microVM, with a non-root user, read-only root, scratch directory only, no network or an allowlisted proxy, a scrubbed environment and limits on CPU, memory, file size, processes and wall-clock time. I check tool arguments in code too, but I treat that as a layer, not the wall." Follow-up: "Are rlimits enough?" No: in the example's tests they stopped a loop, a memory bomb and a big write, but a limited process can still read files and open connections; only the boundary stops that.
How do you prevent path traversal in a file tool?
"Resolve the real path of the root and of the requested path (following symlinks), then check with commonpath that the target is inside the root, and reject null bytes. String startswith fails on prefix collisions like work-evil and on symlinks; in the example's test it allowed 8 of 10 escapes." Follow-up: "What about races?" A symlink can be swapped between check and open; I would open relative to a directory descriptor with O_NOFOLLOW, or rely on the sandbox so the swap does nothing.
How do you restrict which URLs an agent can fetch?
"Parse the URL, require https, compare the exact host with an allowlist, and refuse userinfo, IP literals and unusual ports. Re-check after redirects and resolve DNS myself, or block private ranges in the network layer, because a hostname can resolve to an internal address." Follow-up: "Why not check for the domain in the string?" https://evil.test/?x=good.test and https://[email protected] contain it; the example's naive check allowed 80 percent of such tricks.
What is the difference between a sandbox and a validation layer?
"Validation tries to recognise bad input, which fails against inputs you did not think of. A sandbox limits what any input can do, so a missed attack has bounded effect. I use both, and design so the sandbox holds when validation fails." Follow-up: "Which is more reliable?" The boundary, because it does not depend on parsing correctness.