Overview
Cross-Site Scripting (XSS) is a vulnerability that allows attackers to inject malicious JavaScript code into web pages viewed by other users. This demo contains a Reflected XSS vulnerability where user input from URL parameters is rendered directly in the page without sanitization.Severity Rating
HIGH - CVSS Score: 7.5/10CWE Reference: CWE-79: Improper Neutralization of Input During Web Page Generation
Vulnerable Code
The vulnerability exists in both the Flask backend and Jinja2 template:Why This Is Dangerous
- No Input Sanitization: The
messageparameter from the URL is passed directly to the template without any filtering or encoding - Jinja2 |safe Filter: The
|safefilter explicitly disables Jinja2’s automatic HTML escaping (dashboard.html:14) - User-Controlled Content: Attackers can craft malicious URLs containing JavaScript payloads
- Reflected Immediately: The payload executes as soon as the victim loads the URL
Exploitation Steps
Authenticate to the Application
First, log into the vulnerable application:This is necessary because the dashboard route requires authentication (
vulnerable/app.py:77).Craft XSS Payload
Create a malicious URL with JavaScript in the Result: Popup alert appears, confirming JavaScript execution
message parameter:- Basic Alert
- Event Handler
- DOM Manipulation
- Keylogger
- Redirect Attack
Impact Analysis
Confirmed in Demo
- Arbitrary JavaScript execution
- Access to session cookies
- DOM manipulation
- Page defacement
- Keylogging capability
Real-World Risks
- Session hijacking (cookie theft)
- Credential harvesting
- Malware distribution
- Phishing attacks
- Cryptocurrency miners
- Worm propagation
Types of XSS
Reflected XSS (This Demo)
Reflected XSS (This Demo)
Characteristics:
- Payload comes from HTTP request (URL, form submission)
- Immediately reflected back in the response
- Requires victim to click a malicious link
- Not stored in the database
message parameter in our demoStored XSS (Persistent)
Stored XSS (Persistent)
Characteristics:
- Payload stored in database
- Executes every time the page is loaded
- More dangerous (affects all users)
- No user interaction required after storage
DOM-Based XSS
DOM-Based XSS
Characteristics:
- Payload never reaches the server
- Entirely client-side JavaScript vulnerability
- Harder to detect with server-side tools
Secure Implementation
The secure version properly handles XSS prevention:How the Fix Works
HTML Entity Encoding
The
escape() function converts special characters:| Character | Encoded |
|---|---|
< | < |
> | > |
" | " |
' | ' |
& | & |
Mitigation Strategies
1. Output Encoding (Primary Defense)
1. Output Encoding (Primary Defense)
Always encode user input before rendering:
2. Content Security Policy (CSP)
2. Content Security Policy (CSP)
Implement strict CSP headers:
3. Input Validation
3. Input Validation
Validate and sanitize input:
4. HTTPOnly Cookies
4. HTTPOnly Cookies
5. Template Security
5. Template Security
Use template auto-escaping:
Testing and Detection
Manual Testing Payloads
Automated Tools
XSS Strike
Burp Suite
- Scanner: Automated XSS detection
- Intruder: Custom payload testing
- Repeater: Manual verification
OWASP ZAP
DOMPurify
JavaScript sanitization library:
References
Next Steps
Learn about related vulnerabilities:
- CSRF - Cross-Site Request Forgery
- Session Management - Secure session handling
- Security Headers - Additional security headers