0

I tried to load string using {{ html }} in jquery template. But script injection affects {{html }}. How restrict script injection.

1 Answers1

0

It is not the problem is jQuery-template, it is because that is how html parses the <script> element within string literals. See Why split the <script> tag when writing it with document.write()?

If you want to parse and execute the script, then an easy way is to move the script to a separate file(and include that file in the html page) instead of inlining it.

Or you can escape it like

testTemplate = "<i>{{html txt}}</i>";
$.template('testTemplate', testTemplate);
$.tmpl("testTemplate", {
  txt: "<b>bold</b> and \x3Cscript>alert('abc')\x3C/script>"
}).appendTo("#target");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>
<ul id="target"></ul>
Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531