Skip to main content
HighAI codeOWASP A03:2021CWE-79

How to fix: XSS Risk via innerHTML Assignment

innerHTML assignment was detected with what may be user-controlled data. Direct innerHTML assignment is a primary XSS vector: AI-generated frontend code frequently uses it to render dynamic content.

Why it matters

Assigning user-controlled HTML to innerHTML allows attackers to inject arbitrary JavaScript that executes in the victim's browser, enabling session hijacking, keylogging, and credential theft.

AI-generated frontend code frequently produces patterns like `element.innerHTML = userData` or `element.innerHTML = `<p>${serverData}</p>``. These are safe only if the value is completely static: any user-controlled content makes them XSS vectors.

How to fix it

  1. 1Use textContent instead of innerHTML when setting plain text.
  2. 2If HTML rendering is required, sanitize with DOMPurify before assignment: element.innerHTML = DOMPurify.sanitize(html).
  3. 3Use a Content Security Policy that blocks inline scripts as a defense-in-depth measure.
  4. 4In React, avoid dangerouslySetInnerHTML; if needed, always sanitize with DOMPurify first.

Code examples

DOMPurify sanitization

JavaScript
import DOMPurify from 'dompurify';

// Bad (AI-generated)
element.innerHTML = userContent;

// Good: sanitize first
element.innerHTML = DOMPurify.sanitize(userContent, {
  ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a'],
  ALLOWED_ATTR: ['href'],
});

// Best: use textContent for plain text
element.textContent = userContent;

References

Detected automatically by VulnRadar. The check id is vibe-xss-via-innerhtml, stable across every scan so you can filter or diff on it.

See every ai code check

Questions

How severe is XSS Risk via innerHTML Assignment?
VulnRadar rates this high severity in the ai code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id vibe-xss-via-innerhtml, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does XSS Risk via innerHTML Assignment map to?
It maps to CWE-79. In the OWASP Top 10 it falls under A03:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for xss risk via innerhtml assignment

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: XSS Risk via innerHTML Assignment | VulnRadar