Difficulty: Intermediate
Name the important HTTP security headers, explain what each does, and show how you would set them in an Express app.
HTTP security headers are response headers that tell the browser to enable extra protections for your site. The three most important are Strict-Transport-Security (always use HTTPS), Content-Security-Policy (restrict where scripts and other resources load from) and a framing control (frame-ancestors or X-Frame-Options) to stop clickjacking. In Node.js, the Helmet middleware sets a sensible set of them in one line.
| Header | Example value | Protects against |
|---|---|---|
Strict-Transport-Security | max-age=31536000; includeSubDomains; preload | SSL stripping and HTTP downgrade |
Content-Security-Policy | default-src 'self'; script-src 'self' 'nonce-...' | XSS, data injection, unwanted resource loads |
X-Frame-Options / CSP frame-ancestors | DENY / frame-ancestors 'none' | Clickjacking |
X-Content-Type-Options | nosniff | MIME sniffing that turns uploads into scripts |
Referrer-Policy | strict-origin-when-cross-origin | Leaking full URLs (with tokens or IDs) to other sites |
Permissions-Policy | camera=(), microphone=(), geolocation=() | Third-party scripts using powerful APIs |
Cross-Origin-Opener-Policy | same-origin | Cross-window attacks, enables isolation |
X-XSS-Protection is deprecated. Modern browsers removed the XSS auditor, and the header could even introduce bugs. Helmet now sets it to 0. Use CSP instead.frame-ancestors supersedes X-Frame-Options in modern browsers and supports multiple allowed origins. Sending both is still common for older clients.Referrer-Policy: strict-origin-when-cross-origin is now the browser default, but setting it explicitly documents intent.CSP is the most powerful header and the easiest to break a site with. A practical rollout:
Content-Security-Policy-Report-Only and a report-to (or legacy report-uri) endpoint.'unsafe-inline' and 'unsafe-eval' in script-src.'strict-dynamic' with nonces.Once a browser sees HSTS, it refuses plain HTTP for max-age seconds. includeSubDomains breaks any subdomain still on HTTP, and the preload list is hard to get off. Start with a small max-age (like 300), confirm all subdomains serve HTTPS, then raise it.
In the app (Helmet), at the reverse proxy (Nginx add_header), or at the CDN or hosting platform (Vercel and Netlify config files). Setting them at the edge covers static assets that never hit your app server. Check results with your browser's DevTools Network tab or curl -I.
import express from 'express';
import helmet from 'helmet';
import crypto from 'node:crypto';
const app = express();
// Fresh nonce per response for inline scripts we trust
app.use((req, res, next) => {
res.locals.nonce = crypto.randomBytes(16).toString('base64');
next();
});
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", (req, res) => `'nonce-${res.locals.nonce}'`, 'https://checkout.razorpay.com'],
imgSrc: ["'self'", 'data:', 'https://cdn.example.in'],
objectSrc: ["'none'"],
frameAncestors: ["'none'"],
},
},
strictTransportSecurity: { maxAge: 31536000, includeSubDomains: true },
referrerPolicy: { policy: 'strict-origin-when-cross-origin' },
}));
Helmet sets defaults like `X-Content-Type-Options: nosniff` and `X-Frame-Options`, and lets you customise CSP and HSTS. The nonce function allows only your own inline scripts.
curl -sI https://example.in | grep -iE 'strict-transport|content-security|x-frame|x-content-type|referrer-policy'
A quick way to confirm headers are actually being sent in production, including from a CDN.
Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, frame-ancestors, Helmet