Test UNION-based injection detection (Issue #170)
(t *testing.T)
| 666 | |
| 667 | // Test UNION-based injection detection (Issue #170) |
| 668 | func TestScanSQL_UnionInjection(t *testing.T) { |
| 669 | scanner := NewScanner() |
| 670 | |
| 671 | testCases := []struct { |
| 672 | sql string |
| 673 | shouldDetect bool |
| 674 | description string |
| 675 | }{ |
| 676 | { |
| 677 | sql: "SELECT * FROM users WHERE id = 1 UNION SELECT password FROM admins", |
| 678 | shouldDetect: true, |
| 679 | description: "Basic UNION SELECT injection", |
| 680 | }, |
| 681 | { |
| 682 | sql: "SELECT id FROM users UNION ALL SELECT username FROM admins", |
| 683 | shouldDetect: true, |
| 684 | description: "UNION ALL SELECT injection", |
| 685 | }, |
| 686 | { |
| 687 | sql: "SELECT * FROM users WHERE id = 1 UNION SELECT table_name FROM information_schema.tables", |
| 688 | shouldDetect: true, |
| 689 | description: "UNION with information_schema access", |
| 690 | }, |
| 691 | { |
| 692 | sql: "SELECT * FROM active_users UNION SELECT * FROM archived_users", |
| 693 | shouldDetect: true, |
| 694 | description: "Legitimate UNION (will be flagged but needs review)", |
| 695 | }, |
| 696 | } |
| 697 | |
| 698 | for _, tc := range testCases { |
| 699 | t.Run(tc.description, func(t *testing.T) { |
| 700 | result := scanner.ScanSQL(tc.sql) |
| 701 | hasUnionPattern := false |
| 702 | for _, f := range result.Findings { |
| 703 | // ScanSQL now emits PatternUnionInjection (CRITICAL) or PatternUnionGeneric (HIGH) |
| 704 | if f.Pattern == PatternUnionInjection || f.Pattern == PatternUnionGeneric { |
| 705 | hasUnionPattern = true |
| 706 | break |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | if tc.shouldDetect && !hasUnionPattern { |
| 711 | t.Errorf("expected UNION pattern in: %s", tc.description) |
| 712 | } |
| 713 | }) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // Test stacked query injection detection (Issue #170) |
| 718 | func TestScanSQL_StackedQueryInjection(t *testing.T) { |
nothing calls this directly
no test coverage detected