π The fast, Pythonic way to build MCP servers and clients.
fix(auth): preserve empty authority on custom-scheme redirect URIs (#6347)
Akandwanaho Ivan Alvinc4c58b14add6
diff --git a/fastmcp_slim/fastmcp/server/auth/redirect_validation.py b/fastmcp_slim/fastmcp/server/auth/redirect_validation.pyindex 9bb3c1e69..ecba345a7 100644--- a/fastmcp_slim/fastmcp/server/auth/redirect_validation.py+++ b/fastmcp_slim/fastmcp/server/auth/redirect_validation.py@@ -20,6 +20,25 @@ UNSAFE_REDIRECT_URI_SCHEMES = frozenset()+def _set_query(url: str, query: str) -> str:+ """Replace `url`'s query component while preserving an empty `//` authority.++ `urlunparse` omits `//` when `netloc` is empty on custom schemes that are+ not in `urllib.parse.uses_netloc` (turning `myapp://` into `myapp:` and+ `myapp:///callback` into `myapp:/callback`).+ """+ parsed = urlparse(url)+ result = urlunparse(parsed._replace(query=query))+ prefix = f"{parsed.scheme}://"+ if (+ not parsed.netloc+ and url[: len(prefix)].lower() == prefix+ and not result.startswith(prefix)+ ):+ result = f"{parsed.scheme}://{result[len(parsed.scheme) + 1 :]}"+ return result++def add_query_params(url: str, params: dict[str, str]) -> str:"""Append query parameters to a URL while preserving existing parameters.@@ -32,7 +51,7 @@ def add_query_params(url: str, params: dict[str, str]) -> str:parsed = urlparse(url)new_query = urlencode(params)query = f"{parsed.query}&{new_query}" if parsed.query else new_query- return urlunparse(parsed._replace(query=query))+ return _set_query(url, query)def replace_query_param(url: str, key: str, value: str) -> str:@@ -62,7 +81,7 @@ def replace_query_param(url: str, key: str, value: str) -> str:if not replaced:new_segments.append(new_segment)- return urlunparse(parsed._replace(query="&".join(new_segments)))+ return _set_query(url, "&".join(new_segments))def build_client_redirect(url: str, params: dict[str, str], *, iss: str) -> str:diff --git a/tests/server/auth/test_redirect_validation.py b/tests/server/auth/test_redirect_validation.pyindex f34f6097e..5daa02c7e 100644--- a/tests/server/auth/test_redirect_validation.py+++ b/tests/server/auth/test_redirect_validation.py@@ -203,6 +203,29 @@ class TestBuildClientRedirect:assert "&&" not in resultassert not result.endswith("&")+ @pytest.mark.parametrize(+ "registered_uri,expected_prefix",+ [+ ("myapp://", "myapp://?"),+ ("myapp:///callback", "myapp:///callback?"),+ ("myapp://callback", "myapp://callback?"),+ ("com.example.app:/callback", "com.example.app:/callback?"),+ ],+ )+ def test_preserves_custom_scheme_empty_authority(+ self, registered_uri: str, expected_prefix: str+ ):+ """Regression test for #6296: custom native-app schemes with an empty+ authority (`myapp://`, `myapp:///callback`) must not have their `//`+ stripped by `urlunparse` when query parameters are appended."""+ result = build_client_redirect(+ registered_uri,+ {"code": "abc"},+ iss="https://as.example/",+ )++ assert result == (f"{expected_prefix}code=abc&iss=https%3A%2F%2Fas.example%2F")+class TestNoHandSpecifiedIssOutsideHelper:"""Guard against a future call site reintroducing the duplicate-`iss`