🛡️ Sentinel: [CRITICAL] Prevent Arbitrary Code Execution in AST validation#368
🛡️ Sentinel: [CRITICAL] Prevent Arbitrary Code Execution in AST validation#368bashandbone wants to merge 1 commit into
Conversation
…ation - Added strict whitelisting to ast.Call nodes in `_safe_eval_type`. - This prevents execution of arbitrary callables via dynamic type string evaluation. - Added corresponding documentation in `.jules/sentinel.md`. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
|
đź‘‹ Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a đź‘€ emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's GuideTightens AST-based validation in the DI container to block arbitrary function calls during type-string evaluation by whitelisting only a small set of safe callables, and documents the security fix in the Sentinel journal. Flow diagram for safe type-string evaluation in _safe_eval_typeflowchart TD
A[type_str] --> B[ast.parse]
B --> C[TypeValidator.visit]
C --> D{ast node type}
D -->|Call| E[visit_Call]
D -->|Other| F[generic_visit]
E --> G{func_name in allowed_calls}
G -->|No| H[raise TypeError]
G -->|Yes| I[generic_visit]
F --> J[validation continues]
I --> J
J --> K[eval type_str with globalns]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
🤖 Hi @bashandbone, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
|
🤖 I'm sorry @bashandbone, but I was unable to process your request. Please see the logs for more details. |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider moving
allowed_calls = {"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"}to a class-level constant or shared module-level set so it isn’t reallocated on everyvisit_Calland can be reused/updated more easily. - When
node.funcis neitherast.Namenorast.Attribute,func_nameremains an empty string, which produces a slightly confusing error message; you might want to either reject those nodes with a clearer message or include the fullast.dump(node.func)(or similar) in the exception for easier debugging.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider moving `allowed_calls = {"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"}` to a class-level constant or shared module-level set so it isn’t reallocated on every `visit_Call` and can be reused/updated more easily.
- When `node.func` is neither `ast.Name` nor `ast.Attribute`, `func_name` remains an empty string, which produces a slightly confusing error message; you might want to either reject those nodes with a clearer message or include the full `ast.dump(node.func)` (or similar) in the exception for easier debugging.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR hardens CodeWeaver’s DI container string-annotation resolution by tightening AST validation before eval() is used, with the goal of preventing arbitrary code execution during type evaluation. It also records the incident and mitigation approach in the Sentinel security journal.
Changes:
- Add a
visit_Calloverride in_safe_eval_type’s AST validator to restrict callable execution to a small whitelist. - Mark
_safe_eval_typewith# noqa: C901(complexity). - Add a Sentinel journal entry documenting the ACE vulnerability and prevention guidance.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/codeweaver/core/di/container.py |
Restricts which ast.Call nodes are permitted during type-string evaluation to reduce ACE risk. |
.jules/sentinel.md |
Documents the vulnerability, learning, and prevention approach in the Sentinel journal. |
đź’ˇ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def visit_Call(self, node: ast.Call) -> None: | ||
| # Security Fix: Prevent Arbitrary Code Execution (ACE) | ||
| # Allowing generic ast.Call nodes permits execution of any callable in the globalns. | ||
| # We strictly limit ast.Call nodes to safe, required functions. | ||
| func_name = "" | ||
| 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}") | ||
| self.generic_visit(node) | ||
|
|
🚨 Severity: CRITICAL
đź’ˇ Vulnerability: The
TypeValidatorclass inside_safe_eval_typepreviously usedast.parseand implicitly allowed allast.Callnodes to bypass validation. This meant any callable (likeos.systemviasys.modules) present in theglobalnsdictionary could be executed arbitrarily when the string representation of a type annotation was parsed and then executed using the built-ineval()function.🎯 Impact: This acts as an Arbitrary Code Execution (ACE) vulnerability. Any potentially unsanitized or user-injected code evaluated via
_safe_eval_typeduring dependency resolution could execute malicious payloads under the permissions of the host system running CodeWeaver.đź”§ Fix: Created a custom
visit_Callmethod for theast.NodeVisitorthat explicitly checks the function name being executed. The logic limits execution to exactly six safe callables that CodeWeaver intrinsically relies upon for dependency injection (Depends,depends,Field,PrivateAttr,Tag, andParameter). Any otherast.Callnodes actively trigger aTypeError. Added an inline security comment documenting the reason.âś… Verification: Ran
uv run pytest tests/unit/core/ --no-covlocally to ensure no valid DI annotations were falsely flagged. The entire test suite passed, and code-linting checks viamise //:checkpassed successfully as well. Added a Sentinel journal entry reflecting the learnings to ensure dynamic evaluation vulnerabilities are continually monitored.PR created automatically by Jules for task 17808606302312772616 started by @bashandbone
Summary by Sourcery
Harden AST-based type string evaluation in the DI container to block arbitrary code execution and document the security lesson in the Sentinel journal.
Bug Fixes:
Documentation: