Cross Site Scripting DOM (XSS) 攻击jQuery append() 的处理方法_我要神龙摆尾
做安全红线使用 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').append(val); // console会打印出 cross site
});
</script>
修改方案为
<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之外,html、before、after等方法同样存在此跨站点攻击的问题。
二、传入参数特殊处理
????我们也可以将传入的参数进行特殊符号转化成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 cross-site-scripting (XSS) vulnerabilities. Remove or escape any user input before adding content to the document.
相关文章
- Gin 框架学习笔记(01)? 自定义结构体绑定表单、绑定URI、自定义log、自定义中间件、路由组、解析查询字符串、上传文件、使用HTTP方法_wohu1104的专栏
- 推荐一款好用的go web项目框架_陈董董_go前后端分离的web框架
- Java进阶之梯,成长路线与学习资料,助力突破中间件领域_中间件兴趣圈
- Redux专题-4.中间件-异步请求_小小前端_redux 异步中间件
- 面试题:微服务网关_dalianpai
- mysql proxy 优缺点_浅谈MySQL之ProxySQL_weixin_39641738
- java 统计qps_统计接口QPS_曹逆娘曹
- 二阶段、XA、三阶段、TCC分别是什么?它们之间的关系及区别_老板楼上雅座
- etcd 查看所有key_彻底搞懂 etcd 系列文章(五):etcdctl 的使用_杨仲慈
- 微服务技术栈(十) - MQ(RabbitMQ)_yirenyuan
- web中间件_晓翔仔_web中间件
- 性能知识点总结_vip_by
- p2p借贷项目面试题_JSon liu
- 实战,Spring Boot 整合 阿里开源中间件 Canal 实现数据增量同步_wdjnb
- RabbitMQ下载安装---最新版本--亲测可用_夏七丿_rabbitmq下载
- 尚硅谷ActiveMQ学习笔记(3)-- JMS规范和落地产品_exodus3