为什么浏览器允许 CSRF?

Why do browsers allow CSRF?

我对网络安全还很陌生,当我阅读更多关于不同攻击媒介的信息时,我的想法让我难以置信,他们首先是被允许的。这就像网络设计时使用了损坏的安全模型并且容易受到攻击。

我也对大量模糊和不精确的信息感到惊讶。例如,起初单一来源政策听起来不错,然后我读到它只适用于 XHR,哦,顺便说一句,实际上并没有阻止 XHR cross-origin POST,这是经典的 CSRF 攻击。很高兴我继续阅读。

还有一个 Origin header 服务器可以使用它来确保请求来自正确的地方——但是哎呀,它在浏览器中的设置不一致,如果没有设置,您不能确定是因为 same-origin 请求,还是某些浏览器无法获取的请求类型(可能是 IMG 标签?)。继续阅读。

所以的方式好像是在session的cookie中设置了一个CSRF token,并且也把那个token加到forms/links,然后比较他们在服务器端提交。理论上(并且为了这个问题的目的让我们排除所有 XSS 攻击),来自另一个选项卡的 CSRF 尝试可能会向包含 cookie 的表单发出 POST 请求,但没有将表单输入元素设置为匹配token(因为无法从cookie中读取token),所以服务器会拒绝请求。工作但不顺畅,确保您永远不会忘记检查!

暂时记住这个想法,这是我的问题 -- 为什么浏览器会在来自非来源页面的请求中发送 session cookie饼干的?

我的意思是,浏览器会出于充分的理由拒绝 不同的域发送 cookie,但很乐意将它们从 不同的域发送起源?如果他们不这样做,东西会坏吗?它是否是针对 CSRF 的强大防御,只要求服务器做他们正在做的事情——检查有效的 session cookie?

编辑:也许这是改善情况的尝试? https://datatracker.ietf.org/doc/html/draft-west-origin-cookies-01

I am pretty new to web security, and as I read more about the different attack vectors, my mind boggles that they are allowed in the first place. It's like the web was designed with a broken security model and to be vulnerable.

都是真的。它从一开始就不是为了安全而设计的。 Web 最初设计为静态文档管理和共享系统,允许直接链接到不同机器上的资源。

您今天看到的动态网络 是一个大杂烩 。我们可以使用 CSRF 令牌、HTTP headers 等来修复它,但是如果您在不执行任何这些操作的情况下创建一个动态网站,那么它很可能会受到攻击(并让像我这样的人继续工作)。

查看 its history in the Wikipedia article

I am also amazed at the amount of vague and imprecise information. For example, at first the Single Origin Policy sounds pretty good, then I read that it only applies to XHR, and oh and by the way, doesn't actually prevent XHR cross-origin POST, which is the classic CSRF attack. Glad I kept reading.

也大抵如此。同源策略也适用于 windows 和框架(例如 example.com 不能通过 JavaScript 改变 example.org 的内容,如果 example.com 包含一个 IFrame 到 example.org).是的,可以创建 cross-domain XHR,但如果不启用 CORS,则无法读取响应。这确实可以防止 CSRF 令牌被盗,但正如您所说,如果您没有使用 CSRF 保护,那么这会带来 CSRF 漏洞。

其他防御措施如adding a custom header可用于缓解CSRF,因为自定义headers无法发送cross-domain。

XHR 过去无法访问任何内容 cross-domain,这被认为是一个太大的限制,因此出现了 CORS。以前,由于表单无论如何都可以访问不同的域,因此这并不被视为特别危险的操作。如果采取适当的控制措施,它仍然不是。

There is also an Origin header that the server can use to make sure the request is coming from the right place -- but oops, it is set inconsistently across browsers, and if it is NOT set, you can't be quite sure if it was because of a same-origin request, or a request type that just didn't get it for certain browsers (maybe an IMG tag?). Keep reading.

没错。参见 this answer

why does the browser send the session cookie in a request that originates from a page that is not the origin of the cookie?

因为否则很多东西都会坏掉。有无数的表单旨在从静态站点提交到进行 back-end 处理的动态站点。

有一个新的standard for "same-site" cookies. A less dry explanation is here

基本上可以使用新属性设置 cookie SameSite。在 strict 模式下,当站点不同时不发送 cookie。在 lax 模式下,只有在方法是例如POST,这是CSRF漏洞的主要所在。

你链接的那个是这个的早期草稿。