From edc93e53e96e1787f6750d701f62696ffcd8b925 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Mon, 2 Nov 2026 17:57:42 +0100 Subject: [PATCH 1/3] Remove sorted and tuple from safe_builtins, document why reversed is not added They iterate their argument in C, bypassing the _getiter_ guard. Refs #335 --- CHANGES.rst | 6 ++++++ docs/usage/security_considerations.rst | 26 ++++++++++++++++++++++++++ src/RestrictedPython/Guards.py | 5 +++-- tests/test_Guards.py | 9 ++++++--- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 60c7eb4..0466655 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,12 @@ Changes 8.6 (unreleased) ---------------- +- Remove ``sorted`` and ``tuple`` from ``safe_builtins`` and do not add + ``reversed``: all three 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/docs/usage/security_considerations.rst b/docs/usage/security_considerations.rst index 81ae989..db33149 100644 --- a/docs/usage/security_considerations.rst +++ b/docs/usage/security_considerations.rst @@ -46,6 +46,32 @@ 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) + +``safe_builtins`` therefore leaves out ``reversed``, ``sorted`` and ``tuple``. +If your application needs them, provide wrappers that pass the argument through +``_getiter_`` before handing it to the real builtin, the way +:func:`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..ec5217f 100644 --- a/src/RestrictedPython/Guards.py +++ b/src/RestrictedPython/Guards.py @@ -51,9 +51,7 @@ 'repr', 'round', 'slice', - 'sorted', 'str', - 'tuple', 'zip' ] @@ -137,6 +135,9 @@ # Builtins that are intentionally disabled # 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 # 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..0a1309e 100644 --- a/tests/test_Guards.py +++ b/tests/test_Guards.py @@ -20,9 +20,12 @@ def test_Guards_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', ['reversed', 'sorted', 'tuple']) +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(): From fbeeb21f746990e5436c3123d10d3b1e2f652ebe Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Mon, 2 Nov 2026 18:29:46 +0100 Subject: [PATCH 2/3] Link to the source of guarded_zip in the docs --- COPYRIGHT.txt | 2 +- docs/usage/security_considerations.rst | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) 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 db33149..e3873cd 100644 --- a/docs/usage/security_considerations.rst +++ b/docs/usage/security_considerations.rst @@ -70,7 +70,8 @@ sees the elements: ``safe_builtins`` therefore leaves out ``reversed``, ``sorted`` and ``tuple``. If your application needs them, provide wrappers that pass the argument through ``_getiter_`` before handing it to the real builtin, the way -:func:`AccessControl.ZopeGuards.guarded_zip` does. +`AccessControl.ZopeGuards.guarded_zip `_ +does. .. _providing_import: From 6adaccc909cff7a8d3e8614f85ce359df745fd21 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Tue, 3 Nov 2026 08:28:44 +0100 Subject: [PATCH 3/3] Remove bytes and zip from safe_builtins as they iterate in C as well --- CHANGES.rst | 8 ++++---- docs/usage/security_considerations.rst | 5 ++++- src/RestrictedPython/Guards.py | 4 ++-- tests/test_Guards.py | 12 ++++++------ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 0466655..8374c55 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,10 +4,10 @@ Changes 8.6 (unreleased) ---------------- -- Remove ``sorted`` and ``tuple`` from ``safe_builtins`` and do not add - ``reversed``: all three iterate their argument in C, bypassing the - ``_getiter_`` guard. Applications which need them have to provide wrappers - calling ``_getiter_``. +- 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 diff --git a/docs/usage/security_considerations.rst b/docs/usage/security_considerations.rst index e3873cd..1b39bd7 100644 --- a/docs/usage/security_considerations.rst +++ b/docs/usage/security_considerations.rst @@ -66,8 +66,11 @@ sees the elements: tuple(secret) sorted(secret) reversed(secret) + zip(secret) + bytes(secret) -``safe_builtins`` therefore leaves out ``reversed``, ``sorted`` and ``tuple``. +``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 `_ diff --git a/src/RestrictedPython/Guards.py b/src/RestrictedPython/Guards.py index ec5217f..6feccc9 100644 --- a/src/RestrictedPython/Guards.py +++ b/src/RestrictedPython/Guards.py @@ -31,7 +31,6 @@ 'Ellipsis', 'abs', 'bool', - 'bytes', 'callable', 'chr', 'complex', @@ -52,7 +51,6 @@ 'round', 'slice', 'str', - 'zip' ] _safe_exceptions = [ @@ -134,10 +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 0a1309e..39a17f5 100644 --- a/tests/test_Guards.py +++ b/tests/test_Guards.py @@ -15,12 +15,12 @@ def _write_(x): return x -def test_Guards_bytes(): - """It contains bytes""" - assert restricted_eval('bytes(1)') == bytes(1) - - -@pytest.mark.parametrize('name', ['reversed', 'sorted', 'tuple']) +@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