Stored XSS Attacks
You'll learn how persistent XSS differs from reflected attacks by saving malicious scripts in a database, endangering multiple users over time.
What Makes Stored XSS Dangerous
While reflected XSS (from our previous lesson) requires tricking individual users to click malicious links, stored XSS is far more severe. The attacker's malicious script gets saved to your application's database—in a comment, profile description, forum post, or any user-generated content field. Every user who views that content automatically executes the malicious script.
Think of it like the difference between handing someone a poisoned cookie (reflected) versus poisoning the entire cookie jar at a bakery (stored). Everyone who reaches into that jar gets poisoned automatically.
How Stored XSS Works
- An attacker submits malicious JavaScript through a form (like a comment box)
- Your server saves this input to the database without proper sanitization
- When any user views the page that displays this content, the server retrieves and renders it
- The browser executes the malicious script in every victim's session
For example, if an attacker posts a comment containing <script>/* steal cookies */</script> and your application displays it unescaped, every visitor to that page becomes a victim—not just the person who clicked a specific link.
The Scope of the Threat
Stored XSS creates persistent security holes that can:
- Steal session tokens from hundreds or thousands of users
- Deface your application for all visitors
- Create worms that spread by posting more malicious content
- Remain active until someone notices and removes the malicious data
Key Takeaway: Stored XSS is persistent and affects multiple users automatically because malicious scripts are saved in your database and served repeatedly—making input validation and output escaping absolutely critical for any user-generated content.