Skip to content

fix: prevent gmail.metadata scope from blocking query parameters#200

Merged
jpoehnelt merged 5 commits intogoogleworkspace:mainfrom
priorwave:fix/scope-selection
Mar 5, 2026
Merged

fix: prevent gmail.metadata scope from blocking query parameters#200
jpoehnelt merged 5 commits intogoogleworkspace:mainfrom
priorwave:fix/scope-selection

Conversation

@priorwave
Copy link
Contributor

@priorwave priorwave commented Mar 5, 2026

Summary

  • Filter gmail.metadata from login scopes when broader Gmail scopes (gmail.modify, gmail.readonly, mail.google.com) are present. Google enforces gmail.metadata restrictions (e.g. blocking the q parameter) even when broader scopes are also in the token.
  • Select only the broadest scope per method instead of passing all Discovery Document scopes to yup_oauth2. Method scopes are alternatives, not cumulative.
  • Remove superset fallback in token cache to prevent stale all-scopes tokens from being served when a narrower scope is requested.
  • Apply the same fix to the MCP server code path (src/mcp_server.rs), which had the identical bug of passing all method scopes.

Problem

Running gws gmail users messages list --params '{"userId": "me", "q": "is:unread"}' returned 403: Metadata scope does not support 'q' parameter because:

  1. The Discovery Document lists method scopes as alternatives, but all were passed to yup_oauth2
  2. The login scope picker allowed selecting gmail.metadata alongside broader scopes
  3. Google's API enforces gmail.metadata restrictions even when broader scopes are present in the token

Changes

File Change
src/main.rs select_scope() picks only the first (broadest) scope per method
src/auth_commands.rs filter_redundant_restrictive_scopes() strips gmail.metadata when broader Gmail scopes are present at login
src/token_storage.rs Remove superset cache fallback to prevent stale all-scopes tokens being reused
src/mcp_server.rs Apply select_scope() to the MCP server code path (same bug as CLI)

Test plan

  • cargo clippy -- -D warnings passes
  • cargo test — 446 tests pass (6 new)
  • Manual: gws gmail users messages list --params '{"userId": "me", "q": "is:unread"}' returns results
  • Manual: gws gmail users messages list --params '{"userId": "me", "q": "is:unread in:inbox"}' works
  • Manual: gws drive files list --params '{"pageSize": 3}' still works
  • Manual: Selecting gmail.metadata in scope picker still works (filtered automatically)
  • Manual: MCP server Gmail queries work correctly

Fixes #174

🤖 Generated with Claude Code

Discovery Documents list method scopes as alternatives (any one grants
access), but passing all of them to yup_oauth2 caused Google to include
restrictive scopes like gmail.metadata in the token. The API then
enforced that scope's restrictions, blocking query parameters like `q`.

Select only the first (broadest) scope from the method's scope list.
…uperset fallback

gmail.metadata restricts API behavior (blocks `q` parameter) even when
broader scopes are present in the token. Filter it out during login when
broader Gmail scopes like gmail.modify or mail.google.com are selected.

Also remove the superset fallback in token storage to prevent stale
all-scopes tokens from being reused when a narrower scope is requested.
@priorwave priorwave requested a review from jpoehnelt as a code owner March 5, 2026 20:25
@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@changeset-bot
Copy link

changeset-bot bot commented Mar 5, 2026

🦋 Changeset detected

Latest commit: 89e5540

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-cla
Copy link

google-cla bot commented Mar 5, 2026

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.

@jpoehnelt
Copy link
Member

/gemini review

@jpoehnelt jpoehnelt added cla: yes This human has signed the Contributor License Agreement. complexity: medium Moderate change, some review needed labels Mar 5, 2026
The MCP server had the same bug as the CLI — passing all method scopes
to get_token. Use select_scope to pick only the broadest scope.
@jpoehnelt
Copy link
Member

Some conflicts to fix.

@jpoehnelt
Copy link
Member

More conflicts.

@googleworkspace-bot googleworkspace-bot added area: mcp area: core Core CLI parsing, commands, error handling, utilities labels Mar 5, 2026
@googleworkspace-bot
Copy link
Collaborator

/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

This pull request provides a comprehensive fix for an issue where the restrictive gmail.metadata scope was blocking API calls with query parameters. The changes are well-structured and address the problem from multiple angles: 1. Selecting only the broadest scope from API discovery documents to avoid including restrictive ones by default. 2. Explicitly filtering out gmail.metadata during the login flow if broader Gmail scopes are present. 3. Making the token cache stricter to prevent the reuse of old tokens that contain restrictive scopes. The implementation is clean, idiomatic, and includes appropriate unit tests for the new logic. I have reviewed the changes and found no issues.

@jpoehnelt jpoehnelt enabled auto-merge (squash) March 5, 2026 23:30
@jpoehnelt jpoehnelt merged commit ff53538 into googleworkspace:main Mar 5, 2026
9 of 20 checks passed
@googleworkspace-bot
Copy link
Collaborator

/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

This pull request correctly addresses an issue where the gmail.metadata scope was causing API calls to fail by being overly restrictive. The changes to filter this scope during login and to select only the broadest (first) scope for API calls are well-implemented. The removal of the superset token cache fallback is also a crucial part of this fix. I've found one critical issue in src/main.rs where methods that don't require any scopes would fail, and I've provided a suggestion to fix it.

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

Labels

area: auth area: core Core CLI parsing, commands, error handling, utilities area: mcp cla: yes This human has signed the Contributor License Agreement. complexity: medium Moderate change, some review needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

auth login with full permission doesnt leave functioning gmail token

3 participants