Skip to content

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution in AST evaluation#381

Open
bashandbone wants to merge 1 commit into
mainfrom
sentinel-fix-ast-eval-ace-11637076273986893769
Open

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution in AST evaluation#381
bashandbone wants to merge 1 commit into
mainfrom
sentinel-fix-ast-eval-ace-11637076273986893769

Conversation

@bashandbone

@bashandbone bashandbone commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

🚨 Severity: CRITICAL
💡 Vulnerability: Found a vulnerability in src/codeweaver/core/di/container.py where ast.Call nodes were allowed without any whitelisting during dynamic type evaluation via ast.parse(type_str, mode="eval").
🎯 Impact: This could lead to Arbitrary Code Execution (ACE) if an attacker can control the type string being evaluated, as any callable in the module's global namespace could be executed.
🔧 Fix: Restricted ast.Call nodes to specific allowed functions (Depends, depends, Field, PrivateAttr, Tag, Parameter) before allowing AST evaluation. Added a # noqa: C901 comment to fix the complexity error that Ruff flagged. Also added a journal entry to .jules/sentinel.md.
Verification: Ran uv run pytest tests/unit/core/di --no-cov to verify the patch doesn't introduce regressions. Evaluated code via mise //:format and mise //:check.


PR created automatically by Jules for task 11637076273986893769 started by @bashandbone

Summary by Sourcery

Restrict dynamic type AST evaluation to prevent arbitrary code execution via unsafe function calls.

Bug Fixes:

  • Block arbitrary function calls in AST-based type string evaluation by whitelisting allowed call targets.

Documentation:

  • Document the AST evaluation vulnerability and its mitigation in the Sentinel security journal.

Restricts `ast.Call` nodes during dynamic type evaluation to a specific whitelist (Depends, depends, Field, PrivateAttr, Tag, Parameter) to prevent Arbitrary Code Execution (ACE). Also includes corresponding `# noqa: C901` directive to satisfy complexity linters.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 6, 2026 17:29
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Tightens AST-based type string evaluation in the DI container by whitelisting allowed function calls in ast.Call nodes, suppresses a Ruff complexity warning, and documents the security fix in the Sentinel journal.

Flow diagram for updated AST type evaluation whitelist

flowchart TD
    A["_safe_eval_type(type_str, globalns)"] --> B["ast.parse(type_str, mode=eval)"]
    B --> C["SafeTypeVisitor.visit(tree)"]
    C --> D{"node is ast.Call?"}
    D -- No --> E["super().generic_visit(node)"]
    D -- Yes --> F["Extract func_name from ast.Name or ast.Attribute"]
    F --> G{"func_name in {Depends, depends, Field, PrivateAttr, Tag, Parameter}?"}
    G -- No --> H["raise TypeError(Forbidden function call in type string)"]
    G -- Yes --> E
    E --> I["eval(compile(tree, filename, mode=eval), globalns)"]
Loading

File-Level Changes

Change Details Files
Harden AST validation in _safe_eval_type to prevent arbitrary code execution from type strings.
  • Leave overall _safe_eval_type structure intact but add an explicit guard for ast.Call nodes inside the inner SafeTypeVisitor.generic_visit
  • Extract the function name from ast.Call nodes, supporting both ast.Name and ast.Attribute forms
  • Reject any ast.Call where the resolved function name is not in a small allowlist of safe functions: Depends, depends, Field, PrivateAttr, Tag, Parameter
  • Raise a TypeError with a clear message when a forbidden function call is encountered in a type string
src/codeweaver/core/di/container.py
Address tooling constraints and document the security incident.
  • Add a Ruff complexity suppression comment (# noqa: C901) on the _safe_eval_type method definition to avoid refactoring during the security fix
  • Append a new dated entry to .jules/sentinel.md describing the AST evaluation vulnerability, its impact, and recommended preventive practices
src/codeweaver/core/di/container.py
.jules/sentinel.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

🤖 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.

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've left some high level feedback:

  • Consider extracting the allowed function names for ast.Call into a shared constant (e.g., ALLOWED_TYPE_CALLS) so they’re easier to audit and update centrally for future security reviews.
  • The error message for forbidden calls will currently render func_name as None for more complex call expressions; you may want to special-case this to give a clearer message (e.g., include the underlying AST or source snippet) to aid debugging without leaking sensitive details.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the allowed function names for `ast.Call` into a shared constant (e.g., `ALLOWED_TYPE_CALLS`) so they’re easier to audit and update centrally for future security reviews.
- The error message for forbidden calls will currently render `func_name` as `None` for more complex call expressions; you may want to special-case this to give a clearer message (e.g., include the underlying AST or source snippet) to aid debugging without leaking sensitive details.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @bashandbone, but I was unable to process your request. Please see the logs for more details.

Copilot AI left a comment

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.

Pull request overview

This PR hardens CodeWeaver’s DI container type-string resolution to reduce the risk of arbitrary code execution when evaluating stringified annotations via AST + eval().

Changes:

  • Adds an ast.Call validation step in _safe_eval_type() to whitelist allowed call targets before evaluating the parsed AST.
  • Suppresses Ruff complexity warning for _safe_eval_type() with # noqa: C901.
  • Documents the vulnerability and mitigation in .jules/sentinel.md.

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 Adds AST Call-node whitelisting to constrain what can be invoked during type-string evaluation.
.jules/sentinel.md Adds a Sentinel journal entry describing the AST-eval ACE issue and mitigation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +141 to +149
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", "Tag", "Parameter"}:
raise TypeError(f"Forbidden function call in type string: {func_name}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants