Skip to content

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

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

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

Conversation

@bashandbone

@bashandbone bashandbone commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

🚨 Severity: CRITICAL
💡 Vulnerability: The type validation logic using ast.parse(type_str, mode="eval") allowed generic ast.Call nodes to pass. Even though standard Python literal types were blocked, any accessible callable within the execution environment's scope could be arbitrarily executed, leading to a critical Arbitrary Code Execution (ACE) vulnerability during type resolution.
🎯 Impact: An attacker could potentially inject unvalidated configuration strings into the dependency container to achieve arbitrary code execution.
🔧 Fix: Explicitly restrict allowed callables in the TypeValidator to a predefined whitelist instead of broadly allowing the ast.Call class. Implemented rigorous AST node visitation checks that explicitly validate both node.func.id and node.func.attr. Allowed specific, required functions like Depends, depends, Field, PrivateAttr, Tag, and Parameter.
✅ Verification: Ran format, lint (mise //:format, mise //:check), and the test suite (uv run pytest tests/unit/ tests/di/ --no-cov) to ensure correct validation logic and no regressions.


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

Summary by Sourcery

Restrict dynamic AST-evaluated type strings to a small whitelist of safe callable names to close an arbitrary code execution vulnerability in the DI container.

Bug Fixes:

  • Block arbitrary function calls in AST-based type validation by only allowing specific whitelisted callables in TypeValidator.

Documentation:

  • Document the newly fixed AST call-node vulnerability and its mitigation steps in the Sentinel security log.

… AST evaluation

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
@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 8, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Restricts AST-based type string evaluation to a small, explicit set of safe callables to close an arbitrary code execution vulnerability, and documents the incident in the Sentinel security log.

Sequence diagram for restricted AST call validation in type evaluation

sequenceDiagram
    participant ClientCode
    participant ast
    participant TypeValidator

    ClientCode->>ast: parse(type_str, mode=eval)
    ast-->>ClientCode: ast.AST tree
    ClientCode->>TypeValidator: generic_visit(ast_tree)

    loop visit_nodes
        TypeValidator->>TypeValidator: generic_visit(node)
        alt node is ast.Call
            alt node.func is ast.Name and id in {Depends, depends, Field, PrivateAttr, Tag, Parameter}
                TypeValidator-->>TypeValidator: is_allowed = True
            else node.func is ast.Attribute and attr in {Depends, depends, Field, PrivateAttr, Tag, Parameter}
                TypeValidator-->>TypeValidator: is_allowed = True
            else forbidden callable
                TypeValidator-->>ClientCode: raise TypeError(Forbidden function call in type string.)
            end
        else non_call_node
            TypeValidator-->>TypeValidator: super().generic_visit(node)
        end
    end
Loading

File-Level Changes

Change Details Files
Harden AST type validation by whitelisting allowed function calls and rejecting all other ast.Call nodes.
  • Extend TypeValidator.generic_visit to detect ast.Call nodes in parsed type strings.
  • Introduce an explicit whitelist of allowed callable identifiers (Depends/depends, Field, PrivateAttr, Tag, Parameter).
  • Validate both simple names (ast.Name.id) and attribute access (ast.Attribute.attr) against the whitelist before allowing a call node.
  • Raise TypeError on any function call in a type string that is not in the whitelist, preventing execution of arbitrary callables.
src/codeweaver/core/di/container.py
Record the vulnerability and its remediation details in the Sentinel security documentation.
  • Add a dated Sentinel entry describing the AST-based arbitrary code execution vulnerability.
  • Document the security learning around restricting ast.Call nodes and whitelisting callables.
  • Describe preventative measures for future AST-based validation implementations.
.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 8, 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.

@github-actions

github-actions Bot commented Jun 8, 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.

@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 callable names into a single shared constant or set so that ast.Name and ast.Attribute checks don’t duplicate hardcoded string lists and are easier to maintain.
  • You might want to make the error message include the attempted function name (where available) to aid debugging when a forbidden call is encountered, while still keeping it generic enough not to leak unnecessary details.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the allowed callable names into a single shared constant or set so that `ast.Name` and `ast.Attribute` checks don’t duplicate hardcoded string lists and are easier to maintain.
- You might want to make the error message include the attempted function name (where available) to aid debugging when a forbidden call is encountered, while still keeping it generic enough not to leak unnecessary 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.

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.

1 participant