HTTP的Referrer和Referrer Policy介绍

HTTP Referrer是浏览器发送请求时携带的来源页面信息,用于分析流量来源,但也可能泄露用户敏感信息。Referrer Policy是一组策略,如NoReferrer、SameOrigin等,用来控制何时以及如何发送Referer头部,以平衡隐私与分析需求。这些策略在不同场景下有不同的应用,例如限制跨域信息传递或在HTTPS降级时避免信息泄露。
摘要由CSDN通过智能技术生成

本文链接:https://blog.csdn.net/dujie1219/article/details/111273925

Referrer

referrer是HTTP请求header的报文头,用于指明当前流量的来源参考页面。通过这个信息,我们可以知道访客是怎么来到当前页面的。这对于Web Analytics非常重要,可以用于分析不同渠道流量分布、用户搜索的关键词等。
但是,这个字段同时会造成用户敏感信息泄漏(如:带有敏感信息的重置密码URL,若被Web Analytics收集,则存在密码被重置的危险)。

Referrer 还是 Referer?

Referer是上世纪 90 年代由Philip Hallam-Baker提出来的,当时他将这个请求头叫做Referer,并最终被写进了HTTP/1.0协议(RFC1945):

The Referer request-header field allows the client to specify, for the server's benefit, the address (URI) of the resource from which the Request-URI was obtained.

Referer实际上是Referrer的误写,但是这个错误被发现之前,已经被大量使用,如果要改过来需要所有服务端、客户端的一致配合,还有大量的代码需要排查修改。于是,HTTP 的标准制定者们决定将错就错,不改了。

以下是HTTP/1.1协议(RFC2616):

The Referer[sic] request-header field allows the client to specify, for the server's benefit, the address (URI) of the resource from which the Request-URI was obtained (the "referrer", although the header field is misspelled.)

可以看出,和 HTTP/1.0相比,HTTP/1.1 除了加上了对这个错误的说明之外,没有其他变化。

而在响应header “Referrer-Policy:” 中没有继续沿用之前的错误拼写(不能总惯着他)。

Referrer Policy

一个referrer policy头可以是以下值:"no-referrer", "no-referrer-when-downgrade", "same-origin", "origin", "strict-origin", "origin-when-cross-origin", "strict-origin-when-cross-origin", or "unsafe-url". 另外一个空串""也是被允许的,意思和没有这个头部一致。

  • No Referrer

是最简单的一种策略,在任何情况下都发送包含Referer的头部

  • No Referrer When Downgrade

在同等安全等级下(例如https页面请求https地址),发送包含Referer的头部,但当请求方低于发送方(例如https页面请求http地址),发送不包含Referer的头部

举例:

https://example.com/page.html导航到https://not.example.com/时需要发送包含Referer的头部,Referer的值为https://example.com/page.html

当从同一页面导航到 http://not.example.com/ 时发送没有Referer的头部 

  • Same Origin

当Origin相同是发送包含Referer(包含完整的URL信息) 的头部

举例:

https://example.com/page.html导航到https://example.com/not-page.html时需要发送包含Referer的头部,Referer的值为https://example.com/page.html

当从同一页面导航到 https://not.example.com/ 时发送没有Referer的头部

  • Origin

发送只包含protocal+host的Referer

举例:

https://example.com/page.html导航到任何URL都需要发送包含Referer的头部,但是Referer值为https://example.com/ (注意:为了保证Referer是一个有效的URL,最后的“/”不能丢)

  • Strict Origin

当协议等级相同时(HTTPS->HTTPS, HTTP->HTTP)或者由低级别到高级别时(HTTP->HTTPS)发送只包含protocal+host的Referer;

当有高级别到底级别时(HTTPS->HTTP),发送不包含Referer的头部

举例:

https://example.com/page.html导航到https://not.example.com 时,发送只包含protocal+host的Referer(https://example.com/),导航到http://not.example.com时则不发送Referer信息

http://example.com/page.html导航到http://not.example.com 或者https://example.com需要发送只包含protocal+host的Referer(http://example.com/

  • Origin When Cross Origin

当发生跨域访问的时候发送只包含protocal+host的Referer(同Origin),否则发送包含完整URL信息的Referer

注意:协议升级或者降级也是被视为跨域访问(HTTP->HTTPS, HTTPS->HTTP),但是协议降级时会造成HTTPS的源通过未加密的HTTP请求泄露,通过Strict Origin When Cross Origin解决了这个问题

举例:

https://example.com/page.html导航到https://example.com/not-page.html时发送包含完整URL信息的Referer(https://example.com/page.html),

从同以页面导航到https://not.example.com/ 时发送只包含protocal+host的Referer(https://example.com/

  • Strict Origin When Cross Origin

当发生跨域访问的时候发送只包含protocal+host的Referer(同Origin),否则发送包含完整URL信息的Referer,但当有高级别到底级别时(HTTPS->HTTP),发送不包含Referer的头部

举例:

https://example.com/page.html 导航到https://example.com/not-page.html时,相同域,发送包含完整URL信息的Referer(https://example.com/page.html

从同一页面导航到https://not.example.com/时,发生了跨域,发送只包含protocal+host的Referer(https://example.com/

从同一页面导航到http://not.example.com/,不发送Referer信息(HTTPS-HTTP)

  • Unsafe URL

任何情况下都发送包含Referer(包含完整的URL信息) 的头部

参考:https://www.w3.org/TR/referrer-policy/

如果对你有帮助记得点赞哦!

转载请注明出处:https://blog.csdn.net/dujie1219/article/details/111273925

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值