Skip to content

XSS Prevention and Untrusted Input ​

Find the exact point where an untrusted string becomes markup, a URL, a script or a style, and make it stay data there.

In this chapter: the three kinds of XSS · the four places a string turns into code · sanitising rich HTML and where React stops · the inputs only the browser sees · client checks versus the real control

💡 The Core Idea ​

Cross-Site Scripting (XSS) lets an attacker run their JavaScript in another user's browser. The script runs with the victim's session. It can read the page, send requests as the user, or change what they see.

The root cause is always the same. An untrusted string crosses from data into something the browser interprets: markup, a URL, a script, or a style. Prevention is one question asked at each crossing: what does the browser do with this string here? Where the string came from matters less than where it lands.

How It Works ​

The three types ​

TypeWhere the payload livesExample trigger
ReflectedIn the request, echoed back in the responseA crafted link in a phishing email
StoredSaved in your database, served to many usersA malicious blog comment
DOM-basedNever reaches the serverinnerHTML = location.hash

Stored runs for every visitor with no link to click. DOM-based is the hardest to catch: the payload never appears in a server log or a WAF (web application firewall).

The four crossings ​

Each crossing needs its own defence, because a different part of the browser parses each one.

CrossingSafe defaultWhen you cannot avoid it
MarkuptextContent, JSX textSanitise with DOMPurify
URLBuild with encodeURIComponentAllowlist the scheme or accept a path
ScriptNever build code from stringsEscape <, >, & in embedded JSON
StyleKeep user input out of CSSMap to a fixed set of class names

Where a String Becomes Markup ​

The bug starts at a source the attacker controls. It flows into a sink, an API that parses HTML: innerHTML, outerHTML, insertAdjacentHTML or document.write.

The classic DOM-based bug, and its fix:

typescript
const name: string = new URLSearchParams(location.search).get("name") ?? "";
const el = document.querySelector("#hello")!;
el.innerHTML = `Hi, ${name}!`; // ❌ ?name=<img src=x onerror=alert(1)> runs
el.textContent = `Hi, ${name}!`; // ✅ treated as text, never parsed

Rich HTML you did not write ​

A CMS body must render as HTML, so encoding would break it. You need sanitisation: parse the HTML and strip anything that can run. DOMPurify is the standard. Uploaded SVG belongs here too, since an SVG can carry script. Show uploads with <img>, never as text read into the DOM.

Sanitise with an allowlist:

typescript
import DOMPurify from "dompurify";

const clean: string = DOMPurify.sanitize(dirty, {
  ALLOWED_TAGS: ["p", "b", "i", "em", "strong", "a", "ul", "ol", "li"],
  ALLOWED_ATTR: ["href", "title"],
});
// <img src=x onerror=alert(1)> loses its onerror; <script> is removed

⚠️ Sanitise at render time, not only on save. Data reaches your database through imports, other endpoints and old rows. Only the render path sees all of it.

Where React stops protecting you ​

React escapes every value rendered as JSX text. That covers most markup crossings, but not these.

The escape hatches:

tsx
import DOMPurify from "dompurify";

function Article({ html }: { html: string }) {
  return <div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(html) }} />;
}

function SafeLink({ url, children }: { url: string; children: React.ReactNode }) {
  const safe: string = /^https?:\/\//i.test(url) ? url : "#";
  return <a href={safe}>{children}</a>;
}

dangerouslySetInnerHTML bypasses escaping, so sanitise first. A URL from user input needs its scheme allowlisted. The third blind spot is spreading unknown props ({...userControlled}), which lets an attacker choose the attribute.

Where a String Becomes a URL or a Script ​

URLs. A javascript: URL in href or location is code. A full URL from a query string is an open redirect. Your domain lends its name to a phishing page. Link filters pass it, because the first hop really is your site.

Accept a path, not a URL:

typescript
function safeNext(raw: string | null): string {
  if (!raw || !raw.startsWith("/") || raw.startsWith("//")) return "/";
  return raw;
}

location.href = safeNext(new URLSearchParams(location.search).get("next"));

//evil.com is protocol-relative, so it is rejected too. If you need other origins, keep an allowlist of hostnames.

Scripts. eval, new Function and setTimeout with a string turn data into code, so keep strings out of them. The subtle case is state serialised into the page for hydration. An attacker can close the tag with </script> from inside a JSON string.

Escape JSON before embedding it in HTML:

typescript
function safeJson(data: unknown): string {
  return JSON.stringify(data).replace(/[<>&]/g, (c: string) => `\\u00${c.charCodeAt(0).toString(16)}`);
}

const html: string = `<script>window.__STATE__ = ${safeJson(state)};</script>`;

Inputs Only the Browser Sees ​

A postMessage from an iframe or a value in location.hash never reaches your server. No server handler exists to review, so the browser is the only place a check can happen.

A postMessage handler needs two checks:

typescript
window.addEventListener("message", (event: MessageEvent) => {
  if (event.origin !== "https://widgets.example.com") return; // 1. exact sender
  // 2. exact shape — the sender is not your code
  const parsed = widgetMessageSchema.safeParse(event.data);
  if (!parsed.success) return;
  applyWidgetUpdate(parsed.data);
});

Compare the origin to an exact string: includes("example.com") passes for example.com.attacker.net. When you send, name the target origin. postMessage(data, "*") goes to whatever page holds the frame.

Client Checks Versus the Real Control ​

Validation in the browser is user experience. Anyone can call the API with curl, so the server enforces and the client only gives fast feedback.

One schema for the form and the endpoint:

typescript
import { z } from "zod";

export const contactSchema = z.object({
  email: z.email(),
  message: z.string().min(10).max(2000),
});

const parsed = contactSchema.safeParse(req.body); // on the server, this is the gate
if (!parsed.success) res.status(400).json(parsed.error.issues);

The form uses the same schema through its resolver, so the limits cannot drift. File checks work the same way. file.type is a guess from the extension, so the server still checks size and magic bytes.

When to Use It ​

Untrusted inputChooseWhy
Real HTML from a CMS or editorSanitising with an allowlistEncoding would destroy the markup
A URL or redirect targetAllowlist scheme, or path onlyEncoding does not stop javascript:
A message or fragment read in the pageExact origin check and a schemaNo server will ever see it

The backstop layers. A Content Security Policy stops injected script running. Trusted Types makes innerHTML reject plain strings, so every sanitiser call goes through one policy (Chromium-only as of 2026). An HttpOnly session cookie means a script that does run cannot read the token.

Common Mistakes ​

❌ Filtering input on the way in and trusting it on the way out. ✅ Encode or sanitise where the string is used. Only that point knows the context.

❌ Trusting event.data because the iframe is yours. Anyone can frame your page and post to it. ✅ Check the exact origin, then parse the payload against a schema.

🔑 Key Takeaways ​

  • XSS happens where an untrusted string crosses into markup, a URL, a script or a style, and each crossing needs its own defence.
  • textContent and JSX text are safe. innerHTML, dangerouslySetInnerHTML, javascript: URLs and spread props are not.
  • Sanitise unavoidable HTML with DOMPurify at render time, and accept a path rather than a full URL for any redirect.
  • postMessage payloads and URL fragments never reach the server, so the browser must check the exact origin and the shape.
  • Client validation is user experience. Share one schema so the server's check, the real control, cannot drift from it.

Interview Questions ​

Q: Encoding or sanitisation — when do you use each?

Encoding turns special characters into text (< becomes &lt;). Use it for anything shown as plain text. Sanitisation parses HTML and removes what can run. Use it only for real HTML, such as CMS content, with an allowlist.

Q: How does React prevent XSS, and where does it fall short?

React escapes every value rendered as JSX text. It does not protect dangerouslySetInnerHTML, user URLs in href or src, or spread props. The fixes: sanitise, allowlist the scheme, never spread what you did not build.

Q: A colleague adds a postMessage listener for an analytics widget. What do you check in review?

That the handler compares event.origin to one exact origin before touching event.data. That the payload is parsed against a schema, not destructured. And that the sending side does not use postMessage(data, "*"), which broadcasts to whatever page holds the frame.

Q: Your team wants to rely on CSP instead of fixing every innerHTML. Would you agree?

No. CSP is the second layer. It stops injected code running when you miss one encoding, but it is easy to weaken with 'unsafe-inline' or a broad allowlist. Fix the sinks, then add CSP and Trusted Types so a single mistake is not fatal.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.