js 前端处理xss攻击,对危险的字符串进行过滤 |
您所在的位置:网站首页 › 前端防范xss › js 前端处理xss攻击,对危险的字符串进行过滤 |
es6: npm install xss --save 这里测试使用的是es5,下载链接: 预防xss攻击,过滤标签.js_js过滤xss攻击-互联网文档类资源-CSDN下载 https://raw.githubusercontent.com/leizongmin/js-xss/master/dist/xss.js (过滤的比较严重,将html的所有属性都给过滤了) 调用函数: function filterXSS(string) html: Documentjs: //如果出现在标签属性中会被过滤掉,出现在文本中不会被过滤 //所有事件:error|mousewheel|mouseover|click|load|onload|submit|focus|blur|start //onclick console.log(1,filterXSS('click me')); console.log(2,filterXSS('onclick')); console.log(3,filterXSS('onclick')); //script console.log(4,filterXSS('console.log("8");')); console.log(5,filterXSS(';script;console.log("8");;/script;')); $("#box").html(filterXSS('console.log("8");'));//测试一下被转义成为了html实体,能否被输出 $("#box2").html(filterXSS(';script;console.log("8");;/script;')); //onerror console.log(6,filterXSS(''));//图片的onerror被过滤掉 $("#box3").html(filterXSS('')); //onblur console.log(7,filterXSS('onclick')); //javascript console.log(8,filterXSS('')); var a=filterXSS(""); console.log(9,a); $("#box4").html(a);页面输出: 控制台输出: |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |