MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestScanSQL_UnionInjection

Function TestScanSQL_UnionInjection

pkg/sql/security/scanner_test.go:668–715  ·  view source on GitHub ↗

Test UNION-based injection detection (Issue #170)

(t *testing.T)

Source from the content-addressed store, hash-verified

666
667// Test UNION-based injection detection (Issue #170)
668func 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)
718func TestScanSQL_StackedQueryInjection(t *testing.T) {

Callers

nothing calls this directly

Calls 4

ScanSQLMethod · 0.95
NewScannerFunction · 0.85
RunMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected