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 Python code execution environment with support for restricting imports, built-ins with AST-based validation. This package provides multiple execution methods, including subprocesses and uv, allowing for controlled execution with customizable restrictions on potentially unsafe code.
Demo
Live demo using the default settings.
Installation
pip
pip install restricted
uv
uv add restricted
Usage
With helper function
The helper function could be used to execute code block the easiest with uv.
from restricted.helpers import execute_restricted
# A block of code pretending to be malicious.
code="""
import os
print(os.getcwd())
"""
print(execute_restricted(code))
# Shell Output
ImportError: 'os' is not allowed
Custom restrictions
You can provide your own restricting modules and built-in functions to restrict.
from restricted.helpers import execute_restricted
...
custom_restricted_modules = ["os", "sys", "asyncio", 'builtins']
custom_restricted_builtins = ["print", "open", "min", "max"]
result = execute_restricted(code, restricted_modules=custom_restricted_modules, restricted_builtins=custom_restricted_builtins)
Execute without restriction
During development, code could be tested without restrictions by passing the restrict=False flag to the helper function.
from restricted.helpers import execute_restricted
# A block of code pretending to be malicious.
code="""
import os
print(os.getcwd())
"""
print(execute_restricted(code, restrict=False))
# Shell Output
home/foo/projects/somefolder
Without helper function
For more advanced control over the execution process, you can use the Executor directly. This approach allows you to manage both the restriction and the execution method.
from restricted.core import Executor, Restrictor
code="""
print("Hello World")
"""
custom_restricted_modules = ["os", "sys", "asyncio", 'builtins']
custom_restrictor = Restrictor(restricted_modules=custom_restricted_modules)
executor = Executor(code, restrictor=custom_restrictor)
# Different execution methods
executor.direct_execution() or executor.subprocess_execution()
...
Using only the Restrictor
It's not necessary to always use the Executor. Many use cases could need just the validation and not the execution.
The Restrictor class can be used on it's own for finer control with execution behavior.
from restricted.core import Restrictor, SyntaxParser
code="""
print("Hello World")
"""
tree = SyntaxParser().parse_and_validate(code=code)
# Only restrict certain modules
custom_restricted_modules = ["os", "sys", "asyncio", 'builtins']
restrictor = Restrictor(restrict_modules=True, restrict_builtins=False, restricted_modules=custom_restricted_modules)
# Only restrict certain built-ins
custom_restricted_builtins = ["print", "open", "min", "max"]
restrictor = Restrictor(restrict_modules=False, restrict_builtins=True, restricted_builtins=custom_restricted_builtins)
# Visit the nodes
restrictor.visit(tree)
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.1.5.tar.gz.
File metadata
- Download URL: restricted-0.1.5.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c503d83d4746d6472d7a8113e75653638e4d225001afcd5850b6498fcde3c11e
|
|
| MD5 |
3c6ae2bf0f5c082db41034b404902171
|
|
| BLAKE2b-256 |
e3ff4977ed42ab06f209c1925c6a2a7b109a1815c6c55d659c2608d103a66cf0
|
Provenance
The following attestation bundles were made for restricted-0.1.5.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.1.5.tar.gz -
Subject digest:
c503d83d4746d6472d7a8113e75653638e4d225001afcd5850b6498fcde3c11e - Sigstore transparency entry: 223910768
- Sigstore integration time:
-
Permalink:
bimalpaudels/restricted@60a8300556bdb9eac48ccc49d633844f00807f07 -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/bimalpaudels
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@60a8300556bdb9eac48ccc49d633844f00807f07 -
Trigger Event:
release
-
Statement type:
File details
Details for the file restricted-0.1.5-py3-none-any.whl.
File metadata
- Download URL: restricted-0.1.5-py3-none-any.whl
- Upload date:
- Size: 7.8 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 |
45751f897059621d9bba902ddc0f21467bdc4dcb65c9b1f143c27196f45781f6
|
|
| MD5 |
29f083ac16952b600ba36b154dcacba7
|
|
| BLAKE2b-256 |
9843e901bb98bf8f10268d66509b234414a6ebcf1e2ec2dad97df9b74b26a142
|
Provenance
The following attestation bundles were made for restricted-0.1.5-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.1.5-py3-none-any.whl -
Subject digest:
45751f897059621d9bba902ddc0f21467bdc4dcb65c9b1f143c27196f45781f6 - Sigstore transparency entry: 223910770
- Sigstore integration time:
-
Permalink:
bimalpaudels/restricted@60a8300556bdb9eac48ccc49d633844f00807f07 -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/bimalpaudels
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@60a8300556bdb9eac48ccc49d633844f00807f07 -
Trigger Event:
release
-
Statement type: