Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 2 · Lesson 9 · 3 min read

XSS Explained: Stored, Reflected and DOM-Based

On this page
  1. What is cross-site scripting (XSS)?
  2. Stored vs reflected vs DOM-based XSS
  3. XSS demo: innerHTML vs textContent vs DOMPurify vs CSP
  4. How to prevent XSS
  5. Check yourself

What is cross-site scripting (XSS)?

If an attacker can get JavaScript to run on your site, it runs with the victim's session: it can read the page, make requests as the user, change their settings, or capture what they type. That's cross-site scripting (XSS).

Stored vs reflected vs DOM-based XSS

TypeWhere the payload livesExample
StoredSaved in your database, shown to other usersA comment containing a script, viewed by everyone who opens the post
ReflectedIn the request, echoed back immediatelyA search page printing ?q= unescaped; the victim clicks a crafted link
DOM-basedNever reaches the server; client-side code writes it into the pageJavaScript copying location.hash into innerHTML

All three come from the same mistake: untrusted data inserted into a page as HTML or script.

XSS demo: innerHTML vs textContent vs DOMPurify vs CSP

A page greets the user with a name taken from an untrusted source. The name is:

Code
<img src=x onerror="window.stolen = document.cookie">Ana

Run in real headless Chrome (through Playwright), with a session=abc123 cookie set by the page:

Code
el.innerHTML = 'Hello, ' + attack + '!';                     // 1. HTML parsing
el.textContent = 'Hello, ' + attack + '!';                   // 2. plain text
el.innerHTML = DOMPurify.sanitize('Hello, ' + attack + '!'); // 3. sanitized HTML
// 4. innerHTML again, but the page sends a strict Content-Security-Policy
Config
mode=innerHTML         stolen: session=abc123
mode=textContent       stolen: (none)
mode=sanitized         stolen: (none)
mode=innerHTML&csp=2   stolen: (none)   console: Executing inline event handler violates the following Content Security Policy directive...
  1. innerHTML parses the string as HTML. The image fails to load, onerror fires, and the script reads the cookie. A real attack would send it to the attacker's server.
  2. textContent treats it as text: the user sees the literal characters <img ...>, and nothing runs.
  3. DOMPurify keeps safe HTML and strips the event handler: the result was Hello, <img src="x">Ana!.
  4. A Content Security Policy without 'unsafe-inline' makes the browser refuse to run the inline handler, even though the vulnerable code still executed.

Marking the session cookie HttpOnly (Day 4) would have hidden it from document.cookie, but the script could still act as the user.

Untrusted data must be encoded for its context or sanitized before it reaches the page; a strict CSP is a second layer that blocks injected scripts that slip through.
Untrusted data must be encoded for its context or sanitized before it reaches the page; a strict CSP is a second layer that blocks injected scripts that slip through.

How to prevent XSS

  1. Use frameworks that escape by default. React, Vue, Angular and server templates (Jinja, Razor, Thymeleaf) encode output automatically. Most XSS today comes through their escape hatches: dangerouslySetInnerHTML, v-html, [innerHTML], |safe.
  2. Encode for the context. HTML text, HTML attributes, JavaScript strings, URLs and CSS each need different encoding. Never insert untrusted data into <script> blocks or event-handler attributes. "Encoding" means rewriting the characters that have special meaning in that place, so they're shown instead of obeyed:
Where the data goesDangerous characterBecomes
HTML text<&lt;
HTML attribute (quoted)"&quot;
JavaScript string' and <\', \u003c
URL query valuespace, &, /%20, %26, %2F

Your template engine applies the right one automatically in normal output, which is why rule 1 comes first.

  1. Prefer safe DOM APIs: textContent, setAttribute on safe attributes, createElement.
  2. Sanitize when HTML is required (rich text, Markdown output) with DOMPurify or an equivalent, never with homemade regex.
  3. Validate URLs you put in href and src: allow only https: (and maybe mailto:), since javascript:alert(1) is a valid link.
  4. Add a strict CSP (Day 25) and HttpOnly cookies as layers that limit damage.
  5. Trusted Types (supported in Chromium-based browsers) make DOM sinks like innerHTML reject plain strings, catching DOM XSS at the source.

Check yourself

Stored XSS is saved on the server and served to other users; DOM-based XSS happens entirely in client-side JavaScript that writes untrusted data into the page.

It inserts the string as text, so the browser displays the characters <script> instead of parsing them as HTML, and nothing executes. Framework templates that escape by default work the same way.

CSP is a second layer that can be weakened or bypassed; the injection point is still a bug that may enable other attacks, such as injected HTML or phishing content.

When you must render user-supplied HTML, such as rich text comments. It removes dangerous elements and attributes while keeping safe formatting.

dangerouslySetInnerHTML, along with user-controlled javascript: URLs in href or src. Both bypass React's default escaping, so any user content passed to them must be sanitized or validated first.