Security headers

Also known as: HTTP security headers

On this page 7

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.

HeaderWhat it controlsMain threat reduced
Strict-Transport-SecurityForces HTTPS for the originSSL stripping and downgrade
Content-Security-PolicyAllowed sources for scripts, styles and framesCross-site scripting and injection
X-Frame-OptionsWhether the page may be framedClickjacking
X-Content-Type-OptionsDisables MIME type sniffingContent type confusion
Referrer-PolicyHow much URL data is sent as referrerLeakage of tokens and paths
Permissions-PolicyAccess to camera, microphone, geolocationAbuse of powerful browser features
Core security headers and their purpose

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.

HeaderBaseline valueNotes
Strict-Transport-Securitymax-age=31536000; includeSubDomainsAdd preload only after testing all subdomains
Content-Security-Policydefault-src 'self'; object-src 'none'Prefer nonces or hashes for scripts
X-Frame-OptionsDENY or SAMEORIGINSuperseded by CSP frame-ancestors
X-Content-Type-OptionsnosniffOnly one valid value
Referrer-Policystrict-origin-when-cross-originDefault in modern browsers
Cross-Origin-Opener-Policysame-originIsolates the browsing context
Typical baseline values

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 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.

ControlWhere it actsRelationship to security headers
Input validationServer-side codePrevents malformed data from entering
Output encodingTemplates and renderingRemoves the root cause of XSS
Web application firewallNetwork edgeBlocks known attack patterns in requests
Cookie attributesSet-Cookie headerSecure, HttpOnly and SameSite flags protect sessions
CORS headersBrowser and serverRelax, rather than tighten, the same-origin policy
How security headers relate to other controls

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.

MistakeConsequenceFix
HSTS preload without subdomain testingInternal HTTP hosts become unreachableInventory subdomains before preloading
CSP with 'unsafe-inline' scriptsXSS protection largely lostUse nonces or hashes
Duplicate headers from app and proxyBrowser combines or rejects policiesSet each header in one layer
Headers only on HTML pagesDownloads and errors stay unprotectedApply globally
Relying on a scanner grade aloneWeak values pass as compliantReview directive values manually
Frequent misconfigurations

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.