fix: use discoverOAuthFromWWWAuthenticate for reactive OAuth flow (#18760)#18798
fix: use discoverOAuthFromWWWAuthenticate for reactive OAuth flow (#18760)#18798echoVic wants to merge 1 commit intogoogle-gemini:mainfrom
Conversation
The handleAutomaticOAuth() function was passing the resource_metadata URI (already a .well-known URL) to discoverOAuthConfig() which expects a server URL and constructs .well-known paths from it. This caused double-nested .well-known paths (404) and subsequent ResourceMismatchError. Fix: Use the existing discoverOAuthFromWWWAuthenticate() which directly fetches the resource_metadata URI and validates against the actual server URL, with fallback to base URL discovery. Fixes google-gemini#18760
Summary of ChangesHello @echoVic, 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 a critical bug in the automatic OAuth handling for MCP servers, particularly those with path-based resources. The previous implementation caused failures in OAuth configuration discovery due to incorrect URI handling, resulting in authentication issues. The fix refactors the discovery process to use a more robust utility, ensuring proper resource metadata fetching and validation, thereby improving the reliability of OAuth authentication for affected server configurations. Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
Customization To customize 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 Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
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. |
There was a problem hiding this comment.
Code Review
This pull request fixes a bug in the automatic OAuth flow where a resource_metadata URI was incorrectly passed to a function expecting a server URL. The change to use discoverOAuthFromWWWAuthenticate correctly handles the discovery process, making the implementation more robust by preserving fallback logic and removing redundant serverUrl variable declarations. The security analysis did not find any vulnerabilities, although it noted that the content of several files, such as packages/core/src/tools/mcp-client.ts and packages/core/src/mcp/oauth-utils.ts, could not be read, which prevented SAST reconnaissance passes on them.
Problem
handleAutomaticOAuth()passes theresource_metadataURI (already a.well-knownURL extracted from theWWW-Authenticateheader) todiscoverOAuthConfig(), which expects a server URL and internally constructs.well-knownpaths from it. This causes:.well-knownpaths → 404 on resource metadata fetchResourceMismatchError→ the resource parameter from fallback metadata doesn't match the path-based server URLThis affects all OAuth-protected MCP servers that serve resources under paths (gateways, multi-tenant setups).
Root Cause
Fix
Use the existing
discoverOAuthFromWWWAuthenticate()utility which correctly:resource_metadataURI from theWWW-Authenticateheader.well-knownconstruction)resourceparameter against the actual server URL (RFC 9728 §7.3)Fallback to base URL discovery via
discoverOAuthConfig()is preserved for servers that don't includeresource_metadatain theWWW-Authenticateheader.Fixes #18760