【发布时间】: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