HSTS Preload and Referrer-Policy Explained
On this page
Learning Objective
Configure Strict-Transport-Security correctly including the trust-on-first-use gap that preloading closes, and choose a Referrer-Policy that prevents URL data from leaking to third parties without breaking analytics that depend on it.
Concept Explanation
Strict-Transport-Security tells a browser that this host must be reached over HTTPS for a stated period, and that certificate errors must not be click-throughable. Once the header is remembered, a user typing a bare hostname, following an http link, or being redirected to a plaintext URL never sends a plaintext request at all; the browser rewrites it internally before anything leaves the machine. That is the critical difference from a server-side redirect from HTTP to HTTPS, which still requires one plaintext request to exist, and that one request is precisely what an attacker with network position intercepts.
The attack HSTS closes is SSL stripping, demonstrated by Moxie Marlinspike in 2009 with the sslstrip tool. An attacker in the network path rewrites HTTPS links in plaintext pages to HTTP, proxies the connection, and the victim never sees a certificate warning because they never attempted an HTTPS connection; the browser shows an ordinary unencrypted page and the user has no cue that anything is wrong. HSTS removes the opportunity by making the browser refuse to speak plaintext to that host regardless of what any link says.
HSTS has one structural weakness: it is trust on first use. The browser only knows the policy after it has successfully received the header at least once, so a user's very first visit, or a visit from a fresh profile, is still interceptable. The preload list closes this. Browsers ship a compiled list of hosts that are HTTPS-only from the very first request, and a site can submit itself at hstspreload.org provided it serves the header with a max-age of at least 31536000 seconds, includeSubDomains, and the preload token. The essential caveat is that removal from the list is slow, taking months to propagate through browser release cycles, so preloading is close to irreversible and must not be enabled until every subdomain, including internal tools and legacy hosts, is confirmed to work over HTTPS.
Referrer-Policy addresses a different leak. By default, browsers historically sent the full URL of the referring page in the Referer header, including path and query string. That leaks any secret carried in a URL, and URLs carry secrets constantly: password reset tokens, invitation links, document sharing links, session identifiers in legacy systems, and search queries containing personal information. Every third-party resource the page loads, every analytics beacon, and every outbound link receives it.
The policy values form a spectrum. no-referrer sends nothing. same-origin sends the full URL to your own origin and nothing cross-origin. origin sends only the scheme, host, and port everywhere. strict-origin sends the origin cross-origin but nothing on an HTTPS-to-HTTP downgrade. strict-origin-when-cross-origin sends the full URL same-origin, the origin only cross-origin, and nothing on a downgrade; it is now the default in Chrome since version 85 and Firefox since version 87, and it is the right explicit choice for most sites. unsafe-url sends everything always and, as the name states, should never be used. Pages handling tokens in URLs should go further and use no-referrer on those specific pages.
| Referrer-Policy value | Same-origin request | Cross-origin request | HTTPS to HTTP |
|---|---|---|---|
| no-referrer | Nothing | Nothing | Nothing |
| same-origin | Full URL | Nothing | Nothing |
| strict-origin | Origin only | Origin only | Nothing |
| strict-origin-when-cross-origin | Full URL | Origin only | Nothing |
| no-referrer-when-downgrade | Full URL | Full URL | Nothing |
| unsafe-url | Full URL | Full URL | Full URL |
Worked Example: A Staged HSTS Rollout and a Token Leak
The staged max-age progression below is the standard safe path, because a long max-age committed before every subdomain is ready cannot be undone in clients that already cached it. The Referer example underneath shows the leak that a default policy used to permit.
# STAGE 1 - five minutes. Confirms the header is delivered and nothing breaks.
Strict-Transport-Security: max-age=300
# STAGE 2 - one week, still without subdomains.
Strict-Transport-Security: max-age=604800
# STAGE 3 - add subdomains ONLY after auditing every one of them. Any
# subdomain that cannot serve HTTPS becomes unreachable at this point.
Strict-Transport-Security: max-age=604800; includeSubDomains
# STAGE 4 - one year plus preload. Effectively irreversible; removal from
# the browser-shipped list takes months of release cycles to propagate.
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
# ---- The Referer leak, with and without a policy ----
# A password reset page at:
# https://app.example.com/reset?token=8f3c1d9a2b7e4
# loads an analytics script from a third party. Historically the browser sent:
#
# Referer: https://app.example.com/reset?token=8f3c1d9a2b7e4
#
# ...handing the single-use reset token to the analytics vendor, to their
# CDN logs, and to anyone with access to either.
# Site-wide default:
Referrer-Policy: strict-origin-when-cross-origin
# Stronger, on pages whose URLs carry secrets:
Referrer-Policy: no-referrer
# And the real fix: do not put single-use secrets in URLs at all.Real-World Examples
- Moxie Marlinspike demonstrated SSL stripping with the sslstrip tool at Black Hat DC in 2009, showing that an attacker in the network path could downgrade HTTPS links in plaintext pages and proxy the connection with no certificate warning shown to the victim, because the victim never attempted an HTTPS connection at all; HSTS was standardized in RFC 6797 in 2012 as the direct response to that attack.
- Google operates the .dev and .app top-level domains as entries on the HSTS preload list in their entirety, meaning every hostname under them is HTTPS-only in browsers from the very first request with no per-site configuration and no trust-on-first-use window; it is the clearest demonstration of preloading at scale, and also of its irreversibility, since a site under those TLDs cannot opt out.
Review Questions & Answers
Because the redirect requires a plaintext request to happen first, and that request is what an attacker in the network path intercepts. They answer it themselves, proxy the connection to the real site over HTTPS, and serve the victim plaintext with no certificate warning, since the victim never attempted a secure connection. HSTS makes the browser rewrite the URL internally before anything is sent, so the interceptable request never exists.
A browser only enforces HSTS for a host after it has successfully received the header at least once, so the first visit from any browser profile is still made over plaintext and is interceptable. Preloading closes it by shipping the policy inside the browser itself: hosts on the compiled list are treated as HTTPS-only from the very first request, before any connection to the site has ever occurred.
Because the list is compiled into browser binaries and distributed with releases. Removing an entry requires a request to be processed, the list to be regenerated, and then new browser versions to be built, shipped, and adopted by users, which takes months. Until that propagation completes, every user on an older build still enforces HTTPS for the host, so any subdomain that cannot serve HTTPS remains unreachable for that entire period.
Because it removes the leak while preserving legitimate functionality. Full URLs are still sent for same-origin navigations, which internal analytics and debugging rely on, while cross-origin requests receive only the origin, which is enough for a third party to attribute traffic without receiving paths, query strings, or tokens. It also sends nothing at all on an HTTPS-to-HTTP downgrade. no-referrer is stronger and is the right choice specifically for pages whose URLs carry secrets, but as a site-wide default it tends to break attribution and get reverted.