18.6. Choose safe DOM output for untrusted text
Topics
This question is in
18. URLs, storage and safe HTMLMost 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
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:
<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
escapeHtmlreplaces&,<,>,"and'with&,<,>,"and', each exactly once (<becomes&lt;), and leaves every other character as it is.escapeHtmlconverts any value withString().- 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 (\rand\r\nbecome\n; NUL is removed or replaced), andescapeHtmlleaves them as they are. renderCommentappends oneli.commentto the end oflist(keeping what is there) and returns it.- 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(). - No element is created from the comment's content: the item only ever holds the author element and the
p. - A
websitethat is a string, parses withnew URL(website)(no base) and has the protocolhttp:orhttps:makes the author anawithhrefset to the parsed URL (url.href) andrel="ugc nofollow noopener". - Any other
website(javascript:in any spelling,data:, protocol-relative, relative, empty, missing, not a string) gives aspanand nohrefanywhere. renderCommentnever parses HTML: noinnerHTML,outerHTML,insertAdjacentHTML,DOMParseror the like.
Examples
escapeHtml('<a href="x">Tom & Jerry</a>');
// '<a href="x">Tom & Jerry</a>'
escapeHtml("<"); // '&lt;'renderComment(list, { author: "<img src=x onerror=steal()>", text: "Nice post" });
// <li class="comment"><span class="author"><img src=x onerror=steal()></span><p class="text">Nice post</p></li>
// The visitor sees the author's name as literal text.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
escapeHtmlandrenderCommentfromcomments.js, with lists they create. Payloads callwindow.__xss()if they ever run. - A test replaces
innerHTML,outerHTML,insertAdjacentHTML,DOMParserand similar HTML parsers with functions that throw whilerenderCommentruns. - Out of scope: escaping for JavaScript strings, CSS or URLs (each has its own rules), and formatting such as links inside the text.
Hints
- If
&is escaped last, what happens to the<you produced a moment earlier? - 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