Gli attacchi XSS iniettano script malevoli nelle pagine web.
Codice vulnerabile
// PERICOLOSO!
echo "Ciao " . $_GET["nome"];
// URL: ?nome=Prevenzione: htmlspecialchars
// Converte caratteri speciali in entità HTML
echo "Ciao " . htmlspecialchars($_GET["nome"], ENT_QUOTES, "UTF-8");Funzione helper
function e($string) {
return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
}
echo "Ciao " . e($_GET["nome"]);Content Security Policy
header("Content-Security-Policy: default-src 'self'");Regole
- Sempre escape output utente
- Usa template engine con auto-escape
- Valida e sanitizza input