Illustration of a web push Content Security Policy, a padlock shield guarding a browser window with a service worker gear and bell Illustration of a web push Content Security Policy, a padlock shield guarding a browser window with a service worker gear and bell

How Do You Set Up Web Push With a Content Security Policy?

Set up web push with a Content Security Policy: the worker-src, script-src and connect-src rules you need, and how to debug blocked requests.

To make web push work with a Content Security Policy (CSP), allow your service worker in worker-src (or script-src), allow your push SDK’s script host in script-src, and allow the API hosts it calls in connect-src. The service worker also gets its own policy from its HTTP headers, so check that response too.

A strict CSP is one of the best defences against cross-site scripting, and I’m always happy to see one. However, it’s also the reason many push setups fail quietly. The subscribe button does nothing, the console shows a “Refused to load” message, and nobody connects it to push. Here’s how to set up web push with a Content Security Policy without weakening it more than you need to.

Key facts at a glance:

  • worker-src controls which URLs can be loaded as Worker, SharedWorker or ServiceWorker scripts, per MDN’s worker-src page.
  • Fallback: if worker-src is missing, browsers use child-src, then script-src, then default-src.
  • Separate policy: MDN’s Content-Security-Policy reference notes that workers aren’t governed by the CSP of the page that created them.
  • Same origin: service worker scripts must come from your own origin, so ‘self’ is usually enough for worker-src.
  • Safe testing: the Content-Security-Policy-Report-Only header reports violations without blocking anything.

How does CSP affect web push notifications?

CSP affects web push at three points: loading the push SDK script, registering the service worker, and sending the subscription to your server. If any of those requests is blocked, the visitor never becomes a subscriber, even if they clicked Allow.

Here’s the typical flow and the directive that controls each step:

StepWhat happensCSP directive on your page
1. Load SDKYour page loads the push provider’s JavaScriptscript-src
2. Register workernavigator.serviceWorker.register(‘/sw.js’)worker-src (falls back to child-src, script-src, default-src)
3. SubscribeThe browser creates a push subscriptionNot controlled by your CSP
4. Save subscriptionYour code posts the endpoint to your server or panelconnect-src
5. ManifestBrowser fetches manifest.json, if you use onemanifest-src

Step 3 is handled by the browser and its push service, not by a fetch from your page. So you don’t need to allow fcm.googleapis.com or Mozilla’s push host in connect-src just to subscribe. You only need them if your own code calls those hosts directly. My explainer on how push notifications work covers the full flow.

Which CSP directives does web push need?

Most setups need three changes: worker-src for the service worker, script-src for the SDK, and connect-src for the endpoint that stores subscriptions. Here’s an example for a site that self-hosts its push panel on a subdomain.

Content-Security-Policy:
  default-src 'self';
  script-src 'self' https://push.example.com;
  worker-src 'self';
  connect-src 'self' https://push.example.com;
  img-src 'self' data: https://push.example.com;
  manifest-src 'self';

Replace push.example.com with your panel or provider’s real host. If you load Firebase’s JavaScript SDK from Google’s CDN, you’d also add https://www.gstatic.com to script-src. And because Firebase’s SDK calls Google APIs to register the device, add the googleapis.com hosts it uses to connect-src. The fastest way to get the exact list is to read the blocked URLs in your console, rather than guessing.

Keep img-src in mind if your subscribe widget or bell icon loads images from the provider. Otherwise, the prompt may appear without its icon.

Why does the service worker need its own policy?

The service worker needs its own policy because workers don’t inherit your page’s CSP. Instead, a service worker is governed by the CSP header sent with the service worker script itself. So a strict page policy doesn’t automatically protect, or restrict, sw.js.

This matters in two ways. First, if your server sends a strict CSP on every response, including sw.js, then importScripts() calls inside the worker must match that policy. A Firebase-based worker that imports scripts from www.gstatic.com will fail unless that host is allowed in the worker’s own script-src.

Second, the worker’s fetch() calls follow its own connect-src. For example, if the worker reports click analytics to your panel, that host must be allowed in the policy on sw.js. My guide to adding web push to an existing service worker shows how importScripts fits into a merged worker.

How do you find which CSP rule is blocking push?

Open your browser’s developer tools, reload the page, and click your subscribe button with the Console open. CSP violations appear as “Refused to load” or “Refused to connect” messages that name the blocked URL and the directive.

Then follow these steps:

  1. Note each blocked URL and the directive named in the message.
  2. Check the Application panel to see if the service worker registered.
  3. If it didn’t, check the Network tab for sw.js and read its response headers.
  4. Add the blocked host to the right directive in a Report-Only header first.
  5. Test again, then move the change into the enforced policy.

The Report-Only header is the safest way to test on a live site, because it logs violations without breaking anything. For other console checks, see my guide to debugging web push in Chrome DevTools.

Should you use unsafe-inline to make push work?

No. Adding ‘unsafe-inline’ to script-src removes most of the protection CSP gives you against cross-site scripting. If your push snippet is an inline script, use a nonce or a hash for that script, or move the code into an external file on your own domain.

Here’s how I compare the options:

OptionSecurityEffort
‘unsafe-inline’ in script-srcWeak; allows any inline scriptLowest
Nonce per page loadStrongNeeds server-side templating
Hash of the inline snippetStrongUpdate the hash when the snippet changes
External script file on your domainStrongMove the code once

A nonce or hash keeps the policy strict while still running the one snippet you trust. However, many CMS caching setups make nonces awkward, so an external file is often the simplest fix. If you’re on WordPress, check whether your security plugin or host sets the CSP header, so you edit it in the right place.

Does CSP affect web push on WordPress?

Yes, if your site sends a CSP header. WordPress itself doesn’t add one by default, but security plugins, hosting panels and CDNs often do. So start by finding out where your header comes from, then add the push hosts there.

Also check that the service worker file is served from your site root with a JavaScript content type, because a misconfigured header or redirect can block registration too. My guides on adding push notifications to WordPress and running a self-hosted panel behind Cloudflare cover related setup issues. If everything loads but alerts still don’t arrive, use my checklist for web push notifications not showing.

Where LaraPush fits

LaraPush is a self-hosted web push panel, so the script and API hosts you add to your CSP are your own panel domain rather than a third-party SaaS. It delivers through Firebase Cloud Messaging, so Firebase hosts may also appear in your policy. Plans start from $499 one-time; see LaraPush pricing, the features page, and the setup guide for the snippet and service worker you’ll install. The trade-offs are no free plan, one server per licence, and a VPS you maintain.

Frequently asked questions

Which CSP directive controls service workers?

The worker-src directive controls which URLs can be loaded as service worker scripts. If you don’t set worker-src, browsers fall back to child-src, then script-src, then default-src. Because service workers must be same-origin, worker-src ‘self’ is usually all you need.

Do I need to allow fcm.googleapis.com in connect-src?

Usually not for the subscription itself, because the browser talks to its push service directly. You only need Google hosts if your own code or an SDK calls them from the page, for example Firebase’s JavaScript SDK. Your console will name any host it blocks.

Why does my service worker ignore my page’s Content Security Policy?

Workers aren’t governed by the CSP of the page that created them. A service worker uses the CSP header sent with the service worker script’s own response. So check the headers on sw.js separately, especially if it uses importScripts or fetch.

How can I test CSP changes without breaking push?

Use the Content-Security-Policy-Report-Only header with your proposed policy. Browsers log or report violations but don’t block anything. Once the console stays clean while you subscribe and receive a test notification, move the same policy into the enforced header.

Is it safe to add unsafe-inline for my push snippet?

I don’t recommend it. Allowing ‘unsafe-inline’ in script-src lets any inline script run, which defeats much of the protection against cross-site scripting. Use a nonce or hash for the snippet, or move it into an external JavaScript file on your domain.

Leave a Reply

Your email address will not be published. Required fields are marked *