Skip to content

Commit 73d458b

Browse files
[v1.x] fix(auth): coerce empty-string optional URL fields to None in OAuthClientMetadata (#2405)
1 parent 8d4c2f5 commit 73d458b

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/mcp/shared/auth.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ class OAuthClientMetadata(BaseModel):
7171
software_id: str | None = None
7272
software_version: str | None = None
7373

74+
@field_validator(
75+
"client_uri",
76+
"logo_uri",
77+
"tos_uri",
78+
"policy_uri",
79+
"jwks_uri",
80+
mode="before",
81+
)
82+
@classmethod
83+
def _empty_string_optional_url_to_none(cls, v: object) -> object:
84+
# RFC 7591 §2 marks these URL fields OPTIONAL. Some authorization servers
85+
# echo omitted metadata back as "" instead of dropping the keys, which
86+
# AnyHttpUrl would otherwise reject — throwing away an otherwise valid
87+
# registration response. Treat "" as absent.
88+
if v == "":
89+
return None
90+
return v
91+
7492
def validate_scope(self, requested_scope: str | None) -> list[str] | None:
7593
if requested_scope is None:
7694
return None

tests/shared/test_auth.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Tests for OAuth 2.0 shared code."""
22

3-
from mcp.shared.auth import OAuthMetadata
3+
import pytest
4+
from pydantic import ValidationError
5+
6+
from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthMetadata
47

58

69
class TestOAuthMetadata:
@@ -59,3 +62,80 @@ def test_oauth_with_jarm(self):
5962
"token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"],
6063
}
6164
)
65+
66+
67+
# RFC 7591 §2 marks client_uri/logo_uri/tos_uri/policy_uri/jwks_uri as OPTIONAL.
68+
# Some authorization servers echo the client's omitted metadata back as ""
69+
# instead of dropping the keys; without coercion, AnyHttpUrl rejects "" and
70+
# the whole registration response is thrown away even though the server
71+
# returned a valid client_id.
72+
73+
74+
@pytest.mark.parametrize(
75+
"empty_field",
76+
["client_uri", "logo_uri", "tos_uri", "policy_uri", "jwks_uri"],
77+
)
78+
def test_optional_url_empty_string_coerced_to_none(empty_field: str):
79+
data = {
80+
"redirect_uris": ["https://example.com/callback"],
81+
empty_field: "",
82+
}
83+
metadata = OAuthClientMetadata.model_validate(data)
84+
assert getattr(metadata, empty_field) is None
85+
86+
87+
def test_all_optional_urls_empty_together():
88+
data = {
89+
"redirect_uris": ["https://example.com/callback"],
90+
"client_uri": "",
91+
"logo_uri": "",
92+
"tos_uri": "",
93+
"policy_uri": "",
94+
"jwks_uri": "",
95+
}
96+
metadata = OAuthClientMetadata.model_validate(data)
97+
assert metadata.client_uri is None
98+
assert metadata.logo_uri is None
99+
assert metadata.tos_uri is None
100+
assert metadata.policy_uri is None
101+
assert metadata.jwks_uri is None
102+
103+
104+
def test_valid_url_passes_through_unchanged():
105+
data = {
106+
"redirect_uris": ["https://example.com/callback"],
107+
"client_uri": "https://udemy.com/",
108+
}
109+
metadata = OAuthClientMetadata.model_validate(data)
110+
assert str(metadata.client_uri) == "https://udemy.com/"
111+
112+
113+
def test_information_full_inherits_coercion():
114+
"""OAuthClientInformationFull subclasses OAuthClientMetadata, so the
115+
same coercion applies to DCR responses parsed via the full model."""
116+
data = {
117+
"client_id": "abc123",
118+
"redirect_uris": ["https://example.com/callback"],
119+
"client_uri": "",
120+
"logo_uri": "",
121+
"tos_uri": "",
122+
"policy_uri": "",
123+
"jwks_uri": "",
124+
}
125+
info = OAuthClientInformationFull.model_validate(data)
126+
assert info.client_id == "abc123"
127+
assert info.client_uri is None
128+
assert info.logo_uri is None
129+
assert info.tos_uri is None
130+
assert info.policy_uri is None
131+
assert info.jwks_uri is None
132+
133+
134+
def test_invalid_non_empty_url_still_rejected():
135+
"""Coercion must only touch empty strings — garbage URLs still raise."""
136+
data = {
137+
"redirect_uris": ["https://example.com/callback"],
138+
"client_uri": "not a url",
139+
}
140+
with pytest.raises(ValidationError):
141+
OAuthClientMetadata.model_validate(data)

0 commit comments

Comments
 (0)