Skip to content

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution vulnerability in AST type validation#386

Open
bashandbone wants to merge 1 commit into
mainfrom
sentinel-ast-call-whitelist-6973508759404387362
Open

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution vulnerability in AST type validation#386
bashandbone wants to merge 1 commit into
mainfrom
sentinel-ast-call-whitelist-6973508759404387362

Conversation

@bashandbone

@bashandbone bashandbone commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

🚨 Severity: CRITICAL

💡 Vulnerability: Unrestricted dynamic type resolution
The _safe_eval_type function in src/codeweaver/core/di/container.py evaluated type annotation strings using python's eval() function in a supposedly restricted environment with a filtered dictionary of __builtins__. While an AST validator ensured only a safe subset of nodes were permitted, it mistakenly permitted any ast.Call nodes. This left the system vulnerable to Arbitrary Code Execution, as an attacker could supply a malicious string utilizing functions within the module's global namespace to bypass restrictions.

🎯 Impact: Arbitrary Code Execution (ACE)
This could allow malicious configuration or improperly validated input bound as an annotation to execute code, which could result in compromising the host server, exposing secrets, and further lateral movement.

🔧 Fix: Introduced a Strict Whitelist
The generic_visit override in the AST TypeValidator has been explicitly patched. Now, any ast.Call is verified against a strict whitelist of dependencies utilized within Pydantic models or dependency injection structures within the CodeWeaver codebase (Depends, depends, Field, PrivateAttr, Tag, and Parameter). If an unsupported function call is encountered, a TypeError is raised before evaluation.

Verification:

  1. Ran mise //:format and manually linted using uv run ruff check.
  2. Code correctly passes the test suite (pytest tests/unit/core/di).
  3. Evaluated all tests under tests/unit/ using pytest, asserting 100% successful execution without regression.

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

Summary by Sourcery

Harden dynamic type evaluation in the dependency injection container to prevent arbitrary code execution via unsafe AST function calls.

Bug Fixes:

  • Reject non-whitelisted function calls in AST validation before evaluating dynamic type strings to close an arbitrary code execution vulnerability.

Documentation:

  • Document the newly discovered AST-based arbitrary code execution vulnerability and its remediation steps in the Sentinel security log.

Added strict whitelisting of function calls within the TypeValidator to prevent Arbitrary Code Execution (ACE) vulnerabilities during dependency injection type evaluation. Added corresponding documentation in the sentinel journal.

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 9, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Tightens AST-based validation in the DI container’s _safe_eval_type to block arbitrary code execution by restricting function calls in evaluated type strings to a small, explicit whitelist, and documents the incident in the Sentinel security log.

Flow diagram for tightened AST type validation in _safe_eval_type

flowchart TD
    A[type_str input] --> B[ast.parse]
    B --> C[TypeValidator.generic_visit]
    C --> D{node is ast.Call?}
    D -- no --> G[continue validation]
    D -- yes --> E[extract func_name]
    E --> F{func_name in allowed_calls?}
    F -- no --> H[raise TypeError]
    F -- yes --> G[continue validation]
    G --> I[eval type_str with restricted globalns]
Loading

File-Level Changes

Change Details Files
Harden AST type validation in _safe_eval_type by restricting allowed function calls in evaluated type strings to a strict whitelist.
  • Mark _safe_eval_type as complex in linting with # noqa: C901 to suppress complexity warnings while retaining existing logic.
  • Extend TypeValidator.generic_visit to treat ast.Call nodes specially, extracting the called function’s name from ast.Name or ast.Attribute.
  • Introduce an allowed_calls whitelist (Depends, depends, Field, PrivateAttr, Tag, Parameter) that are permitted in type-evaluating calls.
  • Raise TypeError for any ast.Call whose function name is not in the whitelist, preventing its evaluation via eval().
  • Delegate to super().generic_visit(node) after the new checks so that existing AST validation continues to run.
src/codeweaver/core/di/container.py
Record the newly discovered AST-based arbitrary code execution vulnerability and its remediation in the Sentinel security log.
  • Add a dated Sentinel entry describing the ACE vulnerability arising from unrestricted ast.Call nodes in the type-evaluation path.
  • Document key learnings about the risks of using eval() with dynamic type strings, even under AST validation.
  • Capture prevention guidance emphasizing strict whitelisting of allowed functions in AST validation and disallowing generic calls.
.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 9, 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 9, 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_calls set into a module-level constant (or sharing it with any related DI/type-handling modules) so it’s easier to keep the whitelist in sync as the dependency API evolves.
  • In the ast.Call handling, you might want to treat unresolvable/complex node.func values (where func_name ends up None) explicitly as forbidden with a clearer error message so they can’t slip through and are easier to debug.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the `allowed_calls` set into a module-level constant (or sharing it with any related DI/type-handling modules) so it’s easier to keep the whitelist in sync as the dependency API evolves.
- In the `ast.Call` handling, you might want to treat unresolvable/complex `node.func` values (where `func_name` ends up `None`) explicitly as forbidden with a clearer error message so they can’t slip through and are easier to debug.

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