CyberSearch .in
SUBSCRIBE
← All articles
WEB SECURITY · 7 min read

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.

Day 19 of Web Security, From the Ground Up: cross-site scripting and output encoding

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 HttpOnly cookies (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 &lt;. 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/template do 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, {!! !!} or innerHTML.
  • Building HTML by joining strings together in JavaScript.
  • Putting user data inside <script> blocks, event attributes like onclick, or javascript: 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.
  • HttpOnly session cookies, so scripts can’t read the session directly.
  • Safe JavaScript APIs. In your own scripts, use textContent rather than innerHTML when inserting text.
  • Keep libraries updated. Frameworks and sanitisers fix encoding bugs regularly.

What you can do today

If you use websites:

  1. Keep your browser updated. Browsers include protections that make many attacks harder.
  2. Be careful with links that contain long, strange-looking text after the address, especially in unexpected messages.
  3. 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:

  1. Use a framework or template engine that encodes output by default, and keep that on.
  2. Search your code for escape hatches like innerHTML, dangerouslySetInnerHTML, v-html and |safe, and review each one.
  3. Never put user data directly inside <script> blocks, event attributes or javascript: links.
  4. Use a maintained sanitiser such as DOMPurify wherever users can submit HTML.
  5. 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.

Written & tested by CyberSearch Get one tip a week →
KEEP READING

More from the blog

WEB SECURITYNov 23, 2026 · 7 min
Keeping databases safe with parameterised queries

Day 18 of our web security series: why mixing user input into database commands is dangerous, and the simple habit that prevents SQL injection.

WEB SECURITYNov 22, 2026 · 7 min
Why you never trust user input: validation basics

Day 17 of our web security series: what counts as user input, why it can't be trusted, and how to validate it on the server without making life hard for real users.

WEB SECURITYNov 21, 2026 · 7 min
Broken access control and how to prevent it

Day 16 of our web security series: why the top risk on the OWASP list is so common, the shapes it takes in real applications, and the habits that prevent it.