1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
.. image:: https://github.com/zopefoundation/RestrictedPython/actions/workflows/tests.yml/badge.svg
:target: https://github.com/zopefoundation/RestrictedPython/actions/workflows/tests.yml
.. image:: https://coveralls.io/repos/github/zopefoundation/RestrictedPython/badge.svg?branch=master
:target: https://coveralls.io/github/zopefoundation/RestrictedPython?branch=master
.. image:: https://readthedocs.org/projects/restrictedpython/badge/
:target: https://restrictedpython.readthedocs.org/
:alt: Documentation Status
.. image:: https://img.shields.io/pypi/v/RestrictedPython.svg
:target: https://pypi.org/project/RestrictedPython/
:alt: Current version on PyPI
.. image:: https://img.shields.io/pypi/pyversions/RestrictedPython.svg
:target: https://pypi.org/project/RestrictedPython/
:alt: Supported Python versions
.. image:: 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.
.. code-block:: pycon
>>> 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.
.. code-block:: pycon
>>> 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
<https://restrictedpython.readthedocs.io/en/latest/contributing/index.html>`_.
|