Skip to content

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution vulnerability in container type resolution#376

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

🛡️ Sentinel: [CRITICAL] Fix arbitrary code execution vulnerability in container type resolution#376
bashandbone wants to merge 1 commit into
mainfrom
sentinel/fix-ace-in-ast-eval-12687173228044087168

Conversation

@bashandbone

@bashandbone bashandbone commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

🚨 Severity: CRITICAL
💡 Vulnerability: Unconstrained ast.Call nodes were allowed when evaluating string type annotations in _safe_eval_type inside src/codeweaver/core/di/container.py, allowing the execution of any functions available in the evaluated global scope.
🎯 Impact: If _safe_eval_type parses untrusted or dynamically resolved string contents into an AST tree and then runs eval(), an attacker could inject Python arbitrary code execution commands (such as os.system) directly into the evaluation block.
🔧 Fix: Introduced an exact name matcher for function calls mapped against a hardcoded whitelist composed entirely of safe internal and third-party typing/dependency tools like Depends, Field, GetPydanticSchema, and Parameter.
Verification: Verified by testing explicitly unmapped function nodes (like os.system) locally during discovery which resulted in the proper TypeError blocking their dispatch into eval(). Running mise //:test tests/unit/core/ ensures this strict implementation doesn't break pre-existing dependency injections in the tree.


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

Summary by Sourcery

Harden type string evaluation in the DI container to prevent arbitrary code execution during container type resolution.

Bug Fixes:

  • Block arbitrary function calls in _safe_eval_type by only allowing a small, explicit whitelist of safe callables in AST Call nodes during type evaluation.

Documentation:

  • Document the newly discovered arbitrary code execution risk in type hint evaluation and its mitigation in the security sentinel notes.

… container type resolution

Added a strict whitelist for `ast.Call` nodes parsed inside `_safe_eval_type` before passing strings to `eval()` to prevent Arbitrary Code Execution (ACE) vulnerabilities. Modified `TypeValidator.generic_visit` to intercept `ast.Call` nodes and assert they map strictly to allowed known typing definitions (like `Depends`, `depends`, `Field`, `Parameter`, etc.), raising a TypeError for any unsupported function calls (e.g. system commands). Checked code with format, linting, and tests to ensure correctness.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 2, 2026 17:26
@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 2, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Tightens security of the _safe_eval_type AST validator in the DI container by whitelisting allowed function calls in type strings and documents the vulnerability and mitigation in the Sentinel security log.

Flow diagram for updated _safe_eval_type AST validation

flowchart TD
    A[type_str input] --> B[ast.parse]
    B --> C[SafeNodeVisitor.visit]
    C --> D{node is ast.Call?}
    D -- No --> G[Continue AST validation]
    D -- Yes --> E[Extract func_name from ast.Name or ast.Attribute]
    E --> F{func_name in allowed_funcs?}
    F -- No --> H[raise TypeError]
    F -- Yes --> G
    G --> I[eval compiled AST in restricted globalns]
    I --> J[Return evaluated type or None]
Loading

File-Level Changes

Change Details Files
Harden AST validation for function calls in _safe_eval_type to prevent arbitrary code execution when evaluating string type annotations.
  • Extend the AST NodeVisitor in _safe_eval_type to explicitly inspect ast.Call nodes and extract the called function name from ast.Name or ast.Attribute nodes.
  • Introduce a hardcoded allowlist of safe callable names (e.g., Depends, Field, PrivateAttr, Tag, Parameter, AfterValidator, BeforeValidator, Discriminator, GetPydanticSchema, uuid7) permitted in evaluated type strings.
  • Raise TypeError when an ast.Call is encountered whose function name is not in the allowlist, effectively blocking arbitrary or unrecognized function execution via eval.
  • Annotate _safe_eval_type with noqa: C901 to suppress the complexity linter warning introduced by the additional validation logic.
src/codeweaver/core/di/container.py
Update Sentinel security documentation to record the discovered ACE risk in type hint evaluation and its mitigation approach.
  • Add a new dated entry describing the arbitrary code execution risk arising from unconstrained ast.Call handling in _safe_eval_type.
  • Document the learning that eval-based environments cannot safely handle generic ast.Call nodes without strict whitelisting.
  • Describe prevention guidance recommending exact-name whitelists for allowed callables in type resolution flows.
.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 2, 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 2, 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 ast.Call handling currently treats any attribute call as allowed if the attribute name matches (e.g., os.Depends), so you may want to also validate the full dotted path or restrict calls to bare names only to avoid bypass via arbitrary modules.
  • Consider extracting the allowed_funcs set to a module- or class-level constant so that the whitelist is easier to audit and update in one place, rather than being embedded inside the visitor method.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `ast.Call` handling currently treats any attribute call as allowed if the attribute name matches (e.g., `os.Depends`), so you may want to also validate the full dotted path or restrict calls to bare names only to avoid bypass via arbitrary modules.
- Consider extracting the `allowed_funcs` set to a module- or class-level constant so that the whitelist is easier to audit and update in one place, rather than being embedded inside the visitor method.

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 aims to harden the DI container’s string-based type annotation evaluation to prevent arbitrary code execution by restricting which ast.Call nodes are permitted during _safe_eval_type().

Changes:

  • Added a function-name whitelist for ast.Call nodes during type-string AST validation in Container._safe_eval_type.
  • Documented the vulnerability and mitigation approach in the Jules sentinel security notes.

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 function calls are allowed when validating/evaluating string type annotations.
.jules/sentinel.md Adds a security incident note describing the ACE risk and mitigation strategy.

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

Comment on lines +139 to +162
# Security: Restrict arbitrary function execution in evaluate
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

allowed_funcs = {
"Depends",
"depends",
"Field",
"PrivateAttr",
"Tag",
"Parameter",
"AfterValidator",
"BeforeValidator",
"Discriminator",
"GetPydanticSchema",
"uuid7",
}
if func_name not in allowed_funcs:
raise TypeError(f"Arbitrary function calls are forbidden: {func_name}")

Comment on lines +147 to +151
allowed_funcs = {
"Depends",
"depends",
"Field",
"PrivateAttr",
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