]> granicus.if.org Git - python/commitdiff
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code...
authorMichael Foord <michael@voidspace.org.uk>
Thu, 22 Dec 2011 01:13:37 +0000 (01:13 +0000)
committerMichael Foord <michael@voidspace.org.uk>
Thu, 22 Dec 2011 01:13:37 +0000 (01:13 +0000)
Closes issue 11829.

Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index ffbe66fbe1ff9b32ecb7d8e778d7d537ef004fbf..203175568b9290d646f27d85b05481bcc60603ac 100644 (file)
@@ -1161,10 +1161,11 @@ def getattr_static(obj, attr, default=_sentinel):
     if obj is klass:
         # for types we check the metaclass too
         for entry in _static_getmro(type(klass)):
-            try:
-                return entry.__dict__[attr]
-            except KeyError:
-                pass
+            if _shadowed_dict(type(entry)) is _sentinel:
+                try:
+                    return entry.__dict__[attr]
+                except KeyError:
+                    pass
     if default is not _sentinel:
         return default
     raise AttributeError(attr)
index 56f9929770f8ce96d1684d084fec9ad181e32146..fad4d5af73fc052b915c87a89ef055ad1a0a2d6e 100644 (file)
@@ -1088,6 +1088,23 @@ class TestGetattrStatic(unittest.TestCase):
         self.assertIsNot(inspect.getattr_static(sys, "version", sentinel),
                          sentinel)
 
+    def test_metaclass_with_metaclass_with_dict_as_property(self):
+        class MetaMeta(type):
+            @property
+            def __dict__(self):
+                self.executed = True
+                return dict(spam=42)
+
+        class Meta(type, metaclass=MetaMeta):
+            executed = False
+
+        class Thing(metaclass=Meta):
+            pass
+
+        with self.assertRaises(AttributeError):
+            inspect.getattr_static(Thing, "spam")
+        self.assertFalse(Thing.executed)
+
 class TestGetGeneratorState(unittest.TestCase):
 
     def setUp(self):
index 1ad91c02856802f69f897ffde6010f1f1a47aa6d..9168a55be07b5af9a0e27a93ed9bef3dc3c55ccc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #11829: Fix code execution holes in inspect.getattr_static for
+  metaclasses with metaclasses. Patch by Andreas Stührk.
+
 - Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
 
 - Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas