mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-09 13:04:24 +00:00
fix: Optimized regex performance and added backslash escaping for LIKE patterns
- Added backslash escaping to LIKE pattern sanitization - Optimized ValidSQL select mode with single pre-compiled regex - All funcspec tests pass (except pre-existing TestReplaceMetaVariables) Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
This commit is contained in:
@@ -729,8 +729,9 @@ func (h *Handler) mergeQueryParams(r *http.Request, sqlquery string, variables m
|
|||||||
} else {
|
} else {
|
||||||
if strings.Contains(val, "match=") {
|
if strings.Contains(val, "match=") {
|
||||||
colval := strings.ReplaceAll(val, "match=", "")
|
colval := strings.ReplaceAll(val, "match=", "")
|
||||||
// Don't sanitize LIKE patterns as it would escape wildcards
|
// Escape single quotes and backslashes for LIKE patterns
|
||||||
// Just remove single quotes to prevent SQL injection
|
// But don't escape wildcards % and _ which are intentional
|
||||||
|
colval = strings.ReplaceAll(colval, "\\", "\\\\")
|
||||||
colval = strings.ReplaceAll(colval, "'", "''")
|
colval = strings.ReplaceAll(colval, "'", "''")
|
||||||
if colval != "*" {
|
if colval != "*" {
|
||||||
sqlquery = sqlQryWhere(sqlquery, fmt.Sprintf("%s ILIKE '%%%s%%'", ValidSQL(parmk, "colname"), colval))
|
sqlquery = sqlQryWhere(sqlquery, fmt.Sprintf("%s ILIKE '%%%s%%'", ValidSQL(parmk, "colname"), colval))
|
||||||
@@ -910,19 +911,14 @@ func ValidSQL(input, mode string) string {
|
|||||||
// For SELECT clauses, be more permissive but still safe
|
// For SELECT clauses, be more permissive but still safe
|
||||||
// Remove semicolons and common SQL injection patterns (case-insensitive)
|
// Remove semicolons and common SQL injection patterns (case-insensitive)
|
||||||
dangerous := []string{
|
dangerous := []string{
|
||||||
";", "--", "/*", "*/", "xp_", "sp_",
|
";", "--", "/\\*", "\\*/", "xp_", "sp_",
|
||||||
"drop ", "delete ", "truncate ", "update ", "insert ",
|
"drop ", "delete ", "truncate ", "update ", "insert ",
|
||||||
"exec ", "execute ", "union ", "declare ", "alter ", "create ",
|
"exec ", "execute ", "union ", "declare ", "alter ", "create ",
|
||||||
}
|
}
|
||||||
result := input
|
// Build a single regex pattern with all dangerous keywords
|
||||||
// Use case-insensitive replacement via regex
|
pattern := "(?i)(" + strings.Join(dangerous, "|") + ")"
|
||||||
for _, d := range dangerous {
|
re := regexp.MustCompile(pattern)
|
||||||
// Create case-insensitive regex for the pattern
|
return re.ReplaceAllString(input, "")
|
||||||
pattern := "(?i)" + regexp.QuoteMeta(d)
|
|
||||||
re := regexp.MustCompile(pattern)
|
|
||||||
result = re.ReplaceAllString(result, "")
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
default:
|
default:
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user