Skip to main content

Local Python sandbox using AST rewriting, with optional subprocess isolation and kernel-level enforcement.

Project description

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

For subprocess isolation with kernel-level sandboxing on Linux:

pip install sandtrap[process]

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)

Subprocess

from sandtrap import Policy, IsolatedFS, sandbox

policy = Policy(timeout=5.0, tick_limit=100_000)

with sandbox(policy, isolation="kernel", 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}

isolation="kernel" runs code in a forked child process with:

  • Filesystem restricted to the IsolatedFS root via Landlock (Linux) or Seatbelt (macOS)
  • Syscall filtering via seccomp (Linux) or Seatbelt (macOS)
  • Network blocked at the kernel level (unless the policy enables it)
  • Worker crash doesn't take down the host process

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

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sandtrap-0.1.15.tar.gz (88.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sandtrap-0.1.15-py3-none-any.whl (57.5 kB view details)

Uploaded Python 3

File details

Details for the file sandtrap-0.1.15.tar.gz.

File metadata

  • Download URL: sandtrap-0.1.15.tar.gz
  • Upload date:
  • Size: 88.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for sandtrap-0.1.15.tar.gz
Algorithm Hash digest
SHA256 6d9316b927be35f96cba219829dec717a6141ef6e373232081de95a4fcbb1d9d
MD5 5d56eb726420c23c4cd0663cdc268274
BLAKE2b-256 9d8dccf51ae7e56113bc12f678e88bbffe9381e567c15b17944b22c577de69df

See more details on using hashes here.

File details

Details for the file sandtrap-0.1.15-py3-none-any.whl.

File metadata

  • Download URL: sandtrap-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 57.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for sandtrap-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 ea5978243245ff0734897f2a696021d6cca1e1bf882eb42426d9d80563d741af
MD5 b1f6d9d0e7672e5b6204c8d0818fa55f
BLAKE2b-256 7933454fe8af184f8ec1c8de87bb8c47a338a4447d8872d0e2be763f43d64d11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page