fix(core): use prefix matching for OAuth resource validation - #17431
Conversation
Use prefix matching instead of exact matching for OAuth protected resource validation, aligning with the MCP TypeScript SDK approach. This allows servers that advertise a base resource URL (e.g., https://example.com) to be used with endpoints at sub-paths (e.g., https://example.com/mcp).
Summary of ChangesHello @vrv, 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 refactors the OAuth protected resource validation mechanism to use prefix matching instead of exact URL matching. This change aligns the validation behavior with the MCP TypeScript SDK, enabling servers that advertise a general base URL to be correctly validated against more specific sub-path endpoints. The update improves flexibility and compatibility for OAuth resource discovery. Highlights
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
|
There was a problem hiding this comment.
Code Review
This pull request switches from exact matching to prefix matching for OAuth protected resource validation. While this is a good feature, the current implementation of the prefix matching in checkResourceAllowed has a critical security vulnerability. It allows for partial prefix matches on URL path segments, which could lead to improper authorization. I have provided a detailed comment and a code suggestion to fix this vulnerability by ensuring the matching is done on full path segments.
| // Check if requested path starts with configured path | ||
| // Add trailing slashes to avoid "/api123" matching "/api" | ||
| const requestedPath = requested.pathname.endsWith('/') | ||
| ? requested.pathname | ||
| : requested.pathname + '/'; | ||
| const configuredPath = configured.pathname.endsWith('/') | ||
| ? configured.pathname | ||
| : configured.pathname + '/'; | ||
|
|
||
| return requestedPath.startsWith(configuredPath); |
There was a problem hiding this comment.
The current implementation for prefix matching has a security flaw. It incorrectly allows partial prefix matches on path segments. For example, a configured resource of https://example.com/api would incorrectly allow a requested resource of https://example.com/api-v2. This happens because after appending trailing slashes, "/api-v2/" starts with "/api/".
This can lead to security vulnerabilities if different paths on the same origin have different security policies (e.g., api and api-v2 are different services).
The validation should ensure that it matches full path segments. A requested path should be considered within scope if it's an exact match, or if it's a sub-path (i.e., the configured path is a prefix and is followed by a /).
I've suggested a more robust implementation that correctly handles path segment boundaries.
// Check if requested path is a valid subpath of the configured path.
const requestedPath = requested.pathname;
const configuredPath = configured.pathname;
if (!requestedPath.startsWith(configuredPath)) {
return false;
}
// Exact match is always allowed.
if (requestedPath.length === configuredPath.length) {
return true;
}
// If configured path ends with '/', it's a directory-like prefix.
if (configuredPath.endsWith('/')) {
return true;
}
// If configured path does not end with '/', the next character in the
// requested path must be a '/' to be a valid subpath. This prevents
// partial matches like '/api' matching '/api-v2'.
return requestedPath.charAt(configuredPath.length) === '/';There was a problem hiding this comment.
This happens because after appending trailing slashes, "/api-v2/" starts with "/api/".
No.
|
@galz10 probably want to review this too -- I think the previous code was doing the spec compliant thing but I think most MCP implementations do something like this. I pointed at one example of the typescript mcp sdk but we can look at others as well. I tested just now again with notion, my servers, etc., and HEAD was broken, and with this change I can connect to both. Feel free to test / fix / update / advise as you see fit though. |
|
Hi there! Thank you for your contribution to Gemini CLI. To improve our contribution process and better track changes, we now require all pull requests to be associated with an existing issue, as announced in our recent discussion and as detailed in our CONTRIBUTING.md. This pull request is being closed because it is not currently linked to an issue. You can easily reopen this PR once you have linked it to an issue. How to link an issue: Thank you for your understanding and for being a part of our community! |
|
I've linked it to an issue but I can't reopen it, fyi. |
|
Friendly ping for @galz10 ! |
|
Any chance this will be re-opened? This does indeed fix the problem, I applied locally and confirmed against the official Gitlab MCP server. v0.30.0 currently yields: |
|
thanks for confirming! maybe it'd work better if i just re-opened a new one with the exact same fix... if someone gets to it before i do, please go ahead, just leave a mention that you did it first |
Summary
Use prefix matching instead of exact matching for OAuth protected resource validation, aligning with the MCP TypeScript SDK approach.
This allows servers that advertise a base resource URL (e.g., https://example.com) to be used with endpoints at sub-paths (e.g., https://example.com/mcp).
Details
It appears as though most MCP servers do not implement the OAuth protected resource spec faithfully, returning https://mcp.foo.com for a URL that is https://mcp.foo.com/mcp. The strict checking that is spec compliant unfortunately makes most servers like notion, stripe, etc all not work.
So this implements the same logic as https://github.com/modelcontextprotocol/typescript-sdk/blob/b2326afea6de1104b89a045c5651790490cc8cc8/packages/core/src/shared/authUtils.ts#L26
which does a prefix matching.
Related Issues
Fixes #15754
How to Validate
Try to connect to https://mcp.notion.com/mcp via mcp -- it should complete the oauth process.
Pre-Merge Checklist