# SafeRun
**SafeRun** is a highly secure sandbox library for executing Python code in an isolated environment. Designed to prevent potentially dangerous operations, SafeRun leverages advanced techniques such as AST analysis, resource limitations, chroot jail, privilege dropping, and (optionally) seccomp syscall filtering to offer robust protection while running user code.
> **Note:**
> While SafeRun applies multiple layers of security to restrict malicious behavior, due to the dynamic nature of Python, no sandbox can be 100% foolproof. Use with caution in sensitive environments.
---
## Features
- **Comprehensive AST Checks:**
Analyzes the abstract syntax tree (AST) of the code to disallow dangerous constructs such as `import`, `eval`, `exec`, and access to dunder attributes.
- **Resource Limitations:**
Uses Python's `resource` module to restrict CPU time and memory usage during code execution.
- **Isolated Execution Environment:**
Executes code in a separate process to prevent interference with the main application.
- **Filesystem Isolation (chroot):**
Optionally isolates the filesystem by changing the root directory to a secure, pre-configured directory.
- **Privilege Dropping:**
Reduces process privileges (typically to the `nobody` user) after chroot to minimize risk.
- **Optional Seccomp Filtering:**
If available, applies syscall filtering using the `seccomp` library to restrict system calls further.
---
## Installation
SafeRun is available on PyPI and can be installed using pip:
```bash
pip install SafeRun
Usage
SafeRun can be used in two ways:
1. As a Command-Line Tool
You can run SafeRun directly from the command line using the -m flag. For example, to securely execute a Python file example.py:
python -m SafeRun example.py --time 2 --memory 50 --chroot /tmp/sandbox_chroot
--time: Maximum CPU time (in seconds) allowed for the execution.--memory: Maximum memory (in MB) allowed.--chroot: The chroot directory to isolate the file system (this directory must be pre-configured and secured).
2. As an Importable Library
You can also import and use SafeRun in your Python projects to programmatically execute code in a secure sandbox:
from SafeRun import UltraSecureSandbox
# Create an instance of the sandbox
sandbox = UltraSecureSandbox(
time_limit=2, # CPU time limit in seconds
memory_limit_mb=50, # Memory limit in MB
chroot_dir="/tmp/sandbox_chroot" # Chroot directory (must be pre-configured)
)
# Execute a Python file securely
sandbox.execute_file("example.py")
# Alternatively, execute code provided as a string:
code = """
print("Hello from within the sandbox!")
for i in range(5):
print(i)
"""
sandbox.execute_code(code)
How It Works
-
AST Analysis:
Before executing any code, SafeRun parses the code into an AST and scans for prohibited constructs such as dangerous function calls (eval,exec, etc.) and import statements. If any are found, the execution is aborted. -
Resource Limits:
Using theresourcemodule, SafeRun sets strict limits on the amount of CPU time and memory the sandboxed process can use. This helps prevent denial-of-service attacks or accidental resource overuse. -
Chroot and Privilege Dropping:
The sandboxed process is moved to a restricted filesystem environment usingchroot, and its privileges are lowered (typically to thenobodyuser). This minimizes the potential impact of malicious code. -
Optional Seccomp Filtering:
When theseccomplibrary is available, SafeRun further secures the process by only allowing a minimal set of system calls needed for execution. Any disallowed system call results in immediate termination. -
Isolated Process Execution:
Code is executed in a separate process via Python'smultiprocessingmodule, ensuring that any faults or crashes remain isolated from the main application.
Example: Running Code with SafeRun
Command-Line Example:
Assume you have a Python file named example.py:
# example.py
print("This code is running inside a secure sandbox!")
for i in range(3):
print("Iteration", i)
Run it securely:
python -m SafeRun example.py --time 2 --memory 50 --chroot /tmp/sandbox_chroot
Programmatic Example:
from SafeRun import UltraSecureSandbox
sandbox = UltraSecureSandbox(time_limit=2, memory_limit_mb=50, chroot_dir="/tmp/sandbox_chroot")
sandbox.execute_file("example.py")
License
SafeRun is licensed under the MIT License.
Author
Mohammad Taha Gorji
Enjoy using SafeRun to run your Python code securely!
Metadata
Release files for SafeRun 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| SafeRun-1.0.2.tar.gz | 6.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| SafeRun-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / SafeRun-1.0.2.tar.gz
| Download URL | SafeRun-1.0.2.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
10a025e09259789b0fa4caa4340b8914b2abec98521e722f31748a4074734e3c
|
|
BLAKE2b-256 checksum How to use checksums |
69491c9a09e0608cf9c8a01b8815a46f8b735975eb6e1abbf663674ba23b8ff1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|
Release files / SafeRun-1.0.2-py3-none-any.whl
| Download URL | SafeRun-1.0.2-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9c38b86702e440adb2dfbd4c5ec0fb4ab05419a34db2db84d7211acd98bfccd7
|
|
BLAKE2b-256 checksum How to use checksums |
fda986a9c82fdce1d2587bf71a191aeaf68bc5ee9525cbe27398972ba9838e29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|
Security for SafeRun 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.