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.1.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.1-py3-none-musllinux_1_1_x86_64.whl (8.4 MB view details)

Uploaded Python 3musllinux: musl 1.1+ x86-64

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

Uploaded Python 3musllinux: musl 1.1+ ARM64

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

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ ARM64

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

Uploaded Python 3macOS 11.0+ ARM64

zerobox-0.3.1-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.1.tar.gz.

File metadata

  • Download URL: zerobox-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 253fbf314ac4d6769ad270e9d4d50d08f924704a36812c58f3dcd16b356408c8
MD5 9fd22eeadedcfa011b2ba523ca10b8cd
BLAKE2b-256 d3a49a711acb5074b50524e34fda4b539fc5f22bde483f29fbfb6c1e79d2e0d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1.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.1-py3-none-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.1-py3-none-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f1b53c2aac921be3f062da069f70b65d3ee5326b99bbbaaa13094a93ec701fbb
MD5 ce4796d5540d7853202f3ed1c3fea36d
BLAKE2b-256 f71336122fc11098631c3076297c7a7a01ff5c9f2444054d4c55d0745857109d

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1-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.1-py3-none-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.1-py3-none-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 47969ab61d7fd6539ea3ae354f39e7feda4062dae1901ef1a5cbed84a95da0c4
MD5 7dda3565044eb93bb1752815b5a57c40
BLAKE2b-256 34f26bdecde14120a2a6b42250654de266bd5f8329db08776e6e82e3e62068c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1-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.1-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.1-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 277059d449f475e5853f453d87ac54f4bb274d2bdd0c376812eb1ef2b83795c7
MD5 f7534c64d13fbdfd74caf13e724b02d5
BLAKE2b-256 447d976854f672d033977c46215085deeab9854414dc0a0662c72118bc20baea

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1-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.1-py3-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.1-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 1683e9e1b3c399d14db469f8aabb15c7961c340642a2bc8c0950a1fa62da14a5
MD5 2fa8866e323e85b2eb3146c316158a0d
BLAKE2b-256 a7a8c6bbbdce99ea2c14f017ae699f251423ac1e5835b2764fd405a8e0f58459

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1-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.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b07511ae02a428f92474d5883562378d6997d03db8e333479002d29b846bb1c8
MD5 01bf62176033dca108bf06f52fc3d252
BLAKE2b-256 f4dbf2c7767deb60b288c57f1043c722e8daccafe49327a61ce343192e222ba4

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1-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.1-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zerobox-0.3.1-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 433afb07ec5be1d6ff24d5f24ed9b37a07c1640708fba213d57263e332216df4
MD5 47f72d7d4d6cdc8252c9dc0cad7c2dea
BLAKE2b-256 b2e737a0c8ba8ca031d814f60a6089c5a90af81dafe6149738ee7aa4363d51ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for zerobox-0.3.1-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