【问题标题】:Script injection affects jquery template脚本注入影响 jquery 模板
【发布时间】:2015-10-15 07:33:41
【问题描述】:

我尝试在 jquery 模板中使用 {{ html }} 加载字符串。 但是脚本注入会影响 {{html }}。 如何限制脚本注入。

【问题讨论】:

标签: jquery jquery-templates


【解决方案1】:

问题不在于 jQuery 模板,而是因为 html 是这样解析字符串文字中的 &lt;script&gt; 元素的。见Why split the <script> tag when writing it with document.write()?

如果你想解析和执行脚本,那么一个简单的方法是将脚本移动到一个单独的文件中(并将该文件包含在 html 页面中)而不是内联它。

或者你可以像这样逃避它

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>

【讨论】:

  • 还有其他方法可以限制这种情况吗?
  • @user3312945 我不知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 2016-08-10
相关资源
最近更新 更多