Since there are too many HTML tags to blacklist, we use a smart white list approach, in which we allow only b, i, and br.

HTML
CSS
JavaScript
<center>
<input type="text" id="input"/>
<div id="screen"></div>
</center>

document.querySelector('#input').addEventListener('keyup', function(e) {
  if (e.keyCode === 13) {
    let tagString = document.querySelector('#input').value;

    // allow only `b`, `i`, and `br`
    tagString = tagString.replace(/<(?!\s*b|i|br\s*\/?)[^>]+>/gi, '');

    const range = document.createRange();
    range.selectNode(document.getElementsByClassName('applet').item(0));
    const documentFragment = range.createContextualFragment(tagString);
    document.querySelector('#screen').appendChild(documentFragment);
  }
});
#input {
  font-size: 16px;
  height: 26px;
  width: 100%;
  margin-bottom: 10px;
  padding: 20px;
}
.applet-html {
  padding: 20px;
}
.applet {
  margin-bottom: 20px;
}

Takeaways and notes#

  • LOL
  • Eg: <Img/SrC/onerror=alert('LOL')>
  • Event listeners makes XSS possible for innocuous looking tags
  • Eg: <b onmouseover=alert(1)>click me!</b>