HTTPS and security headers: what to set and why
Security headers are short instructions your server sends with every page. They tell the browser to use HTTPS, which scripts to trust and what not to share. This guide covers what each one does, which are worth your time, and how to turn them on safely.
Updated · For agents: Markdown · skill
What a header can and cannot do
Every time a browser asks for a page, your server answers with the page and a list of headers. Most describe the response: its type, its length, how long to cache it. A few are instructions to the browser about how to treat your site. Those are the security headers.
They are a second line of defence. A Content Security Policy does not remove a flaw that lets an attacker inject a script. It stops the browser from running that script. That is worth having, and it is also why a missing header is not, on its own, a hole in your site. Scanners that mark every missing header in red overstate it. The honest question for each header is what it would protect on your site, and what it costs to set.
For most sites the answer is: HTTPS everywhere is required, four headers are one line each and safe, and one (the Content Security Policy) takes real work.
HTTPS first
HTTPS encrypts the connection between the visitor and your server, so nobody on the same wifi can read or change what is sent. Browsers mark plain HTTP pages as not secure, and features such as payments and location only work over HTTPS.
Three things to get right:
- A valid certificate on every host name you use, renewed automatically. Most hosts and CDNs do this for free.
- One redirect from
http://tohttps://, and from the host name you do not use (www or not) to the one you do. - No mixed content. An HTTPS page that loads a script or image over
http://gets it blocked or upgraded by the browser. Search your templates forhttp://.
The headers, one by one
| Header | What it tells the browser | Effort |
|---|---|---|
Strict-Transport-Security |
Only ever use HTTPS for this site | One line, with care |
X-Content-Type-Options |
Do not guess a file’s type | One line |
Referrer-Policy |
How much of the URL to pass to other sites | One line |
Permissions-Policy |
Which device features pages may use | One line |
Content-Security-Policy |
Where scripts, styles and frames may load from | Real work |
Strict-Transport-Security (HSTS)
Your redirect from HTTP to HTTPS has one weak moment: the very first request, which is still unencrypted. HSTS closes it. Once a browser has seen the header, it goes straight to HTTPS for as long as max-age says, without asking.
Strict-Transport-Security: max-age=31536000; includeSubDomains
Two cautions. includeSubDomains applies to every subdomain, so an old one that only works over HTTP becomes unreachable. And the preload list, which builds your site into browsers so even the first visit is protected, is slow to leave. Start with a short max-age, confirm nothing breaks, then raise it.
X-Content-Type-Options
X-Content-Type-Options: nosniff
Without it a browser may decide that a file you served as text is really a script, and run it. With it, the browser trusts the type you declared. There is no reason not to set this.
Referrer-Policy
When a visitor follows a link from your site, the browser tells the next site where they came from. If your URLs contain search terms, order numbers or reset tokens, that leaks them.
Referrer-Policy: strict-origin-when-cross-origin
This sends the full URL within your own site and only the host name to others. Modern browsers already default to it. Setting it makes the behaviour the same everywhere.
Permissions-Policy
A third-party script on your page can ask for the camera, the microphone or the location in your site’s name. Permissions-Policy switches off what you do not use:
Permissions-Policy: camera=(), microphone=(), geolocation=()
Framing: frame-ancestors
Clickjacking puts your page in an invisible frame on another site and tricks the visitor into clicking something on it. It only matters on pages where one click does something that counts: a payment, a delete, a settings change.
Content-Security-Policy: frame-ancestors 'self'
This replaces the older X-Frame-Options: SAMEORIGIN. If your pages are meant to be embedded, such as a video player or a booking widget, leave it off those pages.
Content-Security-Policy (CSP)
CSP is a list of the places your pages may load scripts, styles, images and frames from. A script injected from anywhere else does not run. It is the strongest header and the easiest to get wrong, because one host missing from the list breaks a feature: the payment form, the chat widget, the analytics.
Add it in three steps:
- Report only. Send
Content-Security-Policy-Report-Onlywith your first draft. Nothing is blocked. The browser console lists what would have been. - Fix the list. Walk through every page type, including checkout and login, and add each host you really use.
- Enforce. Rename the header to
Content-Security-Policy, and check the same pages again.
A policy that allows 'unsafe-inline' scripts protects little. If your site relies on inline scripts, a nonce or a hash per script is the proper fix, and it is a development task.
Cookies and cross-origin requests
Two more settings travel in headers:
- Session cookies should be
Secure(HTTPS only),HttpOnly(not readable by scripts) andSameSite=LaxorStrict(not sent with requests from other sites). Cookies that only remember a language or a theme do not need this. - CORS headers do the opposite of the rest: they relax a browser rule so another site may call your API.
Access-Control-Allow-Origin: *is fine for public data. Combined with credentials, on an API that uses cookies, it lets any site act as your signed-in visitor. Name the origins you trust.
How to add them
Where the headers go depends on what serves your site:
- A CDN or host such as Cloudflare, Netlify or Vercel: a rules page or a headers file.
- nginx or Apache:
add_headerorHeader setin the server configuration. - The application: a middleware, for example Helmet for Node.
Add one header at a time, load the key pages, and read the browser console. Then test again after every release, because a new script from a new host is exactly what a CSP blocks. WitnessQA reports a resource blocked by your own policy as an issue, with the page and the host.
Watch
- Missing HTTP Security Headers - Bug Bounty Tips, LiveOverflow. Why a missing header is not automatically a vulnerability, header by header.
- HTTP Secure Headers for Web App Security, ByteMonk. A quick overview of each header with example values.
- Content Security Policy explained, Jan Goebel. CSP and cross-site scripting on their own.
The checklist
Every step says what to do and how to verify it from outside. The tags are the WitnessQA checks that verify it for you in a free audit. Agents can load the same list as a skill file.
- Serve everything over HTTPS
Every page answers on https:// with a valid certificate. Plain http:// redirects there with one permanent redirect.
Verify: Request the http:// URL of the home page. It answers 301 or 308 to the https:// URL, and that URL answers 200 without a certificate error.
- Load nothing over plain HTTP
Every script, stylesheet, image and frame on an HTTPS page is loaded over HTTPS too.
Verify: Read the HTML of each key page. No
srcorhrefof a loaded resource starts withhttp://. - Tell browsers to stay on HTTPS
Send
Strict-Transport-Securitywith a max-age of at least six months. AddincludeSubDomainsonly when every subdomain works over HTTPS.Verify: Read the response headers of the home page over HTTPS.
strict-transport-securityis present withmax-ageof 15552000 or more. - Stop the browser guessing file types
Send
X-Content-Type-Options: nosniffon every response.Verify: Read the response headers.
x-content-type-optionsisnosniff. - Decide who may frame your pages
Pages with logins, payments or account actions send
Content-Security-Policy: frame-ancestors 'self'(orX-Frame-Options: SAMEORIGIN). Pages meant to be embedded do not need it.Verify: Read the response headers of the login and checkout pages. One of the two headers is present.
- Limit what you tell other sites
Send
Referrer-Policy: strict-origin-when-cross-originor stricter, and keep tokens and personal data out of URLs.Verify: Read the response headers.
referrer-policyis present and is notunsafe-urlorno-referrer-when-downgrade. - Switch off browser features you do not use
Send
Permissions-Policythat turns off camera, microphone and location unless a page needs them.Verify: Read the response headers.
permissions-policyis present. - Add a Content Security Policy, in report-only mode first
Start with
Content-Security-Policy-Report-Only, fix what it reports, then enforce it. Avoidunsafe-inlinefor scripts where you can.Verify: Read the response headers for
content-security-policy. Load each key page in a browser and confirm the console shows no resource refused by the policy. - Protect the session cookie
Cookies that keep a visitor signed in are set with
Secure,HttpOnlyandSameSite=LaxorStrict.Verify: Sign in with a test account and read the
set-cookieheaders. The session cookie carries all three attributes.
See which of these your site passes, with evidence for each.
Run a free audit