Web Workers

Why Your CSP Blocks Web Workers: Understanding worker-src and Its Fallbacks

Learn why CSP blocks web workers, how worker-src fallbacks work, and how to troubleshoot blob URLs and worker policies without broad permissions.

Your website loads, its JavaScript runs, and everything looks normal. Then a feature that processes images, searches documents or performs background calculations stops working.

The browser console points to Content Security Policy (CSP). But the script that creates the worker is already allowed.

Permission to execute a page’s JavaScript does not automatically grant permission to load its workers. Understanding worker-src helps you diagnose the failure and make a focused policy change.

What does worker-src control?

The worker-src directive specifies permitted sources for scripts used by dedicated workers, shared workers and service workers.

For example, this directive permits worker scripts from the page’s own origin:

worker-src 'self'

This directive prevents worker script loading:

worker-src 'none'

These are policy fragments, intended to be incorporated into your existing CSP. They are not complete website security policies.

The directive governs worker loading. It does not replace the separate rules governing ordinary scripts, frames or network connections. MDN’s worker-src reference describes its scope and supported source expressions.

The fallback chain explains many unexpected failures

When worker-src is absent, the browser checks these directives in order:

worker-src → child-src → script-src → default-src

It uses the first directive present in that policy. A blocked request does not move along the chain looking for a more permissive alternative.

Consider this simplified policy:

Content-Security-Policy: default-src 'self'; script-src 'self'; child-src 'none'

A same-origin external script can execute. However, its attempt to create a worker is blocked because worker-src is missing and child-src 'none' supplies the worker restriction.

Adding an explicit worker directive separates those decisions:

Content-Security-Policy: default-src 'self'; script-src 'self'; child-src 'none'; worker-src 'self'

Within this policy, same-origin worker scripts are now permitted while the existing child restriction remains. These fallback rules are documented in the CSP specification.

Check whether your application creates blob workers

Some libraries generate worker code at runtime and load it through a blob: URL instead of a normal JavaScript file.

Inspect the URL mentioned in the console before changing your policy. If the application legitimately needs blob workers, an explicit directive might be:

worker-src 'self' blob:

Do not assume that 'self' covers blob: URLs across your supported browsers. An explicit scheme allowance makes the requirement clear, but it also broadens the permitted worker sources.

Before adding it, check whether the library supports a separately hosted worker file. Choose the approach that fits the application rather than copying a broad allowance into every policy. The CSP source-matching rules explain the distinction.

This allowance applies to dedicated and shared workers created from blob URLs. Service workers cannot be registered from blob URLs. Their script URLs must use HTTP or HTTPS and meet the same-origin and trustworthy-origin requirements described in MDN’s service worker registration documentation.

Loading a worker and restricting its behaviour are separate tasks

Once a worker starts, it has its own execution context.

For a worker loaded from an ordinary HTTP or HTTPS URL, the creating page’s CSP generally does not govern the code executing inside it. To apply a policy there, return a Content-Security-Policy header on the worker script response.

For example, a worker that only needs same-origin script imports and network requests could receive:

Content-Security-Policy: script-src 'self'; connect-src 'self'

That header belongs on the response serving the worker script. Adding it only to the HTML response does not establish the same policy inside that worker.

Blob workers are an important exception: they inherit the creator’s CSP. MDN’s worker security guidance explains this separation and inheritance behaviour.

This distinction matters during troubleshooting. A worker that fails to start and a running worker whose API request is blocked require different investigations.

Diagnose the failure before changing the policy

Start by reproducing the affected feature with browser developer tools open.

  1. Identify the failed operation. Did worker creation fail, or did an import or network request fail after startup?
  2. Record the worker URL. Check whether it is a normal file URL or a generated blob: URL.
  3. Inspect the delivered policies. Review the response headers received by the browser and any applicable CSP meta elements.
  4. Locate the responsible restriction. Check worker-src and its fallback chain for loading failures. For failures inside a worker, inspect the policy that applies to that worker.
  5. Retest the actual feature. Confirm that processing completes and the expected result reaches the page.

Check every enforced policy. Each is enforced independently, so adding worker-src 'self' to one policy cannot override a restriction in another. A second header added by a proxy or hosting configuration can therefore explain why an application-level change appears to have no effect. The W3C guidance on multiple policies describes this behaviour.

Also verify that the failure is actually caused by CSP. Worker creation has other requirements, including origin restrictions and an acceptable JavaScript MIME type. Adding an origin to CSP does not override those requirements. A missing script that returns an HTML error page, for example, needs a deployment fix. See the Worker constructor documentation for these additional constraints.

Test policy changes with reporting

A candidate restriction can be tested through Content-Security-Policy-Report-Only, with reporting configured to send violations to your collection endpoint.

Report-only policies report violations without enforcing them. However, they do not relax a separate enforced policy. If the current enforced CSP blocks a worker, adding a permissive report-only policy will not make it load. MDN’s report-only documentation explains how the two operate together.

For HeaderCentaur users, collected CSP reports provide another way to investigate worker-related violations encountered by visitors. Use those reports alongside browser debugging and feature testing when deciding whether a policy change is appropriate.

Make the change specific to the worker requirement, verify the policies actually delivered, and test both worker startup and the work it performs.