我们在用户会话开始时为 XSRF/CSRF 设置了 cookie。在某些时候,用户导航到不同的域(例如支付),执行一些操作,并导航到我们的域。返回我们的域后,Firefox 和 Safari 无法读取设置为相同站点的 cookie:严格,Chrome 可以。在 Chrome 和 Firefox(但不是 Safari)的情况下,它确实显示在 cookie 的开发人员工具部分下。
MDN 上的相同站点解释解释说,在未来的请求中,cookie 将在请求标头中一起发送。对于所有三种浏览器,情况都是如此。解释没有定论的是是否应该可以通过document.cookie读取这个cookie。对于 Firefox、Safari 和 Chrome,我们可以读取“Lax”cookie,但对于 Chrome,我们可以读取“Strict”cookie。在页面刷新时也是如此,但在打开新选项卡时(即仅通过导航)则不然。
这是 Safari 和 Firefox 或 Chrome 中的错误 - 还是规范不确定?规格(w3?)是什么?
可以使用带有两个虚拟主机的网络服务器轻松地在本地重新创建它,test.internalsite.com
并且test.externalsite.com
这些页面带有一些 PHP:
<?php
setcookie("CSRFLax", "hiLax", array("path"=>"/", "samesite"=>"Lax", "domain"=>"test.internalsite.com"));
setcookie("CSRFStrict", "hiStrict", array("path"=>"/", "samesite"=>"Strict", "domain"=>"test.internalsite.com"));
?>
<html>
<body>External site
<p><a href="http://test.externalsite.com">Go to External site</a></p>
<p>Document cookie: <script>document.write(document.cookie);</script></p>
</body>
</html>
和
<html>
<body>External site
<a href="http://test.internalsite.com">Go to internal Site</a>
</body>
</html>