π‘οΈ Sentinel: [CRITICAL] Fix ACE vulnerability in _safe_eval_type string evaluation#395
π‘οΈ Sentinel: [CRITICAL] Fix ACE vulnerability in _safe_eval_type string evaluation#395bashandbone wants to merge 1 commit into
Conversation
β¦ng evaluation 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 GuideTightens the AST-based validation in Sequence diagram for updated _safe_eval_type AST call validationsequenceDiagram
participant Caller
participant Container
participant TypeValidator
participant eval
Caller->>Container: _safe_eval_type(type_str, globalns)
Container->>TypeValidator: visit(parsed_ast)
TypeValidator->>TypeValidator: generic_visit(node)
alt node is ast.Call
TypeValidator->>TypeValidator: [extract func_name]
alt func_name in allowed_calls
TypeValidator-->>Container: validation_ok
Container->>eval: eval(type_str, globalns)
eval-->>Container: evaluated_type
Container-->>Caller: evaluated_type
else func_name not in allowed_calls
TypeValidator-->>Container: raise TypeError
Container-->>Caller: TypeError
end
else node is not ast.Call
TypeValidator-->>Container: validation_ok
Container->>eval: eval(type_str, globalns)
eval-->>Container: evaluated_type
Container-->>Caller: evaluated_type
end
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 extracting the
TypeValidatorcall-handling logic (and possibly the whole validator) into a dedicated helper/class or module-level function instead of suppressing C901, which will keep_safe_eval_typesimpler and easier to maintain as additional cases are added. - The
allowed_callsset is recreated on everyast.Callvisit; moving this to a module- or class-level constant (e.g.ALLOWED_TYPE_CALLS) would avoid repeated allocations and also centralize the whitelist for easier future updates.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the `TypeValidator` call-handling logic (and possibly the whole validator) into a dedicated helper/class or module-level function instead of suppressing C901, which will keep `_safe_eval_type` simpler and easier to maintain as additional cases are added.
- The `allowed_calls` set is recreated on every `ast.Call` visit; moving this to a module- or class-level constant (e.g. `ALLOWED_TYPE_CALLS`) would avoid repeated allocations and also centralize the whitelist for easier future updates.Help me be more useful! Please click π or π on each comment and I'll use the feedback to improve your reviews.
π¨ Severity: CRITICAL
π‘ Vulnerability: The AST validation logic in
_safe_eval_type(src/codeweaver/core/di/container.py) permitted genericast.Callnodes. Since the subsequent evaluation usedeval()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.π― Impact: An attacker who controls configuration or input that is parsed as a string-based type annotation could execute arbitrary shell commands or malicious Python code within the application context, completely compromising the host system.
π§ Fix: Modified the
TypeValidatorto explicitly intercept and strictly whitelistast.Callnodes to only allow safe metadata-constructing functions likeDepends,depends,Field,Parameter,Tag, andPrivateAttr. Added# noqa: C901to suppress McCabe complexity warnings from the added branches.β Verification: Verified by running unit tests (
pytest tests/unit/core/di/) and ensuring thateval()on type strings correctly raises aTypeErrorwhen malicious functions are attempted while continuing to allow required dependency injection metadata calls.PR created automatically by Jules for task 5687974914215770184 started by @bashandbone
Summary by Sourcery
Tighten validation of string-based type annotations to prevent arbitrary code execution during safe type evaluation.
Bug Fixes:
_safe_eval_typeby whitelisting allowedast.Calltargets when evaluating type strings.Documentation:
ast.Callnodes during type string evaluation.