A tool sandbox is more than a subprocess call: three boundaries, measured against 20 hostile inputs
Environment first: Python 3.12.11 on Linux, running as uid 1234 with an effective capability set of zero (CapEff: 0000000000000000). unshare -n is refused by the kernel here (Operation not permitted), so there is no network-namespace data in this article. The sample is 10 crafted paths and 6 command injections, run against a throwaway directory under /tmp. Every number below describes the behaviour of these validators and limiters, not the behaviour of any model.
Boundary one: argument policy, and the three spellings startswith misses
The tool under test is read_file(path), allowed directory /tmp/sbx/ws/allowed, with a secret at /tmp/sbx/secret.txt, a sibling directory allowed_evil/ whose name shares the prefix, and a symlink allowed/link pointing at the secret. Three implementations: V1 joins and reads; V2 joins then checks startswith(ALLOWED); V3 resolves with realpath and compares commonpath.
| Input path | V1, no check | V2, prefix | V3, realpath |
|---|---|---|---|
allowed/notes.txt | ok | ok | ok |
allowed/./notes.txt | ok | ok | ok |
allowed//notes.txt | ok | ok | ok |
secret.txt | missing | deny | deny |
allowed/../../secret.txt | escape | escape | deny |
allowed/../../../etc/hostname | missing | missing | deny |
allowed_evil/p.txt | escape | escape | deny |
allowed/link | escape | escape | deny |
/etc/hostname | escape | deny | deny |
allowed/%2e%2e/secret.txt | missing | missing | missing |
Escape counts: V1 four out of ten, V2 three out of ten, V3 zero out of ten. The three that V2 misses are one problem in three spellings: .. is never normalised, string prefixes treat allowed_evil as a child of allowed, and a symlink does not reveal its real target until it is opened. All three legitimate spellings (./, //, the plain path) pass in every version, so normalisation is not over-blocking — it is blocking the right thing.
V3's whole decision is two lines:
target = os.path.realpath(os.path.join(ROOT, user_path))
if os.path.commonpath([target, os.path.realpath(ALLOWED)]) != os.path.realpath(ALLOWED):
raise PermissionError(target)Note that V3 also rejects the absolute path /etc/hostname, because os.path.join discards the prefix when the second argument is absolute. V2 rejects it too, but only because the string does not start with the allowed directory — an absolute path that happens to start with /tmp/sbx/ws/allowed would pass.
Boundary two: build the command, do not filter it
The same "list a directory" tool, two ways: string concatenation with shell=True, or an argv list with shell=False.
| User argument | shell=True | argv list |
|---|---|---|
. | no leak | no leak |
; cat /tmp/sbx/secret2.txt | leak | no leak |
&& cat /tmp/sbx/secret2.txt | leak | no leak |
| cat /tmp/sbx/secret2.txt | leak | no leak |
$(cat /tmp/sbx/secret2.txt) | leak | no leak |
`cat /tmp/sbx/secret2.txt` | leak | no leak |
Five out of six against zero out of six. Blocking those five spellings with a denylist means covering ;, &, |, $(, backticks, and then newlines, ${IFS}, encoding and escaping on top — while an argv list removes the class outright, because the argument never reaches a parser. Open-ended extensions are named explicitly in OWASP's mitigation list (run a shell command and fetch a URL are the given examples). Split "read one file" and "list one directory" out instead of handing over a shell.
Boundary three: four ways to run away, and a timeout catches one
subprocess.run(timeout=) is documented as killing the child process and waiting for it after the timeout expires. It kills the child, not the process group. The moment a tool is written as sh -c 'cmd & cmd', the grandchild is reparented and keeps running.
| Runaway mode | Without a limit | With a limit |
|---|---|---|
| Write an 8 MB file | succeeds, 8,388,608 bytes | RLIMIT_FSIZE=1 MB: stops at 1,048,576 bytes, OSError [Errno 27] File too large |
| Allocate 1 GB at once | succeeds | RLIMIT_AS=256 MB: MemoryError |
| Busy loop | runs forever | RLIMIT_CPU=(1, 2): return code -24, i.e. SIGXCPU |
sleep 37 & sleep 37, timeout 1s | — | default timeout: 2 sleep processes survive; start_new_session + killpg: 0 survive |
Three of the four limits hold as soon as they are set. The one that gets missed is the fourth: the default timeout kills the parent, and the processes it forked are still spending your money. The fix is start_new_session=True on the child (the docs state this makes setsid() be called) followed by os.killpg(os.getpgid(p.pid), SIGKILL) on timeout. There is a side effect worth knowing: RLIMIT_FSIZE truncates hard, so a tool writing progress logs gets a half-written file and the caller has to treat Errno 27 as a failure rather than ignore it.
Boundary four is not in the code: identity and least privilege
OWASP LLM06:2025 classes this as Excessive Agency, with three root causes: excessive functionality, excessive permissions, excessive autonomy. Several of its mitigations map directly onto engineering work: expose only the minimum necessary extensions, avoid open-ended ones, let extensions act in the user's context with the minimum OAuth scope, put authorisation in the downstream system rather than letting the model decide (complete mediation), and require human approval for high-impact actions.
Against the three boundaries above: argument policy stops "the path was written wrong", command construction stops "the argument was parsed as syntax", rlimit stops "the resource ran away". But "this tool should never have had delete permission" can only be fixed at the credential layer — a read-only token beats a delete-capable tool plus twenty denylist entries.
Three boundaries are not three copies of one
Each layer stops a different class, and none substitutes for another: V3 stops allowed_evil but not ; cat; argv stops ; cat but not an 8 MB write; rlimit stops the 8 MB write but not reading someone else's file. Order matters too. Argument policy goes first because it is cheapest and easiest for a model to walk around — assume the model will always produce a string you did not think of.
One last note on verification: all three matrices here are reproducible with a few dozen lines of script, no real API key required. Checking hostile inputs into the repository next to the tool definition does far more than a sentence in the system prompt asking the model not to touch anything outside the allowed directory.