Security headers
Goal
Send the headers that tell browsers to apply protections, without breaking the site.
A good result is checked on the live response, applied at one layer, and rolled out in stages where a mistake is hard to undo.
When to use
- A scanner or audit reports missing headers.
- A site is moving to HTTPS only.
- The user asks what a header does before adding it.
- For the CSP itself, use
use_skillwithxss-csp-review.
Steps
- Fetch the real headers with
run_command:curl -sI https://<host>/and one API path. Record them as the baseline. - Find where headers are set now with
rgforhelmet,add_header,Header set,X-Frame-Options,Strict-Transport-Securityand middleware, and read the files. - Choose one layer to own the headers (app middleware, reverse proxy or CDN) so values do not duplicate or conflict.
- Set
Strict-Transport-Securitywith a shortmax-agefirst. Raise it after the whole site, including subdomains, works over HTTPS. Do not addpreloaduntil sure. - Set
X-Content-Type-Options: nosniff, and correctContent-Typewith charset on responses. - Set clickjacking protection with CSP
frame-ancestors(andX-Frame-Optionsfor old browsers) matching which sites may embed yours. - Set
Referrer-Policyto a conservative value, andPermissions-Policyto disable features the app does not use. - Set
Cache-Control: no-storeon responses with private data, and keep the shared cache away from authenticated pages. - Remove headers that leak versions, such as
Serverdetails andX-Powered-By. - Check cookies' flags, and cross-origin isolation headers (
Cross-Origin-Opener-Policy) only where needed. Verify names and values against MDN withread_urlsince browsers change. - Apply with
edit_file, deploy to staging, and re-run the curl check on pages, API and static files, including error pages. - Report before and after and any header deliberately left out with the reason.
Gotchas
- HSTS with a long age on a domain that still has an HTTP-only subdomain locks users out for months. Ramp up slowly.
- Headers set by both the app and the proxy appear twice, and browsers may take the stricter or ignore both.
- Error pages, redirects and static assets often miss headers that the main app adds. Test them.
X-XSS-Protectionis obsolete and can introduce problems; do not add it from an old checklist.- A scanner grade is a prompt for thought, not the goal. A header that breaks login is worse than a missing one.
frame-ancestorsin CSP ignores some legacy behaviour, so confirm with a real embedding test.
Output
- Before and after header table.
- The config changes by file and layer.
- Staged rollout notes for HSTS and any enforcing policy.
