vibe-audit.lab

guides · 2026-11-14

When LLM Output Is Rendered as HTML: The Stored XSS Nobody Escaped

Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team

The short answer

Model output is untrusted input with better grammar. If your UI renders it via innerHTML, dangerouslySetInnerHTML or a markdown-to-HTML pipeline without sanitisation, then anyone who can steer the model — including through prompt injection — can place script in your users' browsers. Render model text as text by default; where rich formatting is a feature, sanitise the HTML with DOMPurify and a strict allow-list.

Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.

The new XSS source in your codebase

Classic XSS thinking tracks user input to a rendering sink. AI features add a source developers instinctively trust: the model. But the model is a function of its inputs — user prompts, retrieved documents, tool results — so its output inherits all of their hostility and adds its own unpredictability. Vibe-Code Audit's LLM-output detector family watches model text flow into the classic sinks: HTML rendering (XSS), SQL construction (injection), and shell execution — because the vulnerability is the composition: an injectable model feeding an unescaped renderer is a stored XSS that writes its own payloads, personalised per victim.

The stored part matters: a chat transcript, summary or generated page is saved and shown to other users — support agents, teammates, customers — so one crafted conversation poisons every later viewer, with the payload executing in a more privileged person's browser. That is a worse shape than the reflected XSS most tutorials teach.

Markdown is not a sanitiser

The common pipeline — model writes markdown, a library converts to HTML, the result is injected — feels safe because markdown is 'just formatting'. But most converters pass raw HTML through by design, and link/image syntax alone enables javascript: URLs and tracking payloads. If the model emits a raw (because a retrieved document suggested it, or a user asked creatively), the pipeline delivers it intact. The rule: sanitise after conversion, with an allow-list sanitiser (DOMPurify) configured for the tags and attributes your feature actually needs — and treat 'the library probably escapes it' as an unverified assumption until you have fed it a payload and watched it die.

What is Insecure LLM output handling?

treating model-generated text as trusted markup or commands — rendering it as raw HTML, interpolating it into SQL, or passing it to a shell — instead of as untrusted data requiring the same escaping and validation as user input.

Safe rendering defaults

Default to text: textContent, framework-escaped interpolation, plain-text chat bubbles — most AI features lose nothing. Where formatting is the product, the pipeline is convert → sanitise → render, in that order, with the sanitiser's configuration reviewed like a security control. Apply the same suspicion beyond HTML: model output interpolated into SQL gets parameterised (the model suggests values, never query text); model output proposed as commands gets allow-listed and confirmed by a human or a policy layer; model-generated filenames and URLs get the traversal and redirect treatments from their own guides. One principle, many sinks: the model is an untrusted author.

Content Security Policy is your safety net here — a strict CSP blunts injected-script execution even when a payload slips through — which is why the headers guide and this one belong to the same rollout.

A test battery for AI rendering

Prompt your staging bot — directly and via a poisoned document — to emit: an img onerror payload, a javascript: link, a markdown image with an onerror attribute, an SVG payload, and a script tag. Every one must arrive in the DOM inert: visible as text or stripped by the sanitiser. Record the battery and re-run it when the model, the prompt, the markdown library or the sanitiser config changes; each of those has shipped an XSS regression somewhere before.

Worked example: BrightDesk, a team inbox with AI summaries

BrightDesk (fictional), summarising about 6,500 support emails a day, rendered summaries through markdown into innerHTML so agents saw nicely formatted text. A scan's LLM-output detector flagged the flow from model text to HTML sink. The team proved it with a test email whose body coaxed the summariser into emitting an image tag with an onerror handler — it executed in the agent view in staging. The pipeline changed to convert-then-sanitise with a tight DOMPurify profile (no event attributes, no javascript: URLs, a short tag allow-list), and the five-payload battery joined CI. Three months later a markdown-library upgrade briefly reopened raw-HTML passthrough; the battery caught it before release, which is what the battery is for.

Your five-action checklist

  1. Trace model output to every sink: HTML rendering, SQL, shell, filenames, URLs — each gets its sink-specific defence.
  2. Default to rendering model text as text; add rich formatting only where it is a deliberate feature.
  3. Where HTML is required, sanitise after markdown conversion with DOMPurify and a strict allow-list, and review that config as a security control.
  4. Build a payload battery (event handlers, javascript: URLs, SVG, raw script — direct and via poisoned content) and run it on every model or rendering change.
  5. Deploy a strict Content-Security-Policy as the net under the tightrope, and re-scan for LLM-output flows after adding any new AI feature.

Sources

Frequently asked questions

Our chatbot only talks to the user themselves. Is XSS still a risk?

Self-XSS is the floor, not the ceiling: transcripts get shared, support staff review them, summaries land in team tools. The moment model output is stored or shown to a second person, it is stored XSS with an AI author.

Does the model 'intend' to attack us?

Intent is irrelevant. The payload can come from a user's prompt, a retrieved document, or a tool result — the model is a conduit. You are not defending against the model; you are defending the channel it writes into.

Is escaping at the model layer (asking it not to emit HTML) a control?

No. Prompt requests are wishes. The control lives in your rendering pipeline: escaping, sanitisation and CSP — layers that hold regardless of what the model emits.

What about AI-generated code we paste into the app?

Same principle, human loop: generated code is untrusted authorship too — which is the premise of scanning it for innerHTML sinks and the other patterns in these guides before it ships.

Related guides

Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back