Full answer
Security headers are cheap, and each one closes a particular risk.
Strict-Transport-Security: max-age=31536000; includeSubDomains. After one HTTPS visit the browser refuses plain HTTP for the site for that time, which stops downgrade and cookie theft on the first request. Roll it out slowly, because a mistake on a subdomain is hard to reverse. Preload lists exist but are a long commitment.X-Content-Type-Options: nosniff. The browser must trust the declaredContent-Type. It stops a text or image upload from being run as a script.Content-Security-Policy: frame-ancestors 'none'(or'self'). This controls who may embed the page in a frame and protects against clickjacking, where a page is hidden in a transparent frame and the user is tricked into clicking it. The olderX-Frame-Optionsheader does the same, and frame-ancestors is preferred when both are present.Content-Security-Policywithscript-src. Limits the damage of XSS.Referrer-Policy: strict-origin-when-cross-origin. Sends only the origin to other sites, so private paths and query strings do not leak. It is also the default in current browsers, but explicit is better.Permissions-Policy: camera=(), microphone=(). Disables powerful features for the page and for embedded frames that do not need them.Cross-Origin-Opener-PolicyandCross-Origin-Embedder-Policy. Isolate the page from other windows, and are needed for some features such as high resolution timers and shared memory.- Cookie flags:
Secure,HttpOnly,SameSite.
Headers are set by the server, a CDN or a hosting configuration, and not by the HTML. Only a few can be set in a meta tag, and frame-ancestors is not one of them.
The Node server below sends several of these. The test loads it in an iframe from another origin, and Chrome blocks the frame because of frame-ancestors 'none'. A variant without the header loads inside the frame. Scanners such as the Mozilla Observatory report missing headers, but they were not run here.
import http from 'node:http';
http.createServer((req, res) => {
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
res.setHeader('Permissions-Policy', 'camera=(), microphone=()');
if (process.env.EMBED !== 'yes') res.setHeader('Content-Security-Policy', "frame-ancestors 'none'");
res.setHeader('Content-Type', 'text/html');
res.end('<!doctype html><title>bank</title><h1>Transfer money</h1>');
}).listen(process.env.PORT, () => console.log('listening'));Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The server was framed from another localhost origin. With frame-ancestors none, Chrome blocked the frame. Without that header, the frame loaded. The five headers were present in the response. No scanner was run.
Key points
- HSTS forces HTTPS. nosniff stops MIME type guessing.
- frame-ancestors stops clickjacking by controlling who may embed the page.
- Referrer-Policy limits what URL data other sites receive.
- Permissions-Policy turns off features the page does not need.
- Set these on the server or CDN. A meta tag supports only a few.
Common mistakes
- Setting frame-ancestors in a meta tag, where it is ignored.
- Enabling HSTS with includeSubDomains without checking every subdomain.
- Treating headers as a replacement for escaping and validation.
- Copying a header list without understanding which risk each one covers.
Follow-ups
- What is clickjacking and how does the attack work?
- Why is HSTS preload a serious commitment?
- What does nosniff protect in a file upload feature?
Tips
- Pair every header with the attack it prevents.
- Say who sets them: server or CDN.