-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix: add RFC 8707 resource validation to OAuth client #2069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
felixweinberger
merged 1 commit into
v1.x
from
fweinberger/v1x-rfc8707-resource-validation
Feb 17, 2026
+102
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,4 @@ | ||
| # Known conformance test failures for v1.x | ||
| # These are tracked and should be removed as they're fixed. | ||
| # | ||
| # auth/resource-mismatch: Client must validate that the Protected Resource | ||
| # Metadata (PRM) resource field matches the server URL before proceeding | ||
| # with authorization (RFC 8707). Implemented on main (PR #2010), needs | ||
| # backport to v1.x. | ||
| client: | ||
| - auth/resource-mismatch | ||
| server: [] | ||
| client: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| from pydantic import AnyHttpUrl, AnyUrl | ||
|
|
||
| from mcp.client.auth import OAuthClientProvider, PKCEParameters | ||
| from mcp.client.auth.exceptions import OAuthFlowError | ||
| from mcp.client.auth.utils import ( | ||
| build_oauth_authorization_server_metadata_discovery_urls, | ||
| build_protected_resource_metadata_discovery_urls, | ||
|
|
@@ -965,7 +966,7 @@ async def test_auth_flow_with_no_tokens(self, oauth_provider: OAuthClientProvide | |
| # Send a successful discovery response with minimal protected resource metadata | ||
| discovery_response = httpx.Response( | ||
| 200, | ||
| content=b'{"resource": "https://api.example.com/mcp", "authorization_servers": ["https://auth.example.com"]}', | ||
| content=b'{"resource": "https://api.example.com/v1/mcp", "authorization_servers": ["https://auth.example.com"]}', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the new validation caught that the mock PRM resource didn't match the fixture's server URL |
||
| request=discovery_request, | ||
| ) | ||
|
|
||
|
|
@@ -2030,3 +2031,85 @@ async def callback_handler() -> tuple[str, str | None]: | |
| await auth_flow.asend(final_response) | ||
| except StopAsyncIteration: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_validate_resource_rejects_mismatched_resource( | ||
| client_metadata: OAuthClientMetadata, mock_storage: MockTokenStorage | ||
| ) -> None: | ||
| """Client must reject PRM resource that doesn't match server URL.""" | ||
| provider = OAuthClientProvider( | ||
| server_url="https://api.example.com/v1/mcp", | ||
| client_metadata=client_metadata, | ||
| storage=mock_storage, | ||
| ) | ||
| provider._initialized = True | ||
|
|
||
| prm = ProtectedResourceMetadata( | ||
| resource=AnyHttpUrl("https://evil.example.com/mcp"), | ||
| authorization_servers=[AnyHttpUrl("https://auth.example.com")], | ||
| ) | ||
| with pytest.raises(OAuthFlowError, match="does not match expected"): | ||
| await provider._validate_resource_match(prm) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_validate_resource_accepts_matching_resource( | ||
| client_metadata: OAuthClientMetadata, mock_storage: MockTokenStorage | ||
| ) -> None: | ||
| """Client must accept PRM resource that matches server URL.""" | ||
| provider = OAuthClientProvider( | ||
| server_url="https://api.example.com/v1/mcp", | ||
| client_metadata=client_metadata, | ||
| storage=mock_storage, | ||
| ) | ||
| provider._initialized = True | ||
|
|
||
| prm = ProtectedResourceMetadata( | ||
| resource=AnyHttpUrl("https://api.example.com/v1/mcp"), | ||
| authorization_servers=[AnyHttpUrl("https://auth.example.com")], | ||
| ) | ||
| # Should not raise | ||
| await provider._validate_resource_match(prm) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_validate_resource_accepts_root_url_with_trailing_slash( | ||
| client_metadata: OAuthClientMetadata, mock_storage: MockTokenStorage | ||
| ) -> None: | ||
| """Root URLs with trailing slash normalization should match.""" | ||
| provider = OAuthClientProvider( | ||
| server_url="https://api.example.com/", | ||
| client_metadata=client_metadata, | ||
| storage=mock_storage, | ||
| ) | ||
| provider._initialized = True | ||
|
|
||
| prm = ProtectedResourceMetadata( | ||
| resource=AnyHttpUrl("https://api.example.com/"), | ||
| authorization_servers=[AnyHttpUrl("https://auth.example.com")], | ||
| ) | ||
| # Should not raise - both already have trailing slashes | ||
| await provider._validate_resource_match(prm) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_get_resource_url_falls_back_when_prm_mismatches( | ||
| client_metadata: OAuthClientMetadata, mock_storage: MockTokenStorage | ||
| ) -> None: | ||
| """get_resource_url returns canonical URL when PRM resource doesn't match.""" | ||
| provider = OAuthClientProvider( | ||
| server_url="https://api.example.com/v1/mcp", | ||
| client_metadata=client_metadata, | ||
| storage=mock_storage, | ||
| ) | ||
| provider._initialized = True | ||
|
|
||
| # Set PRM with a resource that is NOT a parent of the server URL | ||
| provider.context.protected_resource_metadata = ProtectedResourceMetadata( | ||
| resource=AnyHttpUrl("https://other.example.com/mcp"), | ||
| authorization_servers=[AnyHttpUrl("https://auth.example.com")], | ||
| ) | ||
|
|
||
| # get_resource_url should return the canonical server URL, not the PRM resource | ||
| assert provider.context.get_resource_url() == "https://api.example.com/v1/mcp" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.