XSS Prevention: A Practical Checklist
Cross-site scripting is still one of the top web vulnerabilities. Here's how to prevent it.
Cross-site scripting (XSS) has been in the OWASP Top 10 for over a decade. It's still there because developers still make the same mistakes.
The three types
- Reflected XSS - Malicious input reflected in the page response
- Stored XSS - Malicious script stored in database, served to all users
- DOM-based XSS - Client-side JavaScript processes untrusted data unsafely
Prevention checklist
- Sanitize all user input on the server side
- Encode output based on context (HTML, JavaScript, URL, CSS)
- Use Content-Security-Policy headers
- Set HttpOnly flag on cookies
- Use frameworks that escape by default (React, Vue, Angular)
- Validate input with strict whitelists
The single most effective defense is a strong Content-Security-Policy header that restricts inline scripts.