Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
Loading