Cross-site scripting: why output encoding matters
Day 19 of our web security series: how user input can turn into code on a web page, why it's so dangerous, and the habits that keep pages safe.
Yesterday we followed user input into the database and saw how parameterised queries keep data from being treated as a command. Today we follow it to the other place it almost always ends up: a web page that someone else will view.
The problem here is called cross-site scripting, usually shortened to XSS. It’s one of the most common web vulnerabilities, and it’s the one we’ve been pointing at since Day 6, because it gets around the browser’s most important protection.
The same mistake, in a different place
A web page is made of HTML: text mixed with instructions for the browser, such as “make this bold”, “show this image” or “run this script”. When a site shows something a user wrote, like a comment, a profile name or a search term, it puts that text into the page.
If the site inserts the text as it is, the browser can’t tell which parts the developer wrote and which parts came from a user. Text containing characters that mean something in HTML, like < and >, can be read as instructions rather than words. If those instructions include a script, the browser runs it.
That’s XSS: user data gets treated as part of the page’s code. It’s the same root cause as SQL injection, just in the browser instead of the database.
Why it matters for security
Remember the same-origin rule from Day 6: scripts from one site can’t read another site’s data. XSS defeats that wall from the inside. The injected script runs as if it were part of the trusted site, with all of that site’s access.
That means it can:
- Read anything on the page, including personal details and messages.
- Act as the logged-in user: change settings, send messages, make purchases.
- Show a convincing fake login form on the real site’s address.
- Try to steal the session, which is why
HttpOnlycookies (Day 10) are such a valuable extra layer.
And because the code arrives from the real site, padlock and all (Day 4), users have no way to tell anything is wrong.
Three common shapes
Security people usually describe three types. Knowing them helps you see where to look:
- Stored XSS. The input is saved, for example in a comment or a profile, and shown to everyone who views it later. One injection can affect many users.
- Reflected XSS. The input comes from the request itself, such as a search term shown back on the results page, and affects whoever opens a crafted link.
- DOM-based XSS. The page’s own JavaScript takes data, for example from the address bar, and writes it into the page unsafely, without the server being involved at all.
The defences are the same idea in every case.
The main fix: output encoding
Output encoding (also called escaping) converts characters that have a special meaning into a harmless form before they’re placed in the page. For example, < becomes <. The browser then displays the character instead of treating it as the start of an instruction.
The key word is output. Validation (Day 17) happens when data comes in, and it helps. But the safe form of a piece of text depends on where it’s going: inside normal page text, inside an HTML attribute, inside a URL or inside JavaScript each need slightly different encoding. So encoding is done at the moment data is placed into the page, for that exact spot.
Let the framework do it
The good news is that you rarely have to do this by hand. Modern frameworks and template engines encode output automatically:
- React, Vue, Angular and Svelte encode text inserted into templates by default.
- Server templates like Jinja, Twig, Blade, Razor and Go’s
html/templatedo the same.
Most real-world XSS today comes from places where a developer turned that protection off or went around it:
- Functions with names like
dangerouslySetInnerHTML,v-html,|safe,{!! !!}orinnerHTML. - Building HTML by joining strings together in JavaScript.
- Putting user data inside
<script>blocks, event attributes likeonclick, orjavascript:links.
A good habit is to search your code for these escape hatches and make sure each one is truly necessary and only ever receives trusted or sanitised content.
When you really need user-supplied HTML
Some features, like rich-text editors or formatted comments, genuinely need to accept some HTML. Encoding everything would break them. In that case, use a well-maintained HTML sanitiser library such as DOMPurify, which removes anything dangerous and keeps a safe allow-list of tags and attributes. Never write your own filter: the variety of tricks is enormous, and block-lists always miss some.
Extra layers worth having
- Content Security Policy (CSP). A response header that tells the browser which scripts are allowed to run. A strict CSP can stop an injected script even if one slips through. We’ll cover it properly on Day 22.
HttpOnlysession cookies, so scripts can’t read the session directly.- Safe JavaScript APIs. In your own scripts, use
textContentrather thaninnerHTMLwhen inserting text. - Keep libraries updated. Frameworks and sanitisers fix encoding bugs regularly.
What you can do today
If you use websites:
- Keep your browser updated. Browsers include protections that make many attacks harder.
- Be careful with links that contain long, strange-looking text after the address, especially in unexpected messages.
- If a site you’re logged in to suddenly asks for your password again on an ordinary page, close it and log in from the site’s home page instead.
If you run a website:
- Use a framework or template engine that encodes output by default, and keep that on.
- Search your code for escape hatches like
innerHTML,dangerouslySetInnerHTML,v-htmland|safe, and review each one. - Never put user data directly inside
<script>blocks, event attributes orjavascript:links. - Use a maintained sanitiser such as DOMPurify wherever users can submit HTML.
- Read the OWASP Cross Site Scripting Prevention Cheat Sheet and PortSwigger’s guide to XSS for the details.
Coming up next
On Day 20 we look at the settings everyone forgets: security misconfiguration, from default passwords to debug pages left switched on.