fix(tools): check_require_confirmation fails closed on non-bool callable return - #7012
Open
boopathi-376 wants to merge 1 commit into
Open
fix(tools): check_require_confirmation fails closed on non-bool callable return#7012boopathi-376 wants to merge 1 commit into
boopathi-376 wants to merge 1 commit into
Conversation
…ble return cast(bool, ...) is a static-only type hint and has no effect at runtime, so a require_confirmation callable that returns None was silently treated as falsy, letting the tool run without confirmation. Replace the cast with an isinstance(result, bool) runtime check that fails closed on any non-bool return. Fixes google#7010
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
Closes: #7010
Problem
FunctionTool.check_require_confirmationandMcpTool.check_require_confirmationusedcast(bool, ...)on the return value of a user-suppliedrequire_confirmationcallable.typing.castis a static type-checker hint only — it performs no runtime coercion or validation.As a result, a predicate that fell off the end of a branch without an explicit
return(implicitly returningNone) was passed straight through toif require_confirmation:, which evaluatesNoneas falsy — silently letting the tool run without requesting confirmation, even though the caller had explicitly opted into the confirmation gate.Solution
Replaced
cast(bool, ...)with a realisinstance(result, bool)check in bothFunctionTool.check_require_confirmationandMcpTool.check_require_confirmation:bool, behavior is unchanged.None), the gate now fails closed and requires confirmation, rather than silently skipping it.Behavior change (intentional): callers whose
require_confirmationcallable relies on a falsy non-bool return (e.g.None,0,"") to mean "skip confirmation" will now get confirmation required instead. This is the security-relevant fix — a confirmation gate should fail closed on an ambiguous/unanswered predicate rather than open. Truthy non-bool returns (e.g. a string reason like"amount over limit") are unaffected — they already meant "confirm" and continue to.Testing Plan
Unit Tests
Added regression tests in both
test_function_tool.pyandtest_mcp_tool.pycovering:None(implicit fallthrough) → now requires confirmationboolFalse→ still runs without confirmation (no regression)pytest results:
tests/unittests/tools/test_function_tool.py: 41 passed, 3 warnings in 2.06s
tests/unittests/tools/mcp_tool/test_mcp_tool.py: 94 passed, 102 warnings in 9.24s
Manual E2E Tests
Not applicable — change is isolated to a runtime type-check in two
check_require_confirmationmethods, fully covered by the unit tests above.Checklist
Additional context
Credit to @mahirhir for the detailed repro and root-cause analysis in #7010.