Referrer Policy 介紹


 

發布於 署名 4.0 國際 (CC BY 4.0) 原文鏈接:https://caixw.io/posts/2017/referrer-policy.html

 

當用戶在瀏覽器上點擊一個鏈接時,會產生一個 HTTP 請求,用於獲取新的頁面內容,而在該請求的報頭中,會包含一個 Referrer,用以指定該請求是從哪個頁面跳轉頁來的,常被用於分析用戶來源等信息。但是也有成為用戶的一個不安全因素,比如有些網站直接將 sessionid 或是 token 放在地址欄里傳遞的,會原樣不動地當作 Referrer 報頭的內容傳遞給第三方網站。

所以就有了 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=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 無(協議降級)
http://example.com?token=123 https://example.com/path http://example.com?token=123

same-origin

同源,即當協議、域名和端口(如果有一方指定的話)都相同,才會傳遞 Referrer。

原地址 跳轉地址 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 無(協議不同)
http://example.com?token=123 https://example.com/path 無(協議不同)
http://example.com?token=123 http://example.com:88/path 無(端口不同)
https://example.com?token=123 https://caixw.io 無(域名不同)

origin

將當前頁面過濾掉參數及路徑部分,僅將協議、域名和端口(如果有的話)當作 Referrer。

原地址 跳轉地址 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,但是不能降級。

原地址 跳轉地址 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

跨域時(協議、域名和端口只有一個不同)和 origin 模式相同,否則 Referrer 還是傳遞當前頁的全路徑。

原地址 跳轉地址 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 類似,但不能降級。

原地址 跳轉地址 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

任意情況下,都發送當前頁的全部地址到 Referrer,最寬松和不安全的策略。

傳遞方式

Referrer-Policy 報頭

推薦的方式,直接在 Referrer-Policy 報頭中設置。

Referrer-Policy: origin; 

Meta

通過指定 name 值為 referrer 的 meta 標簽,也可以達到相同的效果:

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

content 可以是上面的指定的值,也可以是下面這幾種舊的指令值,會自動作相應的轉換,但不推薦這些舊的指令值:

Legacy Referrer
never no-referrer
default no-referrer-when-downgrade
always unsafe-url
origin-when-crossorigin origin-when-cross-origin

標簽屬性

  • a 和 link 標簽可以通過屬性 rel 指定 noreferrer,僅對當前鏈接有效;
  • aarealinkiframe 和 img 還可以通過 referrerpolicy 指定僅針對當前鏈接的設置。

參考

Referrer Policy

Referrer-Policy

瀏覽器的同源策略


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM