Skip to content

Commit 51c352b

Browse files
authored
Merge pull request #5240 from modelcontextprotocol/v2/fix/GHSA-v38p-wf49-hx2j
fix(fetch): check the host name the connection uses
2 parents ac2b66b + db4cf2b commit 51c352b

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ async def _refuse_private_destination(request: httpx.Request) -> None:
141141
142142
httpx runs request hooks before every request it sends, redirect hops
143143
included, so a public URL cannot bounce the fetch to an internal one.
144+
145+
The name checked is ``raw_host``, the ASCII (IDNA 2008) form the transport
146+
connects to. ``host`` is the decoded Unicode form, which getaddrinfo would
147+
re-encode with IDNA 2003 into a different name for some labels (``ß``
148+
becomes ``ss``), so checking it would vet one name and connect to another.
144149
"""
145-
host = request.url.host
150+
host = request.url.raw_host.decode("ascii")
146151
if not host:
147152
return # httpx rejects a URL with no host itself.
148153
try:

‎src/fetch/tests/test_protocol.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
serve,
3838
)
3939

40-
from .harness import FakeWeb, connect, raw_request
40+
from .harness import PUBLIC_IP, FakeWeb, connect, raw_request
4141

4242
PAGE = "https://example.com/page"
4343
ROBOTS = "https://example.com/robots.txt"
@@ -950,6 +950,34 @@ async def test_unresolvable_host_is_left_to_the_transport(web: FakeWeb) -> None:
950950
assert web.lookups == ["only-the-proxy-knows.example"]
951951

952952

953+
@pytest.mark.parametrize(
954+
"url, connected_host",
955+
[
956+
# IDNA 2003 (the codec getaddrinfo applies to a Unicode name) maps ß
957+
# and final sigma to ss and σ; IDNA 2008, which httpx connects with,
958+
# keeps them. The guard must resolve the name the connection uses.
959+
("http://faß.example.com/", "xn--fa-hia.example.com"),
960+
("http://xn--fa-hia.example.com/", "xn--fa-hia.example.com"),
961+
("http://σοφος.example.com/", "xn--0xaajbq.example.com"),
962+
],
963+
)
964+
async def test_guard_resolves_the_name_the_connection_uses(
965+
web: FakeWeb, url: str, connected_host: str
966+
) -> None:
967+
web.dns["fass.example.com"] = [PUBLIC_IP]
968+
web.dns["σοφοσ.example.com"] = [PUBLIC_IP]
969+
web.dns[connected_host] = ["127.0.0.1"]
970+
web.add(f"http://{connected_host}/", plain("internal-secret"))
971+
async with connect(ignore_robots_txt=True) as (session, _):
972+
result = await call(session, {"url": url})
973+
assert wire(result)["isError"] is True
974+
assert f": {connected_host} resolves to 127.0.0.1, which is not a public" in (
975+
text_of(result)
976+
)
977+
assert web.lookups == [connected_host]
978+
assert web.requests == []
979+
980+
953981
async def test_allow_private_ips_follows_redirect_to_private_address(
954982
web: FakeWeb,
955983
) -> None:

0 commit comments

Comments
 (0)