The Ghost in the Browser: A Guide to XSS Prevention in 2026
By Huzi
File #107 · 4 min read
Cross-site scripting is a stranger standing in your living room wearing your face, telling your family to hand over the keys. The browser cannot tell the difference, because the script runs on your own domain — so it gets the cookies, the session, the DOM. The browser gives it everything.
The quick answer: XSS arrives in three working shapes — stored, reflected and DOM-based — plus mutation XSS, which breaks hand-rolled sanitizers. The defences that hold: context-aware escaping, DOMPurify for rich text, a strict CSP with Trusted Types, and hardened cookies.
The three faces of XSS
Stored XSS is the worst kind: the payload is saved — in a comment, a profile field — and every visitor runs it. Reflected XSS bounces off the server through a URL parameter, arriving as a crafted link.
DOM-based XSS never touches the server at all; it is your own JavaScript mishandling data from location.hash or postMessage. A fourth variant, mutation XSS, is why hand-rolled sanitizers keep losing: browsers re-parse "clean" HTML and it mutates into something executable.
| The vector | The fix that holds |
|---|---|
| Stored payload in a comment or profile field | encode on output, in the right context |
| Reflected payload in a URL parameter | escape the parameter, quote every attribute |
| DOM-based sinks reading location.hash or postMessage | Trusted Types, so dangerous sinks refuse raw strings |
| Mutation XSS | DOMPurify, kept updated |
| Session theft via document.cookie | HttpOnly, SameSite cookies |
What a successful XSS buys the attacker
Session hijacking, first — reading document.cookie wherever it is not marked HttpOnly. Rewriting your page into a fake login form that posts credentials elsewhere.
Chained with an adversary-in-the-middle proxy like Evilginx, a stolen cookie sidesteps MFA entirely. In rare cases, XSS plus a browser zero-day means code running on the victim's machine.
The impersonation has a travel twin: a cloned booking page harvesting card details works exactly like the fake login form. It is why every booking, invoice and itinerary at HTG Travel goes out in writing, on numbers you can verify.
Escape by context
The rule is old and absolute: never trust input. But escaping is contextual, and the right encoding for HTML body will not save you inside a script tag:
- HTML body: entity-encode the angle brackets, ampersands and quotes
- Attributes: quote everything, and keep user data out of event handlers
- JavaScript: serialize safely, then escape the characters that can break out of a block
- URLs: encode the components, allow only known-safe schemes, block javascript: outright
Modern frameworks — React, Vue, Svelte — escape HTML context by default, which is exactly why the explicit escape hatches stand out in review.
Pro tip: React's dangerouslySetInnerHTML, Vue's v-html and Svelte's equivalent are the hatches to flag in code review — when you must render rich text, run it through DOMPurify and keep the library updated, because mXSS bypasses get patched there regularly.
The header that saves you anyway
A strict Content Security Policy is the defence that keeps working when code fails. Nonces over hashes, strict-dynamic so trusted scripts can load their own dependencies, and Trusted Types to make dangerous sinks refuse raw strings:
Content-Security-Policy:
default-src 'self';
script-src 'self' 'nonce-abc123' 'strict-dynamic';
object-src 'none';
base-uri 'self';
frame-ancestors 'none';
require-trusted-types-for 'script';
trusted-types dompurify default;
Then harden the cookies — HttpOnly, SameSite, and an integrity attribute on external scripts so a compromised CDN cannot quietly swap in something else.
Impersonation is the oldest theft there is, and no sanitizer fixes the version happening to Gaza's families, who keep watching their story told by everyone except themselves. Sudan's families verify news of each other across broken networks, hunting the fake from the true — may their hardest information problems become ordinary again.
If you shipped a fix like this under deadline, take the proper week off. We plan honeymoons, including the phase where two sets of in-laws hold strong opinions about the destination — the everyday diplomacy of HTG Travel, and we are very good at it. The working line is +92 317 5054959 for the post-release detox up north.




