HTTP security headers: CSP and HSTS without breaking your site
HTTP security headers are short instructions that a server sends to the browser along with a page. They determine, for example, where the page may load scripts from, whether another site may embed it in a frame, or whether the site may only be opened over HTTPS. According to OWASP, they help prevent vulnerabilities such as cross-site scripting (XSS), clickjacking and information disclosure (OWASP).
If a security scan, an audit or a customer questionnaire has flagged them as missing, add them – but not all at once and not blindly. In short: deploy the simple headers right after testing, run Content-Security-Policy (CSP) first in Report-Only mode, which only reports violations, enable HSTS with a gradually increasing lifetime, and decide on the HSTS preload list last, because it is hard to undo.
Which security headers are worth setting
Not every header a scanner mentions is equally important, and some are no longer recommended. The table summarises MDN and OWASP recommendations for a typical business website or online shop.
| Header | What it does | Recommendation |
|---|---|---|
Content-Security-Policy |
Controls where the page may load resources from, especially JavaScript; it mainly serves as a defence against XSS (MDN: CSP). | Introduce gradually, starting in Report-Only mode. |
Strict-Transport-Security |
The browser remembers for a set number of seconds that the site may only be opened over HTTPS (MDN: HSTS). | Enable after checking subdomains, with a gradually increasing max-age. |
X-Content-Type-Options |
The value nosniff stops the browser from guessing the content type (OWASP). | Set in the first stage, together with a correct Content-Type header. |
Referrer-Policy |
Controls how much information about the originating page is sent with requests in the Referer header (OWASP). | Set explicitly to strict-origin-when-cross-origin. |
frame-ancestors, X-Frame-Options |
Control whether and by whom the page may be embedded in a frame, which protects against clickjacking (MDN: CSP). | Mainly the CSP frame-ancestors directive, with X-Frame-Options as a supplement. |
Permissions-Policy |
Controls which browser features, such as the camera, microphone or geolocation, the page and embedded frames may use (OWASP). | Disable features the site does not use. |
X-XSS-Protection, Expect-CT, Public-Key-Pins |
Headers OWASP does not recommend; in some cases X-XSS-Protection can even create an XSS vulnerability (OWASP). | Do not set X-XSS-Protection or turn it off with the value 0; remove the others. |
OWASP also recommends removing the X-Powered-By header, which reveals the technologies in use (OWASP). The most attention, however, goes to two headers that can break a site if deployed carelessly: CSP and HSTS.
Illustrative example: an online shop with analytics, chat and payments
A fictional company for illustration: a small manufacturer sells through an online shop built on WordPress and WooCommerce. A wholesale customer sent it a security questionnaire, and the attached scan flagged missing CSP and HSTS. The site runs analytics, a chat widget from an external provider, a store locator map and a payment gateway. The shop runs on the main domain, and an older warehouse interface that only works over HTTP runs on one of its subdomains.
A strict CSP copied from a tutorial would block scripts the policy does not account for, and the chat or the analytics would stop working. HSTS with the includeSubDomains directive applies to all subdomains, so browsers that had visited the shop would start opening the warehouse interface over HTTPS, which it does not support (MDN: HSTS). A sensible approach therefore starts with an inventory, not with configuration.
Step-by-step rollout
1. Inventory: what the site loads and where headers are set
List all external services: analytics, marketing scripts, chat, maps, videos, fonts, the payment gateway and form protection. Go through every page type, including the cart, checkout, customer account and admin area; online tools usually test only the homepage of the given address (OWASP).
Also find out where headers are set today: on the hosting, in a CDN, in a plugin or in the theme. If you lack access to any of these places, get it first; the article on taking over your domain, accounts and access will help.
2. Deploy the simple headers first
Start with headers that have a clear value, but test them too: nosniff assumes a correct Content-Type, and Permissions-Policy must not disable a feature the site uses, such as geolocation for finding the nearest store.
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), camera=(), microphone=()
Modern browsers use strict-origin-when-cross-origin by default, but OWASP recommends setting the value explicitly (OWASP).
3. Enforce frame protection separately
CSP can be introduced in parts: you can add clickjacking protection without restricting scripts (MDN: CSP). If the site should not be embedded in other pages, MDN recommends frame-ancestors 'none'; if the system shows its own pages in a frame, such as a preview in the admin area, use 'self'. OWASP recommends preferring frame-ancestors where possible, and MDN describes it as a more flexible replacement for X-Frame-Options (OWASP, MDN: CSP).
4. Run CSP in Report-Only mode first
The Content-Security-Policy-Report-Only header monitors policy violations and their effects without enforcing the policy, so you can fix problems before going live (MDN: Report-Only). For reports to go anywhere, the policy needs a destination: MDN names the report-to directive, whose address is defined by the Reporting-Endpoints header. It marks the older report-uri directive as deprecated, but its example declares both because report-to does not yet have full support across browsers.
If a response contains both Content-Security-Policy and Content-Security-Policy-Report-Only, the browser honours both: it enforces the first, while the second only reports (MDN: CSP). This way you enforce frame protection immediately and only monitor scripts for now:
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy: frame-ancestors 'self'
Content-Security-Policy-Report-Only: script-src 'nonce-{NONCE}' 'strict-dynamic'; object-src 'none'; base-uri 'none'; report-uri https://example.com/csp-reports; report-to csp-endpoint
{NONCE} is a placeholder for a random value the server generates for each response. Report-Only cannot be delivered through a <meta> element; the server must send the header (MDN: Report-Only). Store the reports, decide who reads them, and keep the mode running until it covers every page type and typical situations, such as a campaign.
5. Turn the reports into an enforced policy
For each violation, decide: allow a legitimate service, investigate an unknown source and remove an unnecessary script. MDN recommends a so-called strict CSP based on nonces or hashes, because allowlists of domains are hard to maintain, especially with scripts outside your control (MDN: CSP).
A nonce must be different for every response and unpredictable, so the server cannot serve static HTML (MDN: CSP). If the site uses full-page caching, solve this at the design stage, otherwise a cached page would repeat the same nonce. If a third-party script loads further scripts, a strict CSP blocks them; the 'strict-dynamic' keyword passes trust from a script with a nonce on to the scripts it loads.
Avoid 'unsafe-inline', which according to MDN defeats much of the purpose of CSP (MDN: CSP). Once the reports show no unexplained violations, switch the policy to Content-Security-Policy and keep report-to.
6. Enable HSTS in stages
The server must send the HSTS header over HTTPS only; browsers ignore it when it arrives over HTTP. Requests over HTTP should be permanently redirected to HTTPS, for example with status code 301 (MDN: HSTS). Before enabling it, verify that all subdomains work over HTTPS, including internal ones that are not publicly accessible. Then, following hstspreload.org, increase max-age in stages – 5 minutes, a week, a month (hstspreload.org):
Strict-Transport-Security: max-age=300; includeSubDomains
Strict-Transport-Security: max-age=604800; includeSubDomains
Strict-Transport-Security: max-age=2592000; includeSubDomains
In each stage, watch for broken pages and metrics such as traffic and revenue, fix problems, and wait the full max-age of the stage before moving on. You can then extend the lifetime, for example to a year (31536000 seconds), which is also the minimum for preload. With a misconfiguration or a certificate problem, however, visitors may be unable to reach the site until the HSTS lifetime expires (OWASP). Reliable automatic certificate renewal is therefore a prerequisite; it is covered in the article on shorter SSL certificate validity.
7. Preload only after a deliberate decision
The HSTS preload list is built directly into Chrome, and other major browsers have lists based on it. Inclusion requires a valid certificate, a redirect from HTTP to HTTPS on the same host, HTTPS on all subdomains and a header with a max-age of at least 31536000 seconds (1 year), includeSubDomains and preload (hstspreload.org).
Inclusion is hard to undo: a removal takes months to reach Chrome users, and it cannot be guaranteed for other browsers. In addition, Chrome and Safari automatically upgrade navigations from HTTP to HTTPS regardless of HSTS, and preload only helps when an active attacker makes that upgrade fail. The hstspreload.org site describes the benefit of preloading as minimal compared to HSTS and does not recommend preloading (hstspreload.org). For a typical business website, HSTS without preload is enough.
Where to set the headers
Each header should have one clear place. According to MDN, CSP should be sent with all responses, not just the main document (MDN: CSP).
| Place | Suitable for | Watch out for |
|---|---|---|
| Web server (Apache, Nginx) | Headers with a fixed value: HSTS for HTTPS responses, nosniff, Referrer-Policy. | Without the always parameter, Nginx sends a header only for certain status codes, and add_header in a location block drops headers set one level up; in Apache, unset followed by always set prevents duplicates (OWASP). |
| CDN or reverse proxy | Uniform rules for several servers or websites. | Values from the CDN and the server must not duplicate or overwrite each other; check the final response in the browser. |
| Application or plugin | CSP with a nonce that has to be generated for every response and inserted into the HTML (MDN: CSP). | Full-page caching must not store the nonce; if the server also sets headers, decide who manages which. |
| <meta> element in HTML | CSP for a single-page application with static files and no server infrastructure (MDN: CSP). | It does not support all CSP features, and Report-Only mode does not work through it. |
Trade-offs: protection versus maintenance
According to OWASP, CSP is complex to configure and maintain (OWASP). Every new marketing tool, chat or embedded content may mean a policy change. So define a procedure: who approves a new script, who tests it in Report-Only mode and who updates the header.
Nonces are easier to maintain on dynamically generated pages, but they require changes to templates and caching. Hashes suit static content, but they must be recalculated after every script change. An allowlist of domains looks like the easiest start, but MDN warns that such policies often inadvertently allow unsafe domains (MDN: CSP).
HSTS is a simple header and protects, for example, against an on-path attacker rewriting the redirect from HTTP to HTTPS (hstspreload.org). At the same time, it ties the site to working HTTPS for the whole max-age, so a certificate problem hits harder.
Common mistakes
- Copying values from a tutorial. The HSTS value recommended by OWASP includes both includeSubDomains and preload, and OWASP urges you to read carefully how the header works before using it (OWASP). Meanwhile, hstspreload.org treats the preload directive as a request for inclusion in the list (hstspreload.org).
'unsafe-inline'just to satisfy the scanner. The policy exists, but it loses much of its protection (MDN: CSP).- Report-Only without a report destination. Without report-to, and report-uri for browsers that do not support it, reports go nowhere (MDN: Report-Only).
- A nonce stored in the cache. The same nonce in several responses breaks the requirement that it be different and unpredictable every time.
- Headers set in several places. The server, CDN and plugin may duplicate or overwrite values; in Nginx, a location block can silently drop security headers (OWASP).
- Disabling HSTS by removing the header. If the header is missing, the previous one stays in effect until it expires; you can cancel it with
max-age=0(MDN: HSTS).
Checklist before going live
- The inventory of external services covers every page type and has an owner.
- Each header is set in one known place, and the final response has been verified.
- nosniff, Referrer-Policy, Permissions-Policy and frame-ancestors have been tested in forms, payment, login and the admin area.
- CSP runs in Report-Only mode with report-to and Reporting-Endpoints, and someone reads the reports regularly.
- The enforced CSP does not use
'unsafe-inline'for scripts, and a nonce is generated for every response. - All subdomains, including internal ones, work over HTTPS, and certificate renewal is automated and monitored.
- The HSTS max-age grows in stages, and preload is a deliberate decision, not a default.
- Every new third-party script goes through approval and a Report-Only test.
Frequently asked questions
How long should CSP stay in Report-Only mode?
There is no universal period. The reports should cover every page type and typical situations, and before enforcement they should contain no unexplained violations. Even after enforcement, you can test the next version of the policy in parallel in Report-Only mode (MDN: CSP).
Do I need X-Frame-Options if I have frame-ancestors?
Your main protection should be frame-ancestors. You can keep X-Frame-Options as a supplement; OWASP recommends the value DENY and its example also uses SAMEORIGIN (OWASP). The values should not contradict each other: frame-ancestors 'self' goes with SAMEORIGIN, not DENY.
How do I turn HSTS off if something breaks?
Set max-age=0. It only takes effect once the browser loads the site over HTTPS and receives this header (MDN: HSTS). For a domain on the preload list, removal takes months (hstspreload.org), so start with short stages.
Does a green scanner result mean the site is secure?
No. According to OWASP, CSP is an added layer of security that helps detect and mitigate certain types of attacks (OWASP). Headers do not replace updates, secure code, backups or access management; they are one layer of website security, not all of it.
Help with setting up the headers
If you want to add security headers to your business website or online shop without unnecessary risk of downtime, I will do the inventory, set up Report-Only mode, evaluate the reports and prepare a staged HSTS rollout. See how I work on websites and custom online shops on the Webiant homepage, or write to me via the contact page.
Sources
Sources were verified on 16 November 2026.