Skip to main content
The HTTP Security Headers Guide (with CSP)

The HTTP Security Headers Guide (with CSP)

KU
· Founder & Chief Editor
• 8 min read•Updated •28 views

A practical walkthrough of the HTTP response headers that harden a web app, with a real rollout plan for Content-Security-Policy.

Key takeaways

  • Set Strict-Transport-Security, X-Content-Type-Options nosniff, CSP frame-ancestors, Referrer-Policy and Permissions-Policy in one deploy, because they rarely break anything.
  • Treat Content-Security-Policy as a project: ship it in report-only mode, fix real violations, then enforce a nonce-based policy with object-src and base-uri locked down.
  • Drop X-XSS-Protection entirely.

Ship five headers today: Strict-Transport-Security, X-Content-Type-Options: nosniff, CSP frame-ancestors, Referrer-Policy and Permissions-Policy. They are low-risk and fit in one deploy. Then roll out a nonce-based Content-Security-Policy in report-only mode first, because CSP is the strongest browser defense against XSS and the one most likely to break your site.

Verified against

CSP Level 3 (W3C Working Draft dated 27 October 2026), against the MDN HTTP header reference, the OWASP HTTP Headers Cheat Sheet and the hstspreload.org submission requirements. Checked October 2026.

Response headers are the cheapest defense-in-depth you can ship. They don't fix a broken auth flow or a SQL injection, but they shrink the blast radius when something else goes wrong: "one bug becomes account takeover" turns into "one bug the browser refuses to weaponize." For the wider context on where headers fit, see our application security best practices guide, and for the server-side settings that usually travel with missing headers, the security misconfiguration guide.

The headers that carry their weight

Strict-Transport-Security (HSTS): Forces browsers to use HTTPS for your domain, so a stripped or downgraded connection never gets a chance. Once a browser sees this header, it refuses plain HTTP for the duration of max-age.

code
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

Use a long max-age (two years here). includeSubDomains covers every subdomain, so confirm they all serve HTTPS before enabling it. preload opts you into the browser-baked HSTS list at hstspreload.org, which means the very first request is protected too. As of October 2026 the list requires max-age of at least 31536000 (one year), includeSubDomains and preload. Preload is hard to reverse, so treat it as a commitment.

X-Content-Type-Options: nosniff: Stops the browser from guessing a response's content type. Without it, a browser might treat a user-uploaded text file as JavaScript. There is exactly one value and you always want it.

code
X-Content-Type-Options: nosniff

X-Frame-Options and frame-ancestors: Both defend against clickjacking, where your page gets loaded in an invisible iframe on an attacker's site. X-Frame-Options: DENY is the legacy control; the modern replacement is the CSP frame-ancestors directive, which is more expressive (you can allow specific origins). Set both for coverage across old and new browsers.

code
X-Frame-Options: DENY
Content-Security-Policy: frame-ancestors 'none'

Referrer-Policy: Controls how much of the referring URL leaks to other sites, which matters because paths often carry tokens or IDs. strict-origin-when-cross-origin sends the full URL to your own origin, only the bare origin to other HTTPS sites, and nothing over a downgrade. MDN lists it as the browser default (it replaced no-referrer-when-downgrade), but we set it explicitly so older clients and embedded webviews behave the same way. Go stricter (same-origin or no-referrer) if your URLs carry anything sensitive.

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

Permissions-Policy: Declares which browser features the page and its iframes may use, such as camera, microphone, and geolocation. Deny everything you don't need, so a compromised script can't silently reach for the webcam.

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

Deprecated headers: X-XSS-Protection is dead. Modern browsers removed the auditor it controlled, and MDN warns it can create XSS vulnerabilities in otherwise safe sites. Don't set it, or set X-XSS-Protection: 0 to disable any lingering legacy behavior. CSP is the real XSS defense now.

Content-Security-Policy, the big one

CSP tells the browser exactly where resources may load from and, critically, whether inline scripts are allowed to run. A well-built policy is the single strongest browser control against cross-site scripting, which is why we cover the attack itself in preventing XSS.

The core directives:

  • default-src is the fallback for any resource type you don't name explicitly.
  • script-src governs JavaScript, and it's where the security actually lives.
  • object-src 'none' and base-uri 'none' close off legacy injection vectors most policies forget.
  • frame-ancestors handles clickjacking as noted above.
  • upgrade-insecure-requests rewrites any http:// subresource to https:// at load time.

The hard part is inline scripts. 'unsafe-inline' allows any inline <script> to run, which defeats most of the point, because an injected script is inline too. The two safe alternatives are nonces and hashes. A nonce is a random per-response token you put on both the header and each legitimate <script nonce="...">; the browser runs only scripts carrying the matching value. A hash pins a script by the SHA-256 of its exact contents, which suits static inline blocks that never change. Prefer nonces for server-rendered pages and hashes for fixed snippets. Know how the two interact: browsers that understand nonces ignore 'unsafe-inline' when a nonce is present, but very old CSP1-only browsers ignore the nonce and honor 'unsafe-inline'. Adding it as a fallback never protects those old clients.

A reasonable starter policy, generated fresh per request with a nonce:

code
Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-{RANDOM}';
  style-src 'self';
  img-src 'self' data:;
  object-src 'none';
  base-uri 'none';
  frame-ancestors 'none';
  upgrade-insecure-requests;

Rolling CSP out without an outage

A strict CSP will break things you forgot existed: a third-party analytics tag, an inline onclick handler, a font from a CDN. Deploy it blind and you'll be reverting within the hour. The disciplined path uses report-only mode first.

Content-Security-Policy-Report-Only enforces nothing. The browser evaluates your policy, lets everything load, and posts a JSON violation report to an endpoint you name. report-uri is deprecated in favor of report-to (paired with a Reporting-Endpoints header), and MDN's guidance as of October 2026 is to send both until report-to support is universal, since browsers that understand report-to ignore report-uri. You collect real-world violations from real traffic, learn what your policy would have blocked, and tighten it until the reports go quiet.

code
Content-Security-Policy-Report-Only:
  default-src 'self';
  script-src 'self' 'nonce-{RANDOM}';
  object-src 'none';
  report-uri /csp-reports;
  report-to csp-endpoint;

The process, in order:

  1. Ship the policy in report-only and watch the reports for a week or two across your full traffic mix.
  2. Fix the legitimate violations: add the analytics domain to script-src, replace inline handlers with nonced scripts, move styles out of style attributes.
  3. When reports are down to genuine noise and attack attempts, flip the exact same policy from -Report-Only to Content-Security-Policy.
  4. Keep a reporting endpoint on the enforced policy too, so regressions surface before users file tickets.

Here's the enforced version wired up in Express, generating a nonce per request:

JavaScript
const crypto = require('crypto');

app.use((req, res, next) => {
  const nonce = crypto.randomBytes(16).toString('base64');
  res.locals.nonce = nonce;
  res.setHeader('Content-Security-Policy', [
    "default-src 'self'",
    `script-src 'self' 'nonce-${nonce}'`,
    "style-src 'self'",
    "img-src 'self' data:",
    "object-src 'none'",
    "base-uri 'none'",
    "frame-ancestors 'none'",
    "upgrade-insecure-requests",
  ].join('; '));
  next();
});

Testing what you shipped

Start with curl, because it shows exactly what your edge sends, including headers a CDN added or stripped:

code
$ curl -sI https://example.com | grep -iE 'strict-transport|content-security|x-content-type|x-frame|referrer-policy|permissions-policy'

Then use the graders. securityheaders.com grades a live URL and flags missing or weak headers in seconds. The MDN HTTP Observatory (the successor to Mozilla Observatory, now hosted on MDN) goes deeper and scores your CSP quality specifically, calling out 'unsafe-inline' and missing directives. Run both after every change, and check the browser console, which prints the precise directive that blocked each resource. A common failure mode: the app sets the headers, but a reverse proxy or CDN rule overwrites them, so always test the public URL, not localhost.

Questions people ask

Which HTTP security headers should every site set?

At minimum: Strict-Transport-Security with a long max-age, X-Content-Type-Options: nosniff, a CSP with frame-ancestors (plus X-Frame-Options: DENY for old browsers), Referrer-Policy: strict-origin-when-cross-origin, and a Permissions-Policy denying features you don't use. Those five rarely break anything. A full Content-Security-Policy is the sixth, but it needs a report-only rollout before you enforce it.

Is X-Frame-Options still needed if I use frame-ancestors?

CSP frame-ancestors is the modern control and the more expressive one, since it can allow specific origins; MDN points to it for anything beyond deny or same-origin. X-Frame-Options mostly matters for old browsers that predate it. Sending both costs one header line, so we keep X-Frame-Options: DENY alongside frame-ancestors 'none' unless the page must be framed, in which case frame-ancestors with an explicit origin list is the only correct tool.

Should I use a CSP nonce or a hash?

Use nonces for server-rendered pages, where you can generate a fresh random value per response and stamp it on each legitimate <script>. Use hashes for static inline snippets that never change, such as a small bootstrap script on a CDN-cached page, because a cached page cannot carry a per-request nonce. Never reuse a nonce across responses; a fixed nonce is a password printed in your HTML.

The call we'd make

Set HSTS, nosniff, frame-ancestors, Referrer-Policy, and Permissions-Policy today; they're low-risk and you can ship them in one deploy. Treat CSP as a project, not a header. Start in report-only, drive it with real violation data, and only enforce once the reports are quiet. A nonce-based policy with object-src 'none' and base-uri 'none' is the target. Skip 'unsafe-inline' entirely, retire X-XSS-Protection, and let the browser do the work your application layer can't guarantee.

Sources

Explore topics:SecurityDevOps
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). The HTTP Security Headers Guide (with CSP). DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/security-headers-guide

Markdown

[The HTTP Security Headers Guide (with CSP)](https://www.devopsness.com/blog/security-headers-guide) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.