Python wrapper for Google's nsjail sandboxing tool
Project description
nsjail-python
Python wrapper for Google's nsjail sandboxing tool
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nsjail_python-0.2.0.tar.gz.
File metadata
- Download URL: nsjail_python-0.2.0.tar.gz
- Upload date:
- Size: 76.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
298ce1b1cf5e2c96f113d51c01c3688a8e85b8d71708fd392db5aa0d3abe39fa
|
|
| MD5 |
ccc9c0074ca4e3f33f1e58dd98abe9b5
|
|
| BLAKE2b-256 |
98ae5ed568e5fe524112182ad6146a27a77b3ab3eab42cca5edf52c822c06610
|
Provenance
The following attestation bundles were made for nsjail_python-0.2.0.tar.gz:
Publisher:
publish.yml on teaguesterling/nsjail-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nsjail_python-0.2.0.tar.gz -
Subject digest:
298ce1b1cf5e2c96f113d51c01c3688a8e85b8d71708fd392db5aa0d3abe39fa - Sigstore transparency entry: 1201532772
- Sigstore integration time:
-
Permalink:
teaguesterling/nsjail-python@883175ac0d9ee5ba33b21c259b941a4235efffd1 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/teaguesterling
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@883175ac0d9ee5ba33b21c259b941a4235efffd1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file nsjail_python-0.2.0-py3-none-any.whl.
File metadata
- Download URL: nsjail_python-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
679308572483120e0ec020256555e7e98dce35c22a62e0366fa288faa20bcf42
|
|
| MD5 |
b59143542d4824e8013ed0cd6de0a94d
|
|
| BLAKE2b-256 |
fe8796ac2f5b1f700b9b43bb487cf21a6b855d89190c9870b3bad50db3d45f8b
|
Provenance
The following attestation bundles were made for nsjail_python-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on teaguesterling/nsjail-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nsjail_python-0.2.0-py3-none-any.whl -
Subject digest:
679308572483120e0ec020256555e7e98dce35c22a62e0366fa288faa20bcf42 - Sigstore transparency entry: 1201532780
- Sigstore integration time:
-
Permalink:
teaguesterling/nsjail-python@883175ac0d9ee5ba33b21c259b941a4235efffd1 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/teaguesterling
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@883175ac0d9ee5ba33b21c259b941a4235efffd1 -
Trigger Event:
release
-
Statement type: