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

Uploaded Python 3musllinux: musl 1.1+ x86-64

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

Uploaded Python 3musllinux: musl 1.1+ ARM64

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

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ ARM64

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

Uploaded Python 3macOS 11.0+ ARM64

zerobox-0.3.3-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.3.tar.gz.

File metadata

  • Download URL: zerobox-0.3.3.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.3.tar.gz
Algorithm Hash digest
SHA256 cb7efb6c48c577619d73f213b67c1b61beb94926474ae8319627a83e7eb9d15b
MD5 42fcb74dd1399bd639add2858b1550a2
BLAKE2b-256 096c1c83730fe4db1aeaa4ecdf9de03ab776b0fb62679d7eb975d29504807842

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zerobox-0.3.3-py3-none-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 415a8e94c4ef9b06d96869c9d5058b6632f0a970a5f8480cddd1ac17b41954a9
MD5 f0f1243804edd53c6b76d9d028f8e56f
BLAKE2b-256 f42f60585b80ccfe4f983867d35378113ee9ca6a7a9889f92ce492bbd61e1260

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zerobox-0.3.3-py3-none-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 69bb74eba97032a8f2fedbec21ed1a6263209889e8826e61944dd28fcb83955a
MD5 26533bf9773db369c6470538c0a1919d
BLAKE2b-256 bc9debc8674334e3259a756f75a260c89b50b71ee22b745abcdf3457fab4a63c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zerobox-0.3.3-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 f542c50a71ea53242effb42fad315662d535a058c230b029b1b6125649583ad6
MD5 be29385d59136fbfcbf7c38180e60a52
BLAKE2b-256 06ea3ade9fc48f79d18482ec5ae6f043c8da40b4fb78453d114c723cd7b29a1a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zerobox-0.3.3-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 7488f322ac82011bac8f5968e9a5aeb86446eb9c1d52bb807025cc3b7590e9ab
MD5 2885198d0f787b76a34a3e3a98c205ec
BLAKE2b-256 933cc23d3101a55f09cc2d8dc1e342aae8189650536a7c094dfeeb2a4a2badbb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zerobox-0.3.3-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ab26032423cc01e5cf1074a67f7557c4b86acdb94222f8b36f1c90a677e5709f
MD5 702f7ec9557ce1a885a36bda88762507
BLAKE2b-256 ca6ce44041a4295e895ff424294ac6a61bcc7afdc7ef3f47b8d8f10d475b8e65

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zerobox-0.3.3-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5d066192b4fc71abfbe574de38548bb7a1d38fe9993dc408832a03b7c6e06ca5
MD5 1f056ec3cd56790c66e51ab1188a5987
BLAKE2b-256 1f51fe4177db8fc0a41a491dd893dd9793ee9692d5b9f0820686335bba4263b4

See more details on using hashes here.

Provenance

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