From 76cdda43b6f1b009619598b30224583231f0c37e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 13 Jun 2026 17:45:39 +0000 Subject: [PATCH] fix: restrict ast.Call nodes to explicitly whitelisted functions in di container This commit addresses an Arbitrary Code Execution (ACE) vulnerability by strictly restricting the allowed `ast.Call` nodes during the AST type string validation process inside the Dependency Injection (DI) Container's `_safe_eval_type` function. Unrestricted `ast.Call` evaluation could execute arbitrary functions defined in the global namespace via Python's `eval()` function. We have now enforced a whitelist that explicitly limits calls exclusively to necessary functions: `Depends`, `depends`, `Field`, `PrivateAttr`, `Tag`, and `Parameter`. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com> --- .jules/sentinel.md | 4 ++++ src/codeweaver/core/di/container.py | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 1959a5253..a471d3dcb 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-22 - Unrestricted AST Call nodes in _safe_eval_type +**Vulnerability:** Found `ast.Call` included in the allowed nodes list of `_safe_eval_type` in `src/codeweaver/core/di/container.py` without validation, which introduces a critical Arbitrary Code Execution (ACE) vulnerability as it permits execution of any callable in the module's global namespace via `eval()`. +**Learning:** Permitting generic `ast.Call` nodes when parsing untrusted or complex type strings dynamically evaluated via `eval()` can lead to ACE if the type string references available functions in the global scope. +**Prevention:** Always restrict `ast.Call` nodes to a strict whitelist of explicitly required, known-safe functions (e.g., `Depends`, `Field`) when utilizing AST validation prior to dynamic evaluation. diff --git a/src/codeweaver/core/di/container.py b/src/codeweaver/core/di/container.py index 7cd68ce98..eb4884009 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,20 @@ 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 Enhancement: Prevent Arbitrary Code Execution (ACE) + # Allowing generic ast.Call nodes could permit execution of any callable in the global namespace. + # Restrict to known safe functions used 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 + + allowed_calls = {"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"} + if func_name not in allowed_calls: + raise TypeError(f"Forbidden function call in type string: {func_name}") + super().generic_visit(node) try: