當(dāng)用戶在瀏覽器上點(diǎn)擊一個(gè)鏈接時(shí),會(huì)產(chǎn)生一個(gè) HTTP 請(qǐng)求,用于獲取新的頁面內(nèi)容,而在該請(qǐng)求的報(bào)頭中,會(huì)包含一個(gè) Referrer,用以指定該請(qǐng)求是從哪個(gè)頁面跳轉(zhuǎn)頁來的,常被用于分析用戶來源等信息。但是也有成為用戶的一個(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)頭。但是反過來的話不受影響。通常也會(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 無(協(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 無(協(xié)議不同)
http://example.com?token=123 https://example.com/path 無(協(xié)議不同)
http://example.com?token=123 http://example.com:88/path 無(端口不同)
https://example.com?token=123 https://caixw.io 無(域名不同)
origin
將當(dāng)前頁面過濾掉參數(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 無
origin-when-cross-origin
跨域時(shí)(協(xié)議、域名和端口只有一個(gè)不同)和 origin 模式相同,否則 Referrer 還是傳遞當(dāng)前頁的全路徑。
原地址 跳轉(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 無
https://example.com?token=123 http://example.com/ 無
unsafe-url
任意情況下,都發(fā)送當(dāng)前頁的全部地址到 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è)置。