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
- Trace model output to every sink: HTML rendering, SQL, shell, filenames, URLs — each gets its sink-specific defence.
- Default to rendering model text as text; add rich formatting only where it is a deliberate feature.
- Where HTML is required, sanitise after markdown conversion with DOMPurify and a strict allow-list, and review that config as a security control.
- 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.
- 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
- OWASP Top 10 for Large Language Model Applications, the OWASP GenAI Security Project's risk list for LLM and agent systems: owasp.org, 2025
- OWASP Top 10, the OWASP Foundation's standard awareness list of the most critical web application security risks: owasp.org, 2021
- CWE-89: Improper Neutralization of Special Elements used in an SQL Command, MITRE's weakness entry for SQL injection and its mitigations: cwe.mitre.org, 2026
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
- RAG Poisoning: How One Planted Document Rewrites Your Chatbot's Answers
- Production Source Maps: You Are Probably Shipping Your Original Source Code
- Secrets in Dockerfiles: Why ARG and ENV Bake Keys Into Your Image Forever
- Race Conditions in Checkout and Balance Code: The Double-Spend Your Tests Never Catch
- Web Cache Poisoning and Deception: Making Your CDN Serve the Attack to Everyone
- DOM Clobbering: How a Form Field Named 'config' Silently Replaces Your JavaScript Objects
- 7 Security Mistakes AI Coders Keep Making
- Supply-Chain Attacks in the AI-Coding Era
- Secret Hygiene: Stop API Key Leaks
- What Code Scanners Can't Find
- Bolt.new Pre-Deploy Security Checklist
- Cursor-Generated Code Review Checklist
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.