Stored vs Reflected vs DOM XSS — Security

The three types of XSS are told apart by their source (where user data comes in) and sink (where that data is turned into HTML). Stored XSS: data saved in the…

You already use {{ }} in every Blade template, and you feel XSS is handled. Then a report comes in: the announcements page still shows a pop-up when opened from one particular link. Every template escapes. So how is that possible?

To avoid that kind of surprise, learn the three types of XSS: Stored, Reflected, and DOM-based. The difference is in two questions: where does the untrusted text come from, and who puts it into the page? Picture a plumber called to a leaking pipe. Their first question is always: where does the water come in, and where does it come out? That answer decides which part gets fixed. XSS works the same way: the water is the text from a user, and you look for where that text comes in and where it comes out into the page.

Source and sink

The Lab uses the same three examples as this lesson. In each example, the data is untrusted text.

Stored XSS

The data is stored first, then shown to everyone who opens the page. For example, a comment in the database printed by Blade:

{{-- vulnerable: the comment from the database printed raw --}}
<p>{!! $comment->body !!}</p>

{{-- fix: escaped when displayed --}}
<p>{{ $comment->body }}</p>

Reflected XSS

The data comes from the request and is reflected (copied back) straight into the response, without being stored. For example, a search page that repeats your search term:

// vulnerable: the search term from the URL copied raw into the HTML
res.send("<p>Search results for: " + req.query.q + "</p>");

// fix: escape it first
res.send("<p>Search results for: " + sanitizeHTML(req.query.q) + "</p>");

DOM-based XSS

Here the server doesn't take part in building the HTML. JavaScript in the browser reads data from the URL, then puts it into the page:

const message = new URLSearchParams(location.search).get("message");

// vulnerable: read as HTML
document.getElementById("message").innerHTML = message;

// fix: always becomes text
document.getElementById("message").textContent = message;

Comparison

Type Source Sink Who is affected Fix
Stored database server template every visitor escape when displaying
Reflected request parameter server response people who open that URL escape on the server
DOM-based URL, read by JavaScript innerHTML people who open that URL textContent in JavaScript

⚠️ Wrong type, wrong fix

TL;DR: The three types of XSS are told apart by their source (where user data comes in) and sink (where that data is turned into HTML). Stored XSS: data saved in the database and printed raw, affecting every visitor; fix it by escaping on display ({{ }} in Blade). Reflected XSS: data from a URL or form reflected (copied) straight back into the response by the server, affecting people who open that URL; fix it by escaping on the server. DOM-based XSS: JavaScript in the browser puts data from the URL in with innerHTML, with no server building that HTML; fix it in JavaScript with textContent.

⚡ Source and sink

Meaning: A source is where user data comes in: the database, a URL parameter, or a URL that JavaScript reads. A sink is where data is turned into HTML, such as a server template or innerHTML. XSS happens when data from a source reaches a sink without escaping. So the fix is always at the sink.

⚡ Stored: saved, hits everyone

Write:

<p>{{ $comment->body }}</p>

Meaning: Stored XSS: a comment is saved in the database, then printed raw into the page. It hits every visitor, because the comment keeps being shown. This code is the fix: {{ }} in Blade escapes $comment->body (the comment text) when it is displayed.

⚡ Reflected: bounced back from the request

Write:

res.send("<p>Search results for: " + sanitizeHTML(req.query.q) + "</p>");

Meaning: Reflected XSS: the search term from the URL (req.query.q) is reflected (copied) straight into the response without being stored. Only people who open that URL are affected. The fix is escaping on the server: sanitizeHTML is the escape function from the XSS Prevention lesson.

⚡ DOM-based: built by JavaScript

Write:

document.getElementById("message").textContent = message;
Guess first: every Blade template already uses {{ }}. Why does XSS through innerHTML still run?

Because the HTML is built by JavaScript in the browser, not by the server, so escaping in the server template plays no part. When the source is location.hash (the part of the URL after #), the server never even receives the text.

Meaning: DOM-based XSS: JavaScript in the browser reads message from the URL and puts it into the page. Through innerHTML, message is read as HTML. The fix has to be in JavaScript: textContent, which always makes it text.

What you will learn