gpt4 book ai didi

http - 当 HTTP 服务器接收到 "Origin: null" header 时,它应该做什么? (CORS)

转载 作者:可可西里 更新时间:2023-11-01 17:17:41 26 4
gpt4 key购买 nike

Origin header 的定义说:

origin-or-null  = origin / %s"null" ; case-sensitive

“null”是不是要像域名一样管理?换句话说,服务器可以在使用“null”时接受请求(至少在某些情况下)还是一直被认为是错误?

我在 Fetch Documentation 中寻找解释但到目前为止,我还没有找到这个具体问题的答案。

最佳答案

通过发送 Origin: null header ,浏览器表明请求来自 opaque origin。 .也就是说,浏览器向作为服务器维护者的您发出信号,表明请求不是从实际在 Web 上运行的应用程序以典型方式发起的,并且使用 Ajax 方法或 Fetch 或 XHR 来调用您的服务器——所以它可能不是您实际希望您的服务支持的用例。

因此,在响应Origin: null 请求时,您通常不想发送Access-Control-Allow-Origin 响应 header 。换句话说,您希望浏览器阻止任何前端 JavaScript 代码访问您为此类请求发回的响应。

虽然浏览器将 Origin header 设置为 null 的最常见情况可能是前端代码从某人的本地文件系统(从 file :// URL,而不是来自 Web 服务器)——在许多其他情况下,浏览器也会将 Origin header 设置为 null。有关详尽列表,请参阅 https://stackoverflow.com/a/42242802/441757 .

https://w3c.github.io/webappsec-cors-for-developers/#avoid-returning-access-control-allow-origin-null解释了您应该如何看待此类情况:

It may seem safe to return Access-Control-Allow-Origin: "null" , but the serialization of the Origin of any resource that uses a non-hierarchical scheme (such as data: or file:) and sandboxed documents is defined to be "null". Many User Agents will grant such documents access to a response with an Access-Control-Allow-Origin: "null" header, and any origin can create a hostile document with a "null" Origin. The "null" value for the ACAO header should therefore be avoided.

换句话说,如果您有意允许,您可能认为为 Origin: null 请求发回 Access-Control-Allow-Origin 会很有用来自服务器的响应将在某人的本地文件系统上运行的前端 JavaScript 代码中使用(例如,对于某人进行本地测试)。但是通过这样做,您不仅会允许本地文件系统的情况,还会允许 https://stackoverflow.com/a/42242802/441757 中描述的所有其他情况。 .全有或全无。

关于http - 当 HTTP 服务器接收到 "Origin: null" header 时,它应该做什么? (CORS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com