Web Development

Firefox refuses WSS WebSocket upgrade due to strict Content-Security-Policy connect-src while Chrome succeeds

Environment

Environment

HTTPS site served from https://app.example.com with CSP header 'Content-Security-Policy: default-src 'self'; connect-src 'self';' Backend websocket endpoint wss://ws.example.com. Service Worker installed on the site. Issue reproduces on Firefox 116/120 on macOS and Linux; Chrome and Safari connect ok.

Symptoms

Symptoms

In Firefox the WebSocket connection fails at the upgrade step; console shows a CSP error 'Refused to connect to wss://ws.example.com because it violates the following Content Security Policy directive: "connect-src 'self'"'. The same CSP header allows XMLHttpRequest/fetch to ws.example.com when used via https fetch to an HTTP endpoint; service worker interceptors add fetch handlers.

Observations

Observations

Response header for CSP shows just connect-src 'self'. The WS endpoint is on a different subdomain (ws.example.com). Adding 'wss://ws.example.com' to connect-src appears to fix Chrome but Firefox still rejects when the connection is initiated from a page controlled by a Service Worker script. When the service worker is unregistered the wss connection succeeds in Firefox if connect-src includes the exact wss origin. No mixed-content TLS errors are present.

Already attempted

Already attempted

Updated CSP header to include wss: and specific host; still blocked in Firefox when service worker active. Tried using meta CSP (in-page) — same result. Tried moving WS to same origin — works. Verified TLS certificates chain and hostnames correct. Cleared caches, tested private window.

Question

Question

Why does Firefox block the WSS upgrade even when the connect-src includes the wss origin, and how can I allow the WebSocket connection without disabling the service worker or weakening security?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

CSP connect-src lacks the explicit wss://ws.example.com origin, and Firefox enforces scheme+host for upgrades from documents and workers 80%
Service Worker intercepts or opens the connection from an origin/worker scope not covered by the page's CSP, and CSP for workers differs 60%
Firefox bug or stricter CSP policy enforcement compared to Chrome for Service Worker-initiated upgrades 30%

Summary

Firefox enforces CSP 'connect-src' for WebSocket upgrades and treats the upgrade request originating inside a Service Worker differently: if a SW intercepts or proxies the request, Firefox requires the effective request origin to be allowed in connect-src and also verifies host/ scheme. The likely causes are: connect-src header not including wss scheme or exact origin as required by Firefox when Service Worker mediates the request; Service Worker may be initiating fetch/upgrade from a different origin context or with credentials that change the effective origin; or the CSP is delivered differently to worker contexts (HTTP header vs meta) and not applied in the worker scope. Chrome is more permissive in some service-worker CSP cases, which explains the cross-browser difference.

Suggested Fixes

Diagnostic step 1
Add the explicit wss origin to the server CSP header, e.g. Content-Security-Policy: connect-src 'self' wss://ws.example.com wss:;
Diagnostic step 2
Ensure the CSP header is served to worker scope (Service Worker fetches inherit header if same origin); if worker sits on another scope, set header for that scope too.
Diagnostic step 3
If Service Worker initiates the WS (via fetch or clients.openWindow), verify the worker's scope and that it runs under the same origin; instrument with console warnings in the worker.

Continue troubleshooting

Matched from the title, description and tags