Skip to main content

Python wrapper for Google's nsjail sandboxing tool

Project description

nsjail-python

Python wrapper for Google's nsjail sandboxing tool

PyPI Python Version License: MIT

Installation

# Core library only (expects nsjail on PATH or installed separately)
pip install nsjail-python

# Include pre-built nsjail binary (Linux x86_64/aarch64)
pip install nsjail-python[binary]

# Build nsjail from source during install (needs gcc, make, protoc, etc.)
pip install nsjail-python[build]

# Add protobuf validation support
pip install nsjail-python[proto]

Quick Start

Low-level: NsJailConfig dataclass

from nsjail import NsJailConfig, MountPt, Exe

cfg = NsJailConfig(
    hostname="sandbox",
    time_limit=30,
    mount=[MountPt(src="/", dst="/", is_bind=True, rw=False)],
    exec_bin=Exe(path="/bin/sh", arg=["-c", "echo hello"]),
)

Mid-level: sandbox() preset

from nsjail import sandbox

cfg = sandbox(
    command=["python", "script.py"],
    memory_mb=512,
    timeout_sec=60,
    writable_dirs=["/workspace", "/tmp"],
)

High-level: Jail() fluent builder

from nsjail import Jail

cfg = (
    Jail()
    .sh("pytest tests/ -v")
    .memory(512, "MB")
    .timeout(60)
    .readonly_root()
    .writable("/workspace")
    .writable("/tmp", tmpfs=True, size="64M")
    .no_network()
    .build()
)

Serialization

from nsjail.serializers import to_textproto, to_cli_args, to_file

# Protobuf text format (for --config flag)
print(to_textproto(cfg))

# CLI arguments
args = to_cli_args(cfg, on_unsupported="skip")

# Write to file
to_file(cfg, "sandbox.cfg")

Running nsjail

from nsjail import Runner, Jail

runner = Runner(
    base_config=Jail()
        .command("python", "-m", "pytest")
        .memory(512, "MB")
        .timeout(300)
        .readonly_root()
        .writable("/workspace")
        .build(),
)

result = runner.run(extra_args=["tests/unit/", "-x"])
print(result.returncode, result.stdout)

Documentation

Full documentation is available at nsjail-python.readthedocs.io.

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

nsjail_python-0.4.0.tar.gz (112.1 kB view details)

Uploaded Source

Built Distribution

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

nsjail_python-0.4.0-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file nsjail_python-0.4.0.tar.gz.

File metadata

  • Download URL: nsjail_python-0.4.0.tar.gz
  • Upload date:
  • Size: 112.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nsjail_python-0.4.0.tar.gz
Algorithm Hash digest
SHA256 dbb0be3b550096df3e3ef330bca06e62d5c3e991ea6ef454c4e43c2b47b0c93a
MD5 92d28595073f1f462c1b8496a886fd2a
BLAKE2b-256 6205ce649425a25c4e0489b2e62f8dd9ea6fcc283b09c4af7b28fef451bbaf60

See more details on using hashes here.

Provenance

The following attestation bundles were made for nsjail_python-0.4.0.tar.gz:

Publisher: publish.yml on teaguesterling/nsjail-python

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

File details

Details for the file nsjail_python-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: nsjail_python-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nsjail_python-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9bbb556bd829403b811117b83b902818b533bff35e1959f7270f968f37b4ab03
MD5 68fd5dfdcb5701a8a7ec9fe4e74dc27b
BLAKE2b-256 dd4b16c4fba1e49c88e8cfe04394d513946293aad941b2bc08e4152c9c826743

See more details on using hashes here.

Provenance

The following attestation bundles were made for nsjail_python-0.4.0-py3-none-any.whl:

Publisher: publish.yml on teaguesterling/nsjail-python

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