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
- A source is where data from a user comes in: a comment stored in the database, a URL parameter such as
?q=, or a part of the URL that JavaScript reads. - A sink (like a kitchen sink, where the water comes out) is where that data is turned into HTML: a server template that prints it raw, or
innerHTMLin JavaScript. - XSS happens when data from a source reaches a sink without escaping. The fix is always at the sink: the data goes in as text.
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>
- Source: the database, the
bodycolumn of the comments table. Sink: the server template. - Who is affected: every visitor to that page, because the comment keeps appearing on the page. That's what gives Stored XSS the widest reach.
- Fix: escape when displaying. With
{{ }}, the comment<script>alert(1)</script>shows as text.
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>");
- Source: the URL parameter
q, which the server reads asreq.query.q. Sink: the server response. - Who is affected: only people who open the URL with that
q, since nothing is stored. The attacker usually sends that link to the victim. - Fix: escape on the server, or use a template engine that escapes automatically.
sanitizeHTMLhere is the escape function from the XSS Prevention lesson.
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;
- Source:
location.search(the part of the URL from?on), read in the browser..get("message")takes the value of themessageparameter. Sink:innerHTML. - Who is affected: people who open that URL.
- Escaping in the server template doesn't help, because the HTML is built by JavaScript in the browser, not by the server. When the source is
location.hash(the part of the URL after#), the server never even receives the text, because the browser doesn't send that part. That's why{{ }}in Blade didn't protect the announcements page at the start of this lesson. The fix has to be in JavaScript:textContent.
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
- Thinking data from a URL is always Reflected. When JavaScript in the browser is what puts it into the HTML, it's DOM-based, and the fix is in JavaScript.
- Escaping on one page only. Stored data can appear in many places: the public page, the admin panel, notifications. Every sink has to escape.
- Relying on a filter when data comes in. Data saved before the filter was added, or data from another path, can get past that filter. Escaping on display is still required.
- Forgetting other sinks. Besides
innerHTML, there areouterHTML,insertAdjacentHTMLanddocument.write, which also read a string as HTML. For text, usetextContent.
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.