IHOR.DEVKNOWLEDGE_BASE
MENU

~/blog / Web Quality / security-headers-for-modern-websites.md

// guide · 4 min read

Security Headers for Modern Websites

Deploy browser security headers with a deliberate CSP, HTTPS and HSTS plan, framing policy, MIME protections, referrer limits, and verified production responses.

Quick answer

Choose headers from the browser behavior you need to constrain, then verify the final HTTPS response after proxies and hosting layers. CSP restricts resources and execution, HSTS tells supporting browsers to use HTTPS for the host, framing policy limits embedding, nosniff enforces MIME expectations, and Referrer-Policy controls referrer information. A header checklist does not replace authentication, authorization, or safe rendering.

Avoid copying a maximal policy without inventorying scripts, fonts, images, APIs, embedded content, and framework behavior. A broken production login or blocked image is not evidence of successful hardening. Test changes against the site's supported flows and inspect actual violations.

Practical implementation steps

Map resources and framing

Identify trusted resource origins and whether any page is intentionally embedded. Use CSP frame-ancestors in the response header for framing policy; it is distinct from frame-src, which governs frames your page loads. Do not claim a meta tag supplies frame-ancestors enforcement. Consider compatibility headers where necessary without maintaining contradictory policies.

Roll out CSP with evidence

Use report-only observation when appropriate, then an enforced policy that supports the actual application. Dynamic scripts may need a nonce or hash design integrated with rendering and caching. Adding unsafe-inline or unsafe-eval indiscriminately weakens the intended execution boundary. Violation reports can contain sensitive URLs; review the reporting destination and retention as another data flow.

Deploy HTTPS and HSTS deliberately

Ensure HTTPS works before declaring a long HSTS lifetime. includeSubDomains affects every covered subdomain and should not be enabled until those hosts can support it. Preloading is a separate commitment with recovery implications, not an automatic addition to an example. HSTS is observed over secure connections and does not fix a broken certificate.

Verify every serving layer

Inspect successful HTML, redirects, errors, static assets, and API responses where the policy applies. Confirm the CDN does not remove or duplicate headers. Test login redirects, downloads, external links, and embedded resources. Keep changes in versioned configuration and document which layer owns each policy to avoid competing settings.

Implementation example

This Next.js configuration illustrates three low-complexity response policies. It deliberately leaves script-execution CSP and HSTS to a site-specific design because their resource and host assumptions cannot be filled in generically. Verify the deployed response instead of assuming config execution.

import type { NextConfig } from "next";

const config: NextConfig = {
  async headers() {
    return [{
      source: "/:path*",
      headers: [
        { key: "X-Content-Type-Options", value: "nosniff" },
        { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
        { key: "Content-Security-Policy", value: "frame-ancestors 'self'" },
      ],
    }];
  },
};
export default config;

frame-ancestors self permits same-origin framing. Use none if embedding is not intended, or a deliberate origin list for supported consumers. A framing-only CSP does not restrict scripts; do not describe this fragment as a comprehensive execution policy. nosniff also requires serving correct Content-Type values.

Tradeoffs and alternatives

Application configuration keeps headers close to code, while edge configuration can cover routes outside the framework. Both approaches require ownership and deployment verification. A strict nonce CSP can add rendering and cache complexity; evaluate it against the current application instead of weakening every directive when one dependency fails.

Common mistakes

  • Confusing frame-src and frame-ancestors.
  • Claiming framing-only CSP protects every script.
  • Enabling includeSubDomains before all hosts support HTTPS.
  • Duplicating conflicting policies at edge and application layers.
  • Treating headers as permission checks for private data.

Production checklist

  • Resource and embedding assumptions are recorded.
  • CSP violations and core flows are reviewed.
  • HTTPS and HSTS scope are deliberate.
  • MIME and referrer behavior match response content.
  • Final edge responses are inspected.
  • Each header has one configuration owner.

FAQ

Does report-only CSP block attacks?

It reports matching violations without enforcing that policy. Use it to observe compatibility, then deploy enforcement when the intended rules are verified.

Should HSTS always include preload?

No. Assess the host and subdomain commitment and current preload requirements separately. Do not include a long-lived commitment by copying an example.

Conclusion

Add policies with clear assumptions, test the flows they affect, and inspect the deployed response. Keep execution, framing, transport, and application authorization as distinct boundaries with distinct evidence.

Primary sources

Ihor Kharchyshyn

Independent full-stack product engineer writing source-backed implementation guides for browser extensions and modern web products.

Need this implemented in a real product?

One connected implementation across the interface, application logic, APIs, and data layer.