HTTP Referer 請求標頭標識來源頁面,廣泛用於防盜鏈、統計分析,但極易偽造,絕不可用於身份驗證、授權決策等安全關鍵邏輯。本文系統整理偽造手法、防禦策略與現代替代方案。
$ch = curl_init('https://target.com/protected.jpg');
curl_setopt($ch, CURLOPT_REFERER, 'https://allowed.com/page.html');
curl_exec($ch);
$fp = fsockopen('target.com', 443);
$out = "GET /protected.jpg HTTP/1.1\r\n";
$out .= "Host: target.com\r\n";
$out .= "Referer: https://allowed.com/page.html\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
curl -H "Referer: https://allowed.com/" https://target.com/image.jpg
wget --referer="https://allowed.com/" https://target.com/image.jpg
瀏覽器安全策略禁止 JS 任意設定 Referer,fetch/XMLHttpRequest 的 referrerPolicy 僅能控制發送策略(no-referrer、origin、same-origin 等),無法自訂任意值。
# Nginx
location ~* \.(gif|jpe?g|png|webp)$ {
valid_referers none blocked *.example.com example.com;
if ($invalid_referer) { return 403; }
}
# Apache
SetEnvIfNoCase Referer "^https?://(www\.)?example\.com/" local_ref
SetEnvIf Referer "^$" local_ref
Order Deny,Allow
Deny from all
Allow from env=local_ref
透過 HTTP 標頭或 <meta name="referrer"> 控制瀏覽器發送策略:
Referrer-Policy: strict-origin-when-cross-origin
# 同源發送完整 URL,跨源僅發送 origin,HTTPS→HTTP 不發送
配合 CSP、COOP/COEP、Signed Exchanges 進一步鎖定資源存取上下文。
Referer 是「禮貌性」標頭,非「安全性」憑證。防盜鏈配置簡單有效,能阻擋 90% 以上誤連,但絕不可作為授權依據。關鍵資源務必採用簽名 URL 或 Token 驗證。