XSS安全漏洞
通过URL带入的,这种带入主要是前端解析url中的参数,并对数参数执行了innerHTML 或者 html 或者 append 操作。在将参数html()或者append()到html文件中时,会执行其中的js代码,被错误用户获取到cookie等信息。
示例:
原始链接 : https://xx.xxx.com/efly.html?link=cc
被XSS注入以后的链接 : https://xx.xxx.com/efly.html?link=eeec<img src=1 οnerrοr=alert(document.cookie)>
解决
使用正则匹配去除某些字符串、过滤域名
function filterXss(str, regExp){
// let regex = /<(S*?)[^>]*>.*?|<.*? />/gi;
// 去除包含<>内容的,防止xss漏洞
let filterValue = str.replace(/<.*?>/g,'');
// 去除<开头类型的xss漏洞
filterValue = str.replace(/<+.*$/g,'');
if(regExp && !regExp.test(filterValue)){
filterValue = '';
}
return filterValue;
}