Skip to main content

Python script execution with bubblewrap sandboxing

Project description

bubble-sandbox

A Python-specific driver for configuring bubblewrap sandboxes using a specified Python virtual environment, controlling which packages are available.

Each execution runs in a disposable sandbox with the following constraints:

  • no network access
  • PID isolation
  • read-only filesystem (unless host directories are mounted read-write)

This package provides driver commands:

  • bubble-sandbox runs bwrap command with arguments which populate the sandbox filesystem using a specified Python virtual environment before running a Python script in the sandbox. The command can optionally copy host-sytem files into the /sandbox/workspace directory (the working directory in which the script runs).

Quick Start

Build and run:

uv sync
uv run bubble-sandbox exec-script \
  --environment=bare' \
  --script='script=print("hello world")' | jq
{
  "stdout": "hello world\n",
  "stderr": "",
  "return_code": 0
}

CLI commands

bubble-sandbox list-environments

List available execution environments and their dependencies.

uv run bubble-sandbox list-environments

──────────────────────────── Available environments ────────────────────────────

- bare
- pandas-only

bubble-sandbox exec-script

Execute a Python script in a sandboxed environment.

Inline script:

uv run bubble-sandbox exec-script \
  --environment="bare" \
  --script="import sys; print(sys.version_info)"

──────────── Running script: 'import sys; print(sys.version_info)' ─────────────

sys.version_info(major=3, minor=13, micro=12, releaselevel='final', serial=0)

Script stored in a file (same script as above):

uv run bubble-sandbox exec-script \
  --environment="bare" \
  --script-file=/tmp/foo.py

───────────────────────── Running script: @/tmp/foo.py ─────────────────────────

sys.version_info(major=3, minor=13, micro=12, releaselevel='final', serial=0)

bubble-sandbox exec-command

Execute a shell command in a sandboxed environment.

uv run bubble-sandbox exec-command \
  --environment="bare" \
  --workdir "/tmp/test" \
  "pwd && ls -l"

───────────────────── Running shell command: pwd && ls -l ──────────────────────

/sandbox/work
total 8
-rw-rw-r-- 1 1000 1000  4 Apr  7 16:38 baz.txt
-rw-rw-r-- 1 1000 1000 58 Apr  7 16:38 script.py

bubble-sandbox exec-command

Execute a command line (no shell wrapper) in a sandboxed environment.

$ uv run bubble-sandbox execute -w /tmp/bar/ -e bare -- ls -laF

─────────────────────────── Running command: ls -laF ───────────────────────────

total 12
drwxrwxr-x 2 1000 1000 4096 Apr  7 16:38 ./
drwx------ 4 1000 1000   80 Apr  7 18:11 ../
-rw-rw-r-- 1 1000 1000    4 Apr  7 16:38 baz.txt
-rw-rw-r-- 1 1000 1000   58 Apr  7 16:38 script.py

Note: the -- above prevents the CLI from interpreting the -laf as one of its own arguments.

Configuration

All settings are configured via environment variables with the BUBBLE_SANDBOX_ prefix:

Variable Default Description
BUBBLE_SANDBOX_ENVIRONMENTS_DIR environments Path to environments directory
BUBBLE_SANDBOX_WORKSPACE_DIR workspace_data Root directory for session workspaces and volumes
BUBBLE_SANDBOX_MAX_UPLOAD_SIZE_BYTES 10485760 Max total upload size (10 MB)
BUBBLE_SANDBOX_ALLOWED_EXTENSIONS [".txt",".csv",".md",".json",".yaml",".yml",".tsv",".xml"] Allowed file extensions (JSON list)
BUBBLE_SANDBOX_EXECUTION_TIMEOUT_SECONDS 30 Max script execution time
BUBBLE_SANDBOX_SESSION_IDLE_TIMEOUT_SECONDS 3600 Session idle timeout (seconds)
BUBBLE_SANDBOX_MAX_SESSIONS 50 Maximum concurrent sessions
BUBBLE_SANDBOX_ALLOW_PERSISTENT_SESSIONS true Allow clear_data=false on delete
BUBBLE_SANDBOX_LOG_LEVEL INFO Log level (JSON to stdout)

Adding Environments

Each subdirectory in environments/ is a self-contained Python environment with its own virtual environment and dependencies.

1. Create the environment directory

mkdir environments/my-env

2. Add a pyproject.toml

[project]
name = "my-env"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = [
    "requests>=2.32",
    "beautifulsoup4>=4.12",
]

3. Sync the environment

cd environments/my-env
uv sync

This creates a .venv/ directory with the declared dependencies installed.

Note: Avoid using a Python here which derives from another virtual environment: instead, use the "base" environment. E.g.:

uv sync --python="/opt/Python-3.13.12"

4. Use it

uv run bubble-sandbox exec-script \
  --environment="my-dnv" \
  --script='import requests; print(requests.get("http://example.com").status_code)'

Note: network access is disabled in the sandbox by default, so scripts that make HTTP requests will fail. The environment provides the libraries, not network access.

Bundled environments

Name Dependencies Use case
bare (none) Standard library only
pandas-exec pandas 3.0.1+ Data analysis with pandas

Sandbox Isolation

Each script execution is wrapped in a bubblewrap sandbox that provides:

  • Filesystem isolation: read-only bind mounts for system libraries and the selected venv; a writable tmpfs for /tmp; uploaded files available in the working directory
  • User namespace (--unshare-user): runs as an unprivileged user
  • PID namespace (--unshare-pid): cannot see or signal other processes
  • Network isolation (--unshare-net): no network access (loopback only)
  • Session isolation (--new-session): no TTY control
  • Auto-cleanup (--die-with-parent): sandbox is killed if the server process dies

The sandbox working directory and all uploaded files are deleted after execution completes.

Development

# Install dependencies
uv sync

# Sync environment venvs (for local testing) (see note above).
cd environments/bare && uv sync && cd ../..
cd environments/pandas-exec && uv sync && cd ../..

# Run tests (100% coverage required)
uv run pytest

# Lint and format
uv run ruff check
uv run ruff format --check

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

bubble_sandbox-0.8.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

bubble_sandbox-0.8.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file bubble_sandbox-0.8.0.tar.gz.

File metadata

  • Download URL: bubble_sandbox-0.8.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for bubble_sandbox-0.8.0.tar.gz
Algorithm Hash digest
SHA256 6b1a2d86222037c8bb14bcbd5bd68bae2f49d60208d23d0c840f70b9b56c0595
MD5 57373a9621eaa5f50979a2b9102122c8
BLAKE2b-256 edec6c28d4b9a6e0e38b4a318aea486be8a1390af75fa8f4f42ce0864bbda0f1

See more details on using hashes here.

File details

Details for the file bubble_sandbox-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: bubble_sandbox-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for bubble_sandbox-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2612ffcbb95e2c97dc7124ce759608af77825709d8c158abbe29338e2c76c8c
MD5 57ea3f29a00898f1f02027a3a965ee44
BLAKE2b-256 60daa00394cee5bfcc4160ac425366fd37d5a816023ecffa1000eba5575b6271

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