A closing script tag, even placed inside quotes, even inside <![CDATA section, will close a <script> tag:
<script type="text/javascript">
var = "</script>"; // this closes the script tag
</script>
The js escaper should be able to catch this.
Looking at how others do to escape this sort of things, I found multiple solutions :
- Change
</script> into '</sc' + 'ript>' : not suitable for a generic escaper (i.e. depends on single or double quotes)
- Escape '/'. Changing
</script> into <\/script> seems to successfully avoid the problem. I read that json encoders escape the '/' for this reason, but I'm unsure.
- Use a white list of safe chars and escape everything others to \xHEX and \uUNICODE representation. This is totally safe, this can even escape html special chars and make escape('js') safe for html too (useful when doing things like onclick="fun({{somevar}})"). However this depends on the encoding (the escaper should be encoding aware anyway).
A closing script tag, even placed inside quotes, even inside
<![CDATAsection, will close a<script>tag:The js escaper should be able to catch this.
Looking at how others do to escape this sort of things, I found multiple solutions :
</script>into'</sc' + 'ript>': not suitable for a generic escaper (i.e. depends on single or double quotes)</script>into<\/script>seems to successfully avoid the problem. I read that json encoders escape the '/' for this reason, but I'm unsure.