Cross-Site Scripting (XSS): Attack Techniques and Prevention
Back to Blog
Web Security6 min read14,429 views

Cross-Site Scripting (XSS): Attack Techniques and Prevention

XSS is the SQL injection of the browser. How it works, the three types, why one injection can own thousands of accounts, and how to stop it properly with context-aware output encoding.

Z

ZeroDaysHack Team

November 18, 2026

Cross-site scripting (XSS) is the SQL injection of the browser: the same root mistake — untrusted input ending up somewhere it gets executed — but the interpreter is the victim's browser instead of a database. It's one of the most common web vulnerabilities on the planet, and the gap between "it just pops an alert box" and "it takes over accounts" is smaller than most beginners think. This guide covers how XSS works, the three types, what it really lets an attacker do, and how to stop it properly.

#What XSS actually is

XSS happens when a web app takes input from a user and places it into a page without neutralising it, so the browser interprets that input as live HTML or JavaScript instead of displaying it as text. The attacker's script then runs in another user's browser, inside the trusted origin of the site — which is exactly what makes it dangerous.

The textbook proof-of-concept is a script that just pops up an alert box. That alert is not the attack — it's the proof that arbitrary script runs. Once you can run script in someone else's session, the alert can become anything.

#The three types of XSS

All XSS shares that root cause. The types differ by where the malicious script comes from and how it reaches the victim.

Reflected, stored and DOM-based XSS compared
Reflected bounces off one request, stored is saved on the server, DOM-based never touches the server.
TypeWhere the payload livesImpact
ReflectedIn a crafted link or form the victim is tricked into usingRuns once, for whoever clicks
StoredSaved on the server (a comment, profile, message)Hits every visitor of that page — the worst
DOM-basedIntroduced entirely by client-side JavaScriptServer may never even see the payload
  • Reflected XSS is delivered — the attacker sends a victim a link containing the payload, the server reflects it back into the response, and it runs in that one victim's browser.
  • Stored XSS is the most dangerous: the payload is saved (say, in a comment field) and served to everyone who loads the page, automatically, with no link to click.
  • DOM-based XSS happens when client-side JavaScript reads something (like part of the URL) and writes it into the page unsafely — the vulnerability lives in the front-end code, and the server might never see the payload at all.

#Why one injection can own thousands of accounts

Stored XSS shows why this vulnerability is rated so seriously. One injected script, saved once, runs in every visitor's browser.

How a stored XSS payload reaches every visitor
Inject once, the server stores it, and it runs for every visitor — with each victim's own session.

The key insight: the script runs with the victim's session, inside the site's trusted origin. To the browser, it is the site. That's why XSS leads to real consequences, not just popups:

  • Session theft — read or ride the victim's logged-in session to act as them.
  • Account takeover — change the victim's email or password through the app's own forms.
  • Keylogging and phishing — capture what the victim types, or inject a fake login prompt.
  • Defacement and redirects — rewrite the page or bounce users to a malicious site.

#The fix: encode on output, in the right context

The core fix for XSS is context-aware output encoding: whenever you place untrusted data into a page, escape it for exactly where it's going — HTML body, HTML attribute, JavaScript, or URL each need different treatment. Encoded correctly, the browser renders the attacker's input as harmless text instead of running it.

The vulnerable pattern — untrusted data written straight into the page (don't do this):

vulnerable: raw input becomes live HTML
// BAD — innerHTML parses the string as HTML, so any markup runs
element.innerHTML = userComment;

The safer pattern — assign as text, so the browser never parses it as markup:

safe: the input is treated as text, not HTML
// GOOD — textContent writes a string; no markup is interpreted
element.textContent = userComment;

In the safe version the input lands on the page as the literal characters the user typed — angle brackets and all — instead of being parsed into tags and scripts. That single distinction, data vs. markup, is the whole fix.

#Defence in depth

Output encoding is the fix; these layers catch what slips through and shrink the damage when it does.

Layered defences against cross-site scripting
Encode on output first, then add framework auto-escaping, a strict CSP, sanitisation, and HttpOnly cookies.
  1. Context-aware output encoding — the core fix: escape for HTML, attribute, JS, or URL as appropriate.
  2. Framework auto-escaping — use templating that escapes by default; avoid innerHTML-style sinks.
  3. Content Security Policy (CSP) — a strict CSP blocks inline scripts and unknown origins, a strong second net even if a bug slips through.
  4. Sanitise rich HTML — if you must allow user HTML (a rich-text editor), run it through a vetted sanitiser like DOMPurify rather than trusting it.
  5. HttpOnly cookies — mark session cookies HttpOnly so JavaScript can't read them, blunting cookie theft specifically.

#Practise it safely

#Next steps

XSS and SQL injection are the two canonical injection bugs, and they teach the same durable lesson: keep untrusted data away from any interpreter that might execute it, and when you can't, encode it for its exact destination. If you've read both, you understand the mindset behind most of the OWASP Top 10. From here, deepen the layer underneath with Networking for Hacking, or set up the home lab where all of this becomes muscle memory.