Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@
RestrictedPython
================

RestrictedPython is a defined subset of the Python language which allows to provide a program input into a trusted environment.
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.

For full documentation please see  http://restrictedpython.readthedocs.io/ or the local ``docs/index``.

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_builtins
>>>
>>> source_code = """
... def example():
... return 'Hello World!'
... """
>>>
>>> loc = {}
>>> byte_code = compile_restricted(source_code, '<inline>', 'exec')
>>> exec(byte_code, safe_builtins, 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_builtins
>>>
>>> source_code = """
... import os
...
... os.listdir('/')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the best example and what you want to show here: Code which RestrictedPython forbids? Code which should run through RestrictedPython as it would be harmful otherwise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also not sure about which negative example we should use, but I think we should show an example that will be restricted.

... """
>>> byte_code = compile_restricted(source_code, '<inline>', 'exec')
>>> # exec(byte_code, safe_builtins, {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does happen here? I think Python complains that __import__ is not defined as it is not in safe_builtins.
According to Guards.py it is provided by AccessControl.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it be a good idea to move some of the additional features used in Zope from AccessControl to RestrictedPython to make it easier adoptable, those import checks are one of those thing I would like to see.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import checks might be a bit too Zope specific as they involve SecurityInfo objects which are a subclass of Acquisition.Implicit.

3 changes: 3 additions & 0 deletions docs/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changes
4.0a2 (unreleased)
------------------

- Modified README and setup.py to provide a better desciption test for PyPI.
[loechel]
- Drop support for long-deprecated ``sets`` module.
[tseaver]


4.0a1 (2017-05-05)
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def read(*rnames):
version='4.0a2.dev0',
url='http://pypi.python.org/pypi/RestrictedPython',
license='ZPL 2.1',
description='RestrictedPython provides a restricted execution '
'environment for Python, e.g. for running untrusted code.',
description='RestrictedPython is a defined subset of the Python '
'language which allows to provide a program input into '
'a trusted environment.',
long_description=(read('README.rst') + '\n' +
read('docs', 'CHANGES.rst')),
classifiers=[
Expand Down
32 changes: 32 additions & 0 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Tests about imports
"""

from RestrictedPython import safe_builtins
from tests import c_exec
from tests import e_exec

import pytest


OS_IMPORT_EXAMPLE = """
import os

os.listdir('/')
"""


@pytest.mark.parametrize(*c_exec)
@pytest.mark.parametrize(*e_exec)
def test_os_import(c_exec, e_exec):
"""Test that import should not work out of the box.
TODO: Why does this work.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works because RestrictedPython does not forbid import statements.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above, might be good to move that feature to RestrictedPython.

"""
result = c_exec(OS_IMPORT_EXAMPLE, safe_builtins)
# TODO: there is a tests/__init__.py problem, as it seems to pass the
# safe_builtins into the compile function instead of the source.
assert result.code is None
# assert result.errors == ()

with pytest.raises(NameError):
e_exec(OS_IMPORT_EXAMPLE, safe_builtins)