How do you sandbox an agent and enforce least-privilege tool access?
1.2k views
63 of 83
1
The problem
Question presented to candidate:
"You are giving an agent real tools: a shell, file access, the network, and API tokens for GitHub and your database. Assume the model will sometimes be wrong and sometimes be manipulated by text it reads. How do you sandbox it and enforce least privilege so that a bad step cannot do much damage?"
What a strong answer should cover:
The threat model: assume the agent can be prompt-injected or simply mistaken, so security must hold even when the model does the worst thing its tools allow; instructions in the prompt are not controls
Layers of control in the runtime: expose only the tools a task needs, check every call against an allowlist on parsed arguments (paths, hosts, commands), scope credentials, isolate execution, and audit everything
Execution isolation choices and trade-offs: OS-level sandboxes, containers, user-space kernels and microVMs; an ephemeral filesystem; resource limits; deny-by-default network egress with a host allowlist
Credentials: short-lived, per-task, narrowly scoped tokens, ideally injected by a broker so the agent never holds the secret; no shared admin keys
Correct implementation details: normalise paths before checking, parse URLs to a hostname, parse commands into argv and reject shell metacharacters, block cloud metadata endpoints
Where approval fits: irreversible or outward-facing actions go to a human, but sandboxing is what lets routine actions run without constant prompts
Clarifying questions expected:
Does the agent execute code it writes (shell, interpreter, browser), or only call fixed APIs?
Whose data and credentials are involved, and is it multi-tenant?
What network access does a typical task genuinely need?
Code / implementation expected: Optional โ a policy check for paths, hosts or commands, or a description of the isolation stack.
sandboxing
least-privilege
security
agents
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Engineers giving agents real tools (shell, files, network, API tokens) and candidates facing the "how do you keep an agent safe" question.
Difficulty: Hard
Verified with: Node 22.22.0 and Python 3.12.3 (both examples, identical output). Vendor references (Claude Code sandboxing docs, MCP SDK source comments, Simon Willison on the lethal trifecta) were checked by web search or by reading the published packages in October 2026.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. All program output shown was produced by actually running the examples; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
A hotel gives you a keycard, not a master key. Your card opens your room and the gym, only until checkout, and
The solution is hidden so you can try first
16 min read ยท 12 sections
4
Read the code
Path, egress and credential checks vs naive string tests