Referrer Policy

文章讲述了HTTP头中的Referrer的作用,如何用于WebAnalytics,以及其可能带来的用户隐私风险。介绍了ReferrerPolicy的不同指令值,以控制Referrer的发送策略,确保安全和隐私保护。
摘要由CSDN通过智能技术生成

Referrer

referrerHTTP请求header的报文头,用于指明当前流量的来源参考页面。通过这个信息,我们可以知道访客是怎么来到当前页面的。这对于Web Analytics非常重要,可以用于分析不同渠道流量分布、用户搜索的关键词等。
在这里插入图片描述

但是,这个字段同时会造成用户敏感信息泄漏(如:带有敏感信息的重置密码URL,若被Web Analytics收集,则存在密码被重置的危险)。
所以就有了 Referrer Policy,用于过滤 Referrer 报头内容,目前是一个候选标准,不过已经有部分浏览器支持该标准。具体的可查看这里。

指令值

目前包含了以下几种指令值:

enum ReferrerPolicy {
  "",
  "no-referrer",
  "no-referrer-when-downgrade",
  "same-origin",
  "origin",
  "strict-origin",
  "origin-when-cross-origin",
  "strict-origin-when-cross-origin",
  "unsafe-url"
};
  • 空字符串
    按照浏览器的默认值执行。默认值为 no-referrer-when-downgrade。部分标签可重定义此安全策略。

  • no-referrer
    从字面意思就可以理解,不传递 Referrer 报头的值。

  • no-referrer-when-downgrade
    当发生降级(比如从 https😕/ 跳转到 http😕/ )时,不传递 Referrer 报头。但是反过来的话不受影响。通常也会当作浏览器的默认安全策略。

原地址跳转地址Referrer
https://example.com?token=123https://example.com/pathhttps://example.com?token=123
http://example.com?token=123http://example.com/pathhttp://example.com?token=123
https//example.comhttp://example.com/path无(协议降级)
http://example.com?token=123https://example.com/pathhttp://example.com?token=123
  • same-origin
    同源,即当协议、域名和端口(如果有一方指定的话)都相同,才会传递 Referrer
原地址跳转地址Referrer
https://example.com?token=123https://example.com/pathhttps://example.com?token=123
http://example.com?token=123http://example.com/pathhttp://example.com?token=123
https//example.comhttp://example.com/path无(协议降级)
http://example.com?token=123https://example.com/path无(协议降级)
http://example.com?token=123http://example.com:88/path无(端口不同)
https://example.com?token=123https://caixw.io无(域名不同)
  • origin
    将当前页面过滤掉参数及路径部分,仅将协议、域名和端口(如果有的话)当作 Referrer
原地址跳转地址Referrer
https://example.com?token=123https://example.com/pathhttps://example.com
http://example.com?token=123https://example.com/pathhttp://example.com
https://example.com?token=123https://caixw.iohttps://example.com
  • strict-origin
    类似于 origin,但是不能降级。
原地址跳转地址Referrer
https://example.com?token=123https://example.com/pathhttps://example.com
http://example.com?token=123https://example.com/pathhttp://example.com
http://example.com?token=123http://caixw.iohttp://example.com
https://example.com?token=123http://caixw.io
  • origin-when-cross-origin
    跨域时(协议、域名和端口只有一个不同)和 origin 模式相同,否则 Referrer 还是传递当前页的全路径。
原地址跳转地址Referrer
https://example.com?token=123https://example.com/pathhttps://example.com?token=123
http://example.com?token=123https://example.com/pathhttp://example.com?token=123
http://example.com?token=123http://caixw.iohttp://example.com
  • strict-origin-when-cross-origin
    origin-when-cross-origin 类似,但不能降级。
原地址跳转地址Referrer
https://example.com?token=123https://example.com/pathhttps://example.com?token=123
https://example.com?token=123https://caixw.iohttps://example.com
https://example.com?token=123http://example.com/path
https://example.com?token=123http://example.com/
  • unsafe-url
    任意情况下,都发送当前页的全部地址到 Referrer,最宽松和不安全的策略。

传递方式

Referrer-Policy 报头

推荐的方式,直接在 Referrer-Policy 报头中设置。

Referrer-Policy: origin;

Meta

通过指定 name 值为 referrermeta 标签,也可以达到相同的效果:

<meta name="referrer" content="strict-origin" />

content 可以是上面的指定的值,也可以是下面这几种旧的指令值,会自动作相应的转换,但不推荐这些旧的指令值:

LegacyReferrer
neverno-referrer
defaultno-referrer-when-downgrade
alwaysunsafe-url
origin-when-crossoriginorigin-when-cross-origin
  • 标签属性
    • alink 标签可以通过属性 rel 指定 noreferrer,仅对当前链接有效;
    • aarealinkiframeimg 还可以通过 referrerpolicy 指定仅针对当前链接的设置。

参考:

  1. 浏览器的同源策略
  2. Referrer-Policy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值