|
| 1 | +"""Unit tests for TransportSecuritySettings and TransportSecurityMiddleware.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +import pytest |
| 6 | +from starlette.requests import Request |
| 7 | + |
| 8 | +from mcp.server.transport_security import TransportSecurityMiddleware, TransportSecuritySettings |
| 9 | + |
| 10 | + |
| 11 | +def make_request(headers: dict[str, str], method: str = "GET") -> Request: |
| 12 | + scope = { |
| 13 | + "type": "http", |
| 14 | + "method": method, |
| 15 | + "headers": [(k.lower().encode(), v.encode()) for k, v in headers.items()], |
| 16 | + "path": "/", |
| 17 | + "query_string": b"", |
| 18 | + } |
| 19 | + return Request(scope) |
| 20 | + |
| 21 | + |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | +# TransportSecuritySettings — construction-time warning |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | + |
| 26 | + |
| 27 | +def test_no_warning_when_protection_disabled(caplog: pytest.LogCaptureFixture) -> None: |
| 28 | + with caplog.at_level(logging.WARNING, logger="mcp.server.transport_security"): |
| 29 | + TransportSecuritySettings(enable_dns_rebinding_protection=False) |
| 30 | + assert not caplog.records |
| 31 | + |
| 32 | + |
| 33 | +def test_no_warning_when_allowed_hosts_populated(caplog: pytest.LogCaptureFixture) -> None: |
| 34 | + with caplog.at_level(logging.WARNING, logger="mcp.server.transport_security"): |
| 35 | + TransportSecuritySettings( |
| 36 | + enable_dns_rebinding_protection=True, |
| 37 | + allowed_hosts=["example.com"], |
| 38 | + ) |
| 39 | + assert not caplog.records |
| 40 | + |
| 41 | + |
| 42 | +def test_warning_when_protection_enabled_with_empty_allowed_hosts(caplog: pytest.LogCaptureFixture) -> None: |
| 43 | + with caplog.at_level(logging.WARNING, logger="mcp.server.transport_security"): |
| 44 | + TransportSecuritySettings(enable_dns_rebinding_protection=True) |
| 45 | + assert len(caplog.records) == 1 |
| 46 | + assert "allowed_hosts is empty" in caplog.records[0].message |
| 47 | + assert "HTTP 421" in caplog.records[0].message |
| 48 | + assert "allowed_hosts=" in caplog.records[0].message |
| 49 | + |
| 50 | + |
| 51 | +# --------------------------------------------------------------------------- |
| 52 | +# TransportSecurityMiddleware._validate_host |
| 53 | +# --------------------------------------------------------------------------- |
| 54 | + |
| 55 | + |
| 56 | +def test_validate_host_missing_host() -> None: |
| 57 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["example.com"])) |
| 58 | + assert m._validate_host(None) is False |
| 59 | + |
| 60 | + |
| 61 | +def test_validate_host_exact_match() -> None: |
| 62 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["example.com"])) |
| 63 | + assert m._validate_host("example.com") is True |
| 64 | + |
| 65 | + |
| 66 | +def test_validate_host_exact_no_match() -> None: |
| 67 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["example.com"])) |
| 68 | + assert m._validate_host("other.com") is False |
| 69 | + |
| 70 | + |
| 71 | +def test_validate_host_port_wildcard_match() -> None: |
| 72 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["localhost:*"])) |
| 73 | + assert m._validate_host("localhost:8080") is True |
| 74 | + |
| 75 | + |
| 76 | +def test_validate_host_port_wildcard_different_base() -> None: |
| 77 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["localhost:*"])) |
| 78 | + assert m._validate_host("other:8080") is False |
| 79 | + |
| 80 | + |
| 81 | +def test_validate_host_port_wildcard_no_port() -> None: |
| 82 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["localhost:*"])) |
| 83 | + assert m._validate_host("localhost") is False |
| 84 | + |
| 85 | + |
| 86 | +# --------------------------------------------------------------------------- |
| 87 | +# TransportSecurityMiddleware._validate_origin |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | + |
| 90 | + |
| 91 | +def test_validate_origin_absent_is_allowed() -> None: |
| 92 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_origins=["http://example.com"])) |
| 93 | + assert m._validate_origin(None) is True |
| 94 | + |
| 95 | + |
| 96 | +def test_validate_origin_exact_match() -> None: |
| 97 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_origins=["http://example.com"])) |
| 98 | + assert m._validate_origin("http://example.com") is True |
| 99 | + |
| 100 | + |
| 101 | +def test_validate_origin_exact_no_match() -> None: |
| 102 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_origins=["http://example.com"])) |
| 103 | + assert m._validate_origin("http://other.com") is False |
| 104 | + |
| 105 | + |
| 106 | +def test_validate_origin_port_wildcard_match() -> None: |
| 107 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_origins=["http://localhost:*"])) |
| 108 | + assert m._validate_origin("http://localhost:3000") is True |
| 109 | + |
| 110 | + |
| 111 | +def test_validate_origin_port_wildcard_different_base() -> None: |
| 112 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_origins=["http://localhost:*"])) |
| 113 | + assert m._validate_origin("http://other:3000") is False |
| 114 | + |
| 115 | + |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | +# TransportSecurityMiddleware.validate_request |
| 118 | +# --------------------------------------------------------------------------- |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.anyio |
| 122 | +async def test_validate_request_post_valid_content_type() -> None: |
| 123 | + m = TransportSecurityMiddleware(TransportSecuritySettings(enable_dns_rebinding_protection=False)) |
| 124 | + request = make_request({"content-type": "application/json"}, method="POST") |
| 125 | + assert await m.validate_request(request, is_post=True) is None |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.anyio |
| 129 | +async def test_validate_request_post_invalid_content_type() -> None: |
| 130 | + m = TransportSecurityMiddleware(TransportSecuritySettings(enable_dns_rebinding_protection=False)) |
| 131 | + request = make_request({"content-type": "text/plain"}, method="POST") |
| 132 | + response = await m.validate_request(request, is_post=True) |
| 133 | + assert response is not None |
| 134 | + assert response.status_code == 400 |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.anyio |
| 138 | +async def test_validate_request_get_skips_content_type() -> None: |
| 139 | + m = TransportSecurityMiddleware(TransportSecuritySettings(enable_dns_rebinding_protection=False)) |
| 140 | + request = make_request({}) |
| 141 | + assert await m.validate_request(request, is_post=False) is None |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.anyio |
| 145 | +async def test_validate_request_protection_disabled_allows_any_host() -> None: |
| 146 | + m = TransportSecurityMiddleware(TransportSecuritySettings(enable_dns_rebinding_protection=False)) |
| 147 | + request = make_request({"host": "attacker.example.com"}) |
| 148 | + assert await m.validate_request(request) is None |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.anyio |
| 152 | +async def test_validate_request_valid_host_and_no_origin() -> None: |
| 153 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["example.com"])) |
| 154 | + request = make_request({"host": "example.com"}) |
| 155 | + assert await m.validate_request(request) is None |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.anyio |
| 159 | +async def test_validate_request_invalid_host_returns_421_with_detail() -> None: |
| 160 | + m = TransportSecurityMiddleware(TransportSecuritySettings(allowed_hosts=["example.com"])) |
| 161 | + request = make_request({"host": "attacker.com"}) |
| 162 | + response = await m.validate_request(request) |
| 163 | + assert response is not None |
| 164 | + assert response.status_code == 421 |
| 165 | + assert b"attacker.com" in response.body |
| 166 | + assert b"allowed_hosts" in response.body |
| 167 | + |
| 168 | + |
| 169 | +@pytest.mark.anyio |
| 170 | +async def test_validate_request_invalid_origin_returns_403_with_detail() -> None: |
| 171 | + m = TransportSecurityMiddleware( |
| 172 | + TransportSecuritySettings( |
| 173 | + allowed_hosts=["example.com"], |
| 174 | + allowed_origins=["http://example.com"], |
| 175 | + ) |
| 176 | + ) |
| 177 | + request = make_request({"host": "example.com", "origin": "http://attacker.com"}) |
| 178 | + response = await m.validate_request(request) |
| 179 | + assert response is not None |
| 180 | + assert response.status_code == 403 |
| 181 | + assert b"attacker.com" in response.body |
| 182 | + assert b"allowed_origins" in response.body |
0 commit comments