
// TL;DR
Cross-Site Scripting (XSS) is one of the most common web vulnerabilities on the internet. It lets an attacker get their own JavaScript running in another user’s browser, in the context of a trusted site — which means stolen sessions, hijacked accounts, defaced pages, and worse. This piece explains how XSS works, the three types you’ll encounter, what an attacker can actually do with it, and why input filters alone are not a real defence.
What is XSS?
Cross-Site Scripting (XSS) is a web application vulnerability that allows an attacker to run their own client-side scripts — usually JavaScript — inside web pages viewed by other users.
The root cause is simple: the application takes input from a user and includes it in a page sent to other users without properly neutralising it. If that input can contain script, the script runs. And because it runs inside the trusted site’s origin, the browser treats it as if the site itself wrote it — giving the attacker access to the victim’s session, cookies, and the page’s full behaviour.
A simple example

Imagine a site has an XSS flaw in a feature that reflects your input back to you. An attacker crafts a link containing a malicious script and tricks a victim into visiting it. When the victim’s browser loads the page, the site echoes the attacker’s script into the response, and the browser — trusting the page — executes it.
From there the script might silently read the victim’s session cookie and send it to the attacker, or redirect them to a convincing fake login page. With a stolen session, the attacker can act as the victim: read their data, change their settings, even take over the account. The victim sees a normal, trusted site the whole time.
How XSS actually happens
Every XSS bug comes down to the same pattern — untrusted input reaching an output context without proper encoding:
- The application accepts input somewhere: a search box, a username, a comment field, a URL parameter.
- It places that input into an HTML page, a script block, an attribute, or the DOM.
- It fails to encode or sanitise the input for that specific context.
- The attacker’s input is interpreted as code rather than data, and it executes.
The crucial insight is that safety depends on context. The same string can be harmless in one place and executable in another — which is exactly why naive filtering so often fails (more on that below).
Types of XSS
XSS is usually divided into three categories:
Stored (Persistent) XSS
The malicious input is saved on the server — in a database, a forum post, a profile field — and served to everyone who views the affected page. One injected comment on a support forum, for example, can execute in the browser of every user who reads it. This is the most dangerous type, because it needs no per-victim trickery: the payload simply waits for visitors.
Reflected (Non-Persistent) XSS
The malicious input is not stored; it’s sent in a request (often a URL parameter) and immediately reflected back in the response. It commonly appears in search results and error messages that echo user input. Because it isn’t stored, the attacker has to deliver it per-victim — typically by tricking the target into clicking a specially crafted link.
DOM-based XSS
Here the vulnerability lives entirely in the client-side JavaScript. The page’s own scripts take attacker-controllable data (from the URL, for instance) and write it into the DOM unsafely, so the injection never needs to touch the server at all.
What an attacker can do
XSS is often underestimated as “just a popup.” In reality, running arbitrary script in a victim’s session enables:
- Session hijacking — stealing session tokens to impersonate the user.
- Credential and data theft — capturing anything the page can see, including personal and financial data.
- Account takeover — performing actions as the victim, up to changing their password.
- Website defacement — altering what the page displays.
- Malware delivery and phishing — redirecting users or serving malicious content from a trusted origin.
- Worming — in stored cases, payloads that spread from user to user.
Why filters aren’t enough
A common first instinct is to block “dangerous” words or characters — strip <script>, escape quotes, blacklist the word alert. This feels like a fix, but blacklist filtering is notoriously leaky, and attackers have a long history of slipping past it:
- Encoding the payload (HTML entities, hex,
String.fromCharCode, URL-encoding) so the forbidden characters never appear literally. - Case and obfuscation tricks against filters that only match exact keywords.
- Breaking out of the surrounding context — closing an attribute or tag so a payload that looked contained suddenly isn’t.
- Abusing the filter itself, where stripping a substring from the middle of the input reassembles into a valid payload.
The lesson from decades of these bypasses is blunt: you cannot reliably blacklist your way out of XSS. Filters and WAFs add friction, not safety. If a site relies on them alone, it’s a matter of time.
How to actually prevent XSS
Real defence is about handling data correctly by default, not guessing which inputs are evil:
- Context-aware output encoding. Encode data for the exact place it’s going — HTML body, attribute, JavaScript, URL. This is the single most important control.
- Treat input as data, never code. Use frameworks and templating engines that escape output automatically, and avoid dangerous sinks like
innerHTMLandeval. - Validate input where you can (allow-lists beat block-lists), as defence in depth — not as the primary control.
- Content Security Policy (CSP). A strong CSP limits what scripts can run, blunting the impact even if an injection slips through.
- HttpOnly cookies. Keeping session cookies out of JavaScript’s reach cuts off the most common theft path.
- Get it tested. Filters alone won’t protect you — have the application pentested, or test it yourself with the attacker’s mindset.
Conclusion
XSS endures because it’s easy to introduce and easy to underestimate. The vulnerability isn’t really about <script> tags — it’s about the boundary between data and code blurring whenever untrusted input lands in a page unencoded. Fix that boundary properly, and the whole class of attacks loses its footing.
Thanks for reading — hope it was useful. Happy hacking!
Leave a Reply