fix: prevent SQL injection via OR conditions

main
wizardchen 2026-01-21 14:53:58 +08:00 committed by lyingbug
parent 2a99e2458d
commit 44a9835642
1 changed files with 4 additions and 2 deletions

View File

@ -781,8 +781,10 @@ func (v *SQLSecurityValidator) injectTenantConditions(sql string, tablesInQuery
// Check if WHERE clause exists
wherePattern := regexp.MustCompile(`(?i)\bWHERE\b`)
if wherePattern.MatchString(sql) {
// Add to existing WHERE clause
return wherePattern.ReplaceAllString(sql, fmt.Sprintf("WHERE %s AND ", tenantFilter))
// 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.