HTTP security headers: what to send, and how to check
Security headers are HTTP response headers that tell the browser how to treat your site: use HTTPS only, run only the scripts you allow, refuse to be framed by other sites, and keep referrer and device access in check. They cost nothing to send, take minutes to check, and close whole classes of attack in the browser rather than on the server.
Quick facts
- Where they live
- In HTTP responses from your server, edge network or application
- Who enforces them
- The visitor's browser
- Core set
- Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, frame protection, Referrer-Policy
- Also searched as
- HSTS, HTTP Strict Transport Security, HTTPS strict transport security, X-Frame-Options
- How to check
- Browser developer tools, or a command-line request that prints headers
- Reference
- OWASP HTTP Headers Cheat Sheet
Headers protect visitors' browsers. They do not filter requests to your server; that is a different layer.
The security headers to set
| Header | What it does | Common value |
|---|---|---|
| Strict-Transport-Security | Browser uses HTTPS only for your site, for a period | max-age of a year, includeSubDomains when ready |
| Content-Security-Policy | Limits which scripts, styles and other resources run | Built per site; start in report-only mode |
| X-Content-Type-Options | Stops the browser guessing a file's type | nosniff |
| frame-ancestors | Controls who may put your pages in a frame (X-Frame-Options in older browsers) | 'self' or none |
| Referrer-Policy | Limits what address is sent to other sites | strict-origin-when-cross-origin |
| Permissions-Policy | Turns off browser features you do not use | camera, microphone, geolocation off |
References: RFC 6797 for Strict-Transport-Security, and MDN on Content-Security-Policy, X-Content-Type-Options, frame-ancestors, Referrer-Policy and Permissions-Policy.
Strict-Transport-Security (HSTS): the HTTPS-only header
Strict-Transport-Security, or HSTS, is also searched as HTTP Strict Transport Security or HTTPS strict transport security: one header. A browser that receives it over HTTPS keeps treating the site as HTTPS-only for the seconds set in max-age.
- max-age: 31536000 seconds is the one-year value in the example in RFC 6797.
- includeSubDomains: extends the policy to subdomains, so any still on plain HTTP are requested over HTTPS instead.
- Over HTTPS only. The browser ignores the header on plain HTTP, so test the https:// address.
- max-age=0 ends the policy.
Start with the ones that cannot break anything, nosniff and Referrer-Policy, then HSTS once every subdomain serves HTTPS, then a Content Security Policy in report-only mode. Headers also limit the damage of DOM-based XSS. A policy header also limits skimming scripts; see formjacking.
How to check security headers yourself
- In a browser. Open developer tools, go to the Network tab, reload, select the page request and read Response Headers.
- From the command line. Request only the headers and read them, as below.
- On more than the home page. Check a logged-in page, an error page and a file download; servers often send different headers on each.
- Beyond headers. A separate check answers is my IP blacklisted.
curl -sI https://example.com/ | grep -i -E 'strict-transport|content-security|x-content-type|x-frame|referrer-policy|permissions-policy'
HTTP header test: reading a response
An HTTP header is a name and a value sent with a request or a response. MDN describes headers as the way the client and the server pass additional information with a message; in HTTP/1.x a header is a case-insensitive name, a colon, then the value. RFC 9110 calls them fields: extensible name/value pairs.
An HTTP header tester, or header checker, requests an address and lists the headers that came back. Checking HTTP headers by hand needs only browser developer tools or curl; an online security headers checker does it from its own server. Either way, an HTTP header test shows what the server returned to that one request, so run it against the address visitors land on; a redirect carries headers of its own.
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
This example HTTP header response has three of the headers from the table above; Content-Security-Policy and frame-ancestors are missing, so a security header check would flag both. Read a result with three questions:
- Present? A missing header is what a check reports first.
- Value as intended? A very short max-age is present but gives little protection, because the browser forgets the HTTPS-only rule quickly.
- Sent once? Two copies with different values mean two layers set it.
Where to set security headers
Headers can be added by the application, by the web server, or by a proxy or CDN in front of both. Any of them works; what goes wrong is splitting the job, so that one layer sets a header, another sets it again with a different value, and the browser gets two conflicting answers or uses the first one it finds.
Pick one layer as the owner of each header and check the live response after every change to any of the three. A scanner or a curl -I against the real address shows what the browser receives, which is the only version that counts.
Referrer-Policy: strict-origin-when-cross-origin
This value sends the full address on requests within your site, only the origin (scheme and host) to other sites over HTTPS, and nothing when moving from HTTPS to HTTP. It keeps paths and query strings, which may contain identifiers, from leaking to third parties.
It is the default in current browsers when no policy is set, according to MDN; setting it explicitly makes the behaviour the same in older ones.
Frame protection and clickjacking
A page that can be framed by any site can be overlaid with a decoy (clickjacking), so a user clicks a button they cannot see. The CSP directive frame-ancestors decides which origins may frame your pages; X-Frame-Options does the same for older browsers.
Set frame-ancestors to 'self' unless you know which partners embed your pages, and list them by name if they do.
X-Frame-Options: DENY and SAMEORIGIN
X-Frame-Options is the older header against clickjacking. DENY blocks all framing; SAMEORIGIN allows it only when every ancestor frame has the same origin as the page (MDN). ALLOW-FROM is obsolete and modern browsers ignore it, so name partner sites with frame-ancestors.
It works only as an HTTP header; in a meta element it has no effect. MDN points to frame-ancestors for more comprehensive options. If you send both, make them match: SAMEORIGIN with 'self', DENY with 'none'.
Headers to stop sending
Some headers do harm or nothing. X-XSS-Protection controlled a filter that current browsers have removed, and MDN warns that it could create cross-site scripting holes in otherwise safe pages; leave it out, or send 0, and rely on a Content Security Policy instead.
Server and X-Powered-By often announce the exact software and version behind a site. Removing them does not fix anything, but it stops handing automated scanners a ready list of what to try, and a scanner grade sometimes counts them.
CORS: cross-origin resource sharing
CORS headers relax the browser's same-origin rule: they tell it which other origins may read your responses. They grant access rather than restrict it, so the security question is what you allow. The mistakes that leak data are on CORS misconfiguration.
- Do not reflect any Origin back while also allowing credentials; that lets every site read signed-in responses.
- List allowed origins explicitly, and avoid patterns that match look-alike domains.
- Return CORS headers only where needed, such as API endpoints called from other origins.
Questions
What are HTTP security headers?
HTTP security headers are response headers that tell browsers how to treat a site, such as using HTTPS only, running only allowed scripts, refusing framing by other sites and limiting referrer information.
How do I check my website's security headers?
Open the browser's developer tools, reload the page and read the response headers in the Network tab, or request the page from the command line and print only the headers.
What does Referrer-Policy strict-origin-when-cross-origin mean?
It sends the full address within your site, only the origin to other sites over HTTPS, and nothing when going from HTTPS to HTTP.
Which security headers are most important?
Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, frame protection with frame-ancestors or X-Frame-Options, and Referrer-Policy.
What does Strict-Transport-Security do?
HTTP Strict Transport Security, set with the Strict-Transport-Security header, tells the browser to use HTTPS only for the seconds in max-age. This security header is ignored over plain HTTP, and max-age=0 ends the policy.
Is HTTPS Strict Transport Security the same as HSTS?
Yes. HSTS stands for HTTP Strict Transport Security; searches also write HTTPS strict transport security. All mean the Strict-Transport-Security header defined in RFC 6797.
Do I still need X-Frame-Options?
Against clickjacking, frame-ancestors in Content-Security-Policy is the current control and offers more options. Among security headers, X-Frame-Options (DENY or SAMEORIGIN, HTTP header only) can be sent alongside it for older browsers.