From 174aa5631523fc5d05355f504c254e00343363a0 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 17 Nov 2026 10:30:53 +0100 Subject: [PATCH 1/2] Require a numeric port for wildcard Host and Origin entries --- src/mcp/server/transport_security.py | 22 ++++++---------------- tests/server/test_transport_security.py | 5 +++++ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/mcp/server/transport_security.py b/src/mcp/server/transport_security.py index 91b5fa7edb..732cf84a73 100644 --- a/src/mcp/server/transport_security.py +++ b/src/mcp/server/transport_security.py @@ -57,14 +57,9 @@ def _validate_host(self, host: str | None) -> bool: if host in self.settings.allowed_hosts: return True - # Check wildcard port patterns - for allowed in self.settings.allowed_hosts: - if allowed.endswith(":*"): - # Extract base host from pattern - base_host = allowed[:-2] - # Check if the actual host starts with base host and has a port - if host.startswith(base_host + ":"): - return True + base_host, _, port = host.rpartition(":") + if port.isascii() and port.isdigit() and f"{base_host}:*" in self.settings.allowed_hosts: + return True logger.warning(f"Invalid Host header: {host}") return False @@ -79,14 +74,9 @@ def _validate_origin(self, origin: str | None) -> bool: if origin in self.settings.allowed_origins: return True - # Check wildcard port patterns - for allowed in self.settings.allowed_origins: - if allowed.endswith(":*"): - # Extract base origin from pattern - base_origin = allowed[:-2] - # Check if the actual origin starts with base origin and has a port - if origin.startswith(base_origin + ":"): - return True + base_origin, _, port = origin.rpartition(":") + if port.isascii() and port.isdigit() and f"{base_origin}:*" in self.settings.allowed_origins: + return True logger.warning(f"Invalid Origin header: {origin}") return False diff --git a/tests/server/test_transport_security.py b/tests/server/test_transport_security.py index 67fe4ef1a1..c58801b9fe 100644 --- a/tests/server/test_transport_security.py +++ b/tests/server/test_transport_security.py @@ -41,10 +41,15 @@ def _request(host: str | None, origin: str | None, content_type: str | None = "a pytest.param("evil.example:9000", None, 421, id="host-wildcard-base-mismatch"), pytest.param("good.example", None, None, id="host-exact-no-origin"), pytest.param("wild.example:9000", None, None, id="host-wildcard-match"), + pytest.param("wild.example:evil.example", None, 421, id="host-wildcard-non-numeric-port"), + pytest.param("wild.example:", None, 421, id="host-wildcard-empty-port"), pytest.param("good.example", "http://evil.example", 403, id="origin-no-match"), pytest.param("good.example", "http://evil.example:9000", 403, id="origin-wildcard-base-mismatch"), pytest.param("good.example", "http://good.example", None, id="origin-exact"), pytest.param("good.example", "http://wild.example:9000", None, id="origin-wildcard-match"), + pytest.param( + "good.example", "http://wild.example:9000.evil.example", 403, id="origin-wildcard-non-numeric-port" + ), ], ) async def test_validate_request_checks_host_then_origin( From df9e79f3427c8eea1934bbc192a337901fe6d09c Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 17 Nov 2026 10:38:45 +0100 Subject: [PATCH 2/2] Require a colon before a wildcard port --- src/mcp/server/transport_security.py | 8 ++++---- tests/server/test_transport_security.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/mcp/server/transport_security.py b/src/mcp/server/transport_security.py index 732cf84a73..7e38626977 100644 --- a/src/mcp/server/transport_security.py +++ b/src/mcp/server/transport_security.py @@ -57,8 +57,8 @@ def _validate_host(self, host: str | None) -> bool: if host in self.settings.allowed_hosts: return True - base_host, _, port = host.rpartition(":") - if port.isascii() and port.isdigit() and f"{base_host}:*" in self.settings.allowed_hosts: + base_host, colon, port = host.rpartition(":") + if colon and port.isascii() and port.isdigit() and f"{base_host}:*" in self.settings.allowed_hosts: return True logger.warning(f"Invalid Host header: {host}") @@ -74,8 +74,8 @@ def _validate_origin(self, origin: str | None) -> bool: if origin in self.settings.allowed_origins: return True - base_origin, _, port = origin.rpartition(":") - if port.isascii() and port.isdigit() and f"{base_origin}:*" in self.settings.allowed_origins: + base_origin, colon, port = origin.rpartition(":") + if colon and port.isascii() and port.isdigit() and f"{base_origin}:*" in self.settings.allowed_origins: return True logger.warning(f"Invalid Origin header: {origin}") diff --git a/tests/server/test_transport_security.py b/tests/server/test_transport_security.py index c58801b9fe..67cbd3b5ed 100644 --- a/tests/server/test_transport_security.py +++ b/tests/server/test_transport_security.py @@ -41,14 +41,14 @@ def _request(host: str | None, origin: str | None, content_type: str | None = "a pytest.param("evil.example:9000", None, 421, id="host-wildcard-base-mismatch"), pytest.param("good.example", None, None, id="host-exact-no-origin"), pytest.param("wild.example:9000", None, None, id="host-wildcard-match"), - pytest.param("wild.example:evil.example", None, 421, id="host-wildcard-non-numeric-port"), + pytest.param("wild.example:abc", None, 421, id="host-wildcard-non-numeric-port"), pytest.param("wild.example:", None, 421, id="host-wildcard-empty-port"), pytest.param("good.example", "http://evil.example", 403, id="origin-no-match"), pytest.param("good.example", "http://evil.example:9000", 403, id="origin-wildcard-base-mismatch"), pytest.param("good.example", "http://good.example", None, id="origin-exact"), pytest.param("good.example", "http://wild.example:9000", None, id="origin-wildcard-match"), pytest.param( - "good.example", "http://wild.example:9000.evil.example", 403, id="origin-wildcard-non-numeric-port" + "good.example", "http://wild.example:9000.other.example", 403, id="origin-wildcard-non-numeric-port" ), ], ) @@ -61,6 +61,15 @@ async def test_validate_request_checks_host_then_origin( assert (None if response is None else response.status_code) == expected +@pytest.mark.anyio +async def test_validate_request_wildcard_port_needs_a_colon() -> None: + """A bare `:*` entry does not match a Host that is only digits.""" + middleware = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=[":*"])) + response = await middleware.validate_request(_request("8080", None)) + assert response is not None + assert response.status_code == 421 + + @pytest.mark.anyio async def test_validate_request_skips_host_and_origin_when_protection_is_disabled() -> None: """With DNS-rebinding protection off, any Host/Origin is accepted."""