innerHTML blocks script tags, but a broken image still runs code

innerHTML does not run script tags, but it runs code from other places, like onerror on an image. If the text comes from a user and you want to show it as text, use textContent.

Published 3 min read#App security#Web development

You might think the danger in innerHTML is the <script> tag, so you block it and relax. The browser does not even run a <script> that comes in through innerHTML, and the malicious code walks in through another door: an image with a broken link. This article shows how that happens, and which line in your code closes it.

What is XSS, and what does innerHTML have to do with it?

XSS (Cross-Site Scripting) is a flaw where an attacker can run their own JavaScript on your site's pages, in other visitors' browsers. innerHTML is a property that takes a string, parses it as HTML, and builds elements from it inside the page.

The innerHTML page on MDN says this property is "probably the most common vector for cross-site scripting (XSS) attacks", because it puts strings provided by a user into the page before they are sanitized.

Why isn't blocking <script> enough?

Blocking <script> is not enough because innerHTML does not run it anyway, and the code arrives by other ways. According to the same MDN page, the property prevents <script> elements from executing, but stays open to many other ways an attacker can craft HTML that runs JavaScript.

The mistake is the same in every project: text from the user, such as a name or a comment, gets joined into HTML and put on the page:

const name = new URLSearchParams(location.search).get('name');
greeting.innerHTML = 'Hello ' + name;

The developer sees plain text, and the browser sees HTML it has to build.

How does a broken image run code?

A broken image runs code because the browser tries to load it, fails, and runs the code written in its onerror handler. This is the example on the MDN page itself:

const name = "<img src='x' onerror='alert(1)'>";
el.innerHTML = name; // shows the alert

The src here is x, not a valid image URL, so loading fails and the browser fires an error event. There is no <script> in the string, and the code ran. In place of alert(1) there can be any JavaScript the attacker picks.

How do you close it with one line?

If the text should come out as text, use textContent instead of innerHTML, so the browser never parses it as HTML. MDN says it in a note on the same page: "Node.textContent should be used when you know that the user-provided content should be plain text".

The same example after the fix:

const name = new URLSearchParams(location.search).get('name');
greeting.textContent = 'Hello ' + (name ?? '');

Now if someone sends the image tag as their name, it is written on the screen as characters, and nothing loads or runs.

What if you have to show HTML?

If your feature has to show HTML, like a text editor or formatted comments, the string has to go through a sanitizing function before it reaches the page. MDN suggests a way to force this on all your code:

  1. Give innerHTML TrustedHTML objects instead of plain strings.

  2. Turn on the require-trusted-types-for directive in your CSP, so the browser rejects any plain string that reaches innerHTML.

This way every string has to pass through a transformation function, which is your chance to remove the dangerous parts before it is inserted. And if you want to start with CSP without breaking your site, we wrote about rolling out CSP in report-only mode first.

Where do you start now?

The lesson for you as a front-end developer: text that comes from a user stays text, so put it in textContent, because innerHTML gives it a chance to become code.

  1. Search your project for every innerHTML, outerHTML and insertAdjacentHTML.

  2. For each one ask: does the string contain anything from the user or from the page URL?

  3. If yes and you need text, replace it with textContent.

  4. If you need HTML, put a sanitizing function on the string, and look at Trusted Types.

XSS shows up in places you do not expect, like the translation flaw in vue-i18n. All our app security articles are in one place: app security articles. And if you want someone to look for these doors on your platform before someone else finds them, that is what Muhassan does.

How many innerHTML calls in your project take text from a user?

FAQ

If I strip the word script from the text, is it safe?

No. MDN says innerHTML already does not run script tags, and the danger comes from other ways, such as an image with onerror. Filtering one word does not close those ways.

What is the difference between textContent and innerHTML?

innerHTML parses the text as HTML and builds elements from it. textContent puts the text in as it is, so any tag shows up as characters on the screen and never becomes an element.

What if I have to show HTML that comes from a user?

Pass it through a sanitizing function before it reaches the page. MDN suggests assigning TrustedHTML objects to innerHTML instead of strings, and enforcing that with the require-trusted-types-for CSP directive.

Share

More articles