From 4b149a82deed1ea8ffbe70abbb11095527781a16 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 3 Jun 2026 17:26:03 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITICAL]?= =?UTF-8?q?=20Prevent=20arbitrary=20code=20execution=20in=20AST=20type=20v?= =?UTF-8?q?alidation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com> --- .jules/sentinel.md | 4 ++++ src/codeweaver/core/di/container.py | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 1959a5253..f058de1e9 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -4,3 +4,7 @@ **Vulnerability:** Found an unused `_attempt_import` function in `src/codeweaver/server/mcp/server.py` that dynamically imports a module directly from unvalidated configuration (`import_module(mw.rsplit(".", 1)[0])`), leading to potential arbitrary code execution. **Learning:** Functions that perform dynamic imports should not be left around in the codebase if they are unused, especially if they are designed to take unvalidated strings as input. **Prevention:** Avoid dynamic imports based on configuration or inputs without strict whitelisting. Use tools like `semgrep` with python security rules to actively catch these patterns. +## 2026-04-21 - AST Arbitrary Code Execution Prevented +**Vulnerability:** Found an Arbitrary Code Execution (ACE) vulnerability during dynamic type evaluation via safe `eval()`. The AST validation didn't restrict `ast.Call` nodes, allowing any arbitrary callable in the module's global namespace to be executed during type string resolution. +**Learning:** Even when `eval()` restricts `__builtins__` and dunder accesses, generic `ast.Call` nodes are extremely dangerous. A malicious type annotation string can still invoke arbitrary functions that are available in the module's `globalns`, resulting in arbitrary code execution. +**Prevention:** `ast.Call` nodes must be strictly whitelisted to specific, required functions like `Depends`, `depends`, `Field`, `PrivateAttr`, `Tag`, and `Parameter`. Never trust type annotations derived from external or potentially unvalidated sources. Always whitelist explicit nodes rather than simply allowing classes of node types. diff --git a/src/codeweaver/core/di/container.py b/src/codeweaver/core/di/container.py index 7cd68ce98..dc0802b0b 100644 --- a/src/codeweaver/core/di/container.py +++ b/src/codeweaver/core/di/container.py @@ -84,7 +84,7 @@ def __init__(self) -> None: self._request_cache: dict[Any, Any] = {} # Keys can be types or callables self._providers_loaded: bool = False # Track if auto-discovery has run - def _safe_eval_type(self, type_str: str, globalns: dict[str, Any]) -> Any | None: + def _safe_eval_type(self, type_str: str, globalns: dict[str, Any]) -> Any | None: # noqa: C901 """Safely evaluate a type string using AST validation. Parses the type string into an AST, validates that it contains only safe @@ -136,6 +136,25 @@ def generic_visit(self, node: ast.AST) -> None: if isinstance(node, ast.Attribute) and node.attr.startswith("__"): raise TypeError(f"Forbidden dunder attribute: {node.attr}") + # Security concern: prevent arbitrary code execution by strictly + # limiting the functions that can be called in type annotations. + if isinstance(node, ast.Call): + func_name = None + if isinstance(node.func, ast.Name): + func_name = node.func.id + elif isinstance(node.func, ast.Attribute): + func_name = node.func.attr + + if func_name not in { + "Depends", + "depends", + "Field", + "PrivateAttr", + "Tag", + "Parameter", + }: + raise TypeError(f"Forbidden function call in type string: {func_name}") + super().generic_visit(node) try: