|
| 1 | +"""Unit tests for TransportSecurityMiddleware.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from starlette.requests import Request |
| 5 | + |
| 6 | +from mcp.server.transport_security import TransportSecurityMiddleware, TransportSecuritySettings |
| 7 | + |
| 8 | + |
| 9 | +def make_request(headers: dict[str, str], method: str = "GET") -> Request: |
| 10 | + scope = { |
| 11 | + "type": "http", |
| 12 | + "method": method, |
| 13 | + "path": "/", |
| 14 | + "query_string": b"", |
| 15 | + "headers": [(k.lower().encode(), v.encode()) for k, v in headers.items()], |
| 16 | + } |
| 17 | + return Request(scope) |
| 18 | + |
| 19 | + |
| 20 | +def make_middleware( |
| 21 | + *, |
| 22 | + allowed_hosts: list[str] | None = None, |
| 23 | + allowed_origins: list[str] | None = None, |
| 24 | +) -> TransportSecurityMiddleware: |
| 25 | + return TransportSecurityMiddleware( |
| 26 | + TransportSecuritySettings( |
| 27 | + enable_dns_rebinding_protection=True, |
| 28 | + allowed_hosts=allowed_hosts or [], |
| 29 | + allowed_origins=allowed_origins or [], |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | +# _validate_host |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | + |
| 38 | + |
| 39 | +def test_validate_host_missing_header(): |
| 40 | + mw = make_middleware(allowed_hosts=["example.com"]) |
| 41 | + assert mw._validate_host(None) is False |
| 42 | + |
| 43 | + |
| 44 | +def test_validate_host_exact_match(): |
| 45 | + mw = make_middleware(allowed_hosts=["example.com"]) |
| 46 | + assert mw._validate_host("example.com") is True |
| 47 | + |
| 48 | + |
| 49 | +def test_validate_host_no_match(): |
| 50 | + mw = make_middleware(allowed_hosts=["example.com"]) |
| 51 | + assert mw._validate_host("evil.com") is False |
| 52 | + |
| 53 | + |
| 54 | +def test_validate_host_port_wildcard_matches(): |
| 55 | + mw = make_middleware(allowed_hosts=["example.com:*"]) |
| 56 | + assert mw._validate_host("example.com:8080") is True |
| 57 | + |
| 58 | + |
| 59 | +def test_validate_host_port_wildcard_different_host(): |
| 60 | + mw = make_middleware(allowed_hosts=["example.com:*"]) |
| 61 | + assert mw._validate_host("evil.com:8080") is False |
| 62 | + |
| 63 | + |
| 64 | +def test_validate_host_subdomain_wildcard_base_domain(): |
| 65 | + # "*.example.com" should match the base domain itself |
| 66 | + mw = make_middleware(allowed_hosts=["*.example.com"]) |
| 67 | + assert mw._validate_host("example.com") is True |
| 68 | + |
| 69 | + |
| 70 | +def test_validate_host_subdomain_wildcard_with_subdomain(): |
| 71 | + mw = make_middleware(allowed_hosts=["*.example.com"]) |
| 72 | + assert mw._validate_host("app.example.com") is True |
| 73 | + |
| 74 | + |
| 75 | +def test_validate_host_subdomain_wildcard_with_nested_subdomain(): |
| 76 | + mw = make_middleware(allowed_hosts=["*.example.com"]) |
| 77 | + assert mw._validate_host("api.staging.example.com") is True |
| 78 | + |
| 79 | + |
| 80 | +def test_validate_host_subdomain_wildcard_with_port(): |
| 81 | + # Port should be stripped before subdomain matching |
| 82 | + mw = make_middleware(allowed_hosts=["*.example.com"]) |
| 83 | + assert mw._validate_host("app.example.com:443") is True |
| 84 | + |
| 85 | + |
| 86 | +def test_validate_host_subdomain_wildcard_no_match(): |
| 87 | + mw = make_middleware(allowed_hosts=["*.example.com"]) |
| 88 | + assert mw._validate_host("notexample.com") is False |
| 89 | + |
| 90 | + |
| 91 | +def test_validate_host_subdomain_wildcard_suffix_collision(): |
| 92 | + # "fakeexample.com" must not match "*.example.com" |
| 93 | + mw = make_middleware(allowed_hosts=["*.example.com"]) |
| 94 | + assert mw._validate_host("fakeexample.com") is False |
| 95 | + |
| 96 | + |
| 97 | +# --------------------------------------------------------------------------- |
| 98 | +# _validate_origin |
| 99 | +# --------------------------------------------------------------------------- |
| 100 | + |
| 101 | + |
| 102 | +def test_validate_origin_absent(): |
| 103 | + mw = make_middleware(allowed_origins=["https://example.com"]) |
| 104 | + assert mw._validate_origin(None) is True |
| 105 | + |
| 106 | + |
| 107 | +def test_validate_origin_exact_match(): |
| 108 | + mw = make_middleware(allowed_origins=["https://example.com"]) |
| 109 | + assert mw._validate_origin("https://example.com") is True |
| 110 | + |
| 111 | + |
| 112 | +def test_validate_origin_no_match(): |
| 113 | + mw = make_middleware(allowed_origins=["https://example.com"]) |
| 114 | + assert mw._validate_origin("https://evil.com") is False |
| 115 | + |
| 116 | + |
| 117 | +def test_validate_origin_port_wildcard_matches(): |
| 118 | + mw = make_middleware(allowed_origins=["https://example.com:*"]) |
| 119 | + assert mw._validate_origin("https://example.com:8443") is True |
| 120 | + |
| 121 | + |
| 122 | +def test_validate_origin_port_wildcard_different_host(): |
| 123 | + mw = make_middleware(allowed_origins=["https://example.com:*"]) |
| 124 | + assert mw._validate_origin("https://evil.com:8443") is False |
| 125 | + |
| 126 | + |
| 127 | +def test_validate_origin_subdomain_wildcard_base_domain(): |
| 128 | + # "https://*.example.com" should match the base domain itself |
| 129 | + mw = make_middleware(allowed_origins=["https://*.example.com"]) |
| 130 | + assert mw._validate_origin("https://example.com") is True |
| 131 | + |
| 132 | + |
| 133 | +def test_validate_origin_subdomain_wildcard_with_subdomain(): |
| 134 | + mw = make_middleware(allowed_origins=["https://*.example.com"]) |
| 135 | + assert mw._validate_origin("https://app.example.com") is True |
| 136 | + |
| 137 | + |
| 138 | +def test_validate_origin_subdomain_wildcard_scheme_mismatch(): |
| 139 | + mw = make_middleware(allowed_origins=["https://*.example.com"]) |
| 140 | + assert mw._validate_origin("http://app.example.com") is False |
| 141 | + |
| 142 | + |
| 143 | +def test_validate_origin_subdomain_wildcard_no_match(): |
| 144 | + mw = make_middleware(allowed_origins=["https://*.example.com"]) |
| 145 | + assert mw._validate_origin("https://evil.com") is False |
| 146 | + |
| 147 | + |
| 148 | +# --------------------------------------------------------------------------- |
| 149 | +# validate_request (integration over the public method) |
| 150 | +# --------------------------------------------------------------------------- |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.anyio |
| 154 | +async def test_validate_request_post_invalid_content_type(): |
| 155 | + mw = make_middleware(allowed_hosts=["example.com"]) |
| 156 | + req = make_request({"host": "example.com", "content-type": "text/plain"}, method="POST") |
| 157 | + resp = await mw.validate_request(req, is_post=True) |
| 158 | + assert resp is not None |
| 159 | + assert resp.status_code == 400 |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.anyio |
| 163 | +async def test_validate_request_post_valid_content_type_protection_disabled(): |
| 164 | + mw = TransportSecurityMiddleware(TransportSecuritySettings(enable_dns_rebinding_protection=False)) |
| 165 | + req = make_request({"host": "example.com", "content-type": "application/json"}, method="POST") |
| 166 | + resp = await mw.validate_request(req, is_post=True) |
| 167 | + assert resp is None |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.anyio |
| 171 | +async def test_validate_request_get_protection_disabled(): |
| 172 | + mw = TransportSecurityMiddleware(TransportSecuritySettings(enable_dns_rebinding_protection=False)) |
| 173 | + req = make_request({"host": "evil.com"}, method="GET") |
| 174 | + resp = await mw.validate_request(req, is_post=False) |
| 175 | + assert resp is None |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.anyio |
| 179 | +async def test_validate_request_get_invalid_host(): |
| 180 | + mw = make_middleware(allowed_hosts=["example.com"]) |
| 181 | + req = make_request({"host": "evil.com"}, method="GET") |
| 182 | + resp = await mw.validate_request(req, is_post=False) |
| 183 | + assert resp is not None |
| 184 | + assert resp.status_code == 421 |
| 185 | + |
| 186 | + |
| 187 | +@pytest.mark.anyio |
| 188 | +async def test_validate_request_post_invalid_host(): |
| 189 | + mw = make_middleware(allowed_hosts=["example.com"]) |
| 190 | + req = make_request({"host": "evil.com", "content-type": "application/json"}, method="POST") |
| 191 | + resp = await mw.validate_request(req, is_post=True) |
| 192 | + assert resp is not None |
| 193 | + assert resp.status_code == 421 |
| 194 | + |
| 195 | + |
| 196 | +@pytest.mark.anyio |
| 197 | +async def test_validate_request_invalid_origin(): |
| 198 | + mw = make_middleware(allowed_hosts=["example.com"], allowed_origins=["https://example.com"]) |
| 199 | + req = make_request({"host": "example.com", "origin": "https://evil.com"}, method="GET") |
| 200 | + resp = await mw.validate_request(req, is_post=False) |
| 201 | + assert resp is not None |
| 202 | + assert resp.status_code == 403 |
| 203 | + |
| 204 | + |
| 205 | +@pytest.mark.anyio |
| 206 | +async def test_validate_request_all_valid(): |
| 207 | + mw = make_middleware(allowed_hosts=["example.com"], allowed_origins=["https://example.com"]) |
| 208 | + req = make_request({"host": "example.com", "origin": "https://example.com"}, method="GET") |
| 209 | + resp = await mw.validate_request(req, is_post=False) |
| 210 | + assert resp is None |
| 211 | + |
| 212 | + |
| 213 | +@pytest.mark.anyio |
| 214 | +async def test_validate_request_wildcard_host_end_to_end(): |
| 215 | + mw = make_middleware(allowed_hosts=["*.example.com"], allowed_origins=["https://*.example.com"]) |
| 216 | + req = make_request({"host": "api.example.com", "origin": "https://app.example.com"}, method="GET") |
| 217 | + resp = await mw.validate_request(req, is_post=False) |
| 218 | + assert resp is None |
0 commit comments