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-22 - Arbitrary Code Execution via unvalidated ast.Call in eval()
**Vulnerability:** Found a critical Arbitrary Code Execution (ACE) vulnerability in `src/codeweaver/core/di/container.py` where type hints evaluated dynamically via `eval()` allowed generic `ast.Call` nodes. This permitted execution of any callable in the module's global namespace.
**Learning:** When validating AST trees for dynamic type evaluation using `eval()`, allowing generic `ast.Call` nodes introduces severe ACE risks. Attackers can potentially execute unintended functions present in the environment.
**Prevention:** Strictly whitelist allowable function names inside `ast.Call` nodes (e.g., `Depends`, `depends`, `Field`, `PrivateAttr`, `Tag`, `Parameter`) before passing the tree to `eval()`.
14 changes: 13 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 @@ -130,6 +130,18 @@ def generic_visit(self, node: ast.AST) -> None:
):
raise TypeError(f"Forbidden AST node in type string: {type(node).__name__}")

# Restrict ast.Call to explicitly whitelisted security-safe functions to prevent Arbitrary Code Execution during eval.
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_funcs = frozenset({"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Avoid recreating the allowed_funcs frozenset on every Call node visit.

Because allowed_funcs is defined inside the visit logic, it’s reallocated for every ast.Call. Since the set is static, please move it to a module-level constant or class attribute (e.g., ALLOWED_CALL_FUNCS) so it’s constructed once and reused.

Suggested implementation:

                    if func_name not in ALLOWED_CALL_FUNCS:

Add a module-level constant in src/codeweaver/core/di/container.py, near the top of the file alongside other constants (or just below the imports), for example:

ALLOWED_CALL_FUNCS = frozenset({"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"})

If this code is inside a class and you prefer a class attribute, define instead:

class SomeVisitorOrContainer(...):
    ALLOWED_CALL_FUNCS = frozenset({"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"})

and reference it as self.ALLOWED_CALL_FUNCS (or cls.ALLOWED_CALL_FUNCS in a @classmethod) in place of ALLOWED_CALL_FUNCS in the snippet above.

if func_name not in allowed_funcs:
raise TypeError(f"Forbidden function call in type string: {func_name}")

# Block dunder access to prevent escaping the restricted environment
if isinstance(node, ast.Name) and node.id.startswith("__"):
raise TypeError(f"Forbidden dunder name: {node.id}")
Expand Down
Loading