Skip to content

Commit 9771e6b

Browse files
authored
Keep following a relative redirect when the endpoint URL carries userinfo (#3450)
1 parent a925e55 commit 9771e6b

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/mcp/shared/_httpx_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,18 @@ def next_request_within_origin(response: httpx2.Response) -> httpx2.Request | No
9090
GET: httpx2 turns a POST into a body-less GET for 301/302/303, which would
9191
drop the message), its URL stays within the origin of the request just sent
9292
(same scheme, host and port, or http to https on the same host with default
93-
ports), and the Location carries no userinfo (which httpx2 would otherwise
94-
send as Basic auth). None for anything else, including a non-redirect.
93+
ports), and the Location does not bring userinfo of its own (which httpx2
94+
would otherwise send as Basic auth; userinfo the configured URL already had
95+
is kept by a relative Location and is fine). None for anything else,
96+
including a non-redirect.
9597
"""
9698
next_request = response.next_request
9799
if next_request is None:
98100
return None
99101
sent = response.request
100102
if (
101103
next_request.method != sent.method
102-
or next_request.url.userinfo
104+
or (next_request.url.userinfo and next_request.url.userinfo != sent.url.userinfo)
103105
or not _within_origin(sent.url, next_request.url)
104106
):
105107
return None

tests/shared/test_httpx_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ async def test_redirect_location_with_userinfo_is_not_followed():
208208
assert received == [f"POST {url}"]
209209

210210

211+
async def test_userinfo_of_the_configured_url_kept_by_a_relative_location_is_followed():
212+
"""Userinfo the caller put in the endpoint URL is carried over by a relative Location (URL join
213+
keeps the authority); that is the caller's own credential for the same origin, so the redirect
214+
is followed as httpx2 itself would (SDK-defined)."""
215+
url = "http://user:secret@mcp.example/mcp"
216+
client, received, _ = _recording_client({url: (307, "/mcp/")})
217+
218+
async with client, stream_within_origin(client, "POST", url, content=b"payload") as response:
219+
await response.aread()
220+
221+
assert response.status_code == 200
222+
assert received == [f"POST {url}", "POST http://user:secret@mcp.example/mcp/"]
223+
224+
211225
async def test_request_within_origin_returns_a_read_response():
212226
"""The non-streaming form hands back a response whose body is already read."""
213227
url = "http://mcp.example/mcp"

0 commit comments

Comments
 (0)