security package
SQL injection pattern detection with severity classification.
Import path: github.com/ajitpratap0/GoSQLX/pkg/sql/security (pkg.go.dev)
Part of the GoSQLX API reference.
The security package provides SQL injection pattern detection and security scanning.
Types
Scanner
type Scanner struct {
MinSeverity Severity // Filter findings by minimum severity
}Severity
type Severity string
const (
SeverityLow Severity = "LOW"
SeverityMedium Severity = "MEDIUM"
SeverityHigh Severity = "HIGH"
SeverityCritical Severity = "CRITICAL"
)PatternType
type PatternType string
const (
PatternInputTooLarge PatternType = "INPUT_TOO_LARGE"
PatternUnionInjection PatternType = "UNION_INJECTION"
PatternUnionGeneric PatternType = "UNION_GENERIC"
PatternTautology PatternType = "TAUTOLOGY"
PatternComment PatternType = "COMMENT_BYPASS"
PatternStackedQuery PatternType = "STACKED_QUERY"
PatternUnionBased PatternType = "UNION_BASED"
PatternTimeBased PatternType = "TIME_BASED"
PatternBooleanBased PatternType = "BOOLEAN_BASED"
PatternOutOfBand PatternType = "OUT_OF_BAND"
PatternDangerousFunc PatternType = "DANGEROUS_FUNCTION"
)Finding
type Finding struct {
Severity Severity // Severity level
Pattern PatternType // Pattern type detected
Description string // Description of the finding
Risk string // Risk explanation
Line int // Line number (if available)
Column int // Column number (if available)
SQL string // SQL snippet (if available)
Suggestion string // Fix suggestion
}ScanResult
type ScanResult struct {
Findings []Finding // All findings
TotalCount int // Total findings
CriticalCount int // Critical findings
HighCount int // High severity findings
MediumCount int // Medium severity findings
LowCount int // Low severity findings
}Functions
NewScanner() *Scanner
Create security scanner with default settings (minimum severity: LOW).
scanner := security.NewScanner()NewScannerWithSeverity(minSeverity Severity) (*Scanner, error)
Create scanner with custom minimum severity filter.
scanner, err := security.NewScannerWithSeverity(security.SeverityHigh)
// Only returns HIGH and CRITICAL findings(s *Scanner) Scan(ast *ast.AST) *ScanResult
Scan the full parsed AST, including nested queries, CTEs, expressions, joins,
DML conditions and table functions. Statements retained as raw text (such as
utility statements and unsupported statements) are checked through ScanSQL.
A nil AST returns an empty result.
scanner := security.NewScanner()
tree, err := gosqlx.Parse(sql)
if err != nil {
log.Fatal(err)
}
result := scanner.Scan(tree)
for _, finding := range result.Findings {
fmt.Printf("[%s] %s: %s\n", finding.Severity, finding.Pattern, finding.Description)
}(s *Scanner) ScanSQL(sql string) *ScanResult
Scan the complete raw SQL input for patterns, including comments and statements that may not be represented as expression nodes in the AST. Matching combines the raw view with comment-normalised views that account for quoted strings and identifiers, backslash escape modes, nested block comments, MySQL/MariaDB comments and executable comments. This method has no dialect parameter and considers multiple lexical interpretations.
Recognised dollar-quoted string bodies ($$...$$ and $tag$...$tag$) are
excluded from matching when their openers occur outside strings, identifiers
and comments. This reduces findings on literal content; use the AST scan as
well when dialect-specific structure matters.
Inputs larger than MaxScanInputSize (10 MiB, measured in bytes) are not scanned.
They produce one PatternInputTooLarge finding with SeverityCritical, even
when a minimum-severity filter is configured. IsClean() is false and
HasCritical() is true for this result. Inputs within the limit are scanned
in full, without prefix truncation.
scanner := security.NewScanner()
result := scanner.ScanSQL("SELECT * FROM users WHERE id = '" + userInput + "'")(r *ScanResult) HasCritical() bool
Returns true if any critical findings exist.
(r *ScanResult) HasHighOrAbove() bool
Returns true if any high or critical findings exist.
(r *ScanResult) IsClean() bool
Returns true if no findings exist.
Detection Patterns
The scanner reports the following pattern categories:
- Tautologies - Always-true conditions (e.g.,
1=1,'a'='a') - Comment Bypasses - SQL comment-based injection (
--,/**/,#) - UNION-Based Injection - Data extraction via UNION SELECT
- Stacked Queries - Destructive statements after semicolon (
;DROP,;DELETE) - Time-Based Blind - Time delay functions (SLEEP, WAITFOR DELAY, pg_sleep, BENCHMARK)
- Out-of-Band - Data exfiltration (xp_cmdshell, LOAD_FILE, UTL_HTTP)
- Dangerous Functions - Dynamic SQL execution (EXEC, sp_executesql, PREPARE FROM)
- Boolean-Based - The exported
PatternBooleanBasedcategory; the current scanner does not emit it - UNION Injection Fingerprints (
PatternUnionInjection) - Raw-SQL UNION patterns involving system tables or NULL padding; CRITICAL severity - Generic UNION (
PatternUnionGeneric) - Raw-SQL UNION SELECT; HIGH severity and may be legitimate SQL requiring review - Input Too Large (
PatternInputTooLarge) - Input exceeded the scan limit and was not inspected
Example:
scanner := security.NewScanner()
// Detect tautology
sql := "SELECT * FROM users WHERE username = 'admin' OR 1=1 --"
result := scanner.ScanSQL(sql)
// Finding: TAUTOLOGY, Severity: CRITICAL
// Detect UNION injection
sql = "SELECT * FROM products WHERE id = 1 UNION SELECT NULL, NULL, NULL"
result = scanner.ScanSQL(sql)
// Findings include UNION_INJECTION (CRITICAL) and UNION_GENERIC (HIGH)
// Detect time-based injection
sql = "SELECT * FROM orders WHERE id = 1; SELECT SLEEP(5)"
result = scanner.ScanSQL(sql)
// Finding: TIME_BASED, Severity: HIGH
// Clean SQL
sql = "SELECT * FROM users WHERE id = $1"
result = scanner.ScanSQL(sql)
// result.IsClean() == truePerformance:
- Pre-compiled regex patterns for performance
- Thread-safe pattern compilation (sync.Once)
- Precise system table matching (avoids false positives)
Findings are heuristics for review, not proof that SQL is safe or malicious. Use parameterised queries for untrusted values.