How to add a Content-Security-Policy

Roll out a Content-Security-Policy in report-only mode first, fix what it reports, then enforce it.

Last reviewed

Why it matters

A Content-Security-Policy tells the browser which sources of scripts, styles and frames are allowed, so a script injected through a vulnerable plugin, form or tag can be stopped. Without one the browser has no second line of defence after a cross-site scripting bug.

How to fix it

Ship Content-Security-Policy-Report-Only first. A strict policy blocks inline scripts, inline event handlers and eval, which most themes, page builders, analytics and chat widgets rely on. Report-only enforces nothing and shows what would break. It does not clear this finding: only an enforcing Content-Security-Policy does, so expect the finding to stay during testing.

A cautious starting policy:

default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'

Moving to enforcing:

  1. Leave report-only running while you (or real visitors) use every kind of page: home, product, basket and checkout, contact forms, search, logged-in pages, embedded video and maps. Violations appear in the browser's devtools console, prefixed "[Report Only]" in Chromium.
  2. For each, add the legitimate source to the right directive (for example script-src 'self' https://www.googletagmanager.com) or remove what is blocked.
  3. For inline scripts, the strong fix is a nonce or hash per response, which needs support from the app or plugin. Adding 'unsafe-inline' to script-src silences the violations but gives up most of the protection: tell the client rather than hide it.
  4. When the console stays clean, send the same value under the name Content-Security-Policy.

To collect violations from real visitors, add report-uri and report-to pointing at a reporting endpoint you run or a reporting service, with a Reporting-Endpoints header naming it.

The snippets below are the report-only step; to enforce, change only the header name.

Replace the highlighted values: www.example.com with the site's hostname.

Cloudflare

  1. Rules > Overview > Create rule > Response Header Transform Rule, and name it.
  2. Apply it to all incoming requests, or filter to one hostname.
  3. Under Modify response header choose Set static, Header name Content-Security-Policy-Report-Only, Value the policy above.
  4. Deploy.

Set static replaces a header of the same name from the origin; Add static keeps both. Rules run only on proxied hostnames, and later rules can overwrite earlier ones.

Sources: create a response header rule, response header modification.

nginx

In the server block (and any location with its own add_header):

add_header Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'" always;

Source: ngx_http_headers_module.

Apache

<IfModule mod_headers.c>
    Header always set Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"
</IfModule>

Source: mod_headers.

Caddy

header Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"

Behind reverse_proxy, if the app may send its own policy, use header >Content-Security-Policy-Report-Only ….

Source: the header directive.

Vercel

In vercel.json (merge into the existing file). Vercel's own documentation also recommends starting in report-only and using nonces rather than unsafe-inline.

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        {
          "key": "Content-Security-Policy-Report-Only",
          "value": "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"
        }
      ]
    }
  ]
}

Sources: vercel.json, Vercel security headers.

Netlify

_headers in the publish directory (or the same under [[headers]] in netlify.toml); not applied to Functions, Edge Functions, SSR pages or proxied content:

/*
  Content-Security-Policy-Report-Only: default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'

Source: Netlify custom headers.

WordPress

Plugin route (HTTP Headers): Settings > HTTP Headers > Security, Edit next to Content Security Policy, select On, tick Report-Only, fill in default-src 'self', img-src 'self' data:, object-src 'none', base-uri 'self', form-action 'self', and Save Changes. To enforce later, untick Report-Only.

Code route: inside the send_headers function in wp-content/mu-plugins/security-headers.php (see the HSTS guide for the file):

header( "Content-Security-Policy-Report-Only: default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'" );

WordPress core and plugins add many inline scripts: expect a long violation list and plan time for it.

Sources: HTTP Headers plugin, send_headers.

Shopify

Shopify documents no way for merchants to set response headers on the Online Store, and Shopify sends its own Content-Security-Policy, so a Shopify store will not normally show this finding. Headless Hydrogen storefronts set their policy in code with createContentSecurityPolicy.

Source: Hydrogen content security policy.

Check that it worked

curl -sI https://www.example.com/ | grep -i content-security-policy

During testing you should see one content-security-policy-report-only line; after switching, one content-security-policy line. Violations show in devtools > Console. Run the Owl Keeper Security headers check again once the enforcing header is live: the finding stays during the report-only phase, as expected.

Test it now with the free HTTP status code checker. No account needed.

What can go wrong

  • A policy that is too tight breaks the site, often silently: checkout scripts, payment frames, analytics, chat widgets, video, fonts, forms. Always go through report-only first, and test again after adding any plugin or tag.
  • 'unsafe-inline' in script-src removes most of the benefit.
  • Two policies (say Cloudflare and a plugin) are both enforced, and the strictest wins for each directive. Set it in one place.
  • A <meta> CSP cannot carry frame-ancestors or reporting, and report-only cannot be sent by meta at all.