网页防止xss攻击的方法:
实现过滤器对特殊字符进行转义过滤,例如:
function filter(xss) {
var whiteList = ['h1', 'h2']; // 白名单
var translateMap = { '<': '<', '>': '>' };
return xss.replace(/<\/?(.*?)>/g, function(str, $1, index, origin) {
console.log($1);
if (whiteList.indexOf($1) >= 0) {
return str;
}
return str.replace(/[<>]/g, function(str) {
return translateMap[str];
});
});
}
var search = location.search;
var query = search.slice(1);
var params = query.split('&').map(function(str) {
var list = str.split('=');
var key = list[0];
var val = list[1];
return { key: decodeURIComponent(key), val: decodeURIComponent(val) };
});
console.log(params);
var xss;
params.some(function(item) {
xss = item.val;
return true;
});
console.log(xss);
console.log(filter(xss));
document.open();
document.write(xss);
document.write(filter(xss));
document.close();
//eg http://127.0.0.1:8080/?xss=<script>alert(1)</script><h1><h2>1233</h2></h1>
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 美国服务器中有哪些XSS类型攻击