Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safer_getattr #282

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changes
- Allow to use the package with Python 3.13 -- Caution: No security
audit has been done so far.
- Add support for single mode statements / execution.
- Fix a potential breakout capability in the provided safer_getattr method that is part of the safer_builtins
icemac marked this conversation as resolved.
Show resolved Hide resolved


7.1 (2024-03-14)
Expand Down
2 changes: 2 additions & 0 deletions src/RestrictedPython/Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ def safer_getattr(object, name, default=None, getattr=getattr):
http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/

"""
if not (isinstance(name, str) and type(name) is str):
icemac marked this conversation as resolved.
Show resolved Hide resolved
raise TypeError('name is not of type str')
icemac marked this conversation as resolved.
Show resolved Hide resolved
if name in ('format', 'format_map') and (
isinstance(object, str) or
(isinstance(object, type) and issubclass(object, str))):
Expand Down
36 changes: 36 additions & 0 deletions tests/test_Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,42 @@ def test_Guards__safer_getattr__3():
assert restricted_globals['result'] == 2


SAFER_GETATTR_BREAKOUT = """\
def g(obj, name):
# create class FakeString which inherits from str
class FakeString(str):
# overload startswith() to always return false
def startswith(self, _):
return False
return getattr(obj, FakeString(name))

# call str.__class__.__base__.__subclasses__()
subclasses = g(g(g(str, "__class__"), "__base__"), "__subclasses__")()
# traverse list of subclasses until we reach the BuiltinImporter class
x = "test"
while "BuiltinImporter" not in str(x):
x = subclasses.pop()
continue
# use BuiltinImporter to import 'os' and access to a not allowed function
result = x.load_module('os').getgid()
"""


def test_Guards__safer_getattr__4():
restricted_globals = dict(
__builtins__=safe_builtins,
__name__=None,
__metaclass__=type,
# _write_=_write_,
getattr=safer_getattr,
result=None,
)

with pytest.raises(TypeError) as err:
restricted_exec(SAFER_GETATTR_BREAKOUT, restricted_globals)
assert 'name is not of type str' == str(err.value)
icemac marked this conversation as resolved.
Show resolved Hide resolved


def test_call_py3_builtins():
"""It should not be allowed to access global builtins in Python3."""
result = compile_restricted_exec('builtins["getattr"]')
Expand Down
Loading