Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ You can browse all advisories at https://github.com/Infinitode/CodeSafe/security
- **Workaround for older versions**: leave `allow_attributes` at its default (`False`). There is no complete workaround otherwise - upgrade.
- **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.

### Sandbox escape via `str.format` replacement fields (fixed in v0.0.7)

**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.**

- **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.
- **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`).
- **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).
- **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.
- **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.

**Credit**: Reported by [@stilwel](https://github.com/stilwel) - thank you for the clear write-up.

### Critical Vulnerability Fixed in v0.0.5

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

* **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.
* **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.
* **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.
* **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.
* **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.
* **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.
* **Security Fix**: `__builtins__` is no longer reachable by name from an expression, and can no longer be rebound or mutated.
Expand All @@ -131,6 +143,7 @@ python -m unittest test.py test_poc_vulnerability.py -v
* **Changed**: The `cloudpickle` dependency was removed - CodeSafe no longer imports pickle on the evaluation path at all.
* Added regression tests for every item above (see [Vulnerability & Security Testing](#vulnerability--security-testing-test_poc_vulnerabilitypy)).
* **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).
* **Credit**: The `str.format` replacement-field traversal (`'{0.__class__}'.format(obj)` and similar) was reported by [@stilwel](https://github.com/stilwel) - many thanks.

### Changelog v0.0.6:

Expand Down
36 changes: 36 additions & 0 deletions RELEASE-NOTES-0.0.8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 0.0.8 release note

PyPI snapshots the README into each uploaded version's project page, so credits
and advisory links added to the README *after* 0.0.7 was published are not yet
reflected on https://pypi.org/project/codesafe/. They appear on PyPI with the
next upload.

This note records what changes on the PyPI project page when 0.0.8 is published,
so the reporters can be told exactly when their credit becomes visible there.

## Credits appearing on PyPI for the first time in 0.0.8

- **Juan Lucas Armendares** of **Sunplace Solutions** - reported the
result-deserialization RCE (GHSA-2565-87hp-2588).
- **[@stilwel](https://github.com/stilwel)** - reported the `str.format`
replacement-field traversal.

Both were added to the README after the 0.0.7 upload. They are already visible
on GitHub in the [Security Advisory](README.md#security-advisory) section and
the v0.0.7 changelog.

## Also new on the PyPI page in 0.0.8

- The `str.format` replacement-field traversal section (affected 0.0.3 - 0.0.6,
fixed 0.0.7) and its advisory link.
- The GHSA ID `GHSA-2565-87hp-2588` replacing the "GHSA to be assigned"
placeholder in the advisory table.
- The `SECURITY.md` note documenting the email reporting fallback, and the
request to check spam for replies.

## Publishing

Any tag pushed triggers `.github/workflows/build_and_publish.yml`, which requires
the tag to equal the version in `pyproject.toml`. There are no GitHub Releases
in this repository - PyPI is the only release surface, and GitHub is the
canonical README and advisory home.
1 change: 1 addition & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Only the latest release receives fixes. Please upgrade rather than report issues
| `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 |
| `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 |
| 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 |
| `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 |

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

Expand Down
Loading