sandtrap ⛳
A local Python sandbox using AST rewriting and compiled bytecode execution. Whitelist-based policies control attribute access, imports, and resource usage. Designed as a walled garden for cooperative code (e.g. agent-generated scripts), not for adversarial inputs.
Three isolation levels via the sandbox() factory:
"none"(default) -- in-process, lightweight, shares the host's memory space"process"-- subprocess-backed, crash protection, no kernel restrictions"kernel"-- subprocess + kernel-level isolation (seccomp, Landlock, Seatbelt)
Install
pip install sandtrap
That covers "none" and "process" on every platform, and "kernel" on macOS
(Seatbelt ships with the OS). Kernel mode on Linux needs seccomp and
Landlock bindings:
pip install sandtrap[kernel]
Quick start
In-process (default)
from sandtrap import Policy, sandbox
policy = Policy(timeout=5.0, tick_limit=100_000)
with sandbox(policy) as sb:
result = sb.exec("""
total = sum(range(10))
print(f"total = {total}")
""")
print(result.stdout) # "total = 45\n"
print(result.namespace) # {"total": 45}
print(result.error) # None
print(result.ticks) # 2 (fn calls: sum + print)
In a worker process
from sandtrap import Policy, IsolatedFS, sandbox
policy = Policy(timeout=5.0, tick_limit=100_000)
with sandbox(policy, isolation="process", filesystem=IsolatedFS("/tmp/sandbox")) as sb:
result = sb.exec("""
total = sum(range(10))
print(f"total = {total}")
""")
print(result.stdout) # "total = 45\n"
print(result.namespace) # {"total": 45}
Swap isolation="kernel" for the same thing plus kernel-level restrictions.
Worker-backed isolation
"process" and "kernel" both run code in a separate worker. What that buys,
and what it asks of you:
- A crash stays contained. A segfault or OOM in a C extension costs the call, not your process.
- The worker inherits nothing — not your memory, not your threads. It is forked from a dedicated broker, so a multi-threaded host can't deadlock it (how workers are created).
"kernel"adds a real boundary: filesystem restricted to theIsolatedFSroot via Landlock (Linux) or Seatbelt (macOS), syscall filtering via seccomp or Seatbelt, and network blocked at the kernel level unless the policy enables it.- Your policy must be serializable, since it is sent rather than inherited.
Module grants, module-level functions, and classes cross by name and need
nothing; lambdas, closures, bound methods, and classes defined inside a
function don't.
sandbox()checks at construction and raisesStPolicyNotPortablelisting every problem at once, rather than surfacing pickle's first failure later from inside a worker — see checking a policy is portable, or passstart_method="fork"to keep inheriting memory and accept the deadlock hazard above. - Your entry point must be importable. A worker that inherits no memory
re-imports
__main__, so module-level work there needs anif __name__ == "__main__":guard — and a host run aspython -c, from a REPL, or from a piped heredoc has no importable__main__at all. Run the example above from a file. Servers are unaffected: an ASGI app is imported, not executed as__main__. - Workers cost more than a fork. Each re-imports your granted modules
rather than inheriting them: ~18ms for a stdlib policy, but ~235ms and
~113MB once a heavyweight stack is granted.
preload_grants=Truetrades that back to ~14ms and ~29MB where your grants are safe to import in the broker (the numbers).
Kernel mode is defense-in-depth — a second layer that contains accidental or casual escape (a buggy agent's stray network call, a walk outside the root) under the cooperative-code Python sandbox. It is not a boundary against code actively trying to escape: the inner Python layer isn't adversarial-safe, and the worker→host IPC uses pickle. See the security model for the full picture and the roadmap for hardening plans.
If the platform can't apply the requested kernel restrictions (missing sandtrap[kernel] packages, Landlock-less kernel, unsupported OS), isolation="kernel" fails closed — it raises IsolationUnavailable rather than silently running with no protection. Pass allow_degraded=True to proceed anyway; inspect result.isolation to see exactly what took effect.
Part of the agex stack
sandtrap powers sandboxed code execution in agex, where AI agents write and execute Python directly against host libraries. Filesystem interception is provided by monkeyfs.
Documentation
- Policy & Registration -- configuring what sandboxed code can access
- Sandbox Execution -- running code, results, error handling, REPL-style expression echo
- Process Sandbox -- subprocess isolation with kernel-level restrictions
- Filesystem & Network -- VFS interception, network denial, VFS imports
- Serialization -- pickling functions, classes, and state across turns
- Security Model -- how the sandbox works, what it blocks, threat model
- Roadmap -- planned isolation hardening (restricted deserialization, kernel-mode boundary)
- Forkserver design notes -- why workers stopped being forked from the host, and what it cost
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sandtrap-0.3.3.tar.gz.
File metadata
- Download URL: sandtrap-0.3.3.tar.gz
- Upload date:
- Size: 168.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0e09bc262199adebbacec542173ccc5a887ee690158d0566f47deb124532c3a
|
|
| MD5 |
9473e834cfa742bb48cf7bbe471cbb5d
|
|
| BLAKE2b-256 |
01381f03b503d4a7f4446c543a20947fbfab4040df3e6782e081e491092e4d93
|
File details
Details for the file sandtrap-0.3.3-py3-none-any.whl.
File metadata
- Download URL: sandtrap-0.3.3-py3-none-any.whl
- Upload date:
- Size: 96.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a1dd9267f06ac8d27e9b971931448af6eb04a37d282042cb2e1ed5f41263567
|
|
| MD5 |
a198e12146952ce194977c1daa979b19
|
|
| BLAKE2b-256 |
5279e9f923fcd7d8441585e4d3ce736a717acc23e9d7d5966a57a9c30d1ad7a3
|