Skip to content

QuizNot attempted

XSS

Which is the safest way to show a user-supplied comment?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • element.textContent = comment
  • textContent creates a text node and never parses HTML, so markup in the comment is shown literally.
  • Not innerHTML: parses the string, so <img onerror> payloads run.
  • Not document.write: writes raw HTML into the document (and blows it away after load).
  • Not outerHTML: parses HTML just like innerHTML.
  • If you must render HTML, sanitise it (DOMPurify) and add a CSP.