published fresh until

My agent put user text in innerHTML: how do I fix the XSS risk?

Summary Your React or JavaScript UI renders user text as HTML and may execute posted markup; render text plainly, or sanitize HTML before rendering. A reported scan found innerHTML with user data among recurring AI-code issues.

Not for JSON-LD script tags in Next.js; see nextjs-json-ld-structured-data-without-xss

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The symptom

You asked an agent for a comment box, chat, profile bio or markdown preview. It shows what users typed by assigning to element.innerHTML or using React's dangerouslySetInnerHTML. Anyone can then post markup that runs script in other users' browsers (cross-site scripting, XSS).

What the sources say

  • React's reference page for DOM components says dangerouslySetInnerHTML "should be used with extreme caution", and that unless the markup comes from a completely trusted source, it is trivial to introduce an XSS hole. Its demo is a post body containing an image tag with an onerror handler that runs code. The page says to use it only with trusted and sanitized data, and to build the { __html } object next to the code that generates the HTML, so the risky spot is obvious in review. It does not name a sanitizer library.
  • A maintainer of an open-source scanner posted on r/vibecoding (March 2026) that AI-written frontends often set .innerHTML with dynamic content instead of textContent or a sanitizer such as DOMPurify. One person's scan, not a measured rate.

What to do

1. Find the risky spots. Search for innerHTML, outerHTML, insertAdjacentHTML, document.write and dangerouslySetInnerHTML. Ask the agent to list each with the source of the data. 2. If the content is user text, stop using HTML: in React just render {text} (React escapes it); in plain JS use textContent. 3. If you really need HTML (rich text, rendered markdown), sanitize with a maintained library such as DOMPurify right before rendering, with a tight allow-list of tags. React's page notes that even its markdown example is safe only because the HTML is shown to the same user who wrote it and the parser is trusted. 4. Test it: post <img src=x onerror=alert(1)> as a comment and view the page as a second user. A popup means the hole is open. 5. Defense in depth: a Content-Security-Policy header without unsafe-inline for scripts limits what an injected tag can do. Add it after the fix, not instead of it.

Prompt starting point

"Find every use of innerHTML, insertAdjacentHTML and dangerouslySetInnerHTML. For each, say whether the data can contain user input. Replace with plain text rendering where possible; where HTML is needed, sanitize with DOMPurify using an allow-list. Add a test that renders an img onerror payload and asserts no script runs."

Common mistakes

  • Sanitizing on write only; sanitize on render, or both.
  • Stripping the word "script" with a regex.
  • Fixing the comment list but not the notification, profile or email template that shows the same text.

Caveats

I have not run these changes myself. Library choice and configuration (DOMPurify options) should be checked against its own README.

Sources

  • Common components, dangerouslySetInnerHTML (React docs), read 19 Oct 2026. Checked: "extreme caution", the sentence that unless markup comes from a completely trusted source it is trivial to introduce an XSS vulnerability, the onerror image demo, "only use with trusted and sanitized data", creating the { __html } object close to where the HTML is generated, the markdown example's note that it is safe only because the output is shown to the same user and the parser is trusted, and that the page names no sanitizer (it mentions Trusted Types).
  • Cross Site Scripting Prevention Cheat Sheet (OWASP), read 19 Oct 2026. Checked: refactor away unsafe sinks like innerHTML and use textContent or value; OWASP recommends DOMPurify for HTML sanitization; encode output close to where it is rendered; CSP as an additional layer, not the primary defense; filtering alone is insufficient (it points to the filter-evasion cheat sheet and does not say anything about "script" regexes specifically).
  • DOMPurify README (cure53), read 19 Oct 2026. Checked: it is a DOM-based XSS sanitizer; ALLOWED_TAGS limits output to the listed elements; modifying markup after sanitizing can undo it. Content Security Policy (MDN): avoid 'unsafe-inline'; a CSP is not an alternative to sanitizing and works as defense in depth.
  • Checked 2026-11-15: the r/vibecoding post; the claim that AI-written frontends often use innerHTML stays one maintainer's remark. I tested no code or payload; the <img src=x onerror=alert(1)> test is standard practice but I did not run it.

Started from a post by @DiscussionHealthy802 (r/vibecoding); OWASP, packaged by the editor. Original post

Subjects

More in code review

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# My agent put user text in innerHTML: how do I fix the XSS risk?
Summary: Your React or JavaScript UI renders user text as HTML and may execute posted markup; render text plainly, or sanitize HTML before rendering. A reported scan found innerHTML with user data among recurring AI-code issues.
Copy for agent
get the whatworks thread agent-used-innerhtml-or-dangerouslysetinnerhtml-with-user-text-fix-xss and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread agent-used-innerhtml-or-dangerouslysetinnerhtml-with-user-text-fix-xss
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks