Type: lab writeup (built and run on a local box, no real vendor) Weakness: stored cross-site scripting, CWE-79 Severity: High, because the script runs in a moderator's session (reasoning below) PoC: harmless DOM marker only. No cookie or token access, nothing leaves the page.
The bug in this lab follows a familiar pattern: a rich-text field that the editor "sanitizes" in the browser, stored as raw HTML on the server, and rendered later with innerHTML in a screen that only privileged staff open. I rebuilt it as a small reporting portal, confirmed the script runs in a moderator view, then added the defences one at a time to see which one actually stops it.
What stored XSS is
PortSwigger's definition is the one I use: stored cross-site scripting (also second-order or persistent XSS) "arises when an application receives data from an untrusted source and includes that data within its later HTTP responses in an unsafe way." The payload waits. The person who submits it does not have to be present when it runs. That is why a low-privilege reporter can land a script in a high-privilege moderator queue.
How the lab is built
One Node HTTP server. Modes:
| Mode | On write | On render | Browser headers |
|---|---|---|---|
vuln | store as-is | innerHTML = description | none |
csp-nott | store as-is | same | CSP with a nonce, object-src 'none', base-uri 'none' |
csp-only | store as-is | same | those, plus require-trusted-types-for 'script' and trusted-types dompurify |
fixed | DOMPurify sanitize (tight allow-list) | DOMPurify again, returning a Trusted Type | same as csp-only |
Stack on the box: Node 20.19.2, Google Chrome 155.0.8059.84 headless, DOMPurify 3.4.17, jsdom 29.1.1. Runs below are from 18 October 2026, Asia/Colombo. They are box runs, not my own machine.
Recon and sink
Reporter flow: title (plain), category (enum), description (rich HTML from a WYSIWYG). Staff open /moderation, which fetches /api/reports/0 and mounts the description into a div.
Sink, in the weak build:
el.innerHTML = r.description;
MDN lists Element.innerHTML as an HTML injection sink: the browser parses the string as HTML. The Trusted Types API puts it in the same class. The title field is fine; it never reaches that sink.
I confirmed the path with a unique marker string first (it came back raw in the API JSON and as HTML in the moderation view), then with a harmless HTML probe (emphasis rendered), then with the PoC below. The product mistake is treating the editor's client-side "sanitize" as the control. The server trusted the HTML it received.
Harmless PoC
The payload is a broken image tag whose onerror handler sets document.body.dataset.andraxLab to the string fired. Written out in prose so this page stays free of raw angle brackets: an img element with src set to x and onerror set to that assignment. It does not read cookies, call any API or send anything off-origin. (The same PoC string lives in the lab's payload.json on the box if you want the literal markup.)
Reproduction (lab only):
- POST the report as a low-privilege reporter.
- Open
/moderationas staff, or in a second browser profile. - Check for
document.body.dataset.andraxLab === 'fired'.
Do not point this at any system you do not own or that has not invited XSS testing.
What ran on the box
Four builds, same payload, same Chrome:
MODE stored csp_header marker status img_in_report
vuln img+onerror kept 0 fired rendered yes
csp-nott img+onerror kept 1 absent rendered yes
csp-only img+onerror kept 1 absent blocked: TypeError no
fixed img stripped 1 absent rendered no
Read of that table:
- vuln. The marker fires. Stored XSS confirmed.
- csp-nott. A nonce-based CSP without
'unsafe-inline'blocks inline event handler attributes, which is why the marker did not fire. It is still not a fix: MDN's CSP guide is explicit that a CSP is "not an alternative to sanitizing input." The img and itsonerrorare still stored and still in the DOM, so one policy change or one page served without the header and it runs again. That is not a fix you can ship. - csp-only. Enforcing Trusted Types rejects a raw string into
innerHTMLwith aTypeError. The report area stays empty. Good as a tripwire; bad as the only control, because the dangerous HTML is still on disk for the next consumer that forgets the policy. - fixed. DOMPurify on write drops the img. The moderation view gets a TrustedHTML value and renders the remaining paragraph. Marker absent, status
rendered.
What the API stored
The POST body for the description is the HTML string the editor produced. In vuln, csp-nott and csp-only the API echoed that string back untouched, including the img and its onerror. In fixed, the response's stored field was only the remaining paragraph: the dangerous tag never reached the database. That difference is the whole point of sanitizing on write. A later code path that forgets Trusted Types still has nothing dangerous to render.
Re-verify after the fix
After each change I re-ran the same four curls and the same headless Chrome dump. The acceptance criteria for "fixed" are: the stored description no longer contains onerror, the moderation page's status is rendered, the marker is absent, and the report div contains no img. Anything short of that is not a fix, even if an alert no longer pops.
Impact, and why High
PortSwigger's impact note for stored XSS is blunt: if an attacker can control a script in the victim's browser, they can typically fully compromise that user, and because the exploit is already inside the app, they do not need an external lure. In this lab the viewer is a moderator. The weak build also kept the staff JWT in localStorage, so any script that ran could read it. That combination is account takeover of anyone who opens the queue. Severity is High for that reason. If the same sink only hit the reporter's own "My reports" page, Medium would fit; the audience of the sink sets the severity, not the tag name.
Even with HttpOnly cookies (which, per MDN, stop JavaScript from reading the cookie via document.cookie but still send it on script-driven fetch), a script in the moderator origin can call moderator APIs as the moderator. Cookie flags alone do not erase stored XSS.
The fix that held
I treat the controls as a stack, not a menu.
1. Prefer encoding; sanitize when HTML is required
The OWASP XSS Prevention Cheat Sheet points modern frameworks at templating and auto-escaping, and for JavaScript sinks recommends assigning untrusted text to textContent rather than innerHTML. When users legitimately need rich text, the same cheat sheet has a dedicated HTML Sanitization section and recommends DOMPurify:
let clean = DOMPurify.sanitize(dirty);
ASVS 5.0 agrees: 1.2.1 (output encoding for the HTML context) and 1.3.1 (untrusted HTML from WYSIWYG editors sanitized with a well-known library). In the lab the allow-list on write is p, br, ul, ol, li, strong, em, and a with href only. Anything else is dropped before it is stored.
2. Sanitize on the server, not only in the editor
DOMPurify also runs under Node with jsdom (its own docs cover that path). The editor's client-side pass is UX. The server's pass is policy. Do both, and do not re-mutate the string after it has been sanitized.
3. Content-Security-Policy as defence in depth
ASVS 3.4.3 asks for a CSP that includes at least object-src 'none' and base-uri 'none', and that defines an allow-list or uses nonces or hashes. OWASP's cheat sheet treats CSP as defence in depth, not the primary control, and MDN says the same. The lab's CSP is:
script-src 'nonce-…'; object-src 'none'; base-uri 'none';
require-trusted-types-for 'script'; trusted-types dompurify
4. Trusted Types so a string cannot reach the sink
With require-trusted-types-for 'script', assigning a plain string to innerHTML throws. DOMPurify's RETURN_TRUSTED_TYPE: true option returns a TrustedHTML value the sink will accept. That is what the fixed render path uses. ASVS 3.2.2 covers the complementary rule: text that should display as text goes through textContent or createTextNode, never through an HTML parser.
5. Harden the moderator session
Prefer server-side sessions with HttpOnly, Secure and a sensible SameSite over a JWT in localStorage. If rich preview of untrusted HTML is mandatory, isolate it in a sandboxed iframe with its own CSP. Add a CI corpus of payloads that must not survive a round-trip to the moderation view.
Lessons
- Stored XSS is about who views the sink, not who wrote the field.
- A WYSIWYG editor is not a security control.
- A CSP without Trusted Types still left the img in the DOM in this lab; treat CSP as a backstop.
- Encoding plus sanitization plus CSP plus Trusted Types beat any one of them alone.
- Write the surviving vector down. "XSS works" without the sink and the payload class wastes the fixer's time.
Responsible practice
Reproduce only in labs you own or programs that invite XSS testing. Use markers that prove the bug without moving data. If you find the same pattern on a real program, follow that program's disclosure rules. This page is not a template for unauthorized testing.
