Skip to content

Commit 641b1e5

Browse files
Merge pull request #10 from Infinitode/arena/390295bd-codesafe
docs: credit @stilwel and document the str.format disclosure
2 parents 10b1e1d + 06d5471 commit 641b1e5

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ You can browse all advisories at https://github.com/Infinitode/CodeSafe/security
5353
- **Workaround for older versions**: leave `allow_attributes` at its default (`False`). There is no complete workaround otherwise - upgrade.
5454
- **Advisory**: [GHSA-m85r-gp57-6p5q](https://github.com/Infinitode/CodeSafe/security/advisories/GHSA-m85r-gp57-6p5q) (High, 8.1). Fixed in v0.0.6, which is live on PyPI. A CVE ID will be requested from and assigned by GitHub when the advisory is published; see the advisory for the CVE once it has been assigned.
5555

56+
### Sandbox escape via `str.format` replacement fields (fixed in v0.0.7)
57+
58+
**Versions 0.0.3 through 0.0.6, when `safe_eval()` is called with `allow_attributes=True` (not the default), let an expression read any attribute or mapping key by naming it inside a `str.format()` / `str.format_map()` replacement field.**
59+
60+
- **Vulnerability**: The AST checker only inspects `ast.Attribute` nodes, i.e. attribute names written literally as `obj.attr`. A name inside a replacement field is a plain string constant: `'{0.__class__}'.format(obj)` contains no `ast.Attribute` node at all, because `str.format` performs the lookup itself at runtime. The dunder block therefore did not apply inside templates. The traversal was not limited to dunders - `'{0.secret_attr}'.format(obj)` read any attribute and `'{0[secret]}'.format(d)` read any mapping key.
61+
- **Impact**: Disclosure of host data reachable from anything passed through `allowed_vars`/`allowed_builtins`: module globals (API tokens, connection strings, loaded modules such as `os`), attributes of supplied objects, and arbitrary mapping keys. This is **read-only**: a replacement field cannot contain a call and the resolved value is rendered to `str`, so no code execution was found (CVSS 3.1: 5.9, `AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:N/A:N`).
62+
- **Affected versions**: 0.0.3, 0.0.4, 0.0.5, 0.0.6 (0.0.1 and 0.0.2 predate `allow_attributes` and are not affected by this issue).
63+
- **Fixed in**: Version **0.0.7** - every `str.format`/`format_map` template is now inspected, and any replacement field naming an attribute or a key is rejected. Plain fields (`'{}'`, `'{0:>3}'`, `'{name}'`) still work, nested spec fields are unwrapped and checked, and non-literal templates (built with concatenation, `join` or `chr()`) are rejected because they cannot be inspected.
64+
- **Workaround for older versions**: leave `allow_attributes` at its default (`False`), and do not pass secret-bearing objects through `allowed_vars`/`allowed_builtins`. Otherwise, upgrade.
65+
66+
**Credit**: Reported by [@stilwel](https://github.com/stilwel) - thank you for the clear write-up.
67+
5668
### Critical Vulnerability Fixed in v0.0.5
5769

5870
**Versions 0.0.1 through 0.0.4 contain a known sandbox escape vulnerability in `safe_eval()` that allows arbitrary code execution.**
@@ -120,7 +132,7 @@ python -m unittest test.py test_poc_vulnerability.py -v
120132

121133
* **Security Fix (critical)**: `safe_eval()` no longer deserializes the evaluation result with `cloudpickle` in the calling process. In 0.0.6 the child serialized its result and the parent called `cloudpickle.loads()` on it with full builtins, so an expression could return a `__reduce__` gadget that ran arbitrary code in the *caller* - outside the sandbox that had just been enforced. The result now crosses the process boundary as plain primitives and is rebuilt with a non-executable decoder. See the [Security Advisory](#security-advisory) above.
122134
* **Changed**: `safe_eval()` returns plain data only - `None`, `bool`, `int`, `float`, `complex`, `str`, `bytes`, `Decimal`, `Fraction`. Containers of plain data are opt-in via the new `allow_objects=True` parameter. Lambdas, dynamic classes, instances of user-defined types, modules and generators raise a clear `RuntimeError` instead of being pickled. **This removes a feature that 0.0.6 advertised**; it was also the attack surface.
123-
* **Security Fix**: `str.format`/`format_map` templates that traverse attributes or mapping keys (`'{0.__class__}'.format(obj)`, `'{0[key]}'.format(obj)`) are rejected. They read attributes without ever producing an `ast.Attribute` node for the checker to inspect.
135+
* **Security Fix**: `str.format`/`format_map` templates that traverse attributes or mapping keys (`'{0.__class__}'.format(obj)`, `'{0[key]}'.format(obj)`) are rejected - a replacement field is a string constant, so these lookups produce no `ast.Attribute` node for the checker to inspect. Reported by [@stilwel](https://github.com/stilwel); see the [Security Advisory](#security-advisory) above.
124136
* **Security Fix**: An expression can no longer rebind a safe-builtin *name* and then call it - `(len := host_callable)()`, `[host_callable() for len in [..]]` and `(lambda len: len())(host_callable)` previously reached a caller-supplied callable that was not on `allowed_function_calls`. Such calls now require explicit allowlisting.
125137
* **Security Fix**: Callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) follow the same allowlist as direct calls. `allowed_vars` entries are data, not implicit call permissions.
126138
* **Security Fix**: `__builtins__` is no longer reachable by name from an expression, and can no longer be rebound or mutated.
@@ -131,6 +143,7 @@ python -m unittest test.py test_poc_vulnerability.py -v
131143
* **Changed**: The `cloudpickle` dependency was removed - CodeSafe no longer imports pickle on the evaluation path at all.
132144
* Added regression tests for every item above (see [Vulnerability & Security Testing](#vulnerability--security-testing-test_poc_vulnerabilitypy)).
133145
* **Credit**: The critical result-deserialization issue above was reported by **Juan Lucas Armendares** of **Sunplace Solutions** - many thanks. See [GHSA-2565-87hp-2588](https://github.com/Infinitode/CodeSafe/security/advisories/GHSA-2565-87hp-2588).
146+
* **Credit**: The `str.format` replacement-field traversal (`'{0.__class__}'.format(obj)` and similar) was reported by [@stilwel](https://github.com/stilwel) - many thanks.
134147

135148
### Changelog v0.0.6:
136149

‎RELEASE-NOTES-0.0.8.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 0.0.8 release note
2+
3+
PyPI snapshots the README into each uploaded version's project page, so credits
4+
and advisory links added to the README *after* 0.0.7 was published are not yet
5+
reflected on https://pypi.org/project/codesafe/. They appear on PyPI with the
6+
next upload.
7+
8+
This note records what changes on the PyPI project page when 0.0.8 is published,
9+
so the reporters can be told exactly when their credit becomes visible there.
10+
11+
## Credits appearing on PyPI for the first time in 0.0.8
12+
13+
- **Juan Lucas Armendares** of **Sunplace Solutions** - reported the
14+
result-deserialization RCE (GHSA-2565-87hp-2588).
15+
- **[@stilwel](https://github.com/stilwel)** - reported the `str.format`
16+
replacement-field traversal.
17+
18+
Both were added to the README after the 0.0.7 upload. They are already visible
19+
on GitHub in the [Security Advisory](README.md#security-advisory) section and
20+
the v0.0.7 changelog.
21+
22+
## Also new on the PyPI page in 0.0.8
23+
24+
- The `str.format` replacement-field traversal section (affected 0.0.3 - 0.0.6,
25+
fixed 0.0.7) and its advisory link.
26+
- The GHSA ID `GHSA-2565-87hp-2588` replacing the "GHSA to be assigned"
27+
placeholder in the advisory table.
28+
- The `SECURITY.md` note documenting the email reporting fallback, and the
29+
request to check spam for replies.
30+
31+
## Publishing
32+
33+
Any tag pushed triggers `.github/workflows/build_and_publish.yml`, which requires
34+
the tag to equal the version in `pyproject.toml`. There are no GitHub Releases
35+
in this repository - PyPI is the only release surface, and GitHub is the
36+
canonical README and advisory home.

‎SECURITY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Only the latest release receives fixes. Please upgrade rather than report issues
4646
| `safe_eval` sandbox escape via indirect subscript call | CVE-2026-133467 · [GHSA-7gp8-3j6q-v4h8](https://github.com/Infinitode/CodeSafe/security/advisories/GHSA-7gp8-3j6q-v4h8) | 0.0.1 - 0.0.4 | 0.0.5 |
4747
| `safe_eval` sandbox escape via dunder attribute string-argument (`allow_attributes=True`) | [GHSA-m85r-gp57-6p5q](https://github.com/Infinitode/CodeSafe/security/advisories/GHSA-m85r-gp57-6p5q) · CVE pending | 0.0.3 - 0.0.5 | 0.0.6 |
4848
| Remote code execution in the calling process via unsafe deserialization of the `safe_eval()` result | [GHSA-2565-87hp-2588](https://github.com/Infinitode/CodeSafe/security/advisories/GHSA-2565-87hp-2588) · CVE pending | 0.0.6 | 0.0.7 |
49+
| `safe_eval` attribute/key disclosure via `str.format` replacement fields (`allow_attributes=True`) | [GHSA-2565-87hp-2588](https://github.com/Infinitode/CodeSafe/security/advisories/GHSA-2565-87hp-2588) · advisory pending | 0.0.3 - 0.0.6 | 0.0.7 |
4950

5051
See https://github.com/Infinitode/CodeSafe/security/advisories for the canonical list.
5152

0 commit comments

Comments
 (0)