安全红线使用
Fortify工具进行扫描时,
jquery append会报
Cross Site Scripting DOM风险解决问题有两种办法。

    一、原生dom方式

    使用JavaScript原生dom替换append方法,原生dom忽略<script>标签。比如,下列代码就会报Cross Site Scripting DOM攻击问题

修改方案

<div id="jqueryid">
</div>
<script>
$(document).ready(function(){
  var val = "<script>console.log('cross site');"
  $('#jqueryid')[0].innerHTML = val; // console不再会打印cross site
});
</script>

在jQuery方法中,初append之外,htmlbeforeafter等方法同样存在此跨站点攻击问题

二、传入参数特殊处理

    我们可以将传入的参数进行特殊符号转化成html方式处理

代码如下

<div id="jqueryid">
</div>
<script>
$(document).ready(function(){
  var script = "<script>console.log('cross site');"
  $('#jqueryid').append(encodeHtml(script)); // console不会打印cross site
   
  function encodeHtml(value){
    return $('<div/>').text(value).html();
  }
});
</script>

其实在jQuery的官方文档中,存在如下说明。正是解决站点攻击的方法所在。

This method uses the browser’s innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document. For example, Internet Explorer sometimes leaves off the quotes around attribute values if they contain only alphanumeric characters.

Additional Notes: By design, any jQuery constructor or method that accepts an HTML string — jQuery(), .append(), .after(), etc. — can potentially execute code. This can occur by injection of script tags or use of HTML attributes that execute code (for example, <img οnlοad=””>). Do not use these methods to insert strings obtained from untrusted sources such as URL query parameters, cookies, or form inputs. Doing so can introduce crosssite-scripting (XSS) vulnerabilities. Remove or escape any user input before adding content to the document. 

原文地址:https://blog.csdn.net/u013247068/article/details/126299634

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_28712.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注