StoicBuilder

HandbookIII the CastraCross-site scripting (XSS)

IIIthe Castra, Stay in control

Cross-site scripting (XSS)

1 min readUpdated 28 November

In plain words

Cross-site scripting happens when your app displays a user's text as HTML, so a script hidden in it runs in other visitors' browsers.

Where you see it

You ask the agent to let Chirp posts include bold text and links. It renders posts with dangerouslySetInnerHTML, and someone posts an image tag whose onerror script sends each reader's session token to their own server.

src/components/Post.tsxbrowser
// safe: React escapes the text<p>{post.text}</p> // unsafe: the text becomes real HTML<p dangerouslySetInnerHTML={{ __html: post.text }} />
React escapes text by default. dangerouslySetInnerHTML switches that off.

What goes wrong

  1. Rendering user content with dangerouslySetInnerHTML

    If posts need formatting, clean the HTML with a sanitiser like DOMPurify first, or use Markdown with raw HTML switched off.

  2. Building HTML strings by hand

    Code that glues user text into innerHTML skips React's protection entirely.

    el.innerHTML = "<b>" + comment + "</b>";
  3. Expecting the database to strip scripts

    A script tag sitting in Postgres harms nobody. The danger starts when a page renders it as HTML, so the protection belongs where you display it.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Find every place that renders user content as HTML, such as dangerouslySetInnerHTML, innerHTML or v-html, and switch it to plain text or sanitise it with DOMPurify. Then post <img src=x onerror=alert(1)> as a test and confirm it shows up as plain text.

Quick check

Sign in to earn +10 XP for it

You post <b>hi</b> on Chirp and it shows up in bold on the feed. What does that tell you?

Got a prompt that touches Cross-site scripting (XSS)?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.