innerHTML
The DOM property that reads or replaces an element's contents as an HTML string, which the browser parses into nodes.
Assigning untrusted strings to innerHTML is a classic way to create cross-site scripting holes.
In more detail
It is convenient for building markup, but assigning untrusted text to innerHTML lets attackers inject elements that run script. Use textContent for text, and Trusted Types or sanitization when HTML is really needed.
Related terms
- textContent — The DOM property that reads or replaces all the text inside a node, treating anything you assign as plain text rather than markup.
- cross-site scripting — An attack in which someone gets their own script to run inside your page, where it can read data, act as the user or steal tokens.
- injection sink — A place in code where a string is interpreted as HTML or code, such as innerHTML, document.write or eval, and so can run an attack if the string is untrusted.
- Trusted Types — A browser security feature that makes dangerous DOM sinks such as innerHTML reject plain strings and accept only values produced by a policy you define.
Taught in
- Semantic HTML Elements: Landmarks And Structure — Frontend Architecture & Web Platform
- DOM Manipulation: DocumentFragment And HTML Templates — Frontend Architecture & Web Platform
- Content Security Policy (CSP), Trusted Types And XSS Defenses — Frontend Architecture & Web Platform