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.
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.
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.
All XSS shares that root cause. The types differ by where the malicious script comes from and how it reaches the victim.
| Type | Where the payload lives | Impact |
|---|---|---|
| Reflected | In a crafted link or form the victim is tricked into using | Runs once, for whoever clicks |
| Stored | Saved on the server (a comment, profile, message) | Hits every visitor of that page — the worst |
| DOM-based | Introduced entirely by client-side JavaScript | Server may never even see the payload |
Stored XSS shows why this vulnerability is rated so seriously. One injected script, saved once, runs in every visitor's browser.
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:
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):
// 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:
// 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.
Output encoding is the fix; these layers catch what slips through and shrink the damage when it does.
innerHTML-style sinks.HttpOnly so JavaScript can't read them, blunting cookie theft specifically.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.