Skip to content

fix(email): reject a Gmail error body that parses but isn't an object - #4147

Merged
itomek merged 1 commit into
mainfrom
issue-4110
Sep 23, 2026
Merged

itomek merged 1 commit into
mainfrom
issue-4110

Conversation

@itomek

@itomek itomek commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

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 with gaia 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_inbox tool against a 403 with body [1,2]

Before:

tool returned: 'list' object has no attribute 'get'

After:

tool returned: Gmail refused the request (403 — insufficient permissions). The
connected Google account has not granted this agent a gmail read scope. Re-grant
it with `gaia connectors grants grant google installed:gaia --scopes <scope>`, or
reconnect Google, then retry. (request: GET /users/me/messages; reason: forbidden)

Both captured by driving the registered list_inbox tool over an httpx.MockTransport 403 — 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)
  • Malformed bodies [1,2], {"error": "denied"}, "just a string", {"error": {"errors": "not-a-list"}}, {"error": {"errors": {...}}} all raise MailboxAuthError naming gaia connectors
  • null, {}, {"error": null} still yield ("", "") — no regression on shapes that already worked
  • A well-formed {"error": {"errors": [{"reason": …, "extendedHelp": …}]}} still surfaces both unchanged
🔍 Technical details

_error_detail in src/gaia/agents/tools/_email/gmail.py called .get() on the parsed body and again on its error field without checking either was a dict. The AttributeError escaped _raise, and email_tools.py's _fail serialized str(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, and error.errors. A dict-valued errors was a KeyError on the old errors[0], and a scalar was a TypeError; #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_detail docstring already declared that the raw body must never reach a user, but reason is 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.py rather than alongside the Graph guard's tests in test_email_tools.py, per #4110's stated scope. They cover _error_detail directly, the backend's list_inbox, and the registered tool.

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
@itomek itomek added the ready_for_ci Run CI workflows on draft PR without requesting review label Sep 23, 2026
@github-actions github-actions Bot added tests Test changes agents labels Sep 23, 2026
@itomek
itomek marked this pull request as ready for review September 23, 2026 14:35
@itomek
itomek added this pull request to the merge queue Sep 23, 2026
Merged via the queue into main with commit a55169a Sep 23, 2026
77 of 80 checks passed
@itomek
itomek deleted the issue-4110 branch September 23, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents ready_for_ci Run CI workflows on draft PR without requesting review tests Test changes

Projects

None yet

1 participant