refactor: Centralize and simplify SQL validation logic

This commit is contained in:
begoniezhao
2026-01-21 17:35:46 +08:00
parent 03987b99de
commit fe6f84b67b
4 changed files with 1756 additions and 535 deletions
+15
View File
@@ -117,6 +117,21 @@ func (t *DataAnalysisTool) Execute(ctx context.Context, args json.RawMessage) (*
}, fmt.Errorf("modification queries are not allowed")
}
// Validate SQL with comprehensive security checks
// IMPORTANT: Must enable validateSelectStmt to block RangeFunction attacks
_, validation := utils.ValidateSQL(input.Sql,
utils.WithAllowedTables(schema.TableName),
utils.WithSingleStatement(), // Block multiple statements
utils.WithNoDangerousFunctions(), // Block dangerous functions
)
if !validation.Valid {
logger.Warnf(ctx, "[Tool][DataAnalysis] SQL validation failed for session %s: %v", t.sessionID, validation.Errors)
return &types.ToolResult{
Success: false,
Error: fmt.Sprintf("SQL validation failed: %v", validation.Errors),
}, fmt.Errorf("SQL validation failed: %v", validation.Errors)
}
logger.Infof(ctx, "[Tool][DataAnalysis] Received SQL query for session %s: %s", t.sessionID, input.Sql)
// Execute single query and get results
results, err := t.executeSingleQuery(ctx, input.Sql)
+13 -535
View File
@@ -4,13 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/utils"
pg_query "github.com/pganalyze/pg_query_go/v6"
"gorm.io/gorm"
)
@@ -101,80 +99,6 @@ type DatabaseQueryInput struct {
SQL string `json:"sql" jsonschema:"The SELECT SQL query to execute. DO NOT include tenant_id condition - it will be automatically added for security."`
}
// SQLSecurityValidator provides comprehensive SQL injection protection using PostgreSQL's official parser
type SQLSecurityValidator struct {
allowedTables map[string]bool
allowedFunctions map[string]bool
tenantID uint64
}
// NewSQLSecurityValidator creates a new SQL security validator
func NewSQLSecurityValidator(tenantID uint64) *SQLSecurityValidator {
return &SQLSecurityValidator{
allowedTables: map[string]bool{
"tenants": true,
"knowledge_bases": true,
"knowledges": true,
"sessions": true,
"messages": true,
"chunks": true,
"embeddings": true,
"models": true,
},
// Whitelist of allowed SQL functions (aggregates and safe functions only)
allowedFunctions: map[string]bool{
// Aggregate functions
"count": true,
"sum": true,
"avg": true,
"min": true,
"max": true,
"array_agg": true,
"string_agg": true,
"bool_and": true,
"bool_or": true,
"json_agg": true,
"jsonb_agg": true,
"json_object_agg": true,
"jsonb_object_agg": true,
// Safe scalar functions
"coalesce": true,
"nullif": true,
"greatest": true,
"least": true,
"abs": true,
"ceil": true,
"floor": true,
"round": true,
"length": true,
"lower": true,
"upper": true,
"trim": true,
"ltrim": true,
"rtrim": true,
"substring": true,
"concat": true,
"concat_ws": true,
"replace": true,
"left": true,
"right": true,
"now": true,
"current_date": true,
"current_timestamp": true,
"date_trunc": true,
"extract": true,
"to_char": true,
"to_date": true,
"to_timestamp": true,
"date_part": true,
"age": true,
},
tenantID: tenantID,
}
}
// DatabaseQueryInput defines the input parameters for database query tool
// DatabaseQueryTool allows AI to query the database with auto-injected tenant_id for security
type DatabaseQueryTool struct {
BaseTool
@@ -329,474 +253,28 @@ func (t *DatabaseQueryTool) Execute(ctx context.Context, args json.RawMessage) (
// validateAndSecureSQL validates the SQL query and injects tenant_id conditions
func (t *DatabaseQueryTool) validateAndSecureSQL(sqlQuery string, tenantID uint64) (string, error) {
validator := NewSQLSecurityValidator(tenantID)
return validator.ValidateAndSecure(sqlQuery)
}
// ValidateAndSecure performs comprehensive SQL validation using PostgreSQL's official parser
func (v *SQLSecurityValidator) ValidateAndSecure(sqlQuery string) (string, error) {
// Phase 1: Basic input validation
if err := v.validateInput(sqlQuery); err != nil {
return "", err
}
// Phase 2: Parse SQL using PostgreSQL's official parser
result, err := pg_query.Parse(sqlQuery)
if err != nil {
return "", fmt.Errorf("SQL parse error: %v", err)
}
// Phase 3: Validate that we have exactly one statement
if len(result.Stmts) == 0 {
return "", fmt.Errorf("empty query")
}
if len(result.Stmts) > 1 {
return "", fmt.Errorf("multiple statements are not allowed")
}
stmt := result.Stmts[0].Stmt
// Phase 4: Ensure it's a SELECT statement
selectStmt := stmt.GetSelectStmt()
if selectStmt == nil {
return "", fmt.Errorf("only SELECT queries are allowed")
}
// Phase 5: Validate the SELECT statement recursively
tablesInQuery, err := v.validateSelectStmt(selectStmt)
// Use the new ValidateAndSecureSQL with comprehensive security options
securedSQL, validationResult, err := utils.ValidateAndSecureSQL(
sqlQuery,
utils.WithSecurityDefaults(tenantID),
utils.WithInjectionRiskCheck(),
)
if err != nil {
return "", err
}
// Phase 6: Normalize SQL (removes comments, standardizes format)
normalizedSQL, err := pg_query.Deparse(result)
if err != nil {
return "", fmt.Errorf("failed to normalize SQL: %v", err)
if !validationResult.Valid {
// Build error message from validation errors
var errMsgs []string
for _, valErr := range validationResult.Errors {
errMsgs = append(errMsgs, fmt.Sprintf("%s: %s", valErr.Type, valErr.Message))
}
return "", fmt.Errorf("validation failed: %s", strings.Join(errMsgs, "; "))
}
// Phase 7: Inject tenant_id conditions
securedSQL := v.injectTenantConditions(normalizedSQL, tablesInQuery)
return securedSQL, nil
}
// validateInput performs basic input validation
func (v *SQLSecurityValidator) validateInput(sql string) error {
// Check for null bytes
if strings.Contains(sql, "\x00") {
return fmt.Errorf("invalid character in SQL query")
}
// Check length limits
if len(sql) < 6 {
return fmt.Errorf("SQL query too short")
}
if len(sql) > 4096 {
return fmt.Errorf("SQL query too long (max 4096 characters)")
}
return nil
}
// validateSelectStmt validates a SELECT statement and extracts table information
func (v *SQLSecurityValidator) validateSelectStmt(stmt *pg_query.SelectStmt) (map[string]string, error) {
tablesInQuery := make(map[string]string) // table name -> alias
// Check for UNION/INTERSECT/EXCEPT (compound queries)
if stmt.Op != pg_query.SetOperation_SETOP_NONE {
return nil, fmt.Errorf("compound queries (UNION/INTERSECT/EXCEPT) are not allowed")
}
// Check for WITH clause (CTEs) - could be used for complex attacks
if stmt.WithClause != nil {
return nil, fmt.Errorf("WITH clause (CTEs) is not allowed")
}
// Check for INTO clause (SELECT INTO)
if stmt.IntoClause != nil {
return nil, fmt.Errorf("SELECT INTO is not allowed")
}
// Check for LOCKING clause (FOR UPDATE, etc.)
if len(stmt.LockingClause) > 0 {
return nil, fmt.Errorf("locking clauses (FOR UPDATE, etc.) are not allowed")
}
// Validate FROM clause
for _, fromItem := range stmt.FromClause {
if err := v.validateFromItem(fromItem, tablesInQuery); err != nil {
return nil, err
}
}
// Validate target list (SELECT columns)
for _, target := range stmt.TargetList {
if err := v.validateNode(target); err != nil {
return nil, err
}
}
// Validate WHERE clause
if stmt.WhereClause != nil {
if err := v.validateNode(stmt.WhereClause); err != nil {
return nil, err
}
}
// Validate GROUP BY clause
for _, groupBy := range stmt.GroupClause {
if err := v.validateNode(groupBy); err != nil {
return nil, err
}
}
// Validate HAVING clause
if stmt.HavingClause != nil {
if err := v.validateNode(stmt.HavingClause); err != nil {
return nil, err
}
}
// Validate ORDER BY clause
for _, sortBy := range stmt.SortClause {
if err := v.validateNode(sortBy); err != nil {
return nil, err
}
}
// Ensure at least one valid table is referenced
if len(tablesInQuery) == 0 {
return nil, fmt.Errorf("no valid table found in query")
}
return tablesInQuery, nil
}
// validateFromItem validates a FROM clause item
func (v *SQLSecurityValidator) validateFromItem(node *pg_query.Node, tables map[string]string) error {
if node == nil {
return nil
}
// Handle RangeVar (simple table reference)
if rv := node.GetRangeVar(); rv != nil {
tableName := strings.ToLower(rv.Relname)
// Check for schema qualification (e.g., pg_catalog.pg_class)
if rv.Schemaname != "" {
schemaName := strings.ToLower(rv.Schemaname)
// Block all schema-qualified access except public
if schemaName != "public" {
return fmt.Errorf("access to schema '%s' is not allowed", rv.Schemaname)
}
}
// Validate table name against whitelist
if !v.allowedTables[tableName] {
return fmt.Errorf("table not allowed: %s", rv.Relname)
}
// Get alias
alias := tableName
if rv.Alias != nil && rv.Alias.Aliasname != "" {
alias = strings.ToLower(rv.Alias.Aliasname)
}
tables[tableName] = alias
return nil
}
// Handle JoinExpr (JOIN)
if je := node.GetJoinExpr(); je != nil {
if err := v.validateFromItem(je.Larg, tables); err != nil {
return err
}
if err := v.validateFromItem(je.Rarg, tables); err != nil {
return err
}
if je.Quals != nil {
if err := v.validateNode(je.Quals); err != nil {
return err
}
}
return nil
}
// Handle RangeSubselect (subquery in FROM) - NOT ALLOWED
if node.GetRangeSubselect() != nil {
return fmt.Errorf("subqueries in FROM clause are not allowed")
}
// Handle RangeFunction (function in FROM) - NOT ALLOWED
if node.GetRangeFunction() != nil {
return fmt.Errorf("functions in FROM clause are not allowed")
}
return nil
}
// validateNode recursively validates AST nodes for security issues
func (v *SQLSecurityValidator) validateNode(node *pg_query.Node) error {
if node == nil {
return nil
}
// Check for subqueries (SubLink)
if sl := node.GetSubLink(); sl != nil {
return fmt.Errorf("subqueries are not allowed")
}
// Check for function calls
if fc := node.GetFuncCall(); fc != nil {
return v.validateFuncCall(fc)
}
// Check for column references with schema
if cr := node.GetColumnRef(); cr != nil {
return v.validateColumnRef(cr)
}
// Check for type casts (could be used for attacks)
if tc := node.GetTypeCast(); tc != nil {
if err := v.validateNode(tc.Arg); err != nil {
return err
}
// Validate the target type
if tc.TypeName != nil {
typeName := v.getTypeName(tc.TypeName)
if strings.HasPrefix(strings.ToLower(typeName), "pg_") {
return fmt.Errorf("casting to system type '%s' is not allowed", typeName)
}
}
}
// Recursively check A_Expr (expressions)
if ae := node.GetAExpr(); ae != nil {
if err := v.validateNode(ae.Lexpr); err != nil {
return err
}
if err := v.validateNode(ae.Rexpr); err != nil {
return err
}
}
// Check BoolExpr (AND, OR, NOT)
if be := node.GetBoolExpr(); be != nil {
for _, arg := range be.Args {
if err := v.validateNode(arg); err != nil {
return err
}
}
}
// Check NullTest
if nt := node.GetNullTest(); nt != nil {
if err := v.validateNode(nt.Arg); err != nil {
return err
}
}
// Check CoalesceExpr
if ce := node.GetCoalesceExpr(); ce != nil {
for _, arg := range ce.Args {
if err := v.validateNode(arg); err != nil {
return err
}
}
}
// Check CaseExpr
if caseExpr := node.GetCaseExpr(); caseExpr != nil {
if err := v.validateNode(caseExpr.Arg); err != nil {
return err
}
for _, when := range caseExpr.Args {
if err := v.validateNode(when); err != nil {
return err
}
}
if err := v.validateNode(caseExpr.Defresult); err != nil {
return err
}
}
// Check CaseWhen
if cw := node.GetCaseWhen(); cw != nil {
if err := v.validateNode(cw.Expr); err != nil {
return err
}
if err := v.validateNode(cw.Result); err != nil {
return err
}
}
// Check ResTarget (SELECT list items)
if rt := node.GetResTarget(); rt != nil {
if err := v.validateNode(rt.Val); err != nil {
return err
}
}
// Check SortBy (ORDER BY items)
if sb := node.GetSortBy(); sb != nil {
if err := v.validateNode(sb.Node); err != nil {
return err
}
}
// Check List
if list := node.GetList(); list != nil {
for _, item := range list.Items {
if err := v.validateNode(item); err != nil {
return err
}
}
}
return nil
}
// validateFuncCall validates a function call against the whitelist
func (v *SQLSecurityValidator) validateFuncCall(fc *pg_query.FuncCall) error {
// Get function name
funcName := ""
for _, namePart := range fc.Funcname {
if s := namePart.GetString_(); s != nil {
funcName = strings.ToLower(s.Sval)
}
}
// Check for schema-qualified function calls
if len(fc.Funcname) > 1 {
// Get schema name
schemaName := ""
if s := fc.Funcname[0].GetString_(); s != nil {
schemaName = strings.ToLower(s.Sval)
}
// Block all schema-qualified function calls except pg_catalog for basic functions
if schemaName != "" && schemaName != "pg_catalog" {
return fmt.Errorf("schema-qualified function calls are not allowed: %s", schemaName)
}
}
// Block dangerous function prefixes
dangerousPrefixes := []string{
"pg_", "lo_", "dblink", "file_", "copy_",
}
for _, prefix := range dangerousPrefixes {
if strings.HasPrefix(funcName, prefix) {
return fmt.Errorf("function '%s' is not allowed (dangerous prefix)", funcName)
}
}
// Block specific dangerous functions
dangerousFunctions := map[string]bool{
"current_setting": true,
"set_config": true,
"query_to_xml": true,
"xpath": true,
"xmlparse": true,
"txid_current": true,
}
if dangerousFunctions[funcName] {
return fmt.Errorf("function '%s' is not allowed", funcName)
}
// Check against whitelist
if !v.allowedFunctions[funcName] {
return fmt.Errorf("function not allowed: %s", funcName)
}
// Validate function arguments recursively
for _, arg := range fc.Args {
if err := v.validateNode(arg); err != nil {
return err
}
}
return nil
}
// validateColumnRef validates a column reference
func (v *SQLSecurityValidator) validateColumnRef(cr *pg_query.ColumnRef) error {
// Check for system column access
for _, field := range cr.Fields {
if s := field.GetString_(); s != nil {
colName := strings.ToLower(s.Sval)
// Block access to system columns
systemColumns := []string{"xmin", "xmax", "cmin", "cmax", "ctid", "tableoid"}
for _, sysCol := range systemColumns {
if colName == sysCol {
return fmt.Errorf("access to system column '%s' is not allowed", colName)
}
}
// Block pg_ prefixed identifiers
if strings.HasPrefix(colName, "pg_") {
return fmt.Errorf("access to '%s' is not allowed", colName)
}
}
}
return nil
}
// getTypeName extracts the type name from a TypeName node
func (v *SQLSecurityValidator) getTypeName(tn *pg_query.TypeName) string {
var parts []string
for _, name := range tn.Names {
if s := name.GetString_(); s != nil {
parts = append(parts, s.Sval)
}
}
return strings.Join(parts, ".")
}
// injectTenantConditions adds tenant_id filtering to the query
func (v *SQLSecurityValidator) injectTenantConditions(sql string, tablesInQuery map[string]string) string {
// Tables that require tenant_id filtering
tablesWithTenantID := map[string]bool{
"tenants": true,
"knowledge_bases": true,
"knowledges": true,
"sessions": true,
"chunks": true,
}
// Build tenant conditions
var conditions []string
for tableName, alias := range tablesInQuery {
if tablesWithTenantID[tableName] {
if tableName == "tenants" {
conditions = append(conditions, fmt.Sprintf("%s.id = %d", alias, v.tenantID))
} else {
conditions = append(conditions, fmt.Sprintf("%s.tenant_id = %d", alias, v.tenantID))
}
}
}
if len(conditions) == 0 {
return sql
}
tenantFilter := strings.Join(conditions, " AND ")
// Check if WHERE clause exists
wherePattern := regexp.MustCompile(`(?i)\bWHERE\b`)
if wherePattern.MatchString(sql) {
// Add tenant filter and wrap existing conditions in parentheses to prevent OR injection
// This ensures: WHERE tenant_filter AND (original_conditions)
// Instead of: WHERE tenant_filter AND original_conditions OR malicious_condition
return wherePattern.ReplaceAllString(sql, fmt.Sprintf("WHERE %s AND (", tenantFilter)) + ")"
}
// Add new WHERE clause before ORDER BY, GROUP BY, LIMIT, etc.
clausePattern := regexp.MustCompile(`(?i)\b(GROUP BY|ORDER BY|LIMIT|OFFSET|HAVING|FETCH)\b`)
if loc := clausePattern.FindStringIndex(sql); loc != nil {
return sql[:loc[0]] + fmt.Sprintf(" WHERE %s ", tenantFilter) + sql[loc[0]:]
}
// Add WHERE clause at the end
return fmt.Sprintf("%s WHERE %s", sql, tenantFilter)
}
// formatQueryResults formats query results into readable text
func (t *DatabaseQueryTool) formatQueryResults(
columns []string,
File diff suppressed because it is too large Load Diff
+413
View File
@@ -0,0 +1,413 @@
package utils
import (
"encoding/json"
"fmt"
"testing"
)
func TestParseSQL(t *testing.T) {
tests := []struct {
name string
sql string
wantIsSelect bool
wantTables []string
wantSelect []string
wantWhere []string
wantWhereText string
}{
{
name: "Simple SELECT",
sql: "SELECT id, name, age FROM users WHERE age > 18",
wantIsSelect: true,
wantTables: []string{"users"},
wantSelect: []string{"id", "name", "age"},
wantWhere: []string{"age"},
wantWhereText: "age > 18",
},
{
name: "SELECT with multiple WHERE conditions",
sql: "SELECT u.id, u.name FROM users u WHERE u.age > 18 AND u.status = 'active'",
wantIsSelect: true,
wantTables: []string{"users"},
wantSelect: []string{"id", "name"},
wantWhere: []string{"age", "status"},
wantWhereText: "u.age > 18 AND u.status = 'active'",
},
{
name: "SELECT with JOIN",
sql: "SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100",
wantIsSelect: true,
wantTables: []string{"users", "orders"},
wantSelect: []string{"name", "total"},
wantWhere: []string{"total"},
wantWhereText: "o.total > 100",
},
{
name: "SELECT with aggregate functions",
sql: "SELECT COUNT(id), AVG(score) FROM students WHERE grade = 'A'",
wantIsSelect: true,
wantTables: []string{"students"},
wantSelect: []string{"id", "score"},
wantWhere: []string{"grade"},
wantWhereText: "grade = 'A'",
},
{
name: "SELECT with complex WHERE",
sql: "SELECT * FROM products WHERE price BETWEEN 10 AND 100 AND category IN ('electronics', 'books')",
wantIsSelect: true,
wantTables: []string{"products"},
wantSelect: []string{},
wantWhere: []string{"price", "category"},
wantWhereText: "price BETWEEN 10 AND 100 AND category IN ('electronics', 'books')",
},
{
name: "INSERT statement",
sql: "INSERT INTO users (name, age) VALUES ('John', 25)",
wantIsSelect: false,
},
{
name: "UPDATE statement",
sql: "UPDATE users SET age = 26 WHERE id = 1",
wantIsSelect: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ParseSQL(tt.sql)
// Print result for debugging
resultJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("\nTest: %s\nResult:\n%s\n", tt.name, string(resultJSON))
if result.IsSelect != tt.wantIsSelect {
t.Errorf("IsSelect = %v, want %v", result.IsSelect, tt.wantIsSelect)
}
if !tt.wantIsSelect {
// For non-SELECT statements, just check IsSelect
return
}
if result.ParseError != "" {
t.Errorf("ParseError = %v, want empty", result.ParseError)
}
// Check tables
if len(result.TableNames) != len(tt.wantTables) {
t.Errorf("TableNames count = %d, want %d. Got: %v, Want: %v",
len(result.TableNames), len(tt.wantTables), result.TableNames, tt.wantTables)
} else {
for i, table := range tt.wantTables {
if i < len(result.TableNames) && result.TableNames[i] != table {
t.Errorf("TableNames[%d] = %v, want %v", i, result.TableNames[i], table)
}
}
}
// Check SELECT fields
if len(result.SelectFields) != len(tt.wantSelect) {
t.Errorf("SelectFields count = %d, want %d. Got: %v, Want: %v",
len(result.SelectFields), len(tt.wantSelect), result.SelectFields, tt.wantSelect)
}
// Check WHERE fields
if len(result.WhereFields) != len(tt.wantWhere) {
t.Errorf("WhereFields count = %d, want %d. Got: %v, Want: %v",
len(result.WhereFields), len(tt.wantWhere), result.WhereFields, tt.wantWhere)
}
// Check WHERE clause text
if result.WhereClause != tt.wantWhereText {
t.Errorf("WhereClause = %q, want %q", result.WhereClause, tt.wantWhereText)
}
})
}
}
func ExampleParseSQL() {
sql := "SELECT id, name, email FROM users WHERE age > 18 AND status = 'active'"
result := ParseSQL(sql)
fmt.Printf("Is SELECT: %v\n", result.IsSelect)
fmt.Printf("Tables: %v\n", result.TableNames)
fmt.Printf("SELECT fields: %v\n", result.SelectFields)
fmt.Printf("WHERE fields: %v\n", result.WhereFields)
fmt.Printf("WHERE clause: %s\n", result.WhereClause)
// Output:
// Is SELECT: true
// Tables: [users]
// SELECT fields: [id name email]
// WHERE fields: [age status]
// WHERE clause: age > 18 AND status = 'active'
}
func TestValidateSQL_TableNames(t *testing.T) {
tests := []struct {
name string
sql string
allowedTables []string
wantValid bool
wantErrorType string
}{
{
name: "Valid table name",
sql: "SELECT * FROM users WHERE id = 1",
allowedTables: []string{"users", "orders"},
wantValid: true,
},
{
name: "Invalid table name",
sql: "SELECT * FROM products WHERE id = 1",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorType: "table_not_allowed",
},
{
name: "Multiple tables - all valid",
sql: "SELECT * FROM users u JOIN orders o ON u.id = o.user_id",
allowedTables: []string{"users", "orders"},
wantValid: true,
},
{
name: "Multiple tables - one invalid",
sql: "SELECT * FROM users u JOIN products p ON u.id = p.user_id",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorType: "table_not_allowed",
},
{
name: "Case insensitive table names",
sql: "SELECT * FROM USERS WHERE id = 1",
allowedTables: []string{"users", "orders"},
wantValid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql, WithAllowedTables(tt.allowedTables...))
if validation.Valid != tt.wantValid {
t.Errorf("Valid = %v, want %v", validation.Valid, tt.wantValid)
}
if !tt.wantValid && len(validation.Errors) > 0 {
if validation.Errors[0].Type != tt.wantErrorType {
t.Errorf("Error type = %v, want %v", validation.Errors[0].Type, tt.wantErrorType)
}
}
// Print validation result for debugging
if !validation.Valid {
validationJSON, _ := json.MarshalIndent(validation, "", " ")
fmt.Printf("\nTest: %s\nValidation Result:\n%s\n", tt.name, string(validationJSON))
}
})
}
}
func TestValidateSQL_InjectionRisk(t *testing.T) {
tests := []struct {
name string
sql string
wantValid bool
wantErrorType string
description string
}{
{
name: "Normal WHERE clause",
sql: "SELECT * FROM users WHERE age > 18 AND status = 'active'",
wantValid: true,
description: "Should pass normal conditions",
},
{
name: "SQL injection with 1=1",
sql: "SELECT * FROM users WHERE id = 1 OR 1=1",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 1=1 pattern",
},
{
name: "SQL injection with '1'='1'",
sql: "SELECT * FROM users WHERE username = 'admin' OR '1'='1'",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect '1'='1' pattern",
},
{
name: "SQL injection with 0=0",
sql: "SELECT * FROM users WHERE 0=0",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 0=0 pattern",
},
{
name: "SQL injection with true",
sql: "SELECT * FROM users WHERE true",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 'true' pattern",
},
{
name: "SQL injection with empty string comparison",
sql: "SELECT * FROM users WHERE ''=''",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect empty string comparison",
},
{
name: "SQL injection with 1=0",
sql: "SELECT * FROM users WHERE 1=0",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 1=0 pattern",
},
{
name: "SQL injection with false",
sql: "SELECT * FROM users WHERE false",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 'false' pattern",
},
{
name: "Complex injection with AND",
sql: "SELECT * FROM users WHERE username = 'admin' AND 1=1",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 1=1 even with AND",
},
{
name: "Normal comparison with numbers",
sql: "SELECT * FROM users WHERE status_code = 1",
wantValid: true,
description: "Should allow normal number comparisons",
},
{
name: "Normal string comparison",
sql: "SELECT * FROM users WHERE name = 'John'",
wantValid: true,
description: "Should allow normal string comparisons",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql, WithInjectionRiskCheck())
if validation.Valid != tt.wantValid {
t.Errorf("%s: Valid = %v, want %v", tt.description, validation.Valid, tt.wantValid)
}
if !tt.wantValid && len(validation.Errors) > 0 {
found := false
for _, err := range validation.Errors {
if err.Type == tt.wantErrorType {
found = true
break
}
}
if !found {
t.Errorf("%s: Expected error type %v not found in errors", tt.description, tt.wantErrorType)
}
}
// Print validation result for debugging
if !validation.Valid {
validationJSON, _ := json.MarshalIndent(validation, "", " ")
fmt.Printf("\nTest: %s\nValidation Result:\n%s\n", tt.name, string(validationJSON))
}
})
}
}
func TestValidateSQL_CombinedOptions(t *testing.T) {
tests := []struct {
name string
sql string
allowedTables []string
wantValid bool
wantErrorCnt int
}{
{
name: "Valid SQL with both checks",
sql: "SELECT * FROM users WHERE age > 18",
allowedTables: []string{"users", "orders"},
wantValid: true,
wantErrorCnt: 0,
},
{
name: "Invalid table and injection risk",
sql: "SELECT * FROM products WHERE 1=1",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorCnt: 2, // Both table and injection errors
},
{
name: "Valid table but injection risk",
sql: "SELECT * FROM users WHERE id = 1 OR 1=1",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorCnt: 1, // Only injection error
},
{
name: "Invalid table but no injection",
sql: "SELECT * FROM products WHERE age > 18",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorCnt: 1, // Only table error
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql,
WithAllowedTables(tt.allowedTables...),
WithInjectionRiskCheck(),
)
if validation.Valid != tt.wantValid {
t.Errorf("Valid = %v, want %v", validation.Valid, tt.wantValid)
}
if len(validation.Errors) != tt.wantErrorCnt {
t.Errorf("Error count = %d, want %d", len(validation.Errors), tt.wantErrorCnt)
}
// Print validation result for debugging
validationJSON, _ := json.MarshalIndent(validation, "", " ")
fmt.Printf("\nTest: %s\nValidation Result:\n%s\n", tt.name, string(validationJSON))
})
}
}
func ExampleValidateSQL() {
// Example 1: Validate table names
sql1 := "SELECT * FROM users WHERE age > 18"
_, validation1 := ValidateSQL(sql1, WithAllowedTables("users", "orders"))
fmt.Printf("Example 1 - Valid: %v\n", validation1.Valid)
// Example 2: Detect SQL injection
sql2 := "SELECT * FROM users WHERE id = 1 OR 1=1"
_, validation2 := ValidateSQL(sql2, WithInjectionRiskCheck())
fmt.Printf("Example 2 - Valid: %v\n", validation2.Valid)
if !validation2.Valid {
fmt.Printf("Error: %s\n", validation2.Errors[0].Message)
}
// Example 3: Combined validation
sql3 := "SELECT * FROM products WHERE 1=1"
_, validation3 := ValidateSQL(sql3,
WithAllowedTables("users", "orders"),
WithInjectionRiskCheck(),
)
fmt.Printf("Example 3 - Valid: %v, Error count: %d\n", validation3.Valid, len(validation3.Errors))
// Output:
// Example 1 - Valid: true
// Example 2 - Valid: false
// Error: High-risk SQL injection pattern detected
// Example 3 - Valid: false, Error count: 2
}