温馨提示×

前端如何防止xss攻击

小新
372
2020-12-21 12:50:50
栏目: 网络安全

前端如何防止xss攻击

前端防止xss攻击的方法:

过滤非法字符,例如:

// 过滤XSS反射型漏洞

filterInputTxt: function (html) {

html = html.replace(/(.*<[^>]+>.*)/g,""); // HTML标记

html = html.replace(/([\r\n])[\s]+/g, ""); // 换行、空格

html = html.replace(//g, ""); // HTML注释

html = html.replace(/['"‘’“”!@#$%^&*{}!¥()()×+=]/g, ""); // 非法字符

html = html.replace("alert","");

html = html.replace("eval","");

html = html.replace(/(.*javascript.*)/gi,"");

if (html === "") {

html = "你好";

}

return html;

}

0