Why is innerHTML dangerous?
How innerHTML differs from textContent, what XSS is, and why frameworks escape everything by default.
innerHTML — one line, so handy! Then a certain Oscar left a "review", and after that every visitor got a popup saying "Oscar was here". Today it's a popup. Tomorrow it's a stolen login.An element has two ways to "take some text":
- textContent takes the string as text. Any
<and>stay characters on screen; - innerHTML hands the string to the HTML parser. Everything that looks like a tag becomes a real element — with attributes and handlers.
const comment = "<b>Really</b> tasty!";
document.querySelector("#html").innerHTML = comment;
document.querySelector("#text").textContent = comment;
console.log(document.querySelector("#html").children.length);
console.log(document.querySelector("#text").children.length);
console.log(document.querySelector("#html").textContent);<b> element, the second got plain characters.While you write the string yourself, there's barely any difference. Trouble starts when the string comes from outside: from a form, from the page address, from a server. Then the string's author decides which elements appear on your page. For example, an image with a non-existent address and an onerror attribute: the image fails to load, the browser calls onerror — and runs someone else's code.
const reviews = [
"Best latte in town!",
'<img src="nope.png" onerror="alert(\'Oscar was here\')">',
];
const list = document.querySelector("#reviews");
for (const text of reviews) {
list.innerHTML += `<li>${text}</li>`;
}
console.log(list.querySelectorAll("img").length);alert fires (in the sandbox it's printed to the console). The review became code.This is XSS (cross-site scripting) — one of the most common vulnerabilities on the web. Someone else's code runs inside your site, with the visitor's rights: it reads what the site keeps in localStorage, sends requests on the visitor's behalf, swaps in a fake login form. And if the review is saved on the server, the attack fires for everyone who opens the page.
By the way, a <script> inserted through innerHTML is not run by the browser — that was done on purpose. But the protection leaks: there are dozens of handlers like onerror and onload, so you can't rely on it.
How to protect yourself:
- show user data through textContent (or createElement + textContent) — then a string can't turn into markup;
- if you really do need to build HTML, escape the data:
<→<,&→&and so on. Then the parser shows characters instead of creating tags.
function escapeHtml(text) {
return text
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
console.log(escapeHtml('<img src="x" onerror="alert(1)">'));const reviews = [
"Best latte in town!",
'<img src="nope.png" onerror="alert(\'Oscar was here\')">',
];
const list = document.querySelector("#reviews");
for (const text of reviews) {
const li = document.createElement("li");
li.textContent = text;
list.append(li);
}
console.log(list.querySelectorAll("img").length);textContent: Oscar shows up on the page as text — and runs nothing.That's exactly why frameworks escape everything by default. In React, {comment} in markup always becomes text. You can insert raw HTML, but you have to write a property with the telling name dangerouslySetInnerHTML. In Vue it's v-html, with a warning in the docs. The safe path was made short, the dangerous one long and scary. That's good design: a mistake that's easy to make should be inconvenient.
innerHTML only for markup I typed with my own paws, without a single variable inside. Everything that comes from people goes through textContent. Oscar still writes reviews. Now they just get read.Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.