|
6 | 6 | import pytest |
7 | 7 | from unittest.mock import patch, MagicMock |
8 | 8 |
|
9 | | -from adx_mcp_server.server import config |
| 9 | +from adx_mcp_server.server import config, validate_table_name, validate_sample_size |
10 | 10 |
|
11 | 11 |
|
12 | 12 | class TestListTablesTool: |
@@ -352,3 +352,172 @@ async def test_get_table_details_error(self): |
352 | 352 | finally: |
353 | 353 | config.cluster_url = original_url |
354 | 354 | config.database = original_db |
| 355 | + |
| 356 | + |
| 357 | +class TestValidateTableName: |
| 358 | + """Tests for table name validation to prevent KQL injection.""" |
| 359 | + |
| 360 | + def test_simple_table_name(self): |
| 361 | + assert validate_table_name("my_table") == "my_table" |
| 362 | + |
| 363 | + def test_qualified_table_name(self): |
| 364 | + assert validate_table_name("database.table") == "database.table" |
| 365 | + |
| 366 | + def test_multi_qualified_name(self): |
| 367 | + assert validate_table_name("db.schema.table") == "db.schema.table" |
| 368 | + |
| 369 | + def test_underscore_prefix(self): |
| 370 | + assert validate_table_name("_private_table") == "_private_table" |
| 371 | + |
| 372 | + def test_alphanumeric(self): |
| 373 | + assert validate_table_name("table123") == "table123" |
| 374 | + |
| 375 | + def test_strips_whitespace(self): |
| 376 | + assert validate_table_name(" my_table ") == "my_table" |
| 377 | + |
| 378 | + def test_pipe_injection(self): |
| 379 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 380 | + validate_table_name("sensitive_data | project Secret | take 100 //") |
| 381 | + |
| 382 | + def test_newline_injection(self): |
| 383 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 384 | + validate_table_name("users\n.drop table critical_data") |
| 385 | + |
| 386 | + def test_semicolon_injection(self): |
| 387 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 388 | + validate_table_name("table; .drop table other") |
| 389 | + |
| 390 | + def test_bracket_notation(self): |
| 391 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 392 | + validate_table_name("['injected query']") |
| 393 | + |
| 394 | + def test_space_in_name(self): |
| 395 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 396 | + validate_table_name("table name with spaces") |
| 397 | + |
| 398 | + def test_empty_string(self): |
| 399 | + with pytest.raises(ValueError, match="cannot be empty"): |
| 400 | + validate_table_name("") |
| 401 | + |
| 402 | + def test_whitespace_only(self): |
| 403 | + with pytest.raises(ValueError, match="cannot be empty"): |
| 404 | + validate_table_name(" ") |
| 405 | + |
| 406 | + def test_starts_with_digit(self): |
| 407 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 408 | + validate_table_name("123table") |
| 409 | + |
| 410 | + def test_hyphen_in_name(self): |
| 411 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 412 | + validate_table_name("my-table") |
| 413 | + |
| 414 | + def test_slash_comment_injection(self): |
| 415 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 416 | + validate_table_name("table // comment") |
| 417 | + |
| 418 | + def test_trailing_dot(self): |
| 419 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 420 | + validate_table_name("database.") |
| 421 | + |
| 422 | + def test_leading_dot(self): |
| 423 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 424 | + validate_table_name(".table") |
| 425 | + |
| 426 | + |
| 427 | +class TestValidateSampleSize: |
| 428 | + """Tests for sample_size validation.""" |
| 429 | + |
| 430 | + def test_valid_sample_size(self): |
| 431 | + assert validate_sample_size(10) == 10 |
| 432 | + |
| 433 | + def test_zero(self): |
| 434 | + with pytest.raises(ValueError, match="sample_size must be a positive integer"): |
| 435 | + validate_sample_size(0) |
| 436 | + |
| 437 | + def test_negative(self): |
| 438 | + with pytest.raises(ValueError, match="sample_size must be a positive integer"): |
| 439 | + validate_sample_size(-5) |
| 440 | + |
| 441 | + |
| 442 | +class TestTableNameInjectionPrevention: |
| 443 | + """Integration tests proving tool handlers reject KQL injection payloads.""" |
| 444 | + |
| 445 | + @pytest.mark.asyncio |
| 446 | + async def test_get_table_schema_rejects_injection(self): |
| 447 | + original_url = config.cluster_url |
| 448 | + original_db = config.database |
| 449 | + config.cluster_url = "https://test.kusto.windows.net" |
| 450 | + config.database = "testdb" |
| 451 | + |
| 452 | + try: |
| 453 | + from adx_mcp_server import server |
| 454 | + fn = server.get_table_schema |
| 455 | + |
| 456 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 457 | + if hasattr(fn, 'fn'): |
| 458 | + await fn.fn("sensitive_data | project Secret | take 100 //") |
| 459 | + else: |
| 460 | + await fn("sensitive_data | project Secret | take 100 //") |
| 461 | + finally: |
| 462 | + config.cluster_url = original_url |
| 463 | + config.database = original_db |
| 464 | + |
| 465 | + @pytest.mark.asyncio |
| 466 | + async def test_sample_table_data_rejects_injection(self): |
| 467 | + original_url = config.cluster_url |
| 468 | + original_db = config.database |
| 469 | + config.cluster_url = "https://test.kusto.windows.net" |
| 470 | + config.database = "testdb" |
| 471 | + |
| 472 | + try: |
| 473 | + from adx_mcp_server import server |
| 474 | + fn = server.sample_table_data |
| 475 | + |
| 476 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 477 | + if hasattr(fn, 'fn'): |
| 478 | + await fn.fn("data | take 100 //", 10) |
| 479 | + else: |
| 480 | + await fn("data | take 100 //", 10) |
| 481 | + finally: |
| 482 | + config.cluster_url = original_url |
| 483 | + config.database = original_db |
| 484 | + |
| 485 | + @pytest.mark.asyncio |
| 486 | + async def test_sample_table_data_rejects_invalid_sample_size(self): |
| 487 | + original_url = config.cluster_url |
| 488 | + original_db = config.database |
| 489 | + config.cluster_url = "https://test.kusto.windows.net" |
| 490 | + config.database = "testdb" |
| 491 | + |
| 492 | + try: |
| 493 | + from adx_mcp_server import server |
| 494 | + fn = server.sample_table_data |
| 495 | + |
| 496 | + with pytest.raises(ValueError, match="sample_size must be a positive integer"): |
| 497 | + if hasattr(fn, 'fn'): |
| 498 | + await fn.fn("valid_table", -1) |
| 499 | + else: |
| 500 | + await fn("valid_table", -1) |
| 501 | + finally: |
| 502 | + config.cluster_url = original_url |
| 503 | + config.database = original_db |
| 504 | + |
| 505 | + @pytest.mark.asyncio |
| 506 | + async def test_get_table_details_rejects_injection(self): |
| 507 | + original_url = config.cluster_url |
| 508 | + original_db = config.database |
| 509 | + config.cluster_url = "https://test.kusto.windows.net" |
| 510 | + config.database = "testdb" |
| 511 | + |
| 512 | + try: |
| 513 | + from adx_mcp_server import server |
| 514 | + fn = server.get_table_details |
| 515 | + |
| 516 | + with pytest.raises(ValueError, match="Invalid table name"): |
| 517 | + if hasattr(fn, 'fn'): |
| 518 | + await fn.fn("users details\n.drop table critical_data") |
| 519 | + else: |
| 520 | + await fn("users details\n.drop table critical_data") |
| 521 | + finally: |
| 522 | + config.cluster_url = original_url |
| 523 | + config.database = original_db |
0 commit comments