A highly secure sandbox for executing Python code in an isolated environment.
Project description
# 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!
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 SafeRun-1.0.2.tar.gz.
File metadata
- Download URL: SafeRun-1.0.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10a025e09259789b0fa4caa4340b8914b2abec98521e722f31748a4074734e3c
|
|
| MD5 |
a9fa187d34127657cb146c26de3b8a82
|
|
| BLAKE2b-256 |
69491c9a09e0608cf9c8a01b8815a46f8b735975eb6e1abbf663674ba23b8ff1
|
File details
Details for the file SafeRun-1.0.2-py3-none-any.whl.
File metadata
- Download URL: SafeRun-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c38b86702e440adb2dfbd4c5ec0fb4ab05419a34db2db84d7211acd98bfccd7
|
|
| MD5 |
f2753c97a62189ad421486193750bc3c
|
|
| BLAKE2b-256 |
fda986a9c82fdce1d2587bf71a191aeaf68bc5ee9525cbe27398972ba9838e29
|