A lightweight Python package for analyzing and securely executing code blocks with AST-based restrictions.
Project description
restricted: Enforcing Restrictions on Python Code Execution
Overview
A lightweight Python package for analyzing and securely executing code blocks with AST-based restrictions. This package provides multiple execution methods, including direct execution, subprocesses, and uv, allowing for controlled execution with customizable allow/restrict lists.
Demo
Live demo using the default settings.
Installation
pip
pip install restricted
uv
uv add restricted
Usage
With helper function
The helper function provides the easiest way to execute code with restrictions.
from restricted.helpers import execute_restricted
# A block of code pretending to be malicious.
code = """
import os
print(os.getcwd())
"""
# Restrict specific modules
result = execute_restricted(code, method="direct", restrict=["os"])
print(result)
# Output: RestrictedImportError: 'os' is not allowed
Using allow lists
You can specify exactly what modules/functions are allowed instead of what's restricted.
from restricted.helpers import execute_restricted
code = """
import math
print(math.sqrt(16))
"""
# Only allow specific modules
result = execute_restricted(code, method="direct", allow=["math", "print"])
print(result)
# Output: 4.0
Different execution methods
Choose from different execution methods based on your needs:
from restricted.helpers import execute_restricted
code = """
print("Hello World")
"""
# Direct execution (fastest)
result = execute_restricted(code, method="direct", allow=["print"])
# Subprocess execution (more isolated)
result = execute_restricted(code, method="subprocess", allow=["print"])
# UV execution (using uv for isolation)
result = execute_restricted(code, method="uv", allow=["print"])
Without helper function
For more advanced control over the execution process, you can use the Executor and Restrictor directly.
from restricted.core import Executor, Restrictor
code = """
print("Hello World")
"""
# Create a restrictor with specific restrictions
restrictor = Restrictor(restrict=["os", "sys", "subprocess"])
executor = Executor(code, restrictor=restrictor)
# Execute with your preferred method
result = executor.execute(method="direct")
print(result)
Using only the Restrictor
The Restrictor class can be used independently for validation without execution.
from restricted.core import Restrictor
code = """
import os
print("Hello World")
"""
# Only validate, don't execute
restrictor = Restrictor(restrict=["os"])
try:
validated_code = restrictor.restrict(code)
print("Code is safe to execute")
except Exception as e:
print(f"Code validation failed: {e}")
API Reference
Restrictor
allow: Optional list of allowed modules/functionsrestrict: Optional list of restricted modules/functions- Only one of
alloworrestrictcan be provided
Executor
method: Execution method ("direct","subprocess", or"uv")- Automatically applies restrictions before execution
Exceptions
RestrictedImportError: Raised when a restricted import is detectedRestrictedBuiltInsError: Raised when a restricted built-in is usedScriptExecutionError: Raised when script execution fails
Security Notice
Caution: Always ensure that the code you execute is thoroughly reviewed to avoid potential security risks. Malicious or unsafe code can harm the system or access sensitive resources. Consider running code in a controlled or isolated environment to minimize potential damage.
Contribution
Any contributions to improve this project are welcome! If you have suggestions, bug fixes, or new features to propose, feel free to submit a pull request on GitHub.
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 restricted-0.2.0.tar.gz.
File metadata
- Download URL: restricted-0.2.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
478bdfb112a6a39820d6b837c26c83cb50e1666124d12f6c62573bd0e91262d2
|
|
| MD5 |
03d8e2c2edc6f394762d689bafb39628
|
|
| BLAKE2b-256 |
c6e189988a8980e9e3dab5412d7e95faf059d039de46e064865e9ba5a117e167
|
Provenance
The following attestation bundles were made for restricted-0.2.0.tar.gz:
Publisher:
python-publish.yml on bimalpaudels/restricted
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
restricted-0.2.0.tar.gz -
Subject digest:
478bdfb112a6a39820d6b837c26c83cb50e1666124d12f6c62573bd0e91262d2 - Sigstore transparency entry: 224152721
- Sigstore integration time:
-
Permalink:
bimalpaudels/restricted@9e1de11a1a4954559e1ab756ac37c1ec82ce1368 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/bimalpaudels
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9e1de11a1a4954559e1ab756ac37c1ec82ce1368 -
Trigger Event:
release
-
Statement type:
File details
Details for the file restricted-0.2.0-py3-none-any.whl.
File metadata
- Download URL: restricted-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1004182b4e99cff87cca69a5ec6f6d22dc0e6b51b691031abcaf6c7bc9b7ac5
|
|
| MD5 |
bb987ffd946cd0e916c70ee2c831e57f
|
|
| BLAKE2b-256 |
00ae1eb0daddf156b598608ef5209fc962d3ad62cc38dead0b75c94a207b0741
|
Provenance
The following attestation bundles were made for restricted-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on bimalpaudels/restricted
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
restricted-0.2.0-py3-none-any.whl -
Subject digest:
a1004182b4e99cff87cca69a5ec6f6d22dc0e6b51b691031abcaf6c7bc9b7ac5 - Sigstore transparency entry: 224152732
- Sigstore integration time:
-
Permalink:
bimalpaudels/restricted@9e1de11a1a4954559e1ab756ac37c1ec82ce1368 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/bimalpaudels
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9e1de11a1a4954559e1ab756ac37c1ec82ce1368 -
Trigger Event:
release
-
Statement type: