【发布时间】:2013-01-25 12:09:20
【问题描述】:
问题确实来自这个问题:
Why does the browser modify the ID of an HTML element that contains &#x?
给定以下网页:
<html>
<head>
<script type="text/javascript">
// --------------------------------------------------------
// could calling this method produce an XSS attack?
// --------------------------------------------------------
function decodeEntity(text){
text = text.replace(/<(.*?)>/g,''); // strip out all HTML tags, to prevent possible XSS
var div = document.createElement('div');
div.innerHTML = text;
return div.textContent?div.textContent:div.innerText;
}
function echoValue(){
var e = document.getElementById(decodeEntity("/path/$whatever"));
if(e) {
alert(e.innerHTML);
}
else {
alert("not found\n");
}
}
</script>
</head>
<body>
<p id="/path/$whatever">The Value</p>
<button onclick="echoValue()">Tell me</button>
</body>
</html>
<p> 元素的id 包含为防止 XSS 攻击而转义的字符。 HTML 部分和 JS 部分由服务器生成,服务器在这两个部分上插入相同的转义值(可能来自不安全的源)。
服务器以&#x 格式转义以下字符范围:
- 0x00 – 0x2D
- 0x3A – 0x40
- 0x5B – 0x5E
- 0x60
- 0x7B – 0xFF
- 0x0100 – 0xFFFF
换句话说:没有转义的唯一字符是:
- 0x2E – 0x39(
.、/、0123456789) - 0x41 – 0x5A (
A–Z) - 0x5F (
_) - 0x61 – 0x7A (
a–z)
现在,我必须通过 javascript 访问 <p>。引用问题中的函数echoValue() 总是失败,因为浏览器在HTML 部分将&#x24; 转换为$,但在JS 部分将其保留为&#x24;。
我担心的是,当使用引用答案中提供的decodeEntity() 函数时,通过转义动态字符串消除的 XSS 攻击的可能性会再次出现。
谁能指出是否存在安全问题(which?)或没有(为什么不?)?
【问题讨论】:
-
只要让服务器不在脚本内转义即可。
标签: javascript html xss