Sandbox environment for executing Python code
Project description
Python Sandbox
This python sandbox uses wasmer to execute untrusted python code in a sandbox. It is expiremental and limited in functionality. This was designed to be used in an assignment autograder.
Components
- Rust WASM layer
- RustPython is used as a dependency in the rust part of this library. RustPython doesn't aim to create sandboxed environments but we can create a simple API for interacting with it while it is being run in a
wasm32-wasi
runtime which would effectively sandbox it. - We use WAI to generate glue implementations that we can use to interop between a regular Python environment and RustPython.
- RustPython is used as a dependency in the rust part of this library. RustPython doesn't aim to create sandboxed environments but we can create a simple API for interacting with it while it is being run in a
- Python Layer
- The Python bindings are also generated with WAI and a helper is provided for setup.
Development
You will need Rust and wai-bindgen
installed from WAI.
- To compile the Rust code to a WASI binary, run
cargo build --release --target wasm32-wasi
- Replace
py_sandbox/py_sandbox.wasm
withtarget/wasm32-wasi/release/py_sandbox.wasm
after building to see changes.
- Replace
- To build new
bindings.py
fromsandbox.wai
, runwai-bindgen wasmer-py --import ./sandbox.wai
Usage
from py_sandbox import create_sandbox
# each sandbox has its own memory.
sandbox = create_sandbox()
# returns stdout/err
stdout = sandbox.exec("""
def hello(x):
return x + 5
""")
# result has both stdout/err and return value
result = sandbox.eval("2+2")
print(result.value.value)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wasm_py_sandbox-0.1.0.tar.gz
(3.5 MB
view hashes)
Built Distribution
Close
Hashes for wasm_py_sandbox-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0610bb2ebde9458385f94a272138660a4be962d059fa97250f905e34439aa6a6 |
|
MD5 | 55773e4d29a6c3d197d8767959195df2 |
|
BLAKE2b-256 | aa122ccb6bb6b20988f1df27d849388f69356639710ab9a676bc4ba469e691d6 |