SSOGEN / Learn / Cross-site scripting (XSS)
Learn
Cross-site scripting (XSS)
Cross-site scripting (XSS) is a flaw that lets input reach the browser as code, so an attacker's script runs under your site's origin with your user's session.

A page given <img src=x onerror=...> as a value ran it and changed the tab title to XSS. That is cross-site scripting (XSS): input from a request, a database or a URL reaches the browser as code, so a stranger’s script runs under your site’s origin.
The script can read the page, send requests with the user’s session and change what the user sees.
OWASP files it under Injection in the OWASP Top 10, and MITRE ranks it first in its 2025 CWE Top 25. For a sign-in, it means someone else’s code can use the session after the password and second factor are done.
The same test string as reflected, stored and DOM-based XSS
This string is the only input on this page. It changes the tab title and nothing else.
<img src=x onerror="document.title='XSS'">| Type | How the input travels | Where it becomes code | What closes it |
|---|---|---|---|
Reflected |
Request URL, then the server, then straight back in the same response |
The server’s HTML template |
Encode the value for HTML on output |
Stored |
A form field, then the database, then a later page served to another user |
The same template, at render time |
The same encoding, applied at render, not at save |
DOM-based |
The URL fragment, read by a script in the browser; the fragment is never sent to the server |
An innerHTML assignment in client script |
textContent, or Trusted Types for sinks that must take HTML |
Every code pair below was run on 2026-11-17 in headless Chrome for Testing 153. Vulnerable lines changed the title to XSS; fixed lines printed plain text.
Reflected: the server echoes the request into the page
A results page that prints the query shows the pattern: whatever arrives in q comes back as markup. The attacker sends the victim a link carrying the string (OWASP).
const q = url.searchParams.get('q') ?? '';res.end(`<h1>Results for ${q}</h1>`);const escapeHtml = (s) => s .replace(/&/g, '&').replace(/</g, '<') .replace(/>/g, '>').replace(/"/g, '"') .replace(/'/g, ''');
res.end(`<h1>Results for ${escapeHtml(q)}</h1>`);The browser now receives <img src=x ...> and draws it as text. Django templates and React JSX encode embedded values this way by default (MDN).
Stored: one comment reaches every reader
Stored XSS needs no link. The input waits in the database and runs for each reader. MITRE’s example is an administrator opening a log view that holds a poisoned message.
- Submit. An attacker posts the test input as a comment or a display name.
- Store. The server saves the string unchanged. Encoding here is the wrong place: the same row may later feed a page, an email and an export.
- Render for someone else. Another user opens the thread. The template writes the saved value into the HTML:
<li><img src=x onerror="document.title='XSS'"></li>- Run. The image fails to load and the handler runs with that reader’s session. The fix is the same
escapeHtmlcall, made at render.
DOM-based: the server never sees the input
The fragment after # stays in the browser (MDN), so no server filter or log sees this route.
const name = new URLSearchParams(location.hash.slice(1)).get('name');greeting.innerHTML = 'Hello, ' + name;const name = new URLSearchParams(location.hash.slice(1)).get('name');greeting.textContent = 'Hello, ' + name;Where a feature needs HTML, Trusted Types forces every string through a policy you write. With the header set, a plain string assigned to innerHTML throws a TypeError. With DOMPurify in the policy, the handler is removed.
// Response header: Content-Security-Policy: require-trusted-types-for 'script'const policy = trustedTypes.createPolicy('greeting', { createHTML: (s) => DOMPurify.sanitize(s),});greeting.innerHTML = policy.createHTML('Hello, ' + name);Names to watch in DOM code:
| Kind | Names |
|---|---|
| Sources | The URL (location.search, location.hash) and document.cookie: anything the page did not create. |
| HTML sinks | innerHTML, outerHTML and document.write() parse a string as markup. |
| Code sinks | eval(), Function, and setTimeout() or setInterval() called with a string run it as script. |
The right encoding depends on where the data lands
Each context parses by different rules, so one encoder cannot serve them all.
| Where the data lands | What to do | Never put untrusted data in |
|---|---|---|
HTML body, <div>HERE</div> |
Entity-encode &, <, >, " and ', or set textContent |
A <script> block, an HTML comment or a <style> block |
| Quoted attribute | Quote the value and attribute-encode it; keep the attribute name fixed | Attribute names, event-handler attributes such as onclick, or srcdoc |
| JavaScript string | Insert only inside a quoted string, with an encoder made for that context | Code, identifiers or expressions; strings given to eval() or setTimeout() |
| CSS value | Call element.style.setProperty() with a fixed property name and an allowlisted value |
Selectors, property names or whole declaration blocks |
| URL | Percent-encode each parameter value with encodeURIComponent(); allow only http and https for a whole URL in href or src |
javascript: URLs |
What limits the damage when encoding is missed
OWASP names framework protections, output encoding and HTML sanitization as the best protection. Each layer below cuts the cost of a missed spot.
| Layer | What it does |
|---|---|
| Content Security Policy | When an injected script has no matching nonce or hash, then the browser refuses to run it. A strict policy also blocks inline event handlers, javascript: URLs and eval() (MDN). The W3C draft calls CSP defense in depth, not a first line of defense. |
| HttpOnly cookie | When script reads document.cookie, then the session cookie is not in it. The browser still attaches the cookie to requests the script sends with fetch(), so the script can act as the user (MDN). |
| Trusted Types | When the header Content-Security-Policy: require-trusted-types-for 'script' is set and code assigns a plain string to innerHTML, then the browser throws a TypeError. It sanitizes nothing itself; your policy does. Chrome and Edge 83, Safari 26.0 and Firefox 148 support it, and it has been Baseline newly available since 24 February 2026. |
What a successful XSS does to a sign-in
To the browser, script on your origin is your own code.
| Script on your origin can | What blocks it | What stays open |
|---|---|---|
Read the session cookie through document.cookie |
The HttpOnly attribute |
Requests sent by the script still carry the cookie |
Read a token kept in localStorage |
Keep session identifiers in an HttpOnly cookie, not in local storage (OWASP) |
The script can still send requests as the user, as in the next row |
| Send requests as the user | Encoding, so the script never runs; a strict CSP | SameSite does not apply, because the request comes from the site itself |
| Read a CSRF token from the page | Fixing the XSS itself | OWASP: XSS can defeat all CSRF mitigations, so tokens do not help once script runs |
The SSOGEN Web Agent sets its session cookie, __Host-SSOGEN-APP by default, as Secure, HttpOnly and SameSite=Lax, with no Domain attribute. The Web Agent configuration reference lists the directive. HttpOnly keeps page script from reading it.
Questions people ask
What is cross-site scripting with an example?
Cross-site scripting is input that a page outputs as code. A results page that writes the query into an h1 without encoding is the classic case. A request carrying <img src=x onerror="document.title='XSS'"> makes the browser run the handler under the site’s origin. Encoding the value turns the same string into visible text.
Is XSS still common?
Yes. CWE-79, cross-site scripting, ranks first in MITRE’s 2025 CWE Top 25, ahead of SQL injection at second. OWASP’s Top 10:2025 places it in A05 Injection, which fell from third to fifth, and counts more than 30,000 CVEs for CWE-79.
What is XSS vs CSRF?
XSS runs an attacker’s script in the victim’s browser; CSRF makes the browser send a request the user did not intend. CSRF depends on the cookie the browser attaches to a signed-in user’s requests. XSS can read the page, including CSRF tokens, so OWASP warns that it can defeat every CSRF mitigation.
What are the 3 most common types of cross-site scripting attacks?
Reflected, stored and DOM-based. Reflected XSS returns the request’s input in the immediate response. Stored XSS saves the input and serves it to later readers. DOM-based XSS happens in the browser, where client script writes untrusted data into the page. The three-route table shows one input taking all three.
Next, see session hijacking for what a stolen session buys and JSESSIONID for cookie flags in Tomcat and Spring Boot. Every explainer: Learn hub.
Sources, all read 2026-11-17:
- OWASP, Cross Site Scripting
- XSS Prevention Cheat Sheet
- DOM based XSS Prevention Cheat Sheet
- CSRF Prevention Cheat Sheet
- Session Management Cheat Sheet
- OWASP Top 10:2025, A05 Injection
- MITRE CWE-79
- 2025 CWE Top 25
- MDN, Cross-site scripting
- MDN, Set-Cookie
- MDN, URI fragment
- W3C, Content Security Policy Level 3
- Firefox 148 release notes
- WebKit, Safari 26.0 features
- web-features, Trusted Types
Updated