Skip to content

fix: fail closed on malformed tool confirmation payloads#4644

Open
yuvrajangadsingh wants to merge 3 commits intogoogle:mainfrom
yuvrajangadsingh:fix/confirmation-fail-closed-4576
Open

fix: fail closed on malformed tool confirmation payloads#4644
yuvrajangadsingh wants to merge 3 commits intogoogle:mainfrom
yuvrajangadsingh:fix/confirmation-fail-closed-4576

Conversation

@yuvrajangadsingh
Copy link

Fixes #4576

ToolConfirmation.model_validate() was running in Pydantic's default lax mode, which silently coerces values like "yes", 1, and "true" to True for boolean fields. This means a malformed confirmation payload could accidentally grant tool execution permission.

This PR adds strict=True to both model_validate() call sites and wraps the parsing in a try/except. If the payload is malformed or can't be parsed, it logs a warning and defaults to confirmed=False — fail closed instead of fail open.

Changes

  • request_confirmation.py: added strict=True to model_validate(), wrapped in try/except with fallback to confirmed=False
  • test_request_confirmation.py: added 6 parametrized test cases covering "yes", 1, "true" (both wrapped in JSON response key and direct), and invalid JSON

Test plan

  • all 11 existing confirmation tests pass
  • all 8 runner confirmation tests pass
  • 6 new parametrized tests verify malformed payloads are denied

Use strict=True in ToolConfirmation.model_validate() so that
non-boolean values like "yes", 1, or "true" for the confirmed
field are rejected instead of silently coerced to True.

Malformed or unparseable payloads now fall through to a catch
block that logs a warning and defaults to confirmed=False.

Fixes google#4576
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a potential security vulnerability where lax Pydantic validation could inadvertently allow tool execution due to silent coercion of malformed boolean confirmation payloads. By enforcing strict validation and implementing robust error handling, the system now defaults to denying tool execution for any invalid confirmation input, ensuring a "fail-closed" security posture and preventing unintended actions.

Highlights

  • Strict Validation: Implemented strict validation (strict=True) in ToolConfirmation.model_validate() to prevent silent coercion of malformed boolean values (e.g., "yes", 1, "true") to True.
  • Robust Error Handling: Wrapped the tool confirmation parsing in a try/except block to gracefully handle json.JSONDecodeError and other exceptions that may arise from malformed payloads.
  • Fail-Closed Security: Ensured that any malformed or unparseable tool confirmation payload defaults to confirmed=False, adopting a 'fail-closed' approach to prevent accidental tool execution.
  • Expanded Test Coverage: Added new parametrized test cases to specifically verify that various malformed confirmation payloads are correctly denied and result in confirmed=False.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/google/adk/flows/llm_flows/request_confirmation.py
    • Applied strict=True to ToolConfirmation.model_validate() calls for stricter data parsing.
    • Introduced a try/except block around confirmation parsing to catch json.JSONDecodeError and general exceptions.
    • Logged warnings for malformed payloads and set tool_confirmation to confirmed=False upon encountering an error.
  • tests/unittests/flows/llm_flows/test_request_confirmation.py
    • Added a helper function _build_confirmation_events to streamline test setup for confirmation scenarios.
    • Introduced a new parametrized test test_malformed_confirmation_payload_denied to cover six different malformed payload scenarios, ensuring they are correctly denied.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Feb 27, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a security concern by enforcing strict validation on tool confirmation payloads, preventing accidental tool execution from malformed data. The change to use strict=True with Pydantic's model_validate and wrapping the parsing in a try/except block to fail-closed is a solid improvement. The new tests effectively cover various malformed payload scenarios. I have one suggestion to make the exception handling more specific to improve maintainability.

@yuvrajangadsingh
Copy link
Author

@gemini-code-assist addressed your review, narrowed the except to (json.JSONDecodeError, ValidationError) with explicit pydantic import in 015bd7d. can you re-review?

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses a security vulnerability by enforcing strict validation on tool confirmation payloads, preventing type coercion that could lead to unintended tool execution. The implementation adds strict=True to Pydantic's model_validate and introduces a try/except block to gracefully handle malformed data, defaulting to a safe "fail-closed" state. The accompanying tests are thorough and cover the relevant edge cases. I've provided one suggestion to refactor the payload validation logic for improved readability and maintainability.

@yuvrajangadsingh
Copy link
Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request effectively addresses the identified vulnerability where Pydantic's lax mode could lead to accidental tool execution due to malformed confirmation payloads. By introducing "strict=True" in "model_validate()" and wrapping the parsing in a try/except block, the system now correctly rejects invalid inputs and defaults to "confirmed=False", ensuring a "fail closed" approach. The addition of comprehensive parameterized tests further strengthens the robustness of this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

confirmation: fail closed on malformed tool-confirmation responses

2 participants