Repository navigation
fix(email): reject a Gmail error body that parses but isn't an object - #4147
Merged
Merged
Conversation
A Gmail error body that parses as JSON but isn't the documented
{error: {...}} shape (a bare list, a string, or error as a plain
string) crashed _error_detail with an uncaught AttributeError, which
escaped _raise and reached the model as "'list' object has no
attribute 'get'" instead of the actionable 403 naming the re-grant
command. The parsed body, its error field, and that field's errors
list are now all type-checked before use.
The reason token is also bounded to the shape Gmail documents, the
way the Graph backend already bounds error.code: it is interpolated
into a message a user reads, so an upstream body must not be able to
put arbitrary text there.
Closes #4110
Closed
3 tasks
itomek
marked this pull request as ready for review
September 23, 2026 14:35
4 tasks done
5 of 11 tasks
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.
When Gmail refused a request with an error body that wasn't shaped the way Google documents it, the mail tools handed the model a raw Python message —
'list' object has no attribute 'get'— instead of the 403 that names the fix. The user lost the only actionable part ("re-grant Google access withgaia connectors grants grant google …") and the model had nothing to act on. Any Gmail error body now produces the normal typed message, whatever shape the body arrives in.This is the Gmail twin of #4082, which fixed the same defect in the Outlook/Graph backend; the Gmail backend was written before that fix landed and inherited the original shape. The two paths now guard identically.
Closes #4110
Closes #4132
Evidence — the real
list_inboxtool against a 403 with body[1,2]Before:
After:
Both captured by driving the registered
list_inboxtool over anhttpx.MockTransport403 — the same entry point the model calls.{"error": "denied"}and"just a string"behaved identically.Test plan
pytest tests/unit/agents/tools/test_gmail_backend.py— 65 passed (10 of them new and red before the fix)[1,2],{"error": "denied"},"just a string",{"error": {"errors": "not-a-list"}},{"error": {"errors": {...}}}all raiseMailboxAuthErrornaminggaia connectorsnull,{},{"error": null}still yield("", "")— no regression on shapes that already worked{"error": {"errors": [{"reason": …, "extendedHelp": …}]}}still surfaces both unchanged🔍 Technical details
_error_detailinsrc/gaia/agents/tools/_email/gmail.pycalled.get()on the parsed body and again on itserrorfield without checking either was a dict. TheAttributeErrorescaped_raise, andemail_tools.py's_failserializedstr(exc)as the tool result — so the failure looked like a normal tool error.Three levels are now type-checked, not two: the parsed body,
error, anderror.errors. A dict-valuederrorswas aKeyErroron the olderrors[0], and a scalar was aTypeError; #4110 measured only the first two levels, so this is one guard wider than the issue described.The reason token is also bounded by
_ERROR_REASON_RE([A-Za-z0-9_.]{1,64}), mirroring Graph's_ERROR_CODE_RE. Gmail's_error_detaildocstring already declared that the raw body must never reach a user, butreasonis upstream-controlled and was interpolated into the 403 message unfiltered — the one place the two backends were still asymmetric after the type guards. Real tokens (accessNotConfigured,PERMISSION_DENIED) pass; an HTML fragment does not.Tests live in
test_gmail_backend.pyrather than alongside the Graph guard's tests intest_email_tools.py, per #4110's stated scope. They cover_error_detaildirectly, the backend'slist_inbox, and the registered tool.