🚀 The fast, Pythonic way to build MCP servers and clients.
fix(auth): collapse a repeated `iss` in the client redirect (#6352)
Felix Shie9bf8640d780
diff --git a/fastmcp_slim/fastmcp/server/auth/redirect_validation.py b/fastmcp_slim/fastmcp/server/auth/redirect_validation.pyindex ecba345a7..6077ba91b 100644--- a/fastmcp_slim/fastmcp/server/auth/redirect_validation.py+++ b/fastmcp_slim/fastmcp/server/auth/redirect_validation.py@@ -84,6 +84,34 @@ def replace_query_param(url: str, key: str, value: str) -> str:return _set_query(url, "&".join(new_segments))+def _drop_repeated_query_param(url: str, key: str) -> str:+ """Keep only the first occurrence of `key` in a URL's query string.++ `replace_query_param` rewrites the first match and leaves any later ones+ alone, which is right for a generic helper but wrong for a response+ parameter: RFC 6749 §3.1 says a response parameter "MUST NOT be included+ more than once". A registered redirect_uri can supply the repeats itself+ (e.g. `?iss=a&iss=b`), so the caller that promises a single value has to+ collapse them.++ Byte-preserving like its siblings: every segment other than a dropped+ duplicate is passed through verbatim.+ """+ parsed = urlparse(url)+ segments = parsed.query.split("&") if parsed.query else []++ kept: list[str] = []+ seen = False+ for segment in segments:+ if unquote(segment.split("=", 1)[0]) == key:+ if seen:+ continue+ seen = True+ kept.append(segment)++ return _set_query(url, "&".join(kept))++def build_client_redirect(url: str, params: dict[str, str], *, iss: str) -> str:"""Build a client-facing authorization redirect that carries exactly one `iss`.@@ -103,7 +131,9 @@ def build_client_redirect(url: str, params: dict[str, str], *, iss: str) -> str:`iss` is then set idempotently via `replace_query_param`: an existingoccurrence -- whether contributed by the registered redirect_uri oralready present in `url` -- is overwritten with the canonical value;- otherwise `iss` is appended.+ otherwise `iss` is appended. Any *further* `iss` the redirect_uri+ carried is dropped (see `_drop_repeated_query_param`), so the result+ holds exactly one.`iss` is keyword-only and required so a caller cannot forget to passit. `params` must not itself contain an `"iss"` key -- pass it via the@@ -129,7 +159,7 @@ def build_client_redirect(url: str, params: dict[str, str], *, iss: str) -> str:)if params:url = add_query_params(url, params)- return replace_query_param(url, "iss", iss)+ return _drop_repeated_query_param(replace_query_param(url, "iss", iss), "iss")def _parse_host_port(netloc: str) -> tuple[str | None, str | None]:diff --git a/tests/server/auth/test_redirect_validation.py b/tests/server/auth/test_redirect_validation.pyindex 5daa02c7e..43e21a944 100644--- a/tests/server/auth/test_redirect_validation.py+++ b/tests/server/auth/test_redirect_validation.py@@ -155,6 +155,42 @@ class TestBuildClientRedirect:assert len(iss_values) == 1assert iss_values == ["https://issuer.example.com/"]+ def test_collapses_a_repeated_iss_from_the_registered_redirect_uri(self):+ """A redirect_uri may carry `iss` more than once (`?iss=a&iss=b`).+ `replace_query_param` rewrites only the first match, so the survivor+ used to be `iss=<issuer>&iss=b` -- two response parameters, exactly+ what RFC 6749 §3.1 forbids. The helper promises "exactly one", so+ the later copies are dropped.+ """+ url = "https://client.example.com/cb?iss=a&iss=b"++ result = build_client_redirect(+ url,+ {"error": "access_denied", "state": "xyz"},+ iss="https://issuer.example.com/",+ )++ assert parse_qs(urlparse(result).query)["iss"] == [+ "https://issuer.example.com/"+ ]++ def test_dropping_repeated_iss_keeps_other_query_bytes_verbatim(self):+ """Collapsing the duplicates must not round-trip the rest of the+ query: the valueless `flag` and the non-UTF-8 `sig` stay byte for+ byte, and the surviving `iss` keeps its position.+ """+ url = "https://client.example.com/cb?iss=a&flag&sig=%FF%FE&iss=b"++ result = build_client_redirect(+ url,+ {"code": "abc"},+ iss="https://issuer.example.com/",+ )++ assert urlparse(result).query == (+ "iss=https%3A%2F%2Fissuer.example.com%2F&flag&sig=%FF%FE&code=abc"+ )+def test_preserves_valueless_param_and_non_utf8_bytes_alongside_existing_iss(self,):