From cb202238c25d0ddb7dd3b98b1b171e4041848945 Mon Sep 17 00:00:00 2001 From: Hans Henrik Bergan Date: Sun, 1 Nov 2026 14:03:46 +0000 Subject: [PATCH] Add reversed() to safe_builtins (fixes #335) --- CHANGES.rst | 2 ++ src/RestrictedPython/Guards.py | 1 + tests/test_Guards.py | 5 +++++ 3 files changed, 8 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 60c7eb4..8037d0e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,8 @@ Changes 8.6 (unreleased) ---------------- +- Add support for the ``reversed`` builtin in ``safe_builtins``. + - 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/src/RestrictedPython/Guards.py b/src/RestrictedPython/Guards.py index 83ba10b..5a0ea85 100644 --- a/src/RestrictedPython/Guards.py +++ b/src/RestrictedPython/Guards.py @@ -49,6 +49,7 @@ 'pow', 'range', 'repr', + 'reversed', 'round', 'slice', 'sorted', diff --git a/tests/test_Guards.py b/tests/test_Guards.py index 5895d49..73ec394 100644 --- a/tests/test_Guards.py +++ b/tests/test_Guards.py @@ -25,6 +25,11 @@ def test_Guards_sorted(): assert restricted_eval('sorted([5, 2, 8, 1])') == sorted([5, 2, 8, 1]) +def test_Guards_reversed(): + """It contains reversed""" + assert list(restricted_eval('reversed([5, 2, 8, 1])')) == [1, 8, 2, 5] + + def test_Guards__safe_builtins__1(): """It contains `slice()`.""" assert restricted_eval('slice(1)') == slice(1)