Skip to content

test: pin the reported str.format dunder-traversal bypass - #8

Merged
johanbeytell1 merged 1 commit into
mainfrom
arena/390295bd-codesafe
Nov 28, 2026
Merged

johanbeytell1 merged 1 commit into
mainfrom
arena/390295bd-codesafe

Conversation

@johanbeytell1

Copy link
Copy Markdown
Member

Regression tests for a bypass reported by email (stilwel, stilwels@gmail.com).

The report

On 0.0.3–0.0.6, dunder names inside a str.format replacement field are string literals, not ast.Attribute nodes, so the 0.0.6 "reject all __-prefixed attribute access" rule never sees them:

safe_eval("'{0.__globals__[HOST_SECRET]}'.format(helper)",
          allow_attributes=True, allowed_vars={'helper': helper})
# 0.0.6 -> 'HOST_SECRET_s3cr3t_value_271'

Verdict

Valid, and already fixed in 0.0.7 — no library change needed. This is the same defect class _FORMAT_STRING_TRAVERSES_ATTRIBUTES already closes; this PR locks it down with tests.

Verified against the published wheels:

Version Result
0.0.1, 0.0.2 N/A — predates allow_attributes (TypeError)
0.0.3 – 0.0.6 leaks the secret
0.0.7 UnsafeExpressionError

The reporter scoped it to 0.0.6 only; the real range is 0.0.3 – 0.0.6.

Severity

Agrees with the reporter: disclosure, not RCE. A replacement field cannot contain a call, and its value is rendered to str. Confirmed the leak reads the helper's own module globals, cannot reach codesafe-internal names (KeyError), and does not chain to a callable __import__.

What the tests cover

  • The reported payloads, including __builtins__ chaining attempts.
  • Computed templates (concatenation, join, chr()) — non-literal templates cannot be inspected, so they are rejected.
  • The format_map key path, plus an assertion that plain '{k}'.format_map({...}) still works (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 locally.

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>
@johanbeytell1
johanbeytell1 merged commit 76fc398 into main Nov 28, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant