Security Headers (Helmet)

Difficulty: Intermediate

Question

Name the important HTTP security headers, explain what each does, and show how you would set them in an Express app.

Answer

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.

The headers that matter

HeaderExample valueProtects against
Strict-Transport-Securitymax-age=31536000; includeSubDomains; preloadSSL stripping and HTTP downgrade
Content-Security-Policydefault-src 'self'; script-src 'self' 'nonce-...'XSS, data injection, unwanted resource loads
X-Frame-Options / CSP frame-ancestorsDENY / frame-ancestors 'none'Clickjacking
X-Content-Type-OptionsnosniffMIME sniffing that turns uploads into scripts
Referrer-Policystrict-origin-when-cross-originLeaking full URLs (with tokens or IDs) to other sites
Permissions-Policycamera=(), microphone=(), geolocation=()Third-party scripts using powerful APIs
Cross-Origin-Opener-Policysame-originCross-window attacks, enables isolation

What changed in recent years

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

Rolling out CSP safely

CSP is the most powerful header and the easiest to break a site with. A practical rollout:

  1. Start with Content-Security-Policy-Report-Only and a report-to (or legacy report-uri) endpoint.
  2. Collect violations for a week, fix inline scripts, move to nonces or hashes.
  3. Switch to enforcing mode. Avoid 'unsafe-inline' and 'unsafe-eval' in script-src.
  4. Third-party scripts (analytics, payment SDKs like Razorpay Checkout) need their domains allowed, or use 'strict-dynamic' with nonces.

HSTS cautions

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.

Where to set headers

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.

Code examples

Helmet with a custom CSP in Express

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.

Checking headers from the command line

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.

Key points

  • Top three: HSTS, Content-Security-Policy, and frame-ancestors or X-Frame-Options.
  • X-Content-Type-Options: nosniff stops browsers guessing content types.
  • X-XSS-Protection is deprecated; Helmet sets it to 0.
  • Roll out CSP in Report-Only mode first and avoid 'unsafe-inline' for scripts.
  • Raise HSTS max-age gradually and be careful with includeSubDomains and preload.

Common follow-up questions

  • How does a nonce-based CSP work with server-side rendering?
  • Why is X-XSS-Protection no longer recommended?
  • What is the HSTS preload list and what are the risks of joining it?
  • Where would you set security headers for a static site hosted on a CDN?

Interview tips

  • Pair each header with the attack it prevents; a list of names alone is weak.
  • Mentioning that X-XSS-Protection is deprecated shows current knowledge.
  • Describe a safe CSP rollout with Report-Only; interviewers like operational awareness.

Concepts covered

Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, frame-ancestors, Helmet

More Web Security (OWASP, JWT, XSS, CSRF) questions