@@ -838,6 +838,71 @@ class Host:
838838 with self .assertRaises (UnsafeExpressionError ):
839839 safe_eval (expr , allow_attributes = True , allowed_vars = {"o" : Host ()})
840840
841+ def test_reported_format_field_globals_leak (self ):
842+ """The reported PoC: a format field reads the helper's module globals.
843+
844+ Reported by email: on 0.0.3-0.0.6 dunder names inside a str.format
845+ replacement field are string literals, so the AST checker never sees an
846+ ast.Attribute node and the dunder block does not apply. Affected range
847+ was verified against the published wheels for 0.0.3 through 0.0.6;
848+ 0.0.1/0.0.2 predate allow_attributes. The impact is disclosure, not
849+ execution: a replacement field cannot contain a call, and the resolved
850+ value is rendered to str.
851+ """
852+ secret = "HOST_SECRET_s3cr3t_value_271"
853+
854+ def helper (x ):
855+ return x + 1
856+
857+ helper .__globals__ ["HOST_SECRET" ] = secret
858+ try :
859+ for expr in (
860+ "'{0.__globals__[HOST_SECRET]}'.format(h)" ,
861+ "'{0.__globals__}'.format(h)" ,
862+ "'{0.__class__}'.format('')" ,
863+ "'{0.__class__.__base__.__subclasses__}'.format('')" ,
864+ "'{0.__globals__[__builtins__]}'.format(h)" ,
865+ "'{0.__globals__[__builtins__][__import__]}'.format(h)" ,
866+ ):
867+ with self .assertRaises (UnsafeExpressionError ):
868+ safe_eval (expr , allow_attributes = True , allowed_vars = {"h" : helper })
869+ finally :
870+ helper .__globals__ .pop ("HOST_SECRET" , None )
871+
872+ def test_format_templates_that_are_not_literals_are_blocked (self ):
873+ """Computed templates are refused too - the check is name-independent.
874+
875+ A template assembled from pieces (concatenation, join, chr()) cannot be
876+ resolved statically, so the call is rejected rather than guessed at.
877+ """
878+ for expr in (
879+ "('{0.' + '__class__' + '}').format('')" ,
880+ "''.join(['{0.__class__', '}']).format('')" ,
881+ "f'{chr(123)}0.__class__{chr(125)}'.format('')" ,
882+ ):
883+ with self .assertRaises ((UnsafeExpressionError , RuntimeError )):
884+ safe_eval (expr , allow_attributes = True )
885+
886+ def test_format_map_plain_key_is_data_only (self ):
887+ """format_map with a plain {name} field stays usable and stays local.
888+
889+ A named field only reads a key from the mapping the expression itself
890+ passed in, so nothing outside the expression is reachable; anything
891+ needing an attribute or a nested key expression is rejected.
892+ """
893+ self .assertEqual (safe_eval ("'{k}'.format_map({'k': 1})" , allow_attributes = True ), "1" )
894+
895+ def helper (x ):
896+ return x
897+
898+ for expr in (
899+ "'{0.__globals__}'.format_map({})" ,
900+ "'{0[S]}'.format_map(h.__globals__)" ,
901+ "'{__import__}'.format_map(h.__globals__['__builtins__'])" ,
902+ ):
903+ with self .assertRaises (UnsafeExpressionError ):
904+ safe_eval (expr , allow_attributes = True , allowed_vars = {"h" : helper })
905+
841906 def test_format_string_leak_is_blocked_without_attributes (self ):
842907 with self .assertRaises (UnsafeExpressionError ):
843908 safe_eval ("'{0.__class__}'.format(1)" )
0 commit comments