DEV Community

The seven security headers, and copy-paste config for nginx, Express and Next.js

In our study of 4,326 top websites, the average homepage sent 2.78 of the seven browser security headers, and 23% sent none.

Most of the gap is not hard engineering. It's five to ten lines of config nobody got around to. This post is the config. For each header: what it does, a safe starting value, and the one thing that can break.

1. Strict-Transport-Security (HSTS)

Tells the browser to use HTTPS for your domain for the next N seconds, even if someone types http://.

Strict-Transport-Security: max-age=31536000; includeSubDomains

Can break: any subdomain that still serves plain HTTP. Start with max-age=300 and no includeSubDomains if you're unsure, then raise it. Only add preload once you mean it: removal from the preload list takes months.

2. Content-Security-Policy (CSP)

Restricts where scripts, styles, images and frames may load from. It's the strongest header against XSS and the one most likely to break things.

A starting point for a site that serves its own assets plus one analytics domain:

Content-Security-Policy: default-src 'self'; script-src 'self' https://plausible.io; img-src 'self' data:; style-src 'self' 'unsafe-inline'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'

Can break: inline scripts, third-party widgets, and anything you forgot about. Ship it first as Content-Security-Policy-Report-Only and watch the browser console for a few days.

3. X-Content-Type-Options

Stops the browser guessing a file's type. Without it, a user-uploaded "image" can be sniffed and run as script.

X-Content-Type-Options: nosniff

Can break: almost nothing, unless your server sends wrong Content-Type headers. If so, that's worth knowing.

4. Frame protection

Prevents other sites from loading yours in an iframe (clickjacking). The modern way is CSP frame-ancestors. The old header still helps older browsers:

X-Frame-Options: DENY

Can break: legitimate embeds of your own pages. Use SAMEORIGIN, or list the allowed origins in frame-ancestors.

5. Referrer-Policy

Controls how much of the current URL is sent to the next site when a user clicks a link. Full URLs can leak tokens, search terms and internal paths.

Referrer-Policy: strict-origin-when-cross-origin

This is the default in current browsers, but sending it makes the behaviour explicit and consistent.

6. Permissions-Policy

Turns off browser features your site doesn't use, so injected code or a compromised iframe can't use them either.

Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()

Can break: a feature you do use. Remove that one from the list.

7. Cross-Origin-Opener-Policy (COOP)

Puts your page in its own browsing context group, so a window you open (or that opened you) can't reach back into it. It protects against cross-window attacks and is needed for some performance APIs.

Cross-Origin-Opener-Policy: same-origin

Can break: OAuth or payment popups that talk to your page through window.opener. Use same-origin-allow-popups if you have those.

nginx

Put this in the server block. Note the always: without it nginx drops the headers on error pages.

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Content-Security-Policy "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=()" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;

One gotcha: an add_header inside a location block replaces all the headers inherited from server. If a location sets its own Cache-Control with add_header, repeat the security headers there (or use an include file).

Express

helmet sets sensible defaults for most of these:

import express from "express";
import helmet from "helmet";
const app = express();
app.use(helmet({
  contentSecurityPolicy: {
    directives: {
      "img-src": ["'self'", "data:"],
      "frame-ancestors": ["'none'"]
    },
  },
  strictTransportSecurity: {
    maxAge: 31536000,
    includeSubDomains: true
  },
}));
app.use((_req, res, next) => {
  res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=()");
  next();
});

Next.js

In next.config.js:

const securityHeaders = [
  { key: "Strict-Transport-Security", value: "max-age=31536000; includeSubDomains" },
  { key: "X-Content-Type-Options", value: "nosniff" },
  { key: "X-Frame-Options", value: "DENY" },
  { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
  { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=(), payment=()" },
  { key: "Cross-Origin-Opener-Policy", value: "same-origin" },
];
module.exports = {
  async headers() {
    return [{ source: "/:path*", headers: securityHeaders }];
  },
};

For CSP in Next.js, use a nonce from middleware rather than a static header, because the framework injects inline scripts. The Next.js CSP guide covers it.

Check it

curl -sI https://your-site.example | grep -iE "strict-transport|content-security|x-content-type|x-frame|referrer-policy|permissions-policy|cross-origin-opener"

Or paste your domain into the free security grade, which also ranks the result against the 4,326-site benchmark.

Remember that headers are one layer. They don't replace input validation, dependency updates or a real security review.

Disclosure: I work on Automatic QA at Arclay, which makes the grader linked above.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.