security
security_error
ai_generated
true
XSS: Unsafe use of innerHTML with user-controlled data
ID: security/xss-innerhtml
90%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| any | active | — | — | — |
Root Cause
Setting innerHTML with user input enables Cross-Site Scripting attacks.
genericWorkarounds
-
95% success Use textContent/innerText instead of innerHTML
element.textContent = userInput
-
90% success Use DOMPurify to sanitize HTML before insertion
element.innerHTML = DOMPurify.sanitize(userInput)
Dead Ends
Common approaches that don't work:
-
Sanitize with regex replace of script tags
85% fail
XSS payloads use img onerror, svg onload, and many other vectors beyond script tags.
-
Encode only < and > characters
80% fail
Misses attribute-context XSS, javascript: URLs, and event handlers.