logo
Parslinks
Back to blog
Platform5 min read

Security headers and CSP: five lines that block a big class of attacks

Security headers are free, take minutes to add and are still missing from most sites.

Ehsan Yaghmori

Founder & CTO, Parslinks

A community forum for home bakers lets members format posts with a little HTML. One day a member's post includes a carefully crafted <img onerror=...> that the sanitizer missed. Everyone who opens the thread silently sends their session cookie to an unknown server.

The sanitizer had to be fixed, and it was. But with even a basic Content-Security-Policy, the browser would never have run that script. That's what security headers are: a second line of defense for when the first one misses something.

The five headers that matter

Five security headers and what they stop

Strict-Transport-Security (HSTS): tells browsers to use HTTPS only for your domain, even if someone types http://. It blocks downgrade attacks on public Wi-Fi. Start with a short max-age, then raise it to a year once you're confident. If SSL itself is shaky, read automatic SSL first.

Content-Security-Policy (CSP): an allowlist of where scripts, styles, images and fonts may load from. The most powerful header and the easiest to get wrong; more below.

X-Content-Type-Options: nosniff: stops browsers from guessing file types. Without it, a file uploaded as an image might be executed as a script.

Referrer-Policy: strict-origin-when-cross-origin: when users follow a link to another site, only your origin is sent, not the full URL, which might contain tokens or personal data.

frame-ancestors (in CSP) or X-Frame-Options: prevents other sites from embedding yours in an iframe. Without it, a phishing page can overlay your checkout with an invisible layer and trick users into clicking buttons they can't see.

headers
Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy: default-src 'self'; img-src 'self' data: https://cdn.example.com; frame-ancestors 'none'; object-src 'none'; base-uri 'self'
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
text5 lines

Roll out CSP without breaking things

A strict CSP almost always breaks something on the first try: an analytics snippet, a support widget, a font from another domain. The safe path:

  1. Report-only first. Send the policy as Content-Security-Policy-Report-Only. Browsers block nothing and report violations.
  2. Collect for a week and add legitimate sources to the allowlist.
  3. Enforce, and keep a report-only policy around for testing future changes.

For inline scripts, don't reach for 'unsafe-inline', which throws away most of CSP's value. Use nonces instead: a random value generated per request, so only scripts carrying it run. Next.js and most modern frameworks support this.

The outcome

The forum fixed its sanitizer, made the session cookie HttpOnly and Secure, and added the five headers above, running CSP in report-only mode for a week first. Two analytics scripts and a chat widget went onto the allowlist; every other report came from users' browser extensions. If the sanitizer ever misses something again, the browser won't run the injected script.

For the other response headers that matter, see Cache-Control, file by file.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.