# HTTPS and security headers: what to set and why

> What HTTPS, HSTS, Content Security Policy and the other security headers do, which ones matter for your site, and how to add them without breaking it.

Published 2026-12-02, updated 2026-12-02. Source: https://witnessqa.com/learn/https-and-security-headers/

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.

## 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://` to `https://`, 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](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content). Search your templates for `http://`.

## 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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Strict-Transport-Security) 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](https://hstspreload.org/), 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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Content-Type-Options), 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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Referrer-Policy). 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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/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:

1. **Report only.** Send `Content-Security-Policy-Report-Only` with your first draft. Nothing is blocked. The browser console lists what would have been.
2. **Fix the list.** Walk through every page type, including checkout and login, and add each host you really use.
3. **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) and `SameSite=Lax` or `Strict` (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_header` or `Header set` in 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](https://witnessqa.com/docs/checks/#csp) as an issue, with the page and the host.

## Watch

- [Missing HTTP Security Headers - Bug Bounty Tips](https://www.youtube.com/watch?v=064yDG7Rz80), LiveOverflow. Why a missing header is not automatically a vulnerability, header by header.
- [HTTP Secure Headers for Web App Security](https://www.youtube.com/watch?v=4bQeGUzHpOE), ByteMonk. A quick overview of each header with example values.
- [Content Security Policy explained](https://www.youtube.com/watch?v=txHc4zk6w3s), Jan Goebel. CSP and cross-site scripting on their own.

## The checklist

The same steps as an agent skill: https://witnessqa.com/learn/https-and-security-headers/skill.md

1. **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.
   WitnessQA checks: [seo_https_redirect](https://witnessqa.com/docs/checks/#seo_https_redirect), [seo_host_redirect](https://witnessqa.com/docs/checks/#seo_host_redirect)
2. **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 `src` or `href` of a loaded resource starts with `http://`.
3. **Tell browsers to stay on HTTPS.** Send `Strict-Transport-Security` with a max-age of at least six months. Add `includeSubDomains` only when every subdomain works over HTTPS.
   Verify: Read the response headers of the home page over HTTPS. `strict-transport-security` is present with `max-age` of 15552000 or more.
4. **Stop the browser guessing file types.** Send `X-Content-Type-Options: nosniff` on every response.
   Verify: Read the response headers. `x-content-type-options` is `nosniff`.
5. **Decide who may frame your pages.** Pages with logins, payments or account actions send `Content-Security-Policy: frame-ancestors 'self'` (or `X-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.
6. **Limit what you tell other sites.** Send `Referrer-Policy: strict-origin-when-cross-origin` or stricter, and keep tokens and personal data out of URLs.
   Verify: Read the response headers. `referrer-policy` is present and is not `unsafe-url` or `no-referrer-when-downgrade`.
7. **Switch off browser features you do not use.** Send `Permissions-Policy` that turns off camera, microphone and location unless a page needs them.
   Verify: Read the response headers. `permissions-policy` is present.
8. **Add a Content Security Policy, in report-only mode first.** Start with `Content-Security-Policy-Report-Only`, fix what it reports, then enforce it. Avoid `unsafe-inline` for 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.
   WitnessQA checks: [csp](https://witnessqa.com/docs/checks/#csp)
9. **Protect the session cookie.** Cookies that keep a visitor signed in are set with `Secure`, `HttpOnly` and `SameSite=Lax` or `Strict`.
   Verify: Sign in with a test account and read the `set-cookie` headers. The session cookie carries all three attributes.