leizongmin / js-xss

Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist
http://jsxss.com
Other
5.19k stars 630 forks source link

MSO tags will be escaped #196

Open Alvis-Li opened 4 years ago

Alvis-Li commented 4 years ago

<!--[if !mso]><!--> Code <!--<![endif]--> is going to be escaped as <!--[if !mso]><!--> Code &lt;!--<![endif]-->

@leizongmin Any Suggestions? Or how to avoid it.

andrey-skl commented 2 years ago

Same for me, I could not make this markup to be escaped properly:

<!--[if !mso]><!-->
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!--<![endif]-->
leizongmin commented 2 years ago

@andrey-skl What do you expect to get from this input html?

andrey-skl commented 2 years ago

@leizongmin Thanks for your response!

Given that my options are, say:

{ 
  allowCommentTag: true,
  whiteList: {} // say nothing is allowed
}

I would expect it to preserve these comments as is:

<!--[if !mso]><!-->

<!--<![endif]-->

But actual result is:

<!--[if !mso]><!-->

&lt;!--<![endif]-->

While debugging the library, I found that the reason is that the last <!--<![endif]--> is not parsed as a single tag because it has "<" symbol inside, and it triggers new tag parsing here https://github.com/leizongmin/js-xss/blob/master/lib/parser.js#L63

Just for the reference, here are all such tags that are often used for emails markup https://stackoverflow.design/email/base/mso/

leizongmin commented 2 years ago

@andrey-skl Did you mean that you expect when setting allowCommentTag=true, the content between <!-- and --> is not processed?

andrey-skl commented 2 years ago

@leizongmin in my case, I would like comment tags to stay as is. Sorry, I forgot to mention a "hack" that makes it work like that for me:

  onIgnoreTag: (tag, html, options) => {
    if (tag.startsWith('!--') || tag.startsWith('![endif')) {
      return html;
    }
    return '';
  },

If we drop this hack it renders

  &lt;!--[if !mso]&gt;&lt;!--&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"/&gt;
  &lt;!--&lt;![endif]--&gt;

Which maybe is correct, but not what I need.

If you think this is not possible to preserve these comments tags, it's okay