Created
February 15, 2025 21:20
-
-
Save JuanRamino/b42d62f7b60a77a42701bf78d4747f22 to your computer and use it in GitHub Desktop.
sanitize sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
// Funzione per validare un campo stringa contro parole chiave SQL pericolose e caratteri | |
func isValidSQLField(field string) bool { | |
// Caratteri non permessi (aggiungi o modifica a seconda delle tue necessità) | |
forbiddenChars := []string{";", "--", "/*", "*/", "\\"} // backslash aggiunto per sicurezza generica | |
// Parole chiave SQL non permesse (in lowercase per case-insensitive check) | |
forbiddenKeywords := []string{ | |
"delete", "update", "insert", "grant", "drop", "truncate", "alter", "create", | |
"exec", "execute", "union", "load_file", "outfile", "dumpfile", "rename", | |
"shutdown", "declare", "set ", // "set " con spazio per evitare "dataset" etc. | |
"handler", "lock", "unlock", "flush", "reset", "kill", | |
"password", "identified", "binary", // Parole chiave relative a sicurezza e dati sensibili | |
"hex", "unhex", "ord", "char", "concat", "group_concat", // Funzioni potenzialmente pericolose | |
"sleep", "benchmark", "delay", // Funzioni di timing injection | |
"if", "case", "when", "then", "else", "end", // Costrutti condizionali per logica | |
"while", "loop", "repeat", "for", "cursor", // Costrutti di controllo flusso potenzialmente rischiosi | |
} | |
// 1. Controllo caratteri non permessi | |
for _, char := range forbiddenChars { | |
if strings.Contains(field, char) { | |
return false // Trovato carattere non permesso | |
} | |
} | |
// 2. Controllo parole chiave non permesse (case-insensitive) | |
fieldLower := strings.ToLower(field) // Converti il campo in lowercase per confronto case-insensitive | |
for _, keyword := range forbiddenKeywords { | |
if strings.Contains(fieldLower, keyword) { | |
// Controllo aggiuntivo per evitare falsi positivi (es. "updated" che contiene "update") | |
// Verifica che la keyword sia una "parola intera" o preceduta/seguita da spazi/non alfanumerici | |
// Questo è un approccio semplificato, per una validazione più robusta servirebbero regex o tokenizzazione avanzata | |
if strings.Contains(fieldLower, " "+keyword+" ") || // keyword come parola intera tra spazi | |
strings.HasPrefix(fieldLower, keyword+" ") || // keyword all'inizio seguita da spazio | |
strings.HasSuffix(fieldLower, " "+keyword) || // keyword alla fine preceduta da spazio | |
fieldLower == keyword { // campo è esattamente la keyword | |
return false // Trovata parola chiave non permessa | |
} | |
} | |
} | |
return true // Il campo è considerato valido | |
} | |
func main() { | |
campiDaValidare := []string{ | |
"username", | |
"nome_utente", | |
"campo123", | |
"campo_valido", | |
"CampoInValido", | |
"campo-non-valido", | |
"campo con spazi", | |
"1campo", | |
"user_id", | |
"product_name", | |
"category", | |
"email_address", | |
"indirizzo_spedizione", | |
"valore_ordine", | |
// Campi NON validi (esempi di injection) | |
"username;", // Contiene ";" | |
"delete from users", // Contiene "delete" | |
"update prodotti set", // Contiene "update" | |
"insert into log", // Contiene "insert" | |
"drop table ordini", // Contiene "drop" | |
"grant all on *.* to", // Contiene "grant" | |
"select * from users where id = 1 union select ...", // Contiene "union" | |
"test sleep(5) --", // Contiene "sleep" e "--" | |
"campo_con_/*commento*/injection", // Contiene "/*" | |
"campo_con_*/commento*/injection", // Contiene "*/" (anche se improbabile da solo) | |
"campo_con_\\backslash", // Contiene "\\" | |
"SET PASSWORD FOR", // Contiene "set " (con spazio) | |
"EXEC master.dbo.xp_cmdshell", // Contiene "exec" | |
"SELECT hex('string')", // Contiene "hex" | |
"SELECT benchmark(1000000,MD5('test'))", // Contiene "benchmark" | |
} | |
for _, campo := range campiDaValidare { | |
if isValidSQLField(campo) { | |
fmt.Printf("Campo '%s' è VALIDO\n", campo) | |
} else { | |
fmt.Printf("Campo '%s' è NON VALIDO\n", campo) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment