You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 641b1e5
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: README.md
+14-1Lines changed: 14 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,18 @@ You can browse all advisories at https://github.com/Infinitode/CodeSafe/security
53
53
-**Workaround for older versions**: leave `allow_attributes` at its default (`False`). There is no complete workaround otherwise - upgrade.
54
54
-**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.
55
55
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
+
56
68
### Critical Vulnerability Fixed in v0.0.5
57
69
58
70
**Versions 0.0.1 through 0.0.4 contain a known sandbox escape vulnerability in `safe_eval()` that allows arbitrary code execution.**
***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.
122
134
***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.
124
136
***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.
125
137
***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.
126
138
***Security Fix**: `__builtins__` is no longer reachable by name from an expression, and can no longer be rebound or mutated.
***Changed**: The `cloudpickle` dependency was removed - CodeSafe no longer imports pickle on the evaluation path at all.
132
144
* Added regression tests for every item above (see [Vulnerability & Security Testing](#vulnerability--security-testing-test_poc_vulnerabilitypy)).
133
145
***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.
| 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 |
0 commit comments