Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
21 changes: 20 additions & 1 deletion src/codeweaver/core/di/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Comment on lines +148 to +152
"Tag",
"Parameter",
}:
raise TypeError(f"Forbidden function call in type string: {func_name}")

Comment on lines +141 to +157
super().generic_visit(node)

try:
Expand Down
Loading