【问题标题】:Why cookie is sent in HTTP post when xhr.withCredentials = true;当 xhr.withCredentials = true 时,为什么在 HTTP post 中发送 cookie;
【发布时间】:2020-09-19 08:35:04
【问题描述】:

如果您在 JS 代码下运行,它将使用 cookie 向服务器发出 HTTP POST 请求,但 POST 响应被浏览器阻止,因为来自 yahoo.com 的响应中没有 Access-Control-Allow-Credentials: true 标头。

我的问题是,为什么浏览器允许带有 cookie 的 POST 请求并且不进行预检,因为 xhr.withCredentials=true 会发送 cookie?

对服务器的损坏已经完成,即使浏览器不允许 JS 访问 POST 请求的响应?

这看起来像 CSRF 漏洞。

我在这里遗漏了什么吗?

更新:这是需要 CSRF 令牌的情况吗?

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { 
                console.log(xhr.responseText);
            } else {
                console.log("Request was unsuccessful: " + xhr.status);
            }
        }
    };
    xhr.withCredentials = true;
    xhr.open("post", "https://yahoo.com/example", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhr.send("a=5");
<!DOCTYPE html>
<html>

<body>


<p>Hello</p>




</body>
</html>

【问题讨论】:

    标签: javascript xmlhttprequest cors


    【解决方案1】:

    只需提交一个表单,就可以向任何 URL 发出带有 application/x-www-form-urlencoded 数据的 POST 请求。

    XHR 和 co 不会改变这一点。

    在那里增加额外的安全性是没有意义的,因为那匹马已经狂奔了。

    所以,是的,存在 CSRF 风险,但不是风险。网站需要采取the usual defenses against CSRF

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2020-05-21
      相关资源
      最近更新 更多