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