HTTP security headers
Before a page's content, a web server sends response headers: named values that the browser reads first. HTTP security headers are the ones that restrict what the page may do and how the browser may reach it. Set correctly, they help prevent cross-site scripting, clickjacking and information disclosure.1 A server that sends none of them, or sends them with weak values, is misconfigured.4
| Header | What it controls | Recommended value |
|---|---|---|
Strict-Transport-Security | The browser reaches the host only over HTTPS | max-age=31536000; includeSubDomains |
Content-Security-Policy | Which resources the page may load, and who may frame it | frame-ancestors 'none'; the full policy is in Content Security Policy |
X-Frame-Options | Whether other sites may frame the page | DENY |
X-Content-Type-Options | The browser keeps to the declared file type | nosniff |
Referrer-Policy | How much of the page address other sites receive | strict-origin-when-cross-origin |
Permissions-Policy | Which browser features the page may use | geolocation=(), camera=(), microphone=() |
Cross-Origin-Opener-Policy | Whether the page shares a browsing context group with cross-origin documents (a defence against attacks like Spectre) | same-origin |
X-XSS-Protection | A legacy browser filter | 0, or not sent |
Server, X-Powered-By | Name the software behind the site | Removed, or a value that names nothing |
A header matters only where the browser shows or runs something. On a redirect or an API route that returns JSON, X-Frame-Options gives no protection, and a CSP may be meaningless.1 API endpoints need a short set:5
Content-Security-Policy: default-src 'none'; frame-ancestors 'none'
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniffStrict-Transport-Security
HSTS tells the browser to reach the host only over HTTPS. Later requests over HTTP are upgraded to HTTPS, and the user can no longer click past a certificate error. The header changes future requests, not the current response, and browsers ignore it when it arrives over plain HTTP, where an attacker could have altered it. A request over HTTP should get a permanent redirect, such as 301, to HTTPS, without the header.2
Take a user who visits example.com at home and and receives max-age=31536000; includeSubDomains. Weeks later, on a rogue Wi-Fi access point, the user opens http://login.example.com/. The browser uses HTTPS instead, the attacker has no valid certificate for the domain, and the browser shows a certificate error that the user cannot bypass.2
max-age is the number of seconds the browser remembers the rule. max-age=0 turns HSTS off. includeSubDomains applies the rule to every subdomain. HSTS does not cover the first visit, before the browser has received the header over a secure connection. The preload list, hosted by Google and used by all browsers, mitigates this.2 A listed domain needs max-age of at least 31536000 seconds (one year), includeSubDomains, preload and HTTPS on every subdomain. A removal takes months to reach users, and the list covers internal subdomains too, so hstspreload.org advises against adding preload by default.6 A long max-age also means that if the certificate expires or is revoked, visitors may be unable to reach the site until the time runs out.1
Content-Security-Policy
A CSP controls which resources, in particular scripts, a document may load, mainly as a defence against XSS. It does not replace sanitising input, and 'unsafe-inline' defeats much of its purpose.3
Framing
Without X-Frame-Options or the CSP directive frame-ancestors, the browser lets any site embed the page in a frame.7 That default is what makes clickjacking possible.8 X-Frame-Options: DENY blocks every frame and SAMEORIGIN allows only frames from the same origin. The header has no effect in a <meta> element.7 frame-ancestors is the more flexible replacement, and 'none' is the right value for any site that need not be embedded.3
Other headers
X-Content-Type-Options: nosniff, the header's only valid value,9 makes the browser use the declared Content-Type instead of examining the content. A file served as text/plain is not treated as HTML even if it contains HTML markup, which prevents XSS attacks in which an uploaded file runs as an HTML document. Scripts and style sheets with the wrong type are blocked.10
Referrer-Policy decides how much of the page address the browser sends with requests, in the Referer header. With no policy, browsers use strict-origin-when-cross-origin: other sites get only the origin, and an HTTPS page sends nothing to an HTTP one.11
Permissions-Policy turns off browser features: camera=() disables the camera for the page and its frames,12 so a script injected through XSS cannot switch it on.1 The header is still marked experimental, and some widely used browsers do not support it.12
The legacy filter behind X-XSS-Protection can create XSS in sites that are otherwise safe, so the header should be 0 or absent. Server and X-Powered-By name the software, which helps attackers find its vulnerabilities.1 Express and Next.js send X-Powered-By by default.1314
Setting the headers
Next.js
headers() in next.config.js returns headers by path, and source: '/(.*)' matches every route.15 poweredByHeader: false removes X-Powered-By.14
// next.config.js
module.exports = {
poweredByHeader: false,
async headers() {
return [
{
source: '/(.*)',
headers: [
{ key: 'Strict-Transport-Security', value: 'max-age=31536000; includeSubDomains' },
{ key: 'X-Frame-Options', value: 'DENY' },
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
],
},
]
},
}The Next.js docs show SAMEORIGIN, origin-when-cross-origin and preload;9 the values above follow OWASP and hstspreload.org.
Vercel
On Vercel, headers can also be set in vercel.json. Vercel's own example also sends X-XSS-Protection: 1; mode=block, which switches on the legacy filter described above.16
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" }
]
}
]
}Express
The Helmet package sets 13 headers, among them Strict-Transport-Security: max-age=31536000; includeSubDomains, X-Frame-Options: SAMEORIGIN and X-XSS-Protection: 0, and removes X-Powered-By.17
npm install helmetimport helmet from "helmet";
const app = express();
app.use(helmet());nginx
nginx sends an add_header header only for certain status codes unless the line ends in always. An add_header inside a location block drops the headers set in the server block for that location, unless nginx 1.29.3 or later has add_header_inherit merge;. When nginx forwards to an application that sends the same header, the response carries both copies; proxy_hide_header removes the application's copy:1
proxy_hide_header X-Frame-Options;
add_header "X-Frame-Options" "DENY" always;Cloudflare
In the Cloudflare dashboard, on the Edge Certificates page, select Enable HSTS under HTTP Strict Transport Security (HSTS), then I understand and Next. Set Max Age Header (1 to 12 months) and the subdomain, Preload and No-Sniff Header options, then select Save. Turn on HTTPS first: removing HTTPS before turning off HSTS, or before the max age has passed, makes the site unreachable for the rest of that time.18
Checking the headers
Mozilla's HTTP Observatory, at developer.mozilla.org/en-US/observatory, tests a site's protection against XSS, manipulator-in-the-middle attacks, cross-domain information leakage, insecure cookies, CDN compromises and improperly issued certificates. The scan history of each domain is public.5
Prompt for a coding agent
Add HTTP security headers to every response of this project, in one place only (the framework config, the hosting config such as vercel.json, or the web server, not several). Set: Strict-Transport-Security: max-age=31536000; includeSubDomains (no preload). X-Frame-Options: DENY, unless a page must be shown inside another site's frame. X-Content-Type-Options: nosniff. Referrer-Policy: strict-origin-when-cross-origin. Permissions-Policy: camera=(), microphone=(), geolocation=(), leaving out any feature the app uses. Remove X-Powered-By (poweredByHeader: false in Next.js, helmet or app.disable('x-powered-by') in Express) and remove the version from the Server header. Remove any X-XSS-Protection header other than 0. If nginx is in front, end each add_header line with always and check that location blocks do not drop the headers. Do not add a Content-Security-Policy in this change. List each file you changed.Notable incidents
Ticketmaster UK (2018)
Ticketmaster put a chatbot from Inbenta Technologies on its payment page, loading its JavaScript from Inbenta's server. From 10 February 2018 until it was found on 23 June 2018, code inserted into that script collected names, card numbers, expiry dates and CVV numbers.19 Up to 9.4 million customers across Europe may have been affected, and the ICO fined Ticketmaster £1.25 million.20 Its penalty notice of 13 November 2020 found that Ticketmaster had used neither subresource integrity (SRI) nor a CSP before the breach. Among the measures it expected, it gave SRI as an example, and said that if SRI could not be used, Ticketmaster should have shown it had considered others, such as a CSP.19
In SecFlash
On the Security page, SecFlash reports missing headers as SF-SEC-101 (HSTS), SF-SEC-103 (CSP), SF-SEC-105 (framing), SF-SEC-106 (X-Content-Type-Options), SF-SEC-107 (Referrer-Policy) and SF-SEC-109 (Permissions-Policy). SF-SEC-102 reports a short HSTS max-age, SF-SEC-104 a CSP that allows 'unsafe-inline' or a wildcard in script-src, SF-SEC-108 a server version in the headers, SF-SEC-110 the HTTP Observatory grade and SF-SEC-199 other header findings. The ZAP scan reports the same gaps as SF-SEC-620 to SF-SEC-627. On the Cloud page, SF-CLD-311 reports a Cloudflare zone whose HSTS setting is off.
See also
- Content Security Policy
- Encryption in transit
- Cross-site scripting
- Cross-origin resource sharing
- Software supply chain compromise
- Category: Security concepts
References
- a b c d e f g "HTTP Headers Cheat Sheet". OWASP Cheat Sheet Series.
- a b c d "Strict-Transport-Security header". MDN Web Docs. Mozilla.
- a b c "Content Security Policy (CSP)". MDN Web Docs. Mozilla.
- back "A02:2025 Security Misconfiguration". OWASP Top 10:2025. OWASP Foundation.
- a b "HTTP Observatory FAQ". MDN Web Docs. Mozilla.
- back "HSTS Preload List Submission". hstspreload.org.
- a b "X-Frame-Options header". MDN Web Docs. Mozilla.
- back "CWE-1021: Improper Restriction of Rendered UI Layers or Frames". Common Weakness Enumeration (version 5.0). MITRE.
- a b Vercel (30 June 2026). "headers". Next.js Docs.
- back "X-Content-Type-Options header". MDN Web Docs. Mozilla.
- back "Referrer-Policy header". MDN Web Docs. Mozilla.
- a b "Permissions-Policy header". MDN Web Docs. Mozilla.
- back "Production Best Practices: Security". Express.
- a b Vercel (16 June 2025). "poweredByHeader". Next.js Docs.
- back Vercel (20 March 2026). "How to set a Content Security Policy (CSP) for your Next.js application". Next.js Docs.
- back Vercel (14 August 2026). "Project configuration (vercel.json)". Vercel Docs.
- back "Helmet.js". Helmet documentation.
- back Cloudflare (15 October 2026). "HTTP Strict Transport Security (HSTS)". Cloudflare SSL/TLS docs.
- a b Stephenson Harwood LLP (15 December 2020). "An analysis of the Monetary Penalty Notice issued by the Information Commissioner's Office to Ticketmaster UK Limited". Stephenson Harwood.
- back Society for Computers & Law (16 November 2020). "ICO fines Ticketmaster UK Limited £1.25million for data breach". SCL.
External links
- HTTP Headers Cheat Sheet, OWASP Cheat Sheet Series
- HTTP Observatory, MDN Web Docs