Skip to content

Commit 6d4411f

Browse files
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>
1 parent e10a97d commit 6d4411f

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

‎test_poc_vulnerability.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)