Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ba95f8

Browse files
committed
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Closes issue 11829.
1 parent 65a3f4b commit 3ba95f8

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

Lib/inspect.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,10 +1161,11 @@ def getattr_static(obj, attr, default=_sentinel):
11611161
if obj is klass:
11621162
# for types we check the metaclass too
11631163
for entry in _static_getmro(type(klass)):
1164-
try:
1165-
return entry.__dict__[attr]
1166-
except KeyError:
1167-
pass
1164+
if _shadowed_dict(type(entry)) is _sentinel:
1165+
try:
1166+
return entry.__dict__[attr]
1167+
except KeyError:
1168+
pass
11681169
if default is not _sentinel:
11691170
return default
11701171
raise AttributeError(attr)

Lib/test/test_inspect.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,23 @@ def test_module(self):
10881088
self.assertIsNot(inspect.getattr_static(sys, "version", sentinel),
10891089
sentinel)
10901090

1091+
def test_metaclass_with_metaclass_with_dict_as_property(self):
1092+
class MetaMeta(type):
1093+
@property
1094+
def __dict__(self):
1095+
self.executed = True
1096+
return dict(spam=42)
1097+
1098+
class Meta(type, metaclass=MetaMeta):
1099+
executed = False
1100+
1101+
class Thing(metaclass=Meta):
1102+
pass
1103+
1104+
with self.assertRaises(AttributeError):
1105+
inspect.getattr_static(Thing, "spam")
1106+
self.assertFalse(Thing.executed)
1107+
10911108
class TestGetGeneratorState(unittest.TestCase):
10921109

10931110
def setUp(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Core and Builtins
9797
Library
9898
-------
9999

100+
- Issue #11829: Fix code execution holes in inspect.getattr_static for
101+
metaclasses with metaclasses. Patch by Andreas Stührk.
102+
100103
- Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
101104

102105
- Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas

0 commit comments

Comments
 (0)