From 8fd86f8f361b05965b97e84f1a98028493870c1a Mon Sep 17 00:00:00 2001 From: CodeSafe Maintainers Date: Tue, 1 Dec 2026 12:24:20 +0000 Subject: [PATCH] docs: correct the v0.0.7 changelog and advisory claims Three claims in the README overstated what 0.0.6 actually allowed. Verified against the published 0.0.6 wheel, running from outside the repository so the installed package is the one under test: 1. Rebinding a safe-builtin name ("(len := f)()", "(lambda len: len())(f)", "[f() for len in [1]]") was labelled a fixed vulnerability. It was not: 0.0.6 rejected all three with UnsafeExpressionError, because its call check required the *target* to be allowlisted regardless of the name used. The 0.0.7 name-resolution rule is defence in depth, and the entry now says so. 2. Re-introducing open through allowed_builtins was described as bypassing file_access=False. It did not: 0.0.6 raised UnsafeExpressionError, and with allowed_function_calls=['open'] the file handle still could not cross the result boundary. The 0.0.7 ordering change makes the invariant structural rather than dependent on the AST check; the entry now says that instead. 3. Callables passed to builtins ("sorted(key=f)", "iter(f, None)") were described as merely "following the same allowlist". In fact 0.0.6 *did* invoke the caller's function - it just failed afterwards (AttributeError for sorted, timeout for iter). That gap is real, so the entry keeps its Security Fix status but now states precisely what happened. Also drops the rebinding claim from the summary bullet list, which repeated the same overstatement. Worth recording why this slipped: earlier probes ran from the repository root, so 'import codesafe' picked up the local 0.0.7 tree instead of the installed 0.0.6 wheel, making pre-fix behaviour look already-blocked. All measurements behind this commit were taken from outside the repository. Docs only; 114 tests unaffected. Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com> --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 074b966..d419d5c 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ You can browse all advisories at https://github.com/Infinitode/CodeSafe/security - **Affected versions**: 0.0.6 only. Versions 0.0.1-0.0.5 are not affected by this specific bug: they return results through a `multiprocessing.Queue` with the standard library's `pickle`, and this gadget cannot survive that encoder (`Can't pickle local object`). Those versions are affected by the earlier advisories instead. Note that 0.0.5's `Queue.get()` does unpickle in the parent, so the underlying pattern is similar, but no working payload of this kind was found for 0.0.5. - **Fixed in**: Version **0.0.7**. The result boundary no longer involves pickle at all: the child encodes only plain data (see below) and the parent reconstructs it from primitives. A returned value has no code path into the caller. - **Workaround for 0.0.6**: none that is complete - treat every expression as fully trusted, or upgrade. -- **Related hardening in the same release**: `str.format`/`format_map` templates that name attributes or keys (`'{0.__class__}'.format(obj)`) are rejected; rebinding a safe-builtin name (`(len := f)()`, `[f() for len in [..]]`, `(lambda len: len())(f)`) no longer lets an expression call a non-allowlisted callable; callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) follow the same allowlist; `__builtins__` is not reachable by name; `SystemExit`/`KeyboardInterrupt`/`GeneratorExit` raised by an expression arrive as a catchable `RuntimeError` instead of killing or unwinding the caller; and `file_access=False` can no longer be bypassed by re-introducing `open` through `allowed_builtins`. +- **Related hardening in the same release**: `str.format`/`format_map` templates that name attributes or keys (`'{0.__class__}'.format(obj)`) are rejected; callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) now follow the same allowlist as direct calls - before this, one arriving through `allowed_vars` was invoked by the sandbox even though it was never allowlisted; `__builtins__` is not reachable by name; and `SystemExit`/`KeyboardInterrupt`/`GeneratorExit` raised by an expression arrive as a catchable `RuntimeError` instead of killing or unwinding the caller. **What `safe_eval()` returns in 0.0.7.** Results are restricted to plain data: `None`, `bool`, `int`, `float`, `complex`, `str`, `bytes`, `decimal.Decimal`, `fractions.Fraction`. Containers of plain data (`list`, `tuple`, `dict`, `set`) are returned only when `allow_objects=True` is passed. Lambdas, classes, instances of user-defined types, modules and generators are no longer returned - they raise a clear `RuntimeError` instead of being serialized. If your code relied on returning lambdas or dynamic classes, restructure the expression to return data. @@ -134,11 +134,11 @@ 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 - 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 (defence in depth)**: Rebinding a safe-builtin name and calling through it (`(len := host_callable)()`, `[host_callable() for len in [..]]`, `(lambda len: len())(host_callable)`) is now refused by an explicit name-resolution rule rather than only by the call-target check. Note that 0.0.6 already rejected every such shape with `UnsafeExpressionError` - this hardens the invariant, it does not close an exploitable hole. +* **Security Fix**: Callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) follow the same allowlist as direct calls. On 0.0.6 such a callable *was* invoked inside the sandbox even though it was never allowlisted - it then failed afterwards (an `AttributeError`, or a timeout for `iter`) - so this closes a real gap in the call policy, though it did not by itself yield code execution in the caller. `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. * **Security Fix**: An expression raising `SystemExit`, `KeyboardInterrupt` or `GeneratorExit` no longer terminates or unwinds the calling process; it arrives as a catchable `RuntimeError`. Exceptions of user-defined types are reported the same way instead of being reconstructed in the parent. -* **Security Fix**: `file_access=False` and `network_access=False` are enforced *after* `allowed_builtins` is merged, so passing `open` (or similar) through `allowed_builtins` no longer bypasses the flag. +* **Security (defence in depth)**: `file_access=False` and `network_access=False` are now enforced *after* `allowed_builtins` is merged, so the guarantee no longer depends on the AST call check happening to reject the target first. In 0.0.6 the bypass was already unreachable - `open` passed through `allowed_builtins` was rejected with `UnsafeExpressionError` unless it was *also* listed in `allowed_function_calls`, and in that case the file handle could not cross the result boundary. This makes the invariant structural. * **Security**: Returned data is bounded (nesting depth 100, 1M nodes, 1 MiB of `str`/`bytes`), and int-to-str conversion is capped for the duration of a call so a returned giant integer cannot burn CPU in the caller. * **Changed**: The result transport is a `multiprocessing.Pipe` drained by a reader thread instead of a `Queue`. A result larger than the OS pipe buffer can no longer deadlock the parent, and the timing of `EvaluationTimeoutError` is tighter. * **Changed**: The `cloudpickle` dependency was removed - CodeSafe no longer imports pickle on the evaluation path at all.