相关疑难解决方法(0)

此代码是否容易受到XSS攻击?

问题确实出现在这个问题上:
为什么浏览器会修改包含&#x的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/&#x24;whatever"));
        if(e) {
          alert(e.innerHTML);
        }
        else {
          alert("not found\n");
        }
      }
    </script>
  </head>
  <body>
    <p id="/path/&#x24;whatever">The Value</p>
    <button onclick="echoValue()">Tell me</button>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

id该的<p>元素包含以防止XSS攻击了逃脱字符.HTML部分和JS部分由服务器生成,服务器在两个部分上插入相同的转义值(可能来自不安全的源).

服务器以下列&#x格式转义以下字符范围: …

html javascript xss

6
推荐指数
1
解决办法
1705
查看次数

标签 统计

html ×1

javascript ×1

xss ×1