Guides

How to add a Content Security Policy header

These examples use report-only, which does not block anything. When the reports are quiet, remove -Report-Only from the name.

nginx

add_header Content-Security-Policy-Report-Only "default-src 'self'; object-src 'none'; base-uri 'self'" always;

always adds it to error pages too. A location block with any add_header of its own drops the ones it would have inherited, so repeat it there.

Apache

Header always set Content-Security-Policy-Report-Only "default-src 'self'; object-src 'none'; base-uri 'self'"

It works in the main configuration or in .htaccess, and needs mod_headers (a2enmod headers).

Express

app.use((req, res, next) => {
  res.setHeader(
    "Content-Security-Policy-Report-Only",
    "default-src 'self'; object-src 'none'; base-uri 'self'"
  );
  next();
});

Put it before your routes. Helmet can build the header for you. It enforces a policy by default, so set reportOnly: true first.

Cloudflare and Netlify

Both read a file called _headers in the folder you publish:

/*
  Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; base-uri 'self'

On Cloudflare it applies to static files, not to responses your own Worker or Function creates. For a site behind Cloudflare's proxy, add a response header transform rule instead.

Vercel

// vercel.json
{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        {
          "key": "Content-Security-Policy-Report-Only",
          "value": "default-src 'self'; object-src 'none'; base-uri 'self'"
        }
      ]
    }
  ]
}

IIS

<!-- web.config -->
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Content-Security-Policy-Report-Only"
           value="default-src 'self'; object-src 'none'; base-uri 'self'" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

A meta tag

If you cannot set headers, an enforced policy can go in the page, before anything it should apply to:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; object-src 'none'">

Reporting, report-only, frame-ancestors and sandbox do not work this way, so use a header if you can.

Check that it works

curl -sI https://your-site.example | grep -i content-security

If it appears twice, two layers are each adding one. Browsers apply both, so a page can only do what both allow. Then paste the policy into the CSP checker to find weak spots.

Add reporting

None of the examples above send reports yet. Add report-uri, report-to and the Reporting-Endpoints header, as shown in report-uri and report-to compared.

Last checked 24 November 2026.

Read next