diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 1959a5253..ce9c7feb3 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 - Restricting ast.Call to Prevent Arbitrary Code Execution during Type String Eval +**Vulnerability:** The AST validation logic in `_safe_eval_type` (`src/codeweaver/core/di/container.py`) permitted generic `ast.Call` nodes. Since the subsequent evaluation used `eval()` with the module's global namespace, this allowed for Arbitrary Code Execution (ACE) if an attacker could inject an arbitrary function call into a string-based type annotation. +**Learning:** Even when limiting builtins, evaluating strings as code (`eval`) with access to an entire module's namespace is inherently risky. `ast.Call` nodes must be strictly constrained. +**Prevention:** Whitelist `ast.Call` nodes specifically to the required subset of safe, metadata-constructing functions (e.g., `Depends`, `Field`, `Parameter`). Never allow unrestrained function calls when dynamically resolving type annotations. diff --git a/src/codeweaver/core/di/container.py b/src/codeweaver/core/di/container.py index 7cd68ce98..d3ec26126 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,19 @@ 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}") + # Restricting generic ast.Call nodes to a strict whitelist prevents Arbitrary Code + # Execution (ACE) vulnerabilities during type string resolution. + if isinstance(node, ast.Call): + allowed_calls = {"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"} + 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 allowed_calls: + raise TypeError(f"Forbidden function call in type string: {func_name}") + super().generic_visit(node) try: