Skip to content

zopefoundation/RestrictedPython

Folders and files

NameName
Last commit message
Last commit date

Latest commit

bd936f7 · Mar 20, 2025
Mar 17, 2025
Jan 6, 2025
Jan 23, 2025
Jan 23, 2025
Aug 30, 2021
Apr 13, 2022
Mar 17, 2025
Mar 17, 2025
Sep 22, 2023
Mar 20, 2025
Jan 28, 2025
Apr 8, 2010
Apr 8, 2010
Oct 9, 2024
Mar 10, 2023
Jul 12, 2020
Apr 20, 2022
Mar 17, 2025
Oct 9, 2024
Mar 20, 2025
Mar 17, 2025

Repository files navigation

https://coveralls.io/repos/github/zopefoundation/RestrictedPython/badge.svg?branch=master Documentation Status Current version on PyPI Supported Python versions

https://github.com/zopefoundation/RestrictedPython/raw/master/docs/logo.jpg

RestrictedPython

RestrictedPython is a tool that helps to define a subset of the Python language which allows to provide a program input into a trusted environment. RestrictedPython is not a sandbox system or a secured environment, but it helps to define a trusted environment and execute untrusted code inside of it.

Warning

RestrictedPython only supports CPython. It does _not_ support PyPy and other Python implementations as it cannot provide its restrictions there.

For full documentation please see http://restrictedpython.readthedocs.io/.

Example

To give a basic understanding what RestrictedPython does here two examples:

An unproblematic code example

Python allows you to execute a large set of commands. This would not harm any system.

>>> from RestrictedPython import compile_restricted
>>> from RestrictedPython import safe_globals
>>>
>>> source_code = """
... def example():
...     return 'Hello World!'
... """
>>>
>>> loc = {}
>>> byte_code = compile_restricted(source_code, '<inline>', 'exec')
>>> exec(byte_code, safe_globals, loc)
>>>
>>> loc['example']()
'Hello World!'

Problematic code example

This example directly executed in Python could harm your system.

>>> from RestrictedPython import compile_restricted
>>> from RestrictedPython import safe_globals
>>>
>>> source_code = """
... import os
...
... os.listdir('/')
... """
>>> byte_code = compile_restricted(source_code, '<inline>', 'exec')
>>> exec(byte_code, safe_globals, {})
Traceback (most recent call last):
ImportError: __import__ not found

Contributing to RestrictedPython

If you want to help maintain RestrictedPython and contribute, please refer to the documentation Contributing page.