diff --git a/CHANGES.rst b/CHANGES.rst index 60c7eb4..8374c55 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 `_) + - 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 diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 79859e0..b8581dd 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -1 +1 @@ -Zope Foundation and Contributors \ No newline at end of file +Zope Foundation and Contributors diff --git a/docs/usage/security_considerations.rst b/docs/usage/security_considerations.rst index 81ae989..1b39bd7 100644 --- a/docs/usage/security_considerations.rst +++ b/docs/usage/security_considerations.rst @@ -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 `_ +does. + .. _providing_import: Providing ``__import__`` moves the boundary out of RestrictedPython diff --git a/src/RestrictedPython/Guards.py b/src/RestrictedPython/Guards.py index 83ba10b..6feccc9 100644 --- a/src/RestrictedPython/Guards.py +++ b/src/RestrictedPython/Guards.py @@ -31,7 +31,6 @@ 'Ellipsis', 'abs', 'bool', - 'bytes', 'callable', 'chr', 'complex', @@ -51,10 +50,7 @@ 'repr', 'round', 'slice', - 'sorted', 'str', - 'tuple', - 'zip' ] _safe_exceptions = [ @@ -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 diff --git a/tests/test_Guards.py b/tests/test_Guards.py index 5895d49..39a17f5 100644 --- a/tests/test_Guards.py +++ b/tests/test_Guards.py @@ -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():