當(dāng)用戶在瀏覽器上點(diǎn)擊一個(gè)鏈接時(shí)羡洛,會(huì)產(chǎn)生一個(gè) HTTP 請(qǐng)求鹤竭,用于獲取新的頁(yè)面內(nèi)容踊餐,而在該請(qǐng)求的報(bào)頭中,會(huì)包含一個(gè) Referrer臀稚,用以指定該請(qǐng)求是從哪個(gè)頁(yè)面跳轉(zhuǎn)頁(yè)來(lái)的吝岭,常被用于分析用戶來(lái)源等信息。但是也有成為用戶的一個(gè)不安全因素烁涌,比如有些網(wǎng)站直接將 sessionid 或是 token 放在地址欄里傳遞的苍碟,會(huì)原樣不動(dòng)地當(dāng)作 Referrer 報(bào)頭的內(nèi)容傳遞給第三方網(wǎng)站。
所以就有了 Referrer Policy撮执,用于過濾 Referrer 報(bào)頭內(nèi)容微峰,目前是一個(gè)候選標(biāo)準(zhǔn),不過已經(jīng)有部分瀏覽器支持該標(biāo)準(zhǔn)抒钱。具體的可查看這里蜓肆。
指令值
目前包含了以下幾種指令值:
enum ReferrerPolicy {
"",
"no-referrer",
"no-referrer-when-downgrade",
"same-origin",
"origin",
"strict-origin",
"origin-when-cross-origin",
"strict-origin-when-cross-origin",
"unsafe-url"
};
空字符串
按照瀏覽器的默認(rèn)值執(zhí)行颜凯。默認(rèn)值為 no-referrer-when-downgrade。部分標(biāo)簽可重定義此安全策略仗扬。
no-referrer
從字面意思就可以理解症概,不傳遞 Referrer 報(bào)頭的值。
no-referrer-when-downgrade
當(dāng)發(fā)生降級(jí)(比如從 https:// 跳轉(zhuǎn)到 http:// )時(shí)早芭,不傳遞 Referrer 報(bào)頭彼城。但是反過來(lái)的話不受影響。通常也會(huì)當(dāng)作瀏覽器的默認(rèn)安全策略退个。
原地址 跳轉(zhuǎn)地址 Referrer
https://example.com?token=123 https://example.com/path https://example.com?token=123
http://example.com?token=123 http://example.com/path http://example.com?token=123
https//example.com http://example.com/path 無(wú)(協(xié)議降級(jí))
http://example.com?token=123 https://example.com/path http://example.com?token=123
same-origin
同源募壕,即當(dāng)協(xié)議、域名和端口(如果有一方指定的話)都相同语盈,才會(huì)傳遞 Referrer舱馅。
原地址 跳轉(zhuǎn)地址 Referrer
https://example.com?token=123 https://example.com/path https://example.com?token=123
http://example.com?token=123 http://example.com/path http://example.com?token=123
https//example.com http://example.com/path 無(wú)(協(xié)議不同)
http://example.com?token=123 https://example.com/path 無(wú)(協(xié)議不同)
http://example.com?token=123 http://example.com:88/path 無(wú)(端口不同)
https://example.com?token=123 https://caixw.io 無(wú)(域名不同)
origin
將當(dāng)前頁(yè)面過濾掉參數(shù)及路徑部分,僅將協(xié)議刀荒、域名和端口(如果有的話)當(dāng)作 Referrer代嗤。
原地址 跳轉(zhuǎn)地址 Referrer
https://example.com?token=123 https://example.com/path https://example.com
http://example.com?token=123 https://example.com/path http://example.com
https://example.com?token=123 https://caixw.io https://example.com
strict-origin
類似于 origin,但是不能降級(jí)缠借。
原地址 跳轉(zhuǎn)地址 Referrer
https://example.com?token=123 https://example.com/path https://example.com
http://example.com?token=123 https://example.com/path http://example.com
http://example.com?token=123 http://caixw.io http://example.com
https://example.com?token=123 http://caixw.io 無(wú)
origin-when-cross-origin
跨域時(shí)(協(xié)議干毅、域名和端口只有一個(gè)不同)和 origin 模式相同,否則 Referrer 還是傳遞當(dāng)前頁(yè)的全路徑泼返。
原地址 跳轉(zhuǎn)地址 Referrer
https://example.com?token=123 https://example.com/path https://example.com?token=123
http://example.com?token=123 https://example.com/path http://example.com?token=123
http://example.com?token=123 http://caixw.io http://example.com
strict-origin-when-cross-origin
與 origin-when-cross-origin 類似溶锭,但不能降級(jí)。
原地址 跳轉(zhuǎn)地址 Referrer
https://example.com?token=123 https://example.com/path https://example.com?token=123
https://example.com?token=123 https://caixw.io https://example.com
https://example.com?token=123 http://example.com/path 無(wú)
https://example.com?token=123 http://example.com/ 無(wú)
unsafe-url
任意情況下符隙,都發(fā)送當(dāng)前頁(yè)的全部地址到 Referrer趴捅,最寬松和不安全的策略。
傳遞方式
Referrer-Policy 報(bào)頭
推薦的方式霹疫,直接在 Referrer-Policy 報(bào)頭中設(shè)置拱绑。
Referrer-Policy: origin;
Meta
通過指定 name 值為 referrer 的 meta 標(biāo)簽,也可以達(dá)到相同的效果:
content 可以是上面的指定的值丽蝎,也可以是下面這幾種舊的指令值猎拨,會(huì)自動(dòng)作相應(yīng)的轉(zhuǎn)換,但不推薦這些舊的指令值:
Legacy Referrer
never no-referrer
default no-referrer-when-downgrade
always unsafe-url
origin-when-crossorigin origin-when-cross-origin
標(biāo)簽屬性
a 和 link 標(biāo)簽可以通過屬性 rel 指定 noreferrer屠阻,僅對(duì)當(dāng)前鏈接有效红省;
a、area国觉、link吧恃、iframe 和 img 還可以通過 referrerpolicy 指定僅針對(duì)當(dāng)前鏈接的設(shè)置。