Skip to content

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution in AST eval via unrestricted ast.Call#380

Open
bashandbone wants to merge 1 commit into
mainfrom
sentinel/fix-ace-in-ast-eval-15109523051562673142
Open

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution in AST eval via unrestricted ast.Call#380
bashandbone wants to merge 1 commit into
mainfrom
sentinel/fix-ace-in-ast-eval-15109523051562673142

Conversation

@bashandbone

@bashandbone bashandbone commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

🚨 Severity: CRITICAL
💡 Vulnerability: Discovered that the dynamic type evaluation in Container._safe_eval_type using safe AST traversal permitted arbitrary ast.Call nodes.
🎯 Impact: This allowed for Arbitrary Code Execution (ACE) via the module's global namespace if unvalidated strings were processed, circumventing standard Python eval restrictions.
🔧 Fix: Implemented strict whitelisting to explicitly reject ast.Call nodes unless the function being called is one of the strictly recognized list (Depends, depends, Field, PrivateAttr, Tag, Parameter).
✅ Verification: Successfully ran mise //:test tests/unit/core/di and mise //:test locally to verify functionality and ensure no regressions. Also passed full repository linting and formatting.


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

Summary by Sourcery

Harden safe type evaluation in the DI container to prevent arbitrary code execution via unsafe AST calls and document the new Sentinel finding.

Bug Fixes:

  • Block arbitrary ast.Call nodes in Container._safe_eval_type by only allowing calls to a small, explicitly whitelisted set of functions when evaluating type strings.

Documentation:

  • Add a Sentinel security entry documenting the AST-based arbitrary code execution vulnerability in type hint evaluation and its prevention strategy.

…unrestricted ast.Call

* Discovered that the dynamic type evaluation in `Container._safe_eval_type` using safe AST traversal permitted arbitrary `ast.Call` nodes.
* This created an Arbitrary Code Execution (ACE) vulnerability, allowing attackers to execute any callable function present in the module's global namespace via unvalidated string inputs.
* Implemented strict whitelisting within `TypeValidator.generic_visit` to explicitly block any `ast.Call` nodes unless the function name matches an approved set of safe, required functions (e.g., `Depends`, `depends`, `Field`, `Parameter`).
* Added an inline comment documenting the security concern and updated the `.jules/sentinel.md` journal with critical learnings.
* Ran formatting, linting, targeted tests, and the full test suite to ensure the fix is secure and introduces no regressions.

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.

Copilot AI review requested due to automatic review settings June 5, 2026 17:43
@sourcery-ai

sourcery-ai Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Tightens the safe AST-based type evaluation in the DI container by whitelisting allowable function calls in type strings and documents the new security mitigation in the Sentinel notes.

File-Level Changes

Change Details Files
Harden safe AST evaluation in the DI container to block arbitrary function calls in type strings.
  • Add a complexity lint ignore comment to the _safe_eval_type method to satisfy C901 checks.
  • Extend the AST visitor in _safe_eval_type to detect ast.Call nodes during traversal.
  • Introduce a strict whitelist of allowed callable names (Depends, depends, Field, PrivateAttr, Tag, Parameter) when parsing type strings.
  • Extract the function name from ast.Name and ast.Attribute nodes and raise TypeError if the function is not whitelisted.
  • Ensure forbidden function calls in type strings abort evaluation before reaching runtime execution.
src/codeweaver/core/di/container.py
Document the newly discovered AST-based ACE vulnerability and its mitigation in the Sentinel security log.
  • Add a dated Sentinel entry describing the AST call vulnerability in the DI container.
  • Capture key learnings about the risk of allowing arbitrary function calls in type-hint AST evaluation.
  • Document prevention guidance emphasizing strict whitelisting of allowed functions during AST validation.
.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 5, 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 5, 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:

  • The allowed_funcs set is recreated on every ast.Call visit; consider hoisting it to an outer constant or class attribute to avoid repeated allocation and keep the whitelist centralized.
  • When func_name cannot be resolved (stays None), the error message currently includes None; consider using a more generic message or explicitly distinguishing unresolved call targets for clearer debugging.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `allowed_funcs` set is recreated on every `ast.Call` visit; consider hoisting it to an outer constant or class attribute to avoid repeated allocation and keep the whitelist centralized.
- When `func_name` cannot be resolved (stays `None`), the error message currently includes `None`; consider using a more generic message or explicitly distinguishing unresolved call targets for clearer debugging.

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.

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 Container._safe_eval_type (used to resolve string-based type annotations in the DI container) to reduce the risk of arbitrary code execution by restricting which ast.Call nodes are permitted during AST-validated eval.

Changes:

  • Add an ast.Call whitelist in the AST validator used by _safe_eval_type.
  • Suppress cyclomatic complexity warning for _safe_eval_type via # noqa: C901.
  • Document the new Sentinel finding in .jules/sentinel.md.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/codeweaver/core/di/container.py Restricts which call expressions are allowed when evaluating string type hints via AST validation.
.jules/sentinel.md Documents the AST-eval call restriction as a Sentinel security entry.

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

self._providers_loaded: bool = False # Track if auto-discovery has run

def _safe_eval_type(self, type_str: str, globalns: dict[str, Any]) -> Any | None:
def _safe_eval_type(self, type_str: str, globalns: dict[str, Any]) -> Any | None: # noqa: C901
Comment on lines +142 to +151
if isinstance(node, ast.Call):
allowed_funcs = {"Depends", "depends", "Field", "PrivateAttr", "Tag", "Parameter"}
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 allowed_funcs:
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