Track 08 · Lesson 4 of 20Attacks and defenses

Track 08 · Attacks and defenses

Security headers

Lesson 4 of 20· 6 min

HTTP headers are small instructions sent with every response. A few lines of configuration switch on powerful browser protections against downgrades, framing, file-type tricks and leaky URLs.

Grade a response

Switch headers on one by one

F

0 of 100

Missing: Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy, Cross-Origin-Opener-Policy. Version headers are still visible.

  • max-age=63072000; includeSubDomains; preloadStops downgrades to plain HTTP (SSL stripping).
  • default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'Limits where scripts can come from, so most XSS can’t run; also blocks framing.
  • nosniffStops browsers from guessing file types, so an uploaded “image” can’t run as a script.
  • DENYOlder clickjacking protection for browsers that don’t support frame-ancestors.
  • strict-origin-when-cross-originStops full URLs, which may contain tokens, from leaking to other sites.
  • camera=(), microphone=(), geolocation=()Switches off powerful features you don’t use, even for embedded frames.
  • same-originKeeps pop-ups and other windows from reaching into your page.
  • Server: nginx/1.18.0, X-Powered-By: ExpressStops advertising exact software versions to attackers looking for known bugs.

The response a browser receives

HTTP/2 200
content-type: text/html; charset=utf-8
server: nginx/1.18.0
x-powered-by: Express

Setting them in Next.js

// next.config.ts
import type { NextConfig } from "next"

const securityHeaders = [
  { key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains" },
  { key: "X-Content-Type-Options", value: "nosniff" },
  { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
  { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
  { key: "X-Frame-Options", value: "DENY" },
  { key: "Cross-Origin-Opener-Policy", value: "same-origin" },
  // A strict script-src in Next.js needs nonces: see the Next.js CSP guide
]

const nextConfig: NextConfig = {
  poweredByHeader: false, // removes X-Powered-By
  async headers() {
    return [{ source: "/:path*", headers: securityHeaders }]
  },
}

export default nextConfig

The baseline#

  • Strict-Transport-Security: always HTTPS.
  • Content-Security-Policy: where scripts may come from, and with frame-ancestors, who may frame you.
  • X-Content-Type-Options: nosniff: don’t guess file types.
  • X-Frame-Options: DENY: the older framing rule, for older browsers.
  • Referrer-Policy: don’t leak full URLs, which may contain tokens, to other sites.
  • Permissions-Policy: switch off camera, microphone and location if you don’t use them.

And remove Server and X-Powered-By version banners: they tell attackers which known bugs to try.

This site, for real#

The suckless_dev site sends this baseline too. Check it: the test below fetches the page you’re reading and grades the headers that actually arrive.

This page’s headers

A real request, right now

Notice the honest ◐ on the Content-Security-Policy: it stops framing and plugin tricks, but doesn’t restrict scripts yet. A strict script policy is the strongest header and the hardest to adopt, and it gets its own lesson.

In a code review

LowFrontend / DevOps

Missing baseline security headers

Fix: Add HSTS, frame-ancestors 'none' or 'self', nosniff and a Referrer-Policy at the proxy or framework level.

0 of 3 checked

Check yourself

What does nosniff stop?