Skip to main content

Hardened Python sandbox for running untrusted code — subprocess, seccomp, landlock strategies with Z3 formal verification.

Project description

hydra-sandbox

PyPI Python License Tests Coverage

Hardened Python execution sandbox for running untrusted code.

Built for developers building LLM agents, code review bots, and online coding platforms who need to execute user-submitted or model-generated code without risking the host system.

Features

  • Multiple isolation strategies — subprocess (cross-platform), seccomp (Linux syscall filtering), landlock (Linux filesystem sandbox)
  • Import guard — blocks dangerous stdlib modules (subprocess, ctypes, socket, multiprocessing, etc.)
  • Network isolation — monkey-patches socket.socket to block outbound connections
  • Resource limits — CPU time, memory, and file descriptor caps (POSIX)
  • Secret purging — strips API keys and tokens from the child environment
  • AST signature verification — validates function name and parameter lists before execution (zero latency, no LLM cost)
  • Z3 formal verification — optional pre/post-condition checking with mathematical proofs

Installation

pip install hydra-sandbox

# With optional dependencies
pip install hydra-sandbox[verify]       # Z3 formal verification
pip install hydra-sandbox[seccomp]      # Linux seccomp support
pip install hydra-sandbox[landlock]     # Linux landlock support
pip install hydra-sandbox[all]          # everything

Quick start

from hydra_sandbox import execute_python

# Run untrusted code in an isolated subprocess
result = execute_python(
    "print(1 + 1)",
    timeout=5,
)

print(result.success)    # True
print(result.stdout)     # "2"
print(result.exit_code)  # 0

Strategy selection

# Auto-detect the strongest available strategy (default)
result = execute_python(code, strategy="auto")

# Explicit strategies
result = execute_python(code, strategy="subprocess")      # cross-platform
result = execute_python(code, strategy="seccomp")          # Linux only
result = execute_python(code, strategy="seccomp+landlock") # strongest

AST verification

from hydra_sandbox import verify_ast_signature, extract_expected_signature

# Verify a function signature without executing
error = verify_ast_signature(
    "def add(x, y): return x + y",
    "add",
    ["x", "y"],
)
print(error)  # None — signature is correct

# Extract expected signature from a description
sig = extract_expected_signature(
    "Write function 'process(data: bytes, key: str) -> str'"
)
print(sig)  # ("process", ["data", "key"])

Z3 formal verification

from z3 import Int
from hydra_sandbox.verify import check_spec, VerificationSpec

x, y = Ints("x y")
spec = VerificationSpec(
    precondition=x > 0,
    postcondition=x + y > y,
)
result = check_spec(spec)
print(result.valid)  # True — mathematically proven

Security

See SECURITY.md for the threat model and supported versions.

The subprocess strategy provides Python-level isolation suitable for most use cases. For production deployments handling truly untrusted code, use seccomp+landlock on a Linux host with additional hardening.

Telemetry

hydra-sandbox includes optional, opt-in telemetry. It is disabled by default and sends nothing unless you explicitly enable it.

When enabled, it sends exactly two data points ONCE per process lifetime:

  • Package version (__version__)
  • Strategy selected (subprocess, seccomp, etc.)

No user code, no environment variables, no PII, no IP address logging.

export HYDRA_SANDBOX_TELEMETRY=1   # opt in
export HYDRA_SANDBOX_NO_TELEMETRY=1  # explicitly opt out (default)

License

MIT — see LICENSE for details.

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

hydra_sandbox-0.1.0.tar.gz (49.5 kB view details)

Uploaded Source

Built Distribution

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

hydra_sandbox-0.1.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file hydra_sandbox-0.1.0.tar.gz.

File metadata

  • Download URL: hydra_sandbox-0.1.0.tar.gz
  • Upload date:
  • Size: 49.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hydra_sandbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d1d4e8e9493d6a7173fa7ee84e7a87a2dc24820c0a0d303c2687dfbb2b9515ab
MD5 06fc24d8d6f3cebae7eec4d459fa42d6
BLAKE2b-256 7d4b0948ac7dc782bf62e2ed1a1811925761f3c728cf48d6a0aaf8bc02761f6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hydra_sandbox-0.1.0.tar.gz:

Publisher: publish.yml on akaradje/hydra-sandbox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hydra_sandbox-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hydra_sandbox-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hydra_sandbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e57e2f5f1b55beb2a452c9efc2a49953401d4c45519d7d3b7a71c256b74614c6
MD5 9416cd65d7d577bb7a54370db433204a
BLAKE2b-256 70db3a0d99c10083f80a1bf7a578009b29c44d29fcc2efbdb428c555b181810a

See more details on using hashes here.

Provenance

The following attestation bundles were made for hydra_sandbox-0.1.0-py3-none-any.whl:

Publisher: publish.yml on akaradje/hydra-sandbox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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