gpt4 book ai didi

http-headers - Origin header 的存在是否意味着 CORS 请求

转载 作者:行者123 更新时间:2023-12-05 03:09:43 24 4
gpt4 key购买 nike

假设带有 Origin header 的 HTTP 请求是 CORS 请求是否安全?

如果不是,区分 CORS 请求和源自外部服务器上的 PHP 应用程序的常规 HTTP 请求的正确方法是什么?

最佳答案

如果“CORS 请求”是指跨域请求——即使用 the CORS protocol 的请求—那么不,假设带有 Origin header 的请求是跨域请求是不安全的。

这是因为,除了要求浏览器在所有使用 CORS 协议(protocol)的跨源请求中发送一个 Origin 外,the Fetch spec also requires browsers to send the Origin header for all requests whose method is neither GET nor HEAD :

If the CORS flag is set or httpRequest’s method is neither GET nor HEAD, then append Origin/httpRequest’s origin, serialized and UTF-8 encoded, to httpRequest’s header list.

因此浏览器还必须为所有 POST 请求发送 Origin header 。

获取规范 further states :

A CORS request is an HTTP request that includes an Origin header. It cannot be reliably identified as participating in the CORS protocol as the Origin header is also included for all requests whose method is neither GET nor HEAD.

因此,规范实际上将 CORS 请求 定义为“任何具有 Origin header 的请求”——即使该请求不是跨源的并且所以不使用 the CORS protocol .

这似乎是一种奇怪的定义方式,但无论如何,重要的是要记住规范在其他任何地方提到 CORS 请求,它并不一定意味着跨源请求或甚至是“参与 CORS 协议(protocol)的请求”——因为根据规范中的上述定义,同源 POST 请求也是 CORS 请求

因此,服务器端无法可靠地将给定请求识别为参与 CORS 协议(protocol)。只有发送请求的浏览器知道——除了 Origin 之外,没有其他 header 我们可以假设浏览器将始终发送 CORS 协议(protocol)请求。

关于http-headers - Origin header 的存在是否意味着 CORS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41966610/

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