Skip to curriculum
Jump to the editor

18.6. Choose safe DOM output for untrusted text

Easy
Topics
15–20 min

Most cross-site scripting bugs are not missing sanitizers: they are a line like item.innerHTML = "<b>" + comment.author + "</b>". The moment a user names themselves <img src=x onerror=…>, every visitor runs their code. The fix is to choose the right way to output text: as text, never as markup. And when a string of HTML truly cannot be avoided (a template sent by email, a server-side string), escape every value that goes into it.

Write two functions in comments.js: escapeHtml(value) and renderComment(list, comment).

Signature

JAVASCRIPT
function escapeHtml(value)            // returns a string
function renderComment(list, comment) // returns the new <li>

comment is untrusted: { author, text, website }, where website is optional. renderComment appends this to list:

HTML
<li class="comment">
  <a class="author" href="https://…" rel="ugc nofollow noopener">the author</a>
  <p class="text">the text</p>
</li>

When the website is not safe to link, the author is a <span class="author"> with no href.

Requirements

  1. escapeHtml replaces &, <, >, " and ' with &amp;, &lt;, &gt;, &quot; and &#39;, each exactly once (&lt; becomes &amp;lt;), and leaves every other character as it is.
  2. escapeHtml converts any value with String().
  3. Escaped text placed in element content, in a double-quoted attribute or in a single-quoted attribute runs nothing, and reads back as the original text when it has no carriage return (\r) or NUL (\u0000) character. Those two are the exception because HTML parsing changes them when they appear as they are (\r and \r\n become \n; NUL is removed or replaced), and escapeHtml leaves them as they are.
  4. renderComment appends one li.comment to the end of list (keeping what is there) and returns it.
  5. The author and the text appear exactly as written, markup characters, spaces, line breaks, carriage returns and NUL characters included; non-strings go through String().
  6. No element is created from the comment's content: the item only ever holds the author element and the p.
  7. A website that is a string, parses with new URL(website) (no base) and has the protocol http: or https: makes the author an a with href set to the parsed URL (url.href) and rel="ugc nofollow noopener".
  8. Any other website (javascript: in any spelling, data:, protocol-relative, relative, empty, missing, not a string) gives a span and no href anywhere.
  9. renderComment never parses HTML: no innerHTML, outerHTML, insertAdjacentHTML, DOMParser or the like.

Examples

JAVASCRIPT
escapeHtml('<a href="x">Tom & Jerry</a>');
// '&lt;a href=&quot;x&quot;&gt;Tom &amp; Jerry&lt;/a&gt;'
escapeHtml("&lt;");   // '&amp;lt;'
JAVASCRIPT
renderComment(list, { author: "<img src=x onerror=steal()>", text: "Nice post" });
// <li class="comment"><span class="author">&lt;img src=x onerror=steal()&gt;</span><p class="text">Nice post</p></li>
// The visitor sees the author's name as literal text.
JAVASCRIPT
renderComment(list, { author: "Ada", text: "Hi", website: "javascript:steal()" });
// <span class="author">Ada</span>: no link
renderComment(list, { author: "Ada", text: "Hi", website: "https://ada.example" });
// <a class="author" href="https://ada.example/" rel="ugc nofollow noopener">Ada</a>

Environment

  • Plain JavaScript in a browser page; the tests call the globals escapeHtml and renderComment from comments.js, with lists they create. Payloads call window.__xss() if they ever run.
  • A test replaces innerHTML, outerHTML, insertAdjacentHTML, DOMParser and similar HTML parsers with functions that throw while renderComment runs.
  • Out of scope: escaping for JavaScript strings, CSS or URLs (each has its own rules), and formatting such as links inside the text.

Hints

  1. If & is escaped last, what happens to the &lt; you produced a moment earlier?
  2. Why is a link to javascript:… dangerous even though no HTML is parsed?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…