Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions test_poc_vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
Loading