Skip to main content

A Python decorator that transparently executes functions in isolated microVMs

Project description

pctx-sandbox

Made by

from pctx_sandbox import sandbox

Python 3.10+ License: MIT

A Python decorator that executes untrusted code in isolated Podman containers, designed for safe execution of LLM-generated code on local machines.

Installation

Prerequisites

Install Podman (required for container isolation):

macOS

brew install podman
podman machine init
podman machine start

Linux (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install podman

Linux (Fedora/RHEL)

sudo dnf install podman

Install pctx-sandbox

pip install pctx-sandbox

Quick Start

from pctx_sandbox import sandbox

@sandbox(dependencies=["pandas"])
def process_data(data: list[dict]) -> dict:
    import pandas as pd
    df = pd.DataFrame(data)
    return {"rows": len(df), "columns": list(df.columns)}

# Just call it normally - runs in isolated Podman container
result = process_data([{"name": "Alice", "age": 30}])

How Dependencies Work

The @sandbox decorator handles dependencies automatically:

  1. Specify dependencies using standard pip syntax:

    @sandbox(dependencies=["requests==2.31.0", "pandas>=2.0.0"])
    def fetch_and_process(url: str) -> dict:
        import requests
        import pandas as pd
        # Your code here
    
  2. Dependency caching with warm pools - Dependencies are installed once and reused:

    • Each unique combination of dependencies creates a cached virtual environment
    • A pool of warm workers is maintained per dependency set for instant execution
    • Workers are automatically rotated after 100 jobs or 1 hour
    • Cache key is based on the sorted list of dependencies
  3. Isolation guarantees:

    • Dependencies are installed only in the sandbox environment
    • Your host system remains unchanged
    • Different sandboxes can use conflicting dependency versions
  4. No dependencies omit the parameter:

    @sandbox()
    def safe_computation(x: int) -> int:
        return x ** 2
    

Container Management

The Podman container auto-starts on first use. To manage it manually:

podman ps                                    # Check container status
podman stop pctx-sandbox-agent              # Stop the container
podman rm -f pctx-sandbox-agent             # Remove container
podman rmi -f pctx-sandbox-agent            # Remove image

Development

# See all available commands
make help

How It Works

Container-Based Security Architecture:

Host System
  └── Podman Container (Rootless)
      └── Warm Process Pool
          └── Isolated Python Workers
  1. Container Isolation (Podman): Rootless containers with isolated filesystem, environment, and resources
  2. Warm Process Pool: Pre-initialized workers for fast execution (no cold-start overhead)
  3. Resource Limits: Enforced CPU and memory limits via cgroups
  4. Execution: Functions run with no access to host credentials, files, or processes

Requirements

  • Podman (see installation instructions above)
  • Python 3.10+

Security

Podman Provides Strong Isolation

Podman is a daemonless container engine that provides OCI-standard container isolation with rootless execution by default.

Core Security Mechanisms:

  1. OCI Container Isolation - Complete process isolation using:

    • PID namespace: Containerized processes cannot see host processes
    • Mount namespace: Isolated filesystem, cannot access host files
    • Network namespace: Network isolation (configurable)
    • User namespace: Rootless containers map to unprivileged user on host
    • IPC namespace: No shared memory with host processes
    • UTS namespace: Separate hostname
    • Cgroup namespace: Isolated cgroup view
  2. Rootless by Default - Runs entirely as non-root user, no daemon needed

  3. Cgroups v2 - Enforces resource limits:

    • CPU time limits
    • Memory limits
    • Process count limits
    • I/O bandwidth control
  4. SELinux/AppArmor Integration - Additional security layers where available

Security Validation

All security claims are validated by comprehensive tests in tests/security/test_sandbox_security.py. The test suite covers:

  • Filesystem Isolation: Verifies host credentials (SSH keys, AWS/GCP credentials) are inaccessible
  • Environment Isolation: Ensures host environment variables don't leak into sandbox
  • Network Isolation: Confirms network access is blocked by default
  • Process Isolation: Validates sandbox cannot see or interact with host processes
  • Privilege Isolation: Tests that privilege escalation (root, sudo, chown) is blocked
  • Resource Limits: Confirms timeouts and memory limits are enforced
  • Syscall Filtering: Verifies dangerous syscalls (ptrace, mount) are blocked

Run security tests:

uv run pytest tests/security/ -v

Limitations

Not a Security Boundary (Same as Docker):

Like all container solutions, Podman provides strong isolation but not a perfect security boundary. Linux namespaces were designed for resource partitioning, not security isolation.

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

pctx_sandbox-0.1.0.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

pctx_sandbox-0.1.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pctx_sandbox-0.1.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pctx_sandbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ca36079b3838f79a501b24e3c495c1611b94406b2a29d91f54f57de1c4157b15
MD5 34f6988eb10dceeaf1eae18270137df6
BLAKE2b-256 5931209c8e5215064bbca61ebf5158ded27244785528eaf600091c5c728652ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pctx_sandbox-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pctx_sandbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44d1087e8c0bb69b3461d87fdbec5a7120cd6fead8070997ec31988abd9b4d0c
MD5 908038d1c9a207d6712781dc419ec947
BLAKE2b-256 c8d41b13f575d2017f7a932e0d52ad99964c10d4fa01adb2cacefa1081fc77fa

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