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.5.tar.gz (7.2 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.5-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

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

Hashes for restricted-0.1.5.tar.gz
Algorithm Hash digest
SHA256 c503d83d4746d6472d7a8113e75653638e4d225001afcd5850b6498fcde3c11e
MD5 3c6ae2bf0f5c082db41034b404902171
BLAKE2b-256 e3ff4977ed42ab06f209c1925c6a2a7b109a1815c6c55d659c2608d103a66cf0

See more details on using hashes here.

Provenance

The following attestation bundles were made for restricted-0.1.5.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.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

Hashes for restricted-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 45751f897059621d9bba902ddc0f21467bdc4dcb65c9b1f143c27196f45781f6
MD5 29f083ac16952b600ba36b154dcacba7
BLAKE2b-256 9843e901bb98bf8f10268d66509b234414a6ebcf1e2ec2dad97df9b74b26a142

See more details on using hashes here.

Provenance

The following attestation bundles were made for restricted-0.1.5-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