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
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changes
8.6 (unreleased)
----------------

- Remove ``bytes``, ``sorted``, ``tuple`` and ``zip`` from ``safe_builtins``
and do not add ``reversed``: all of them iterate their argument in C,
bypassing the ``_getiter_`` guard. Applications which need them have to
provide wrappers calling ``_getiter_``.
(`#335 <https://github.com/zopefoundation/RestrictedPython/issues/335>`_)

- Add a "Security considerations" documentation page. It explains why the guards
only cover attribute access written in the restricted source, and why
providing an ``__import__`` implementation hands the security boundary to the
Expand Down
2 changes: 1 addition & 1 deletion COPYRIGHT.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Zope Foundation and Contributors
Zope Foundation and Contributors
30 changes: 30 additions & 0 deletions docs/usage/security_considerations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ You gain more by auditing what you put into ``globals`` and ``__builtins__``
than by relying on the guards.
The guards are a second line of defence, not the boundary.

.. _builtins_that_iterate:

Builtins that iterate bypass ``_getiter_``
..........................................

The same applies to iteration.
``compile_restricted`` routes ``for`` loops, comprehensions and unpacking
**written in the restricted source** through ``_getiter_``.
A builtin that iterates its argument itself does so in C, so the guard never
sees the elements:

.. code-block:: python

# `_getiter_` runs
[x for x in secret]

# no `_getiter_` call; every element is read without a guard
tuple(secret)
sorted(secret)
reversed(secret)
zip(secret)
bytes(secret)

``safe_builtins`` therefore leaves out ``bytes``, ``reversed``, ``sorted``,
``tuple`` and ``zip``.
If your application needs them, provide wrappers that pass the argument through
``_getiter_`` before handing it to the real builtin, the way
`AccessControl.ZopeGuards.guarded_zip <https://github.com/zopefoundation/AccessControl/blob/master/src/AccessControl/ZopeGuards.py>`_
does.

.. _providing_import:

Providing ``__import__`` moves the boundary out of RestrictedPython
Expand Down
9 changes: 5 additions & 4 deletions src/RestrictedPython/Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
'Ellipsis',
'abs',
'bool',
'bytes',
'callable',
'chr',
'complex',
Expand All @@ -51,10 +50,7 @@
'repr',
'round',
'slice',
'sorted',
'str',
'tuple',
'zip'
]

_safe_exceptions = [
Expand Down Expand Up @@ -136,7 +132,12 @@
# any

# Builtins that are intentionally disabled
# bytes - iterates its argument in C, bypassing the `_getiter_` guard
# compile - don't let them produce new code
# reversed - iterates its argument in C, bypassing the `_getiter_` guard
# sorted - iterates its argument in C, bypassing the `_getiter_` guard
# tuple - iterates its argument in C, bypassing the `_getiter_` guard
# zip - iterates its arguments in C, bypassing the `_getiter_` guard
# dir - a general purpose introspector, probably hard to wrap
# execfile - no direct I/O
# file - no direct I/O
Expand Down
19 changes: 11 additions & 8 deletions tests/test_Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ def _write_(x):
return x


def test_Guards_bytes():
"""It contains bytes"""
assert restricted_eval('bytes(1)') == bytes(1)


def test_Guards_sorted():
"""It contains sorted"""
assert restricted_eval('sorted([5, 2, 8, 1])') == sorted([5, 2, 8, 1])
@pytest.mark.parametrize('name',
['bytes',
'reversed',
'sorted',
'tuple',
'zip'])
def test_Guards__safe_builtins__iterating(name):
"""It does not contain builtins which iterate without `_getiter_`."""
assert name not in safe_builtins
with pytest.raises(NameError):
restricted_eval(f'{name}([5, 2, 8, 1])')


def test_Guards__safe_builtins__1():
Expand Down
Loading