mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Closes issue 11829.
This commit is contained in:
		
							parent
							
								
									65a3f4b8c5
								
							
						
					
					
						commit
						3ba95f8bd9
					
				
					 3 changed files with 25 additions and 4 deletions
				
			
		| 
						 | 
					@ -1161,10 +1161,11 @@ def getattr_static(obj, attr, default=_sentinel):
 | 
				
			||||||
    if obj is klass:
 | 
					    if obj is klass:
 | 
				
			||||||
        # for types we check the metaclass too
 | 
					        # for types we check the metaclass too
 | 
				
			||||||
        for entry in _static_getmro(type(klass)):
 | 
					        for entry in _static_getmro(type(klass)):
 | 
				
			||||||
            try:
 | 
					            if _shadowed_dict(type(entry)) is _sentinel:
 | 
				
			||||||
                return entry.__dict__[attr]
 | 
					                try:
 | 
				
			||||||
            except KeyError:
 | 
					                    return entry.__dict__[attr]
 | 
				
			||||||
                pass
 | 
					                except KeyError:
 | 
				
			||||||
 | 
					                    pass
 | 
				
			||||||
    if default is not _sentinel:
 | 
					    if default is not _sentinel:
 | 
				
			||||||
        return default
 | 
					        return default
 | 
				
			||||||
    raise AttributeError(attr)
 | 
					    raise AttributeError(attr)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1088,6 +1088,23 @@ def test_module(self):
 | 
				
			||||||
        self.assertIsNot(inspect.getattr_static(sys, "version", sentinel),
 | 
					        self.assertIsNot(inspect.getattr_static(sys, "version", sentinel),
 | 
				
			||||||
                         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):
 | 
					class TestGetGeneratorState(unittest.TestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -97,6 +97,9 @@ Core and Builtins
 | 
				
			||||||
Library
 | 
					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 #1785: Fix inspect and pydoc with misbehaving descriptors.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas
 | 
					- Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue