π‘οΈ Sentinel: [CRITICAL] Fix Arbitrary Code Execution in ast.Call dynamic type resolution#389
Conversation
β¦mic type resolution Implemented strict whitelisting for `ast.Call` nodes during dynamic type evaluation in `src/codeweaver/core/di/container.py` to prevent Arbitrary Code Execution (ACE) vulnerabilities. Only verified, trusted functions (Depends, depends, Field, PrivateAttr, Tag, Parameter) are allowed. 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 guide (collapsed on small PRs)Reviewer's GuideRestricts dynamic type evaluation in the DI container by whitelisting allowed ast.Call targets during type-string evaluation and documents the security fix in the Sentinel journal. Sequence diagram for whitelisted ast.Call validation in _safe_eval_typesequenceDiagram
participant Caller
participant Container
participant TypeValidator
participant AST
Caller->>Container: _safe_eval_type(type_str, globalns)
Container->>AST: parse(type_str)
Container->>TypeValidator: visit(parsed_ast)
loop visit_nodes
TypeValidator->>TypeValidator: generic_visit(node)
alt [node is ast.Call]
TypeValidator->>TypeValidator: extract func_name
TypeValidator->>TypeValidator: check func_name in allowed_funcs
alt [func_name not allowed]
TypeValidator-->>Container: raise TypeError
Container-->>Caller: TypeError
else [func_name allowed]
TypeValidator-->>Container: continue
end
else [node not ast.Call]
TypeValidator-->>Container: continue
end
end
Container-->>Caller: evaluated_type_or_None
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:
- The
_safe_eval_typemethod is now complex enough to require# noqa: C901; consider extracting the AST validation logic (e.g., call validation, dunder checks) into smaller helper methods or a dedicated validator class to keep this function simpler and easier to reason about. - When rejecting an
ast.Callwherefunc_namecannot be determined (non-Name/Attribute), the error message will showfunc_nameasNone; consider emitting a clearer, generic error in that case and centralizing theallowed_funcsset (e.g., as a constant) so that future updates to the whitelist are easier and less error-prone.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `_safe_eval_type` method is now complex enough to require `# noqa: C901`; consider extracting the AST validation logic (e.g., call validation, dunder checks) into smaller helper methods or a dedicated validator class to keep this function simpler and easier to reason about.
- When rejecting an `ast.Call` where `func_name` cannot be determined (non-Name/Attribute), the error message will show `func_name` as `None`; consider emitting a clearer, generic error in that case and centralizing the `allowed_funcs` set (e.g., as a constant) so that future updates to the whitelist are easier and less error-prone.Help me be more useful! Please click π or π on each comment and I'll use the feedback to improve your reviews.
π¨ Severity: CRITICAL
π‘ Vulnerability: Unrestricted dynamic type evaluation
eval()allowed arbitraryast.Callexecution. By supplying a maliciously crafted type string to the restricted environment, arbitrary Python functions present in the environment's__builtins__or global namespace could be executed.π― Impact: High risk of Arbitrary Code Execution (ACE) allowing malicious logic execution within the host environment.
π§ Fix: Implemented strict whitelisting within
TypeValidatorto only allow explicit and knownast.Callnodes (Depends,depends,Field,PrivateAttr,Tag,Parameter). Also accommodated explicitly prefixed functions (e.g.,cyclopts.Parameter). Rejected unknown functions with aTypeError.β Verification: Tested against standard unit tests and locally verified that arbitrary functions are rejected. Added a corresponding security journal entry tracking the vulnerability.
PR created automatically by Jules for task 3684479189248078740 started by @bashandbone
Summary by Sourcery
Harden dynamic type evaluation in the DI container to prevent arbitrary code execution and document the security fix in the Sentinel journal.
Bug Fixes:
Documentation: