fix: normalize OAuth redirect URI URL subtypes#2953
Closed
fengjikui wants to merge 1 commit into
Closed
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #2687 by normalizing
OAuthClientMetadata.redirect_urisentries at the model boundary. If callers pass a Pydantic URL subtype such asAnyHttpUrl, the value is revalidated through the declaredAnyUrlfield type instead of being stored as the stricter subtype.Root cause
Pydantic v2 URL equality is type-strict. A stored
AnyHttpUrl("https://example.com/callback")and an incomingAnyUrl("https://example.com/callback")serialize to the same string, but compare non-equal. That makesvalidate_redirect_uri()reject a registered redirect URI during OAuth flows.Changes
redirect_urisfield validator that stringifies existingAnyUrlinstances before Pydantic validates the field.OAuthClientInformationFull(redirect_uris=[AnyHttpUrl(...)])validating an incoming equivalentAnyUrl(...).Validation
uv run pytest tests/shared/test_auth.py::test_redirect_uri_subtypes_normalized_for_validation -qfailed before the fix withInvalidRedirectUriError.uv run ruff format --check src/mcp/shared/auth.py tests/shared/test_auth.pyuv run ruff check src/mcp/shared/auth.py tests/shared/test_auth.pyuv run pytest tests/shared/test_auth.py tests/client/test_auth.py tests/interaction/auth/test_authorize_token.py -q-> 155 passed, 1 xfailedgit diff --check