1. Start with the threat and the browser behavior
Security headers are response metadata that tell a browser how it should handle a page or resource. They can reduce exposure to classes of browser-side problems, but they do not repair vulnerable application logic. A strong Content Security Policy (CSP), for example, can constrain script execution and resource loading, yet output encoding, input validation and safe templating still matter. Likewise, HSTS can make future browser visits HTTPS-only, but it cannot fix an invalid certificate or a compromised origin.
Before adding a header, inventory the site's actual behavior: first-party JavaScript, inline scripts and styles, analytics, payment widgets, iframes, fonts, image CDNs, API origins and any user-uploaded files. A header that blocks a required dependency creates an outage; a header made deliberately permissive just to avoid breakage may provide much less protection than the configuration appears to promise.
2. Establish a conservative baseline
A practical baseline for many ordinary sites starts with headers that have narrow, understandable effects:
X-Content-Type-Options: nosniff ; Referrer-Policy: strict-origin-when-cross-origin ; X-Frame-Options: DENY ; Permissions-Policy: camera=(), microphone=(), geolocation=()X-Content-Type-Options: nosniff tells browsers to respect declared MIME types instead of trying to infer a different type. This is especially useful when scripts, styles or uploaded content could otherwise be interpreted in an unsafe way.
Referrer-Policy controls how much of the current URL is disclosed in the Referer request header. strict-origin-when-cross-origin keeps the full URL for same-origin requests while sending only the origin to a different HTTPS origin and withholding referrer information on downgrade. Sites with stricter privacy requirements can evaluate same-origin or no-referrer, but compatibility with analytics and legitimate cross-site workflows must be tested.
Permissions-Policy can disable browser capabilities that the application does not use. Browser support varies by feature, so treat it as defense-in-depth and verify the exact directives against current browser documentation.
3. Treat CSP as an application change, not a copy-and-paste header
CSP is one of the most powerful browser security controls because it can restrict where scripts and other resources may load from and what they may do. It is also one of the easiest headers to deploy badly.
Do not begin by copying a huge domain allowlist from a scanner recommendation. First identify every legitimate script execution path. A strict policy usually uses nonces or hashes for scripts instead of broadly allowing inline execution. Avoid 'unsafe-inline' and 'unsafe-eval' unless a documented compatibility constraint makes them temporarily unavoidable.
A small starting policy for a simple application might resemble:
Content-Security-Policy: ; default-src 'self'; ; object-src 'none'; ; base-uri 'self'; ; frame-ancestors 'none'; ; form-action 'self';That is only an example. Real applications often need explicit script-src, style-src, img-src, font-src, connect-src and frame-src rules. Before enforcing a new policy, MDN recommends using Content-Security-Policy-Report-Only to observe violations. Review the reports, distinguish real dependencies from injection/noise, then narrow the policy deliberately.
A useful deployment sequence is: inventory resources, create a candidate policy, run it in report-only mode, test representative user journeys, correct legitimate violations, enforce on a small environment, then monitor production. A CSP that is never reviewed after new third-party scripts are added will drift.
4. Use HSTS only after HTTPS is operationally boring
HSTS tells a browser that future requests to a hostname must use HTTPS. This protects against protocol downgrade on later visits, but the browser will also refuse to let a user click through an invalid certificate for an HSTS host.
A cautious rollout begins with the apex host after HTTPS, renewal and rollback paths are proven:
Strict-Transport-Security: max-age=86400After operating successfully, the organization may increase max-age. Adding includeSubDomains is a materially larger decision because every covered subdomain must support HTTPS for the policy lifetime. Preloading is larger again: browser preload requirements include a long max-age and includeSubDomains, and removal is not instantaneous. Do not preload a domain simply to improve a scanner grade.
Also remember that browsers only honor HSTS received over HTTPS. Sending the header over HTTP does not create a trustworthy policy.
5. Decide how clickjacking protection is expressed
X-Frame-Options: DENY is easy to understand for a site that never needs to be embedded. Modern CSP can express framing policy with frame-ancestors, which is more flexible. If the application legitimately embeds or is embedded by specific origins, design that relationship explicitly instead of disabling protection globally.
When both legacy X-Frame-Options and CSP frame-ancestors are present, test actual target browsers and application flows. The objective is not to maximize the number of headers; it is to ensure the effective policy matches the product's intended embedding model.
6. Verify headers on the responses that matter
A homepage check is not enough. Inspect HTML documents, API responses, static assets, redirects, error pages and user-uploaded content. A reverse proxy or CDN may add, remove or overwrite headers differently from the application server.
Examples:
curl -sS -D - -o /dev/null https://example.com/ ; curl -sS -D - -o /dev/null https://example.com/login ; curl -sS -D - -o /dev/null https://example.com/not-foundFor CSP, use browser developer tools and report-only telemetry rather than relying only on a header dump. Verify that core paths still work: sign-in, form submission, downloads, payments, API calls, embedded media and any third-party authentication redirects.
A scanner can reveal missing or weak headers, but a “green” scanner result does not establish that application authorization, session management, input handling or server-side controls are correct.
7. Keep the policy maintainable
Store security-header configuration as code where possible. Record why each third-party origin is allowed and who owns the dependency. Make header changes reviewable alongside application changes. If a new analytics or ad script requires CSP expansion, review the requested source rather than opening broad wildcards.
For services that will add advertising later, plan CSP and consent together. Ad scripts introduce external script and network origins; blindly relaxing CSP on launch day is a predictable failure mode. Test the final consent/CMP and advertising integration in a protected environment before public enforcement changes.
Operational checklist
- Inventory actual resource origins and browser features.
- Add low-risk headers and verify all response classes.
- Build CSP from application requirements; use report-only first.
- Confirm certificate issuance/renewal before increasing HSTS lifetime.
- Do not add includeSubDomains until all subdomains are ready.
- Test login, forms, APIs, redirects and third-party integrations.
- Capture before/after headers and browser console evidence.
- Treat scanner scores as evidence inputs, not release qualification.
- Recheck after CDN, framework or advertising changes.
- Maintain an explicit rollback for every enforcement change.
Related guides
- ACME Certificate Renewal Failures: A Staging-First Troubleshooting Playbook — Web Infrastructure
- Cloudflare Tunnel Troubleshooting: Separate 1033, 502, Origin TLS and Application Failures — Web Operations
- The eMudhra CA Compromise: How to Handle Emergency Certificate Distrust Without Breaking Everything — PKI Security
Primary sources & further reading
- OWASP Secure Headers Project ↗
- OWASP Secure Headers — Best Practices ↗
- MDN — Content Security Policy implementation ↗
- MDN — Strict-Transport-Security ↗
- MDN — X-Content-Type-Options ↗
- MDN — Referrer Policy ↗
Sources are credited for verification, not reproduced. Linked pages may change after publication.