V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fstar
V2EX  ›  程序员

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

  •  
  •   fstar · 2022-04-30 20:37:46 +08:00 · 1985 次点击
    这是一个创建于 720 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    点击不少啊,回答却寥寥无几,还是离题的回答,说明这个问题确实比较偏门。
    看懂问题的不知道答案,看不懂的一顿乱答。
    kidonng
        5
    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)。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   916 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:43 · PVG 04:43 · LAX 13:43 · JFK 16:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.