|
5 | 5 |
|
6 | 6 | import yaml |
7 | 7 |
|
8 | | -from spark_history_mcp.config.config import AuthConfig, Config, ServerConfig |
| 8 | +from spark_history_mcp.config.config import ( |
| 9 | + AuthConfig, |
| 10 | + Config, |
| 11 | + ServerConfig, |
| 12 | + TransportSecurityConfig, |
| 13 | +) |
9 | 14 |
|
10 | 15 |
|
11 | 16 | class TestConfig(unittest.TestCase): |
@@ -185,3 +190,239 @@ def test_model_serialization(self): |
185 | 190 | # Test with explicit exclude |
186 | 191 | server_dict = server.model_dump(exclude={"auth"}) |
187 | 192 | self.assertNotIn("auth", server_dict) |
| 193 | + |
| 194 | + |
| 195 | +class TestTransportSecurityConfig(unittest.TestCase): |
| 196 | + """Test cases for TransportSecurityConfig. |
| 197 | +
|
| 198 | + See: https://github.com/modelcontextprotocol/python-sdk/issues/1798 |
| 199 | + """ |
| 200 | + |
| 201 | + def test_transport_security_default_values(self): |
| 202 | + """Test that transport security defaults are set correctly.""" |
| 203 | + ts_config = TransportSecurityConfig() |
| 204 | + |
| 205 | + # Default should be disabled for backwards compatibility |
| 206 | + self.assertFalse(ts_config.enable_dns_rebinding_protection) |
| 207 | + self.assertEqual(ts_config.allowed_hosts, []) |
| 208 | + self.assertEqual(ts_config.allowed_origins, []) |
| 209 | + |
| 210 | + def test_transport_security_from_yaml(self): |
| 211 | + """Test loading transport security from YAML config.""" |
| 212 | + config_data = { |
| 213 | + "servers": {"local": {"url": "http://localhost:18080", "default": True}}, |
| 214 | + "mcp": { |
| 215 | + "transports": ["streamable-http"], |
| 216 | + "port": "18888", |
| 217 | + "transport_security": { |
| 218 | + "enable_dns_rebinding_protection": True, |
| 219 | + "allowed_hosts": ["localhost:*", "127.0.0.1:*", "my-gateway:*"], |
| 220 | + "allowed_origins": ["http://localhost:*", "http://127.0.0.1:*"], |
| 221 | + }, |
| 222 | + }, |
| 223 | + } |
| 224 | + |
| 225 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 226 | + yaml.dump(config_data, temp_file) |
| 227 | + temp_file_path = temp_file.name |
| 228 | + |
| 229 | + try: |
| 230 | + with patch.dict(os.environ, {"SHS_MCP_CONFIG": temp_file_path}): |
| 231 | + config = Config() |
| 232 | + |
| 233 | + # Verify transport security config |
| 234 | + ts = config.mcp.transport_security |
| 235 | + self.assertIsNotNone(ts) |
| 236 | + self.assertTrue(ts.enable_dns_rebinding_protection) |
| 237 | + self.assertEqual( |
| 238 | + ts.allowed_hosts, ["localhost:*", "127.0.0.1:*", "my-gateway:*"] |
| 239 | + ) |
| 240 | + self.assertEqual( |
| 241 | + ts.allowed_origins, ["http://localhost:*", "http://127.0.0.1:*"] |
| 242 | + ) |
| 243 | + finally: |
| 244 | + os.unlink(temp_file_path) |
| 245 | + |
| 246 | + def test_transport_security_disabled_in_yaml(self): |
| 247 | + """Test explicitly disabling transport security in YAML.""" |
| 248 | + config_data = { |
| 249 | + "servers": {"local": {"url": "http://localhost:18080", "default": True}}, |
| 250 | + "mcp": { |
| 251 | + "transports": ["streamable-http"], |
| 252 | + "transport_security": { |
| 253 | + "enable_dns_rebinding_protection": False, |
| 254 | + }, |
| 255 | + }, |
| 256 | + } |
| 257 | + |
| 258 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 259 | + yaml.dump(config_data, temp_file) |
| 260 | + temp_file_path = temp_file.name |
| 261 | + |
| 262 | + try: |
| 263 | + with patch.dict(os.environ, {"SHS_MCP_CONFIG": temp_file_path}): |
| 264 | + config = Config() |
| 265 | + |
| 266 | + ts = config.mcp.transport_security |
| 267 | + self.assertIsNotNone(ts) |
| 268 | + self.assertFalse(ts.enable_dns_rebinding_protection) |
| 269 | + finally: |
| 270 | + os.unlink(temp_file_path) |
| 271 | + |
| 272 | + def test_transport_security_default_when_not_specified(self): |
| 273 | + """Test transport security defaults when not specified in config.""" |
| 274 | + config_data = { |
| 275 | + "servers": {"local": {"url": "http://localhost:18080", "default": True}}, |
| 276 | + "mcp": {"transports": ["streamable-http"]}, |
| 277 | + } |
| 278 | + |
| 279 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 280 | + yaml.dump(config_data, temp_file) |
| 281 | + temp_file_path = temp_file.name |
| 282 | + |
| 283 | + try: |
| 284 | + with patch.dict(os.environ, {"SHS_MCP_CONFIG": temp_file_path}): |
| 285 | + config = Config() |
| 286 | + |
| 287 | + # Transport security should have default values |
| 288 | + ts = config.mcp.transport_security |
| 289 | + self.assertIsNotNone(ts) |
| 290 | + self.assertFalse(ts.enable_dns_rebinding_protection) |
| 291 | + self.assertEqual(ts.allowed_hosts, []) |
| 292 | + self.assertEqual(ts.allowed_origins, []) |
| 293 | + finally: |
| 294 | + os.unlink(temp_file_path) |
| 295 | + |
| 296 | + def test_transport_security_integration_with_mcp_library(self): |
| 297 | + """Test that transport security config integrates with MCP library.""" |
| 298 | + from mcp.server.transport_security import TransportSecuritySettings |
| 299 | + |
| 300 | + # Create config with transport security enabled |
| 301 | + ts_config = TransportSecurityConfig( |
| 302 | + enable_dns_rebinding_protection=True, |
| 303 | + allowed_hosts=["localhost:*", "127.0.0.1:*"], |
| 304 | + allowed_origins=["http://localhost:*"], |
| 305 | + ) |
| 306 | + |
| 307 | + # Convert to MCP library's TransportSecuritySettings |
| 308 | + ts_settings = TransportSecuritySettings( |
| 309 | + enable_dns_rebinding_protection=ts_config.enable_dns_rebinding_protection, |
| 310 | + allowed_hosts=ts_config.allowed_hosts, |
| 311 | + allowed_origins=ts_config.allowed_origins, |
| 312 | + ) |
| 313 | + |
| 314 | + # Verify the settings are correctly transferred |
| 315 | + self.assertTrue(ts_settings.enable_dns_rebinding_protection) |
| 316 | + self.assertEqual(ts_settings.allowed_hosts, ["localhost:*", "127.0.0.1:*"]) |
| 317 | + self.assertEqual(ts_settings.allowed_origins, ["http://localhost:*"]) |
| 318 | + |
| 319 | + def test_transport_security_partial_config(self): |
| 320 | + """Test transport security with partial configuration.""" |
| 321 | + config_data = { |
| 322 | + "servers": {"local": {"url": "http://localhost:18080", "default": True}}, |
| 323 | + "mcp": { |
| 324 | + "transports": ["streamable-http"], |
| 325 | + "transport_security": { |
| 326 | + "enable_dns_rebinding_protection": True, |
| 327 | + # Only specifying allowed_hosts, not allowed_origins |
| 328 | + "allowed_hosts": ["localhost:*"], |
| 329 | + }, |
| 330 | + }, |
| 331 | + } |
| 332 | + |
| 333 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 334 | + yaml.dump(config_data, temp_file) |
| 335 | + temp_file_path = temp_file.name |
| 336 | + |
| 337 | + try: |
| 338 | + with patch.dict(os.environ, {"SHS_MCP_CONFIG": temp_file_path}): |
| 339 | + config = Config() |
| 340 | + |
| 341 | + ts = config.mcp.transport_security |
| 342 | + self.assertTrue(ts.enable_dns_rebinding_protection) |
| 343 | + self.assertEqual(ts.allowed_hosts, ["localhost:*"]) |
| 344 | + # allowed_origins should default to empty list |
| 345 | + self.assertEqual(ts.allowed_origins, []) |
| 346 | + finally: |
| 347 | + os.unlink(temp_file_path) |
| 348 | + |
| 349 | + def test_transport_security_wildcard_patterns(self): |
| 350 | + """Test various wildcard patterns for hosts and origins.""" |
| 351 | + ts_config = TransportSecurityConfig( |
| 352 | + enable_dns_rebinding_protection=True, |
| 353 | + allowed_hosts=[ |
| 354 | + "localhost:*", |
| 355 | + "127.0.0.1:*", |
| 356 | + "192.168.1.100:*", |
| 357 | + "my-gateway.example.com:*", |
| 358 | + "internal-service:8080", # Specific port |
| 359 | + ], |
| 360 | + allowed_origins=[ |
| 361 | + "http://localhost:*", |
| 362 | + "https://localhost:*", |
| 363 | + "http://127.0.0.1:*", |
| 364 | + "https://my-gateway.example.com:*", |
| 365 | + "http://internal-service:8080", # Specific port |
| 366 | + ], |
| 367 | + ) |
| 368 | + |
| 369 | + # Verify all patterns are stored correctly |
| 370 | + self.assertEqual(len(ts_config.allowed_hosts), 5) |
| 371 | + self.assertEqual(len(ts_config.allowed_origins), 5) |
| 372 | + self.assertIn("localhost:*", ts_config.allowed_hosts) |
| 373 | + self.assertIn("internal-service:8080", ts_config.allowed_hosts) |
| 374 | + self.assertIn("http://localhost:*", ts_config.allowed_origins) |
| 375 | + self.assertIn("https://localhost:*", ts_config.allowed_origins) |
| 376 | + |
| 377 | + |
| 378 | +class TestAppTransportSecurityIntegration(unittest.TestCase): |
| 379 | + """Test app.py integration with transport security settings.""" |
| 380 | + |
| 381 | + def test_app_run_configures_transport_security(self): |
| 382 | + """Test that app.run() correctly configures transport security.""" |
| 383 | + from mcp.server.transport_security import TransportSecuritySettings |
| 384 | + |
| 385 | + from spark_history_mcp.core.app import mcp |
| 386 | + |
| 387 | + config_data = { |
| 388 | + "servers": {"local": {"url": "http://localhost:18080", "default": True}}, |
| 389 | + "mcp": { |
| 390 | + "transports": ["streamable-http"], |
| 391 | + "port": "18888", |
| 392 | + "address": "localhost", |
| 393 | + "debug": False, |
| 394 | + "transport_security": { |
| 395 | + "enable_dns_rebinding_protection": True, |
| 396 | + "allowed_hosts": ["localhost:*", "test-gateway:*"], |
| 397 | + "allowed_origins": ["http://localhost:*"], |
| 398 | + }, |
| 399 | + }, |
| 400 | + } |
| 401 | + |
| 402 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 403 | + yaml.dump(config_data, temp_file) |
| 404 | + temp_file_path = temp_file.name |
| 405 | + |
| 406 | + try: |
| 407 | + with patch.dict(os.environ, {"SHS_MCP_CONFIG": temp_file_path}): |
| 408 | + config = Config() |
| 409 | + |
| 410 | + # Manually apply the transport security settings as run() would |
| 411 | + if config.mcp.transport_security: |
| 412 | + ts_config = config.mcp.transport_security |
| 413 | + mcp.settings.transport_security = TransportSecuritySettings( |
| 414 | + enable_dns_rebinding_protection=ts_config.enable_dns_rebinding_protection, |
| 415 | + allowed_hosts=ts_config.allowed_hosts, |
| 416 | + allowed_origins=ts_config.allowed_origins, |
| 417 | + ) |
| 418 | + |
| 419 | + # Verify settings were applied |
| 420 | + ts = mcp.settings.transport_security |
| 421 | + self.assertIsNotNone(ts) |
| 422 | + self.assertTrue(ts.enable_dns_rebinding_protection) |
| 423 | + self.assertEqual(ts.allowed_hosts, ["localhost:*", "test-gateway:*"]) |
| 424 | + self.assertEqual(ts.allowed_origins, ["http://localhost:*"]) |
| 425 | + finally: |
| 426 | + os.unlink(temp_file_path) |
| 427 | + # Reset to None to avoid affecting other tests |
| 428 | + mcp.settings.transport_security = None |
0 commit comments