Skip to content

Commit df9e79f

Browse files
committed
Require a colon before a wildcard port
1 parent 174aa56 commit df9e79f

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎src/mcp/server/transport_security.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def _validate_host(self, host: str | None) -> bool:
5757
if host in self.settings.allowed_hosts:
5858
return True
5959

60-
base_host, _, port = host.rpartition(":")
61-
if port.isascii() and port.isdigit() and f"{base_host}:*" in self.settings.allowed_hosts:
60+
base_host, colon, port = host.rpartition(":")
61+
if colon and port.isascii() and port.isdigit() and f"{base_host}:*" in self.settings.allowed_hosts:
6262
return True
6363

6464
logger.warning(f"Invalid Host header: {host}")
@@ -74,8 +74,8 @@ def _validate_origin(self, origin: str | None) -> bool:
7474
if origin in self.settings.allowed_origins:
7575
return True
7676

77-
base_origin, _, port = origin.rpartition(":")
78-
if port.isascii() and port.isdigit() and f"{base_origin}:*" in self.settings.allowed_origins:
77+
base_origin, colon, port = origin.rpartition(":")
78+
if colon and port.isascii() and port.isdigit() and f"{base_origin}:*" in self.settings.allowed_origins:
7979
return True
8080

8181
logger.warning(f"Invalid Origin header: {origin}")

‎tests/server/test_transport_security.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def _request(host: str | None, origin: str | None, content_type: str | None = "a
4141
pytest.param("evil.example:9000", None, 421, id="host-wildcard-base-mismatch"),
4242
pytest.param("good.example", None, None, id="host-exact-no-origin"),
4343
pytest.param("wild.example:9000", None, None, id="host-wildcard-match"),
44-
pytest.param("wild.example:evil.example", None, 421, id="host-wildcard-non-numeric-port"),
44+
pytest.param("wild.example:abc", None, 421, id="host-wildcard-non-numeric-port"),
4545
pytest.param("wild.example:", None, 421, id="host-wildcard-empty-port"),
4646
pytest.param("good.example", "http://evil.example", 403, id="origin-no-match"),
4747
pytest.param("good.example", "http://evil.example:9000", 403, id="origin-wildcard-base-mismatch"),
4848
pytest.param("good.example", "http://good.example", None, id="origin-exact"),
4949
pytest.param("good.example", "http://wild.example:9000", None, id="origin-wildcard-match"),
5050
pytest.param(
51-
"good.example", "http://wild.example:9000.evil.example", 403, id="origin-wildcard-non-numeric-port"
51+
"good.example", "http://wild.example:9000.other.example", 403, id="origin-wildcard-non-numeric-port"
5252
),
5353
],
5454
)
@@ -61,6 +61,15 @@ async def test_validate_request_checks_host_then_origin(
6161
assert (None if response is None else response.status_code) == expected
6262

6363

64+
@pytest.mark.anyio
65+
async def test_validate_request_wildcard_port_needs_a_colon() -> None:
66+
"""A bare `:*` entry does not match a Host that is only digits."""
67+
middleware = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=[":*"]))
68+
response = await middleware.validate_request(_request("8080", None))
69+
assert response is not None
70+
assert response.status_code == 421
71+
72+
6473
@pytest.mark.anyio
6574
async def test_validate_request_skips_host_and_origin_when_protection_is_disabled() -> None:
6675
"""With DNS-rebinding protection off, any Host/Origin is accepted."""

0 commit comments

Comments
 (0)