Skip to main content

Sandbox any command with file, network, and credential controls.

Project description

Zerobox Python SDK

Zerobox PyPI version Zerobox license

Python SDK for zerobox. Sandbox any command with file, network, and credential controls.

pip install zerobox

Installing the wheel drops the zerobox CLI into your environment's bin/ and exposes a Python SDK.

For CLI usage, secrets concepts, the full flag reference, performance numbers, and platform support see the main README.

Quick start

from zerobox import Sandbox

sandbox = Sandbox.create({"allow_write": ["/tmp"]})
print(sandbox.sh("echo hello").text())

Commands

Three ways to run a command. Each returns a ShellCommand you terminate with .text(), .json(), or .output().

Shell

name = "world"
sandbox.sh(f"echo hello {name}").text()

Inline Python

data = sandbox.py("import json; print(json.dumps({'sum': 1 + 2}))").json()

Explicit command + args

sandbox.exec("python3", ["-c", "print('hi')"]).text()

Results

Method On success On non-zero exit
.text() Returns stdout as a string Raises SandboxCommandError
.json() Parses stdout as JSON Raises SandboxCommandError
.output() Returns CommandOutput(code, stdout, stderr) Returns the same shape, never raises
data = sandbox.sh("cat data.json").json()
result = sandbox.sh("exit 42").output()
# CommandOutput(code=42, stdout='', stderr='')

Async API

Use AsyncSandbox in async applications so waiting for the sandboxed subprocess does not block the event loop. The command shape is the same as Sandbox, but creation and terminators are awaited.

from zerobox import AsyncSandbox

sandbox = await AsyncSandbox.create({"allow_write": ["/tmp"]})

text = await sandbox.sh("echo hello").text()
data = await sandbox.sh("printf '{\"ok\": true}'").json()
result = await sandbox.exec("python3", ["-c", "print('hi')"]).output()

Async commands accept the same timeout option:

import subprocess

try:
    await sandbox.sh("sleep 60").text(timeout=1.0)
except subprocess.TimeoutExpired:
    print("cancelled")

Error handling

Non-zero exit raises SandboxCommandError:

from zerobox import Sandbox, SandboxCommandError

sandbox = Sandbox.create()
try:
    sandbox.sh("exit 1").text()
except SandboxCommandError as e:
    print(e.code, e.stderr)

Secrets

Pass API keys that the sandboxed process never sees. The proxy substitutes the real value only for approved hosts.

import os
from zerobox import Sandbox

sandbox = Sandbox.create({
    "secrets": {
        "OPENAI_API_KEY": {
            "value": os.environ["OPENAI_API_KEY"],
            "hosts": ["api.openai.com"],
        },
        "GITHUB_TOKEN": {
            "value": os.environ["GITHUB_TOKEN"],
            "hosts": ["api.github.com"],
        },
    },
})

sandbox.sh('curl -H "Authorization: Bearer $OPENAI_API_KEY" https://api.openai.com/v1/models').text()

See the main README for how placeholder substitution works.

Snapshots

Record filesystem changes and roll them back automatically:

sandbox = Sandbox.create({
    "allow_write": ["."],
    "restore": True,
})
sandbox.sh("npm install").text()

Record without rolling back:

sandbox = Sandbox.create({
    "allow_write": ["."],
    "snapshot": True,
    "snapshot_exclude": ["node_modules"],
})
sandbox.sh("npm install").text()

Cancellation

Pass a timeout (seconds) to any terminator:

import subprocess
try:
    sandbox.sh("sleep 60").text(timeout=1.0)
except subprocess.TimeoutExpired:
    print("cancelled")

Environment variables

sandbox = Sandbox.create({
    "env": {"NODE_ENV": "production"},
    "allow_env": ["PATH", "HOME"],
    "deny_env": ["AWS_SECRET_ACCESS_KEY"],
})

See the main README for what's inherited by default and the CLI equivalents.

Options

Sandbox.create(options) accepts a SandboxOptions dataclass or a plain dict. All fields are optional.

Field Type Description
profile str | list[str] Named profile(s). A list merges left-to-right. Default "workspace".
allow_read / deny_read list[str] Readable / blocked paths.
allow_write / deny_write list[str] Writable / blocked paths.
allow_net bool | list[str] True allows all. A list restricts to those domains.
deny_net list[str] Blocked domains.
allow_all bool Full filesystem + network access.
no_sandbox bool Disable the sandbox entirely.
strict_sandbox bool Fail instead of falling back to weaker isolation.
cwd str Working directory.
env dict[str, str] Explicit env vars.
allow_env bool | list[str] Inherit parent env vars.
deny_env list[str] Blocked env vars.
snapshot bool Record filesystem changes.
restore bool Record and roll back after exit. Implies snapshot.
snapshot_paths / snapshot_exclude list[str] Tracked paths / excluded patterns.
secrets dict[str, SecretConfig] Secrets with per-host scopes.
debug bool Print sandbox config to stderr.

Unknown dict keys (e.g. accidental allowWrite instead of allow_write) raise TypeError at construction time.

Caveats

Sandbox.py(code) runs whichever python3 is on PATH inside the sandbox. If your active interpreter lives outside the sandbox's readable roots (for example uv-managed Pythons under ~/.local/share/uv/), fall back to:

import sys
sandbox = Sandbox.create({"allow_read": [sys.prefix]})
sandbox.exec(sys.executable, ["-c", "print('hi')"]).text()

Other SDKs

License

Apache-2.0

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

zerobox-0.3.2.tar.gz (15.0 kB view details)

Uploaded Source

Built Distributions

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

zerobox-0.3.2-py3-none-musllinux_1_1_x86_64.whl (8.5 MB view details)

Uploaded Python 3musllinux: musl 1.1+ x86-64

zerobox-0.3.2-py3-none-musllinux_1_1_aarch64.whl (8.0 MB view details)

Uploaded Python 3musllinux: musl 1.1+ ARM64

zerobox-0.3.2-py3-none-manylinux_2_17_x86_64.whl (7.1 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

zerobox-0.3.2-py3-none-manylinux_2_17_aarch64.whl (6.7 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

zerobox-0.3.2-py3-none-macosx_11_0_arm64.whl (7.2 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

zerobox-0.3.2-py3-none-macosx_10_12_x86_64.whl (7.7 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

Details for the file zerobox-0.3.2.tar.gz.

File metadata

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

File hashes

Hashes for zerobox-0.3.2.tar.gz
Algorithm Hash digest
SHA256 a944669ed9eb4236d109ee3cc35b952ee36d475749ada67a0d89b4e7f1d754e0
MD5 f134c8ef0c28790caddbc647f3d3f4e0
BLAKE2b-256 76487f765df7cd392aa4f509fc509b2c2a4229fe1019554bd7d81d9ecefe5f36

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2.tar.gz:

Publisher: release.yml on afshinm/zerobox

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

File details

Details for the file zerobox-0.3.2-py3-none-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.2-py3-none-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 27a58a7b12371e215bd9194af6dc23912cbb81868b6479aab033af2bcd4a1700
MD5 88f10d0756b4520c6c4ff33d67606ce5
BLAKE2b-256 c6ce6290349d415f91280f284045fa7886076516e4698b0bb6e64d39b214c56e

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2-py3-none-musllinux_1_1_x86_64.whl:

Publisher: release.yml on afshinm/zerobox

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

File details

Details for the file zerobox-0.3.2-py3-none-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.2-py3-none-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 a5381ca83c6c21af5ae77a5b9bc030eb914a19a151a86b68374a8d78936c6130
MD5 9c29301cda44e8aeb98063b1f3dcec0c
BLAKE2b-256 b755a27d6de43945c3e9e7e9738d3f39fc4733eee7aa0043e56e7332c83d0d57

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2-py3-none-musllinux_1_1_aarch64.whl:

Publisher: release.yml on afshinm/zerobox

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

File details

Details for the file zerobox-0.3.2-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.2-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c31977ada2ea06209fa770138a36292fd108c7811d6891c80aeccebe10ea749f
MD5 b8d5b7dd28c71710ba71b24b94082faa
BLAKE2b-256 2740e10675eb99022c2ea75e716ff79f483c9f0de2fe8ce190df849dc3d66f86

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2-py3-none-manylinux_2_17_x86_64.whl:

Publisher: release.yml on afshinm/zerobox

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

File details

Details for the file zerobox-0.3.2-py3-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.2-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 f838dc191a5c08a9a09667c1f4022b8aa0f75f8e418f9990d330dc4b7dacc172
MD5 795db059992e63c78c48258c2edfb2e2
BLAKE2b-256 41cc62f0f3b02bf1d5093a938cae56bd8d23d92bd755464c1a4f4c65937af80f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2-py3-none-manylinux_2_17_aarch64.whl:

Publisher: release.yml on afshinm/zerobox

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

File details

Details for the file zerobox-0.3.2-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.2-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d13c107253e3c34c76cd498630071db2116b3f53f5876b0002cdccd7c88a0262
MD5 ce6aed66e47963371a3e685d3fca1996
BLAKE2b-256 ead180a7f9e500bebe6db2cc08fc9ca02edc165b34739cc63c5f6be3b9c6a778

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2-py3-none-macosx_11_0_arm64.whl:

Publisher: release.yml on afshinm/zerobox

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

File details

Details for the file zerobox-0.3.2-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.2-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0a453b86a3a2edcabc40209e7d2a67866433589ea56ee0b945fb72f8ce7a46b1
MD5 00416f4d7532bbfee6f0b95c1cdc3797
BLAKE2b-256 56fee8003b6d286b62867f58cb4e81fe4fc8f5d5248b068729ec9f9d368f0672

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.2-py3-none-macosx_10_12_x86_64.whl:

Publisher: release.yml on afshinm/zerobox

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