From 6d4411fdc7bef5c410b4439303fe5fda0d6e50e4 Mon Sep 17 00:00:00 2001 From: CodeSafe Maintainers Date: Sat, 28 Nov 2026 18:13:41 +0000 Subject: [PATCH] test: pin the reported str.format dunder-traversal bypass A report received by email showed that on 0.0.3-0.0.6 a dunder name inside a str.format replacement field is a string literal, not an ast.Attribute node, so the 0.0.6 'reject all __-prefixed attribute access' rule never saw it: safe_eval("'{0.__globals__[HOST_SECRET]}'.format(helper)", allow_attributes=True, allowed_vars={'helper': helper}) returns the host module's secret. The reporter (stilwel) scoped it to 0.0.6; verification against the published wheels shows 0.0.3 through 0.0.6 all leak, while 0.0.1/0.0.2 predate allow_attributes and raise TypeError. No library change is needed: this is the same defect class 0.0.7 already fixed in _FORMAT_STRING_TRAVERSES_ATTRIBUTES. This commit locks it down with tests. Severity agrees with the reporter - disclosure, not execution. A replacement field cannot contain a call, and its value is rendered to str. Confirmed that the leak reads the helper's own module globals, cannot reach codesafe-internal names, and does not chain to __builtins__['__import__'] in a callable form. Also covers computed/concatenated templates (rejected because a non-literal template cannot be inspected) and the format_map key path, and asserts that a plain '{k}'.format_map({...}) keeps working since it can only read a mapping the expression itself supplied. All three tests fail on the published 0.0.6 and pass on 0.0.7. 114 tests pass. Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com> --- test_poc_vulnerability.py | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test_poc_vulnerability.py b/test_poc_vulnerability.py index f740e51..99d763d 100644 --- a/test_poc_vulnerability.py +++ b/test_poc_vulnerability.py @@ -838,6 +838,71 @@ class Host: with self.assertRaises(UnsafeExpressionError): safe_eval(expr, allow_attributes=True, allowed_vars={"o": Host()}) + def test_reported_format_field_globals_leak(self): + """The reported PoC: a format field reads the helper's module globals. + + Reported by email: on 0.0.3-0.0.6 dunder names inside a str.format + replacement field are string literals, so the AST checker never sees an + ast.Attribute node and the dunder block does not apply. Affected range + was verified against the published wheels for 0.0.3 through 0.0.6; + 0.0.1/0.0.2 predate allow_attributes. The impact is disclosure, not + execution: a replacement field cannot contain a call, and the resolved + value is rendered to str. + """ + secret = "HOST_SECRET_s3cr3t_value_271" + + def helper(x): + return x + 1 + + helper.__globals__["HOST_SECRET"] = secret + try: + for expr in ( + "'{0.__globals__[HOST_SECRET]}'.format(h)", + "'{0.__globals__}'.format(h)", + "'{0.__class__}'.format('')", + "'{0.__class__.__base__.__subclasses__}'.format('')", + "'{0.__globals__[__builtins__]}'.format(h)", + "'{0.__globals__[__builtins__][__import__]}'.format(h)", + ): + with self.assertRaises(UnsafeExpressionError): + safe_eval(expr, allow_attributes=True, allowed_vars={"h": helper}) + finally: + helper.__globals__.pop("HOST_SECRET", None) + + def test_format_templates_that_are_not_literals_are_blocked(self): + """Computed templates are refused too - the check is name-independent. + + A template assembled from pieces (concatenation, join, chr()) cannot be + resolved statically, so the call is rejected rather than guessed at. + """ + for expr in ( + "('{0.' + '__class__' + '}').format('')", + "''.join(['{0.__class__', '}']).format('')", + "f'{chr(123)}0.__class__{chr(125)}'.format('')", + ): + with self.assertRaises((UnsafeExpressionError, RuntimeError)): + safe_eval(expr, allow_attributes=True) + + def test_format_map_plain_key_is_data_only(self): + """format_map with a plain {name} field stays usable and stays local. + + A named field only reads a key from the mapping the expression itself + passed in, so nothing outside the expression is reachable; anything + needing an attribute or a nested key expression is rejected. + """ + self.assertEqual(safe_eval("'{k}'.format_map({'k': 1})", allow_attributes=True), "1") + + def helper(x): + return x + + for expr in ( + "'{0.__globals__}'.format_map({})", + "'{0[S]}'.format_map(h.__globals__)", + "'{__import__}'.format_map(h.__globals__['__builtins__'])", + ): + with self.assertRaises(UnsafeExpressionError): + safe_eval(expr, allow_attributes=True, allowed_vars={"h": helper}) + def test_format_string_leak_is_blocked_without_attributes(self): with self.assertRaises(UnsafeExpressionError): safe_eval("'{0.__class__}'.format(1)")