Skip to content

fix(core): validate MCP OAuth resources from metadata URL - #27139

Closed
pragnyanramtha wants to merge 6 commits into
google-gemini:mainfrom
pragnyanramtha:fix-mcp-oauth-root-resource-20017
Closed

fix(core): validate MCP OAuth resources from metadata URL#27139
pragnyanramtha wants to merge 6 commits into
google-gemini:mainfrom
pragnyanramtha:fix-mcp-oauth-root-resource-20017

Conversation

@pragnyanramtha

@pragnyanramtha pragnyanramtha commented May 16, 2026

Copy link
Copy Markdown

Fixes #20017.

Summary

  • derive the expected protected resource from the metadata URL that actually returned OAuth resource metadata
  • keep path-based RFC 9728 validation intact when the path metadata endpoint succeeds
  • allow root .well-known/oauth-protected-resource fallback metadata to validate against the root resource for path-based MCP server URLs
  • apply the same metadata-URL-based validation to WWW-Authenticate discovery

Root cause

For path-based MCP URLs like https://mcp.slack.com/mcp, discovery first tries the path-based metadata URL and then falls back to the root metadata URL when the path endpoint is unavailable. After that fallback succeeded, validation still compared the returned root resource against the original MCP server path, producing errors such as Protected resource https://mcp.slack.com does not match expected https://mcp.slack.com/mcp.

Relation to prior OAuth fixes: #19038 fixed the earlier 401/resource-metadata routing path. This PR is a follow-up for the later #20017 root-metadata fallback/resource mismatch.

Validation

  • npm exec -- vitest run packages/core/src/mcp/oauth-utils.test.ts
  • npm run test:ci --workspace @google/gemini-cli-core -- src/mcp/oauth-utils.test.ts
  • npm exec -- prettier --check packages/core/src/mcp/oauth-utils.ts packages/core/src/mcp/oauth-utils.test.ts
  • npm exec -- eslint packages/core/src/mcp/oauth-utils.ts packages/core/src/mcp/oauth-utils.test.ts --max-warnings 0
  • npm run typecheck --workspace @google/gemini-cli-core
  • npm run build --workspace @google/gemini-cli-core
  • git diff --check

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/extensions Issues related to Gemini CLI extensions capability labels May 16, 2026
@pragnyanramtha
pragnyanramtha marked this pull request as ready for review May 16, 2026 09:55
@pragnyanramtha
pragnyanramtha requested a review from a team as a code owner May 16, 2026 09:55
@gemini-code-assist

Copy link
Copy Markdown
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 an issue where path-based MCP server URLs failed validation during OAuth metadata discovery when falling back to root-based endpoints. By shifting the validation logic to rely on the metadata URL itself rather than the original server path, the system now correctly handles resource identification in both standard and fallback scenarios, ensuring better adherence to RFC 9728.

Highlights

  • RFC 9728 Validation Logic: Updated the OAuth resource validation to derive the expected protected resource directly from the metadata URL, ensuring compliance with RFC 9728 even when falling back to root-based discovery.
  • Metadata URL Fallback Handling: Improved the fallback mechanism for path-based MCP URLs, allowing successful root-based metadata discovery to correctly validate against the root resource.
  • WWW-Authenticate Discovery: Applied consistent metadata-URL-based validation to WWW-Authenticate discovery, resolving potential resource mismatch errors.
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 the 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 counterproductive. 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.

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.

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

Copy link
Copy Markdown
Contributor

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 introduces the buildResourceParameterFromMetadataUrl utility to OAuthUtils to correctly extract resource identifiers from metadata URLs according to RFC 9728. The OAuth discovery logic has been updated to use this method for improved validation, supported by new test cases for various discovery scenarios. Feedback was provided regarding a high-severity security vulnerability in the new utility method, where improper handling of protocol-relative paths could allow for URL host injection and bypass origin-binding security requirements.

Comment thread packages/core/src/mcp/oauth-utils.ts Outdated
@gemini-cli

gemini-cli Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'.

This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

@gemini-cli

gemini-cli Bot commented May 31, 2026

Copy link
Copy Markdown
Contributor

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

@gemini-cli gemini-cli Bot closed this May 31, 2026
@sripasg sripasg added the size/m A medium sized PR label Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/extensions Issues related to Gemini CLI extensions capability priority/p2 Important but can be addressed in a future release. size/m A medium sized PR status/pr-nudge-sent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth authentication fails: Protected resource origin does not match exact connection URL path

2 participants