diff options
author | Michael Foord <michael@python.org> | 2011-03-15 23:20:44 (GMT) |
---|---|---|
committer | Michael Foord <michael@python.org> | 2011-03-15 23:20:44 (GMT) |
commit | dcebe0f2dca40a22654c5f09ce1bd141419ea8d3 (patch) | |
tree | cb76f16fb999a3ac559cdfdf996ad3eef90ac8df /Lib | |
parent | c867239a3121a51102d99c9bc1dbdf30566b0cbe (diff) | |
download | cpython-dcebe0f2dca40a22654c5f09ce1bd141419ea8d3.zip cpython-dcebe0f2dca40a22654c5f09ce1bd141419ea8d3.tar.gz cpython-dcebe0f2dca40a22654c5f09ce1bd141419ea8d3.tar.bz2 |
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/inspect.py | 27 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 47 |
2 files changed, 68 insertions, 6 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index ed10ac5..aa951d8 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1069,15 +1069,16 @@ def _check_instance(obj, attr): instance_dict = object.__getattribute__(obj, "__dict__") except AttributeError: pass - return instance_dict.get(attr, _sentinel) + return dict.get(instance_dict, attr, _sentinel) def _check_class(klass, attr): for entry in _static_getmro(klass): - try: - return entry.__dict__[attr] - except KeyError: - pass + if not _shadowed_dict(type(entry)): + try: + return entry.__dict__[attr] + except KeyError: + pass return _sentinel def _is_type(obj): @@ -1087,6 +1088,19 @@ def _is_type(obj): return False return True +def _shadowed_dict(klass): + dict_attr = type.__dict__["__dict__"] + for entry in _static_getmro(klass): + try: + class_dict = dict_attr.__get__(entry)["__dict__"] + except KeyError: + pass + else: + if not (type(class_dict) is types.GetSetDescriptorType and + class_dict.__name__ == "__dict__" and + class_dict.__objclass__ is entry): + return True + return False def getattr_static(obj, attr, default=_sentinel): """Retrieve attributes without triggering dynamic lookup via the @@ -1101,8 +1115,9 @@ def getattr_static(obj, attr, default=_sentinel): """ instance_result = _sentinel if not _is_type(obj): - instance_result = _check_instance(obj, attr) klass = type(obj) + if not _shadowed_dict(klass): + instance_result = _check_instance(obj, attr) else: klass = obj diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index ccfcaba..331d247 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -906,6 +906,53 @@ class TestGetattrStatic(unittest.TestCase): self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3) self.assertEqual(inspect.getattr_static(Something, 'foo'), 3) + def test_dict_as_property(self): + test = self + test.called = False + + class Foo(dict): + a = 3 + @property + def __dict__(self): + test.called = True + return {} + + foo = Foo() + foo.a = 4 + self.assertEqual(inspect.getattr_static(foo, 'a'), 3) + self.assertFalse(test.called) + + def test_custom_object_dict(self): + test = self + test.called = False + + class Custom(dict): + def get(self, key, default=None): + test.called = True + super().get(key, default) + + class Foo(object): + a = 3 + foo = Foo() + foo.__dict__ = Custom() + self.assertEqual(inspect.getattr_static(foo, 'a'), 3) + self.assertFalse(test.called) + + def test_metaclass_dict_as_property(self): + class Meta(type): + @property + def __dict__(self): + self.executed = True + + class Thing(metaclass=Meta): + executed = False + + def __init__(self): + self.spam = 42 + + instance = Thing() + self.assertEqual(inspect.getattr_static(instance, "spam"), 42) + self.assertFalse(Thing.executed) class TestGetGeneratorState(unittest.TestCase): |