webzod
account

Add a Content-Security-Policy on Netlify

headers.csp.missing · effort: a project

Why it matters

Without a CSP the browser runs any script that ends up in your pages, so a single injection bug gives an attacker control of what visitors see and do. A policy limits where scripts, frames and plugins may load from.

Staged change. Do this in stages and check each one before the next, so a mistake does not break your site or your mail.

The fix

  1. Stage 1: add the lines below to the _headers file in the publish directory (or a [[headers]] block in netlify.toml) and redeploy. They do not apply to Netlify Functions, Edge Functions or proxied responses; those must send the header themselves.
  2. Browse every page type with the developer console open and add the sources it reports to the policy.
  3. Stage 2: when nothing legitimate is reported, rename the header to Content-Security-Policy.
_headers
/*
  Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; img-src 'self' data: https:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'

Read the documentation (opens in a new tab)

Scan your domain to see whether this applies to you.