Referrer Policy
By the end of this lesson you will be able to say exactly what the Referer header reveals for any request, choose the right referrer policy for a page, set it in four different ways, and watch the browser apply each policy in a live lab.
What you should know first: HTML Security Fundamentals, which introduced referrerpolicy and rel="noreferrer", Creating Hyperlinks, and Same-Origin Policy, because every rule below depends on whether a request is same-origin or cross-origin.
What the Referer header leaks
When a page loads an image, a script or a frame, or when a visitor clicks a link, the browser may send a Referer request header with the address of the page that made the request. The spelling with one "r" is a typo from the 1990s that became part of HTTP. The newer names, such as Referrer-Policy and document.referrer, spell it correctly.
Think of a courier parcel. The sender's address on the slip can be just "Kochi", or it can be the full house number, floor and phone number. A referrer policy decides how much of the address goes on the slip.
The full address of a page can carry private data:
https://clinic.example.in/reports?patient=meera&test=thyroid: a patient's name and test.https://shop.example.in/reset-password?token=8f3a91c2: a secret that lets anyone reset an account.https://admin.example.in/students/1042/edit: the shape of a private admin panel.https://shop.example.in/search?q=pregnancy+test: a search that the visitor would not want shared.
If that page embeds a map, an analytics script or a chat widget, or the visitor clicks an outside link, the other company's server could log that address. Two parts are never sent under any policy: the fragment (the part after #) and any username:password@ in the URL.
On the receiving side, the server sees the Referer header (in PHP, $_SERVER['HTTP_REFERER']), and a script can read document.referrer. Both are supplied by the browser, so a server must never trust them for security decisions. A visitor or a tool can change or remove them.
Every policy value
A referrer policy tells the browser how much of the address to send. There are eight values. "Full" means the URL without its fragment; "origin" means only the scheme, host and port, such as https://clinic.example.in/. A downgrade is a request from an https page to an http address.
| Policy | Same-origin request | Cross-origin, HTTPS to HTTPS | HTTPS to HTTP |
|---|---|---|---|
no-referrer | Nothing | Nothing | Nothing |
no-referrer-when-downgrade | Full | Full | Nothing |
origin | Origin | Origin | Origin |
origin-when-cross-origin | Full | Origin | Origin |
same-origin | Full | Nothing | Nothing |
strict-origin | Origin | Origin | Nothing |
strict-origin-when-cross-origin | Full | Origin | Nothing |
unsafe-url | Full | Full | Full |
The word strict in a name means "never send anything on a downgrade". unsafe-url is named that way on purpose: it sends secrets in the URL to every site, even over plain http.
The default: strict-origin-when-cross-origin
If no policy is set anywhere, modern browsers use strict-origin-when-cross-origin: full URL within your own origin, origin only to other sites, nothing on a downgrade. Chrome 85, Firefox 87 and Safari 15 made this the default. Older browsers used no-referrer-when-downgrade, which sent the full URL to every HTTPS site, so you will still see older guides recommend setting a policy "to be safe".
Browsers may also trim referrers further than a page asks, for example in private windows or tracking-protection modes. So never build a feature that depends on receiving a full referrer from another site.
Step 1: watch the browser apply each policy
This lab creates two tiny pages for each policy. A blob: URL page has the same origin as the lab. A data: URL page has an opaque origin, so to the browser it counts as a cross-origin request. Each tiny page sends its document.referrer back with postMessage().
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Referrer lab</title>
<style>
body { font-family: sans-serif; padding: 1rem; }
table { border-collapse: collapse; }
td, th { border: 1px solid #bbb; padding: 4px 8px; text-align: start; font-size: 0.9rem; }
#frames { display: none; }
</style>
</head>
<body>
<p>Address of this page: <code id="me"></code></p>
<table>
<thead><tr><th>referrerpolicy</th><th>Same-origin page saw</th><th>Cross-origin page saw</th></tr></thead>
<tbody id="rows"></tbody>
</table>
<div id="frames"></div>
<script>
const policies = ["", "no-referrer", "origin", "same-origin", "strict-origin", "origin-when-cross-origin", "unsafe-url"];
const reporter = "<script>parent.postMessage(document.referrer, '*')<\/script>";
const senders = new Map(); // frame window -> [row index, column]
document.querySelector("#me").textContent = document.baseURI;
policies.forEach((policy, i) => {
const tr = document.createElement("tr");
for (const text of [policy || "(not set: default)", "...", "..."]) {
const td = document.createElement("td");
td.textContent = text;
tr.append(td);
}
document.querySelector("#rows").append(tr);
const sameOrigin = URL.createObjectURL(new Blob([reporter], { type: "text/html" }));
const crossOrigin = "data:text/html," + encodeURIComponent(reporter);
[sameOrigin, crossOrigin].forEach((src, column) => {
const frame = document.createElement("iframe");
if (policy) frame.referrerPolicy = policy;
frame.src = src;
document.querySelector("#frames").append(frame);
senders.set(frame.contentWindow, [i, column + 1]);
});
});
window.addEventListener("message", (event) => {
const place = senders.get(event.source);
if (!place) return;
const cell = document.querySelector("#rows").rows[place[0]].cells[place[1]];
cell.textContent = event.data === "" ? "(nothing)" : event.data;
});
</script>
</body>
</html>In the editor, the full address is the Try-it page's own URL, such as https://course.ethicscomputer.in/tryit/html/referrer-policy/1, and the origin is https://course.ethicscomputer.in/. The table matches the policy table above. The default row sends the full URL to the same-origin page and only the origin to the cross-origin page, exactly as strict-origin-when-cross-origin promises. same-origin sends nothing across origins, and unsafe-url leaks the full path to both.
Four ways to set the policy
1. The HTTP header (best for a whole site). It covers every page, including those whose HTML you do not control. You can list several values; the browser uses the last one it understands, which lets you add a newer value with a fallback:
<?php
declare(strict_types=1);
// A password reset page: its URL contains a token, so send no referrer at all.
header('Referrer-Policy: no-referrer');On Apache, one line in .htaccess (needs mod_headers) sets it for the whole folder:
Header always set Referrer-Policy "strict-origin-when-cross-origin"Tested on Apache 2.4 from XAMPP, running on a local test port:
curl -s -I http://127.0.0.1:8472/site/index.html | grep -i "^HTTP|^referrer"HTTP/1.1 200 OK Referrer-Policy: strict-origin-when-cross-origin
2. The <meta> tag (when you cannot set headers). Put it early in <head>, because it applies to requests made after the browser reads it:
<meta name="referrer" content="same-origin">3. The referrerpolicy attribute (one element). It works on <a>, <area>, <img>, <iframe>, <script> and <link>, and it overrides the page's policy for that one request. fetch() has the same control as an option: fetch(url, { referrerPolicy: "no-referrer" }).
<img src="https://maps.example.com/static?city=Jaipur" alt="Map of Jaipur" referrerpolicy="no-referrer">
<a href="https://partner.example.org/" referrerpolicy="origin">Our delivery partner</a>4. rel="noreferrer" (links and forms). On <a>, <area> and <form> it sends no referrer and also implies noopener, so the new page cannot reach back through window.opener.
Choosing a policy
Step 2: check a page before you choose
Before picking a value, check what one real page of yours would leak. Paste its address and a third-party address it calls, then try each policy.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Referer calculator</title>
<style>
body { font-family: sans-serif; padding: 1rem; }
label { display: block; margin-block-start: 0.6rem; }
input, select { inline-size: 100%; max-inline-size: 30rem; }
output { display: block; margin-block-start: 1rem; font-family: monospace; font-weight: bold; }
</style>
</head>
<body>
<label for="from">Page the visitor is on</label>
<input id="from" value="https://clinic.example.in/reports?patient=meera&test=thyroid#summary">
<label for="to">Address being requested</label>
<input id="to" value="https://maps.example.com/embed">
<label for="policy">Referrer policy</label>
<select id="policy">
<option>strict-origin-when-cross-origin</option>
<option>no-referrer</option>
<option>no-referrer-when-downgrade</option>
<option>origin</option>
<option>origin-when-cross-origin</option>
<option>same-origin</option>
<option>strict-origin</option>
<option>unsafe-url</option>
</select>
<output id="result"></output>
<script>
function refererFor(policy, fromText, toText) {
const from = new URL(fromText);
const to = new URL(toText);
from.hash = ""; // fragments and passwords are never sent
from.username = "";
from.password = "";
const full = from.href;
const originOnly = from.origin + "/";
const sameOrigin = from.origin === to.origin;
const downgrade = from.protocol === "https:" && to.protocol === "http:";
switch (policy) {
case "no-referrer": return null;
case "no-referrer-when-downgrade": return downgrade ? null : full;
case "origin": return originOnly;
case "origin-when-cross-origin": return sameOrigin ? full : originOnly;
case "same-origin": return sameOrigin ? full : null;
case "strict-origin": return downgrade ? null : originOnly;
case "unsafe-url": return full;
default: return sameOrigin ? full : downgrade ? null : originOnly;
}
}
function update() {
const out = document.querySelector("#result");
try {
const value = refererFor(document.querySelector("#policy").value,
document.querySelector("#from").value, document.querySelector("#to").value);
out.textContent = value === null ? "No Referer header is sent" : "Referer: " + value;
} catch {
out.textContent = "Type two complete URLs, starting with https:// or http://";
}
}
document.querySelectorAll("input, select").forEach((el) => el.addEventListener("input", update));
update();
</script>
</body>
</html>With the default policy, the map service receives only https://clinic.example.in/. Switch to unsafe-url and Meera's test name appears in the header. Change the second address to an http:// URL and the strict policies send nothing.
A sensible plan for most sites:
- Keep
strict-origin-when-cross-originas the site-wide header, so it is explicit and survives browser changes. - Use
no-referreron pages whose URL holds a secret or personal data, such as password resets, invoices or medical reports. Better still, keep such data out of URLs and use POST forms or sessions. - Use
same-originfor an intranet or admin panel that should never reveal its addresses. - Avoid
unsafe-urlandno-referrer-when-downgrade.
Effect on analytics
Referrers are how analytics tools fill their "where did visitors come from" reports. Your policy only affects what you send to others; what you receive depends on the other site's policy.
- With the default, a blog in Pune that links to you still shows up as a source, but only as
https://blog.example.in/, not the exact article. - With
no-referrer, visits from your pages to a partner appear in the partner's reports as "direct" traffic. - Big search engines send only their origin, so you see the search engine but not the search words.
- A link from an HTTPS page to an HTTP site carries no referrer under the default, which is one more reason to serve everything over HTTPS.
If you need campaign tracking, put it in the link itself with UTM parameters, such as ?utm_source=newsletter&utm_campaign=diwali. These are part of the destination URL, so they survive any referrer policy. Never put personal data in them.
Common mistakes
- Secrets in URLs, protected only by a referrer policy. URLs also end up in history, logs and screenshots; move secrets out of them.
- Using
unsafe-urlto "fix" analytics. It leaks full URLs to every embedded third party. - Placing the
<meta>tag after scripts and images. Requests made before it use the old policy. - Trusting
Refererfor security. It can be removed or faked; use CSRF tokens. - Writing
Referrer-PolicyasReferer-Policy. The header name uses the correct spelling; only the request header has the typo.
Interview questions
1. What is the default referrer policy in modern browsers, and what does it send?
strict-origin-when-cross-origin. Same-origin requests get the full URL without the fragment, cross-origin HTTPS requests get only the origin, and requests from HTTPS to HTTP get no referrer at all.
2. What is the difference between origin and strict-origin?
Both send only the origin. strict-origin sends nothing when the request goes from HTTPS to HTTP, while origin sends the origin even over the insecure connection. The "strict" prefix always means "nothing on a downgrade".
3. How can you set a referrer policy, and which one wins?
With the Referrer-Policy response header, a <meta name="referrer"> tag, the referrerpolicy attribute on an element (or the referrerPolicy option in fetch()), and rel="noreferrer" on links and forms. An element's own attribute overrides the document's policy for that request.
4. Can a server rely on the Referer header to block CSRF?
Not on its own. The header can be missing because of a strict policy, a privacy setting or a proxy, and non-browser clients can send anything. Use CSRF tokens and SameSite cookies; the Origin header is a better extra check than Referer.
5. Does rel="noreferrer" do anything besides hiding the referrer?
Yes. It also implies noopener, so the opened page gets window.opener set to null and cannot navigate or script the original tab.
Key points
Referercan leak paths and query strings; fragments and URL passwords are never sent.- Eight policies decide between full URL, origin only or nothing, per same-origin, cross-origin and downgrade.
- Modern browsers default to
strict-origin-when-cross-origin; set it explicitly as a site-wide header. - Set policy by header,
<meta name="referrer">, thereferrerpolicyattribute orrel="noreferrer". - Use
no-referreron pages with secrets in the URL, and never useunsafe-url. - Referrers feed analytics; use UTM parameters for campaigns instead of looser policies.
Exercise
Kabir maintains an online exam portal for a coaching centre in Rohtak. The result page URL is https://exams.example.in/result?roll=1042&dob=2008-03-14, and it shows an embedded chart from https://charts.example.com, a logo from a sponsor site https://sponsor.example.net and a link to a scholarship partner. The whole site has no referrer policy. Write the header for the site, the extra rule for the result page, and the three tags. Then state exactly what each third party receives in the Referer header after your change.
Show solution
Site-wide, sent by the server for every page:
Referrer-Policy: strict-origin-when-cross-originThe result page holds personal data in its URL, so its PHP sends header('Referrer-Policy: no-referrer');, and the tags add the same intent:
<iframe src="https://charts.example.com/marks?exam=jee-mock-3" title="Marks chart"
referrerpolicy="no-referrer" loading="lazy"></iframe>
<img src="https://sponsor.example.net/logo.png" alt="Sponsor logo" referrerpolicy="no-referrer">
<a href="https://scholarships.example.org/" rel="noreferrer">Apply for a scholarship</a>All three receive no Referer header at all. Before the change, the browser default already sent each of them only https://exams.example.in/, never the roll number, but relying on an unstated default is fragile; the explicit header and attributes make the intent clear. The real fix is to stop putting the roll number and date of birth in the URL and load the result through a logged-in session.
Quick Quiz
1. A page at https://shop.example.in/orders/55 uses the default policy and loads a script from https://cdn.example.com. What Referer is sent?
Cross-origin HTTPS requests get only the origin under strict-origin-when-cross-origin.
2. Which policy sends nothing to other origins but the full URL within your own origin?
same-origin sends the full URL to same-origin requests and nothing to anyone else.
3. Which part of a URL is never sent in the Referer header?
The browser always strips the fragment and any username or password before sending a referrer.
4. Your server sends Referrer-Policy: no-referrer, strict-origin-when-cross-origin. What does a modern browser use?
The header accepts a list so that a newer value can come after an older fallback; the last supported one wins.
Learn HTML with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
