From 02dd857a5d9686826307f68778016d948670fdf2 Mon Sep 17 00:00:00 2001 From: CodeSafe Maintainers Date: Tue, 1 Dec 2026 12:29:31 +0000 Subject: [PATCH] docs: correct the file_access and callable-allowlist claims (take 2) PR #13 got these wrong, and its own measurement method was at fault. Re-ran every case against the installed 0.0.6 wheel from outside the repository, with a counting host function, so 'was it called' is measured rather than inferred: sorted([2,1], key=f), f unallowlisted -> 0.0.6 returned [2, 1], host_calls=0 max([2,1], key=f) -> 0.0.6 returned 2, host_calls=0 iter(f, None) -> 0.0.6 timed out, host_calls=0 So 0.0.6 never invoked these callbacks. PR #13 claimed it did; that came from a marker file written by the *0.0.7* run, because the probe executed from the repository root where 'import codesafe' resolves to the local tree. Same class of mistake as the one #13 was meant to fix. Corrected: - Callables via builtins: 0.0.6 already refused these paths. 0.0.7's allowlist rule is defence in depth, not a fix. Reclassified. - file_access: measured again and PR #13 was wrong in the other direction. With open in allowed_builtins AND in allowed_function_calls, 0.0.6 did create the file despite file_access=False. So this IS a real fix (misconfiguration- dependent, not a sandbox escape). Restored to Security Fix with the precondition stated. - Rebinding: unchanged from #13 - 0.0.6 rejected all shapes, defence in depth. Docs only; 114 tests unaffected. Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com> --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d419d5c..f2c7fd3 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; 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. +- **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, so an `allowed_vars` entry can no longer be reached as an implicit call grant; `__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. @@ -135,10 +135,10 @@ python -m unittest test.py test_poc_vulnerability.py -v * **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 (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 (defence in depth)**: Callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`, `max(key=...)`) now go through the same allowlist as direct calls. 0.0.6 already refused these paths - `sorted([2,1], key=f)` with an unallowlisted `f` returned without ever calling it, and `iter(f, None)` timed out without calling it - so this is not a fixed hole. It makes the rule explicit: `allowed_vars` entries are data, never an implicit call grant. * **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 (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 Fix**: `file_access=False` and `network_access=False` are now enforced *after* `allowed_builtins` is merged, so the flags are authoritative regardless of what the host supplies. In 0.0.6 the flag could be bypassed by passing `open` in `allowed_builtins` *and* naming it in `allowed_function_calls`: the expression then opened and wrote the file inside the evaluation subprocess despite `file_access=False`. It required the host to grant both, so this was a misconfiguration-dependent bypass rather than a sandbox escape, but the flag was not being honoured. * **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.