Cross-Site Scripting (XSS) is a security vulnerability that occurs when a website allows users to input data (like comments, search terms, or form fields) that contains malicious JavaScript code. If this code isn't properly sanitised, it gets executed in other users' browsers when they view the page.
When an attacker successfully executes an XSS attack, they can:
Steal login cookies to hijack user sessions
Capture keystrokes (like passwords)
Take screenshots
Redirect users to malicious sites
Deface websites
Access user's webcam or microphone
Try this in the feedback form:
<script>alert("XSS Test!")</script>
What's happening:
Your script gets saved to success_feedback.html
When anyone views the feedback page, the script runs
They see a popup message
Great way to test if XSS is possible
Try this in the feedback form:
<script>window.location.href="http://example.com"</script>
What's happening:
When users view the feedback, they get redirected
Could send users to malicious sites
Replace example.com with any URL
Try this in the feedback form:
<img src="x" onerror="alert('Image XSS!')"/>
What's happening:
Creates an image tag that fails to load
When image fails, runs our code
Often bypasses basic script filters
The onerror event triggers because 'x' isn't a valid image
Try this in the feedback form:
<button onclick="alert('Clicked!')">Click Me!</button>
What's happening:
Creates an innocent-looking button
Runs JavaScript when clicked
Shows how interactive elements can be dangerous
Looking at the code:
def listFeedback():
f = open("templates/partials/success_feedback.html", "w")
for row in data:
f.write("<p>\n")
f.write(f"{row[1]}\n") # Direct insertion of feedback!
f.write("</p>\n")
The feedback system:
Takes user input without checking it
Writes it directly to an HTML file
Serves that HTML to other users
Never sanitizes or escapes special characters
Feedback form - directly writes to HTML
URL parameters (try adding ?url=javascript:alert(1))
Username field - might display on success page
Any place your input shows up on screen
Basic Tests:
<script>alert("XSS")</script>
<img src="x" onerror="alert('XSS')">
<button onclick="alert('XSS')">Click me!</button>
More Advanced:
<div onmouseover="alert('XSS')">Hover over me!</div>
<iframe src="javascript:alert('XSS')"></iframe>
Look at this template code:
<div>Welcome back, {{username}}!</div>
<div class="feedback">{{user_feedback}}</div>
Questions:
What could happen if username contains <script> tags?
Should we allow HTML in feedback at all?
How could we safely display user content?
Input Validation:
Check length
Allow only specific characters
Reject known bad patterns
Output Encoding:
Convert special characters to HTML entities
Example: < becomes <
Use template engine's built-in escaping
Content Security Policy (CSP):
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">