Security headers are HTTP response headers that a web server sends to instruct the browser to apply specific protective policies while it loads and runs a page. In application security, they form a defense-in-depth layer within cybersecurity practice: they do not fix vulnerable code, but they reduce the impact of common attacks such as cross-site scripting, clickjacking, protocol downgrade and data leakage through referrers.
The browser is the enforcement point. A server declares a policy once per response, for example Strict-Transport-Security or Content-Security-Policy, and every compliant browser applies it to that origin. Because the configuration usually lives in a web server, reverse proxy or framework middleware, HTTP security headers are among the cheapest controls to deploy, yet they are also easy to misconfigure.
How security headers work in the browser
Security headers work by shifting enforcement to the client: the server states rules, and the browser refuses actions that violate them. A header can restrict where scripts may load from, forbid the page from being displayed inside a frame, force all future connections to use TLS, or stop the browser from guessing a file type from its content.
Some headers act on the current response only, while others create persistent state in the browser. Strict-Transport-Security is the main example of the second kind: once received over a valid HTTPS connection, the browser remembers it for the declared max-age, often one year or longer, and silently upgrades every later request to HTTPS. This persistence is powerful but means a mistake also persists until the policy expires.
The following table summarizes the headers most often audited in web assessments and what each one controls.
| Header | What it controls | Main threat reduced |
|---|---|---|
| Strict-Transport-Security | Forces HTTPS for the origin | SSL stripping and downgrade |
| Content-Security-Policy | Allowed sources for scripts, styles and frames | Cross-site scripting and injection |
| X-Frame-Options | Whether the page may be framed | Clickjacking |
| X-Content-Type-Options | Disables MIME type sniffing | Content type confusion |
| Referrer-Policy | How much URL data is sent as referrer | Leakage of tokens and paths |
| Permissions-Policy | Access to camera, microphone, geolocation | Abuse of powerful browser features |
Key directives and recommended values
Each header has a small set of directives, and the chosen values matter more than the mere presence of the header. A Content-Security-Policy that allows 'unsafe-inline' scripts, for instance, offers little protection against XSS, even though a scanner may report the header as present.
The table below lists commonly recommended baseline values. They are starting points that must be adapted to the application, not universal settings.
| Header | Baseline value | Notes |
|---|---|---|
| Strict-Transport-Security | max-age=31536000; includeSubDomains | Add preload only after testing all subdomains |
| Content-Security-Policy | default-src 'self'; object-src 'none' | Prefer nonces or hashes for scripts |
| X-Frame-Options | DENY or SAMEORIGIN | Superseded by CSP frame-ancestors |
| X-Content-Type-Options | nosniff | Only one valid value |
| Referrer-Policy | strict-origin-when-cross-origin | Default in modern browsers |
| Cross-Origin-Opener-Policy | same-origin | Isolates the browsing context |
Some older headers are now deprecated or harmful. X-XSS-Protection controlled a filter that modern browsers have removed, and enabling it in legacy browsers could introduce new information leaks; current guidance is to omit it or set it to 0. Public-Key-Pins was withdrawn because a wrong pin could lock users out of a site for months.
Security headers compared with related controls
Security headers complement but never replace secure coding. Output encoding and input validation remove the root cause of injection flaws, whereas a header limits what an attacker can achieve if a flaw remains. This distinction matters in risk assessments, because a strong policy can turn a critical XSS into a low-impact finding without making the code correct.
The comparison below places security headers next to neighboring controls in the same area.
| Control | Where it acts | Relationship to security headers |
|---|---|---|
| Input validation | Server-side code | Prevents malformed data from entering |
| Output encoding | Templates and rendering | Removes the root cause of XSS |
| Web application firewall | Network edge | Blocks known attack patterns in requests |
| Cookie attributes | Set-Cookie header | Secure, HttpOnly and SameSite flags protect sessions |
| CORS headers | Browser and server | Relax, rather than tighten, the same-origin policy |
A frequent misconception concerns CORS. Headers such as Access-Control-Allow-Origin are often grouped with security headers, yet they grant permissions to other origins. An overly broad CORS configuration weakens security, so it belongs in the same review but with the opposite goal.
Deploying and testing security headers
The safest rollout is incremental and observable. Teams usually start with low-risk headers such as X-Content-Type-Options and Referrer-Policy, then introduce HSTS with a short max-age of minutes or days, and raise it once no HTTP-only resources remain.
For Content Security Policy, the Content-Security-Policy-Report-Only variant lets a team collect violation reports without blocking anything. After the reports show which sources are legitimate, the policy moves to enforcement mode.
- Set headers in one central place, such as the reverse proxy or framework middleware, to avoid gaps between routes.
- Apply headers to error pages and redirects as well, since attackers often target those responses.
- Verify the result with automated scans in the CI/CD pipeline and with browser developer tools.
- Document every exception, for example a third-party widget that requires framing.
Limitations and common mistakes
Security headers depend on browser support and correct delivery. Non-browser clients such as mobile API libraries or command-line tools ignore most of them, so they offer no protection for pure APIs beyond a few edge cases. A header that a CDN strips or overwrites also gives a false sense of safety.
The table below lists recurring mistakes found during audits and how to correct them.
| Mistake | Consequence | Fix |
|---|---|---|
| HSTS preload without subdomain testing | Internal HTTP hosts become unreachable | Inventory subdomains before preloading |
CSP with 'unsafe-inline' scripts | XSS protection largely lost | Use nonces or hashes |
| Duplicate headers from app and proxy | Browser combines or rejects policies | Set each header in one layer |
| Headers only on HTML pages | Downloads and errors stay unprotected | Apply globally |
| Relying on a scanner grade alone | Weak values pass as compliant | Review directive values manually |
Another limitation is maintenance cost. A strict CSP must change whenever the application adds an analytics script or a new content delivery domain, and teams that cannot keep up tend to loosen policies until they protect very little.
In simple terms
Security headers are short instructions that a website attaches to every page it sends. The browser reads them and follows the rules, such as always use a secure connection, never show this page inside another site or only run code from these trusted places.
Think of them as house rules posted at the door. They do not repair a broken lock, but they make it much harder for an intruder to do damage once inside. The main takeaway is that these headers are cheap, quick protection that works best on top of well-written code.
A practical example
A regional online retailer runs a checkout application behind a reverse proxy. An external penetration test finds a stored XSS flaw in product reviews and notes that the site sends no security headers at all. Fixing the review template will take two sprints, so the security team adds headers at the proxy within one day.
They enable X-Content-Type-Options: nosniff, Referrer-Policy: strict-origin-when-cross-origin and X-Frame-Options: DENY immediately. HSTS starts with a max-age of 300 seconds and is raised to one year after two weeks without errors. A Content Security Policy runs in report-only mode for ten days, revealing two legitimate payment and analytics domains, and then moves to enforcement with script nonces.
When testers retry the XSS payload, the browser blocks the injected script and sends a violation report. The flaw is still fixed in code later, but the exposure window is closed in days rather than weeks.