Skip to content

Commit 2dfb51a

Browse files
fix(auth): coerce empty-string optional URL fields to None in OAuthClientMetadata (#2404)
1 parent 941089e commit 2dfb51a

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

AGENTS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545

4646
- Framework: `uv run --frozen pytest`
4747
- Async testing: use anyio, not asyncio
48-
- Do not use `Test` prefixed classes, use functions
48+
- Do not use `Test` prefixed classes — write plain top-level `test_*` functions.
49+
Legacy files still contain `Test*` classes; do NOT follow that pattern for new
50+
tests even when adding to such a file.
4951
- IMPORTANT: Tests should be fast and deterministic. Prefer in-memory async execution;
5052
reach for threads only when necessary, and subprocesses only as a last resort.
5153
- For end-to-end behavior, an in-memory `Client(server)` is usually the

src/mcp/shared/auth.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ class OAuthClientMetadata(BaseModel):
6767
software_id: str | None = None
6868
software_version: str | None = None
6969

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

0 commit comments

Comments
 (0)