小编dag*_*han的帖子

用于 Javascript 的 XSS 安全 html 解码

我需要在 javascript 中解码 html。例如:

var str = 'apple & banana';
var strDecoded = htmlDecode(str); // I expect 'apple & banana'
Run Code Online (Sandbox Code Playgroud)

不能保证给定的 str 已经被编码并且常见的 jquery 和 DOM 技巧容易受到 XSS 攻击:

var attackStr = '&amp;</textarea><img src=x onerror=alert(1)>&#x30cf;&#x30ed;&#x30fc;&#x30ef;&#x30fc;&#x30eb;&#x30c9;'; // if you see 1 alerted, it means it is XSS vulnerable
var strDecoded; // I wish to get: &</textarea><img src=x onerror=alert(1)>???????

strDecoded = $('<div/>').html(attackStr).text(); // vulnerable in all browsers

strDecoded = $('<textarea/>').html(attackStr).text(); // vulnerable in ie 9 and firefox


var dv = document.createElement('div');
dv.innerHTML …
Run Code Online (Sandbox Code Playgroud)

html javascript security xss jquery

3
推荐指数
1
解决办法
6566
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1

security ×1

xss ×1