Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

fix(auth): avoid rereading failed authorization forms (#5570)

Junpei Zhange9f0443ca60a
diff --git a/fastmcp_slim/fastmcp/server/auth/handlers/authorize.py b/fastmcp_slim/fastmcp/server/auth/handlers/authorize.py
index d560acd1d..b442cc65a 100644
--- a/fastmcp_slim/fastmcp/server/auth/handlers/authorize.py
+++ b/fastmcp_slim/fastmcp/server/auth/handlers/authorize.py
@@ -252,11 +252,16 @@ class AuthorizationHandler(SDKAuthorizationHandler):
if request.method == "GET":
client_id = request.query_params.get("client_id")
else:
- form = await request.form()
- client_id_value = form.get("client_id")
- # Ensure client_id is a string, not UploadFile
- if isinstance(client_id_value, str):
- client_id = client_id_value
+ # The SDK handler has already parsed POST form data. Re-reading
+ # it here can raise "Stream consumed" when parsing failed and
+ # the request body cannot be replayed. Only enhance errors when
+ # the SDK left a successfully parsed form cached on the request.
+ form = getattr(request, "_form", None)
+ if form is not None:
+ client_id_value = form.get("client_id")
+ # Ensure client_id is a string, not UploadFile
+ if isinstance(client_id_value, str):
+ client_id = client_id_value
# If we have a client_id and the error is about it not being found,
# enhance the response
diff --git a/tests/server/auth/test_enhanced_error_responses.py b/tests/server/auth/test_enhanced_error_responses.py
index 091205493..b0184d9c5 100644
--- a/tests/server/auth/test_enhanced_error_responses.py
+++ b/tests/server/auth/test_enhanced_error_responses.py
@@ -180,6 +180,43 @@ class TestEnhancedAuthorizationHandler:
assert "Link" in response.headers
assert "/register" in response.headers["Link"]
+ def test_malformed_post_form_returns_error_without_rereading_stream(
+ self, oauth_proxy
+ ):
+ app = Starlette(routes=oauth_proxy.get_routes())
+ body = b"--expected-boundary\r\nnot a valid multipart field"
+
+ with TestClient(app) as client:
+ response = client.post(
+ "/authorize",
+ content=body,
+ headers={
+ "Content-Type": "multipart/form-data; boundary=expected-boundary"
+ },
+ )
+
+ assert response.status_code == 400
+
+ def test_unregistered_post_client_still_returns_enhanced_error(self, oauth_proxy):
+ app = Starlette(routes=oauth_proxy.get_routes())
+
+ with TestClient(app) as client:
+ response = client.post(
+ "/authorize",
+ data={
+ "client_id": "unregistered-post-client",
+ "redirect_uri": "http://localhost:12345/callback",
+ "response_type": "code",
+ "code_challenge": "test-challenge",
+ "state": "test-state",
+ },
+ headers={"Accept": "text/html"},
+ )
+
+ assert response.status_code == 400
+ assert "text/html" in response.headers["content-type"]
+ assert "unregistered-post-client" in response.text
+
def test_successful_authorization_not_enhanced(self, oauth_proxy):
"""Test that successful authorizations are not modified by enhancement."""
app = Starlette(routes=oauth_proxy.get_routes())