twobotechnologies

Information security · Identity & access management · 5 min read

Security Headers for Login Pages: A Practical Set

Short answer

A login page should send a Content-Security-Policy that blocks framing (frame-ancestors 'none') and restricts where forms post (form-action), Strict-Transport-Security, Cache-Control: no-store, a strict Referrer-Policy, Cross-Origin-Opener-Policy and X-Content-Type-Options, and set its session cookies with Secure, HttpOnly and SameSite.

Key takeaways

  • frame-ancestors 'none' in CSP stops clickjacking of the login form; keep X-Frame-Options: DENY for older browsers.
  • form-action limits where credentials can be posted, which blunts injected or modified forms.
  • Cache-Control: no-store keeps login pages and post-login responses out of shared and browser caches.
  • A strict Referrer-Policy stops codes and tokens in URLs from leaking to third parties through the Referer header.
  • Session cookies need Secure, HttpOnly and a SameSite value, and the __Host- prefix where possible.

A login page handles the most valuable thing a user types all day. It's also the page attackers most want to frame, inject into or copy. HTTP response headers can't fix weak authentication, but they close several browser-side attacks cheaply. This article lists the headers worth sending on login, SSO and consent pages, with a reason for each and a copy-ready set at the end.

Why do login pages need stricter headers than the rest of the site?

Login pages take credentials and issue sessions, so clickjacking, script injection, caching and referrer leaks do more damage there than on ordinary pages.

A marketing page that gets framed is an annoyance. A login form inside an attacker's invisible frame can trick users into clicking "Allow" on a consent screen or typing into a field they can't see. A script injected into a login page can read the password as it's typed. The same headers help everywhere, but on authentication pages you can afford to be strict, because those pages rarely need third-party content.

How do you stop clickjacking?

Send Content-Security-Policy: frame-ancestors 'none' (or 'self' if you truly frame your own login), plus X-Frame-Options: DENY for older browsers.

frame-ancestors tells the browser which origins may embed the page. 'none' means nobody. If a legitimate part of your own site embeds the login in an iframe, use 'self', or list the exact origins.

Modern browsers give frame-ancestors priority. X-Frame-Options: DENY is the older header and still worth sending as a fallback.

How do you limit where the password can be sent?

Add form-action to the CSP, listing only the origins your login form should post to.

If an attacker manages to inject HTML, a classic trick is to change the form's action so credentials go to their server. form-action 'self' (plus your identity provider's origin, if the form posts there) tells the browser to block submissions anywhere else.

Watch for redirects: some browsers apply form-action to redirects after the submission too, so include any origin the login flow legitimately redirects through.

How should scripts be restricted?

Use a script-src based on nonces or hashes, with no 'unsafe-inline', and keep third-party scripts off login pages entirely.

Analytics, chat widgets and tag managers have no business on a page where passwords are typed. Each third-party script can read the form. Remove them from authentication pages, then lock scripts down:

Content-Security-Policy: default-src 'self'; script-src 'nonce-RANDOM' 'strict-dynamic'; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'

Generate a new random nonce on every response. Roll the policy out with Content-Security-Policy-Report-Only first to see what it would block.

Why does caching matter on a login page?

Cache-Control: no-store keeps login pages and the responses right after login out of browser and shared caches.

Without it, a shared computer or a misconfigured proxy can keep a copy of a page that contains a CSRF token, a username or, worse, the first page of the signed-in session. no-store tells every cache not to keep the response at all. Send it on the login page, the post-login landing page and anything showing account details.

How do you stop tokens leaking through the Referer header?

Send a strict Referrer-Policy, such as no-referrer on authentication pages, so URLs with codes or tokens don't reach other sites.

OAuth and SSO flows put sensitive values in URLs: authorization codes, state values, sometimes session identifiers in old systems. If the page then loads an image or a link from another origin, the browser can send the full URL in the Referer header. no-referrer sends nothing. strict-origin-when-cross-origin, the browser default in most modern browsers, sends only the origin cross-site and is a sensible baseline for the rest of a site.

Which other headers are worth adding?

Add HSTS, X-Content-Type-Options, Cross-Origin-Opener-Policy and a restrictive Permissions-Policy.

Header Value Why
Strict-Transport-Security max-age=31536000; includeSubDomains Forces HTTPS on return visits, blocking SSL-stripping
X-Content-Type-Options nosniff Stops browsers guessing content types
Cross-Origin-Opener-Policy same-origin Cuts the link to windows that opened the page
Permissions-Policy camera=(), microphone=(), geolocation=() Turns off features a login page never uses

Add preload to HSTS only once every subdomain serves HTTPS, since preload lists are slow to undo. If your login opens a popup for an external identity provider, test Cross-Origin-Opener-Policy carefully; same-origin-allow-popups may be needed.

How should session cookies be set?

Set session cookies with Secure, HttpOnly, SameSite=Lax or Strict, and the __Host- prefix where you can.

Secure keeps the cookie off plain HTTP. HttpOnly hides it from JavaScript. SameSite limits cross-site sending, which helps against CSRF. The __Host- prefix makes the browser reject the cookie unless it is Secure, has Path=/ and has no Domain attribute, so a sibling subdomain can't overwrite it. SAML and some OAuth flows post back cross-site, so a cookie read during that callback may need SameSite=None; Secure; keep that exception to the one cookie that needs it.

What does a complete set look like?

A copy-ready baseline for a login page fits in eight headers.

Content-Security-Policy: default-src 'self'; script-src 'nonce-RANDOM' 'strict-dynamic'; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'
X-Frame-Options: DENY
Strict-Transport-Security: max-age=31536000; includeSubDomains
Cache-Control: no-store
Referrer-Policy: no-referrer
X-Content-Type-Options: nosniff
Cross-Origin-Opener-Policy: same-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()

Adjust the CSP origins to your real identity provider and assets, then test the full login flow, including password reset and MFA, before enforcing. The Twobo security header builder turns the core of this set (HSTS, a starter CSP, nosniff and Referrer-Policy) into ready config for nginx, Apache, IIS or Jetty, and our article on adding HSTS to Jetty-based servers covers one server in detail.

Questions

Is X-Frame-Options still needed if I use CSP frame-ancestors?

Modern browsers use frame-ancestors and ignore X-Frame-Options when both are present. Sending X-Frame-Options: DENY as well costs nothing and covers older clients.

Should login pages use a strict CSP with nonces?

Yes if you can. Login pages are high-value targets for script injection, and a nonce- or hash-based script-src blocks most injected scripts. Start in report-only mode to find what breaks.

Which Referrer-Policy is best for an SSO page?

no-referrer is the strictest. strict-origin-when-cross-origin is a reasonable default for the rest of a site, since it never sends the path or query string to other origins.

Do these headers replace other login protections?

No. They harden the page in the browser. You still need rate limiting, phishing-resistant multi-factor authentication where possible, secure password storage and monitoring.

Sources

Tagged: Security, HSTS, Authentication, Single sign-on

The Twobo Technologies editors write practical explainers on identity and API security and maintain the free tools on this site. Spotted an error or an outdated detail? Email contact@twobotechnologies.com.

Related articles