问题确实出现在这个问题上:
为什么浏览器会修改包含的HTML元素的ID?
鉴于以下网页:
<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>
Run Code Online (Sandbox Code Playgroud)
在id该的<p>元素包含以防止XSS攻击了逃脱字符.HTML部分和JS部分由服务器生成,服务器在两个部分上插入相同的转义值(可能来自不安全的源).
服务器以下列&#x格式转义以下字符范围: …