Skip to main content

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.

Demo

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

restricted-0.1.4.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

restricted-0.1.4-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file restricted-0.1.4.tar.gz.

File metadata

  • Download URL: restricted-0.1.4.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for restricted-0.1.4.tar.gz
Algorithm Hash digest
SHA256 81a3cf8198383cf30384609d31bab3b74b1949eca503545144e2f55ba0cdc447
MD5 94b0236c5cb1fa080e713d8d641f2ae8
BLAKE2b-256 1f3944dca6febffbf31152c489e2e3f8b0da19f626b472a5b65bd77119ae6b9a

See more details on using hashes here.

Provenance

The following attestation bundles were made for restricted-0.1.4.tar.gz:

Publisher: python-publish.yml on bimalpaudels/restricted

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file restricted-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: restricted-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for restricted-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 587386022f69d11ab35dd1b4543adda748411ceb303be0e90fcd1421beedef19
MD5 60f8e7c1f2146da4a3a633da47a4d4fb
BLAKE2b-256 565fe5a7ce574a9c167c1323fb726a113cce49d85438a7d1b541afbc66d1ef80

See more details on using hashes here.

Provenance

The following attestation bundles were made for restricted-0.1.4-py3-none-any.whl:

Publisher: python-publish.yml on bimalpaudels/restricted

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page