Skip to content

Commit d701cc3

Browse files
authored
Merge commit from fork
1 parent 57a2acb commit d701cc3

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Changes
1010
it as ``getattr`` implementation. Such use should now follow the same policy
1111
and give the same level of protection as direct attribute access in an
1212
environment based on ``RestrictedPython``'s ``safe_builtints``.
13+
- Prevent information leakage via ``AttributeError.obj``
14+
and the ``string`` module.
1315

1416

1517
7.2 (2024-08-02)

src/RestrictedPython/Utilities.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ def __getattr__(self, attr):
2929
if attr in self.__excludes:
3030
raise NotImplementedError(
3131
f"{self.__mod.__name__}.{attr} is not safe")
32-
return getattr(self.__mod, attr)
32+
try:
33+
return getattr(self.__mod, attr)
34+
except AttributeError as e:
35+
e.obj = self
36+
raise
3337

3438

3539
utility_builtins['string'] = _AttributeDelegator(string, "Formatter")

tests/builtins/test_utilities.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ def test_string_in_utility_builtins():
77
from RestrictedPython.Utilities import utility_builtins
88

99
# we no longer provide access to ``string`` itself, only to
10-
# a restricted view of it
11-
assert utility_builtins['string'].__name__ == string.__name__
10+
# a restricted view of it (``rstring``)
11+
rstring = utility_builtins['string']
12+
assert rstring.__name__ == string.__name__
13+
14+
# ensure it does not provide access to ``string`` via
15+
# ``AttributeError.obj``
16+
try:
17+
rstring.unexisting_attribute
18+
except AttributeError as e:
19+
assert e.obj is rstring
20+
1221

1322

1423
def test_math_in_utility_builtins():

0 commit comments

Comments
 (0)