为什么非跨域请求时 HTTP 请求头有时候也会带上 Origin 头字段?

2022-04-30 20:37:46 +08:00
 fstar

比如同源的 POST 请求,会带上 Origin 。这个是为了解决什么问题,明明不是跨域请求。

2003 次点击
所在节点    程序员
5 条回复
richangfan
2022-04-30 20:45:09 +08:00
不带上 Origin 怎么知道不是跨域请求
iqoo
2022-04-30 22:53:13 +08:00
tedding
2022-05-01 00:31:13 +08:00
昨天刚学的 词 RTFM STFW……欢迎使用 MDN
fstar
2022-05-01 13:57:35 +08:00
发现一些回答都没认真看问题,就回答了(当然我有时候也会犯这个问题)。
以为我没看文档,然后叫我去看文档。

同源请求在非 GET 和 OPTION 的时候会带上 Origin ,这我知道。但我无法理解这个设计,因为没有带上 Origin 的必要。

点击不少啊,回答却寥寥无几,还是离题的回答,说明这个问题确实比较偏门。
看懂问题的不知道答案,看不懂的一顿乱答。
kidonng
2022-05-01 21:37:47 +08:00
一点想法,不一定对:

Q:为什么要有 `Origin`?
A: https://fetch.spec.whatwg.org/#origin-header
> The `Origin` header is a version of the `Referer` [sic] header that does not reveal a path.
通过不同 Referrer Policy 可以精准控制 `Origin` 和 `Referrer` 的发送

Q:为什么要有 `Referer`?
A: https://httpwg.org/specs/rfc7231.html#header.referer
> The "Referer" [sic] header field allows the user agent to specify a URI reference for the resource from which the target URI was obtained (i.e., the "referrer", though the field name is misspelled).
`Referer`(以及 `Origin`)作为重要的来源信息是默认发送的,此时尚不考虑跨域。

“非跨域请求时 HTTP 请求头有时候也会带上 Origin 头字段”实际上是默认行为,应该问的是“为什么有的时候不会带 Origin/Referer”,对此问题不同上下文有不同回答(见 https://www.w3.org/TR/referrer-policy/ 里“Integration with Fetch/HTML/CSS”),一句话概括大概就是“except in cases where the origin information would be sensitive or unnecessary.” ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin#description)。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/850254

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX