Cross Site Scripting (XSS) is a web security vulnerability allowing attackers to execute malicious scripts in user browsers, potentially compromising sensitive data and interactions.
Types of XSS
- Reflected XSS:
- User input immediately returned in web application responses without sanitization.
- Typically exploited through error messages or search results.
- Stored XSS (Persistent):
- Malicious script stored on a web application’s server (e.g., in comments), executed when accessed by users.
- DOM-Based XSS:
- Attack payload executed in the browser by modifying the DOM environment without altering the HTTP response.
Possible Attacks
- Cookie Theft: Access session cookies to hijack accounts.
- Keylogging: Record keystrokes to capture sensitive data.
- Phishing: Embed fake forms to steal credentials.
Countermeasures
- Encoding:
- Escape user input to prevent browsers from interpreting it as code (e.g., replacing
<
with<
).
- Escape user input to prevent browsers from interpreting it as code (e.g., replacing
- Validation:
- Filter malicious input using blacklisting or whitelisting techniques.
- Input Handling Contexts:
- Ensure user input is processed securely in all contexts (HTML, attributes, JavaScript).
- Secure Input Handling:
- Perform sanitization both server-side (to protect against XSS) and client-side (to protect against DOM-based XSS).