Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Content Security Policy blocks the WebSocket connection to ws://localhost

Your error

Connecting to 'ws://localhost:3000/' violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.2 more lines it can show

Fix it fast

Most common cause, 1 of 2

The socket server runs on another port or host, which 'self' does not match

Add the socket's exact origin to connect-src, keeping 'self' for your own fetch calls.

Fix
# development
Content-Security-Policy: default-src 'self'; connect-src 'self' ws://localhost:3000

# production
Content-Security-Policy: default-src 'self'; connect-src 'self' wss://socket.example.com
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

"Connecting to 'ws://localhost:3000/' violates the following Content Security Policy directive" means the socket URL is missing from connect-src, or from default-src when connect-src is absent. Under CSP Level 3, 'self' covers only a socket on the page's own host and port. Add the socket origin, such as ws://localhost:3000 or wss://socket.example.com, to connect-src.

  • Applies to Content Security Policy Level 3, Chrome and Edge, Helmet 8.3 on Express 5.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the policy the page really receives, from the response header and from any meta tag, and write the page origin and the socket URL next to each other: scheme, host and port.

Run this first
curl -sI http://localhost:5173/ | grep -i "content-security-policy" | tr ';' '\n'
grep -rn "Content-Security-Policy" --include=*.html .

Then find the line you see in this list and open its fix:

  1. 1The socket server runs on another port or host, which 'self' does not matchThe page and the socket differ in port (localhost:5173 and localhost:3000), in host (example.com and socket.example.com) or in spelling (localhost and 127.0.0.1)
  2. 2A middleware sends a default policy that has no connect-srcYou never wrote a CSP, yet the header is there; the message says 'connect-src' was not explicitly set and quotes default-src 'self'

What does the error look like?

  • Connecting to 'ws://localhost:3000/' violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
  • Connecting to 'wss://socket.example.com/' violates the following Content Security Policy directive: "connect-src 'self'".
  • The WebSocket never reaches the server: no request shows in its log

Cause 1 of 2

The socket server runs on another port or host, which 'self' does not match

You seeThe page and the socket differ in port (localhost:5173 and localhost:3000), in host (example.com and socket.example.com) or in spelling (localhost and 127.0.0.1)

Check

A CSP violation for a ws:// or wss:// URL on another port or subdomain means 'self' does not reach the socket: in CSP Level 3, 'self' matches a WebSocket URL only when its host equals the page's host and the ports are equal or both default; it then accepts ws from an http page and wss from either. Anything on a different port or subdomain needs its own entry. MDN adds that 'self' does not resolve to WebSocket schemes in every browser, which is one more reason to list the origin.

Check
// in the page console
console.log(location.origin, new URL(socketUrl).origin);

Fix

Add the socket's exact origin to connect-src, keeping 'self' for your own fetch calls. Use the ws:// origin of the dev server locally and the wss:// origin in production.

Fix
# development
Content-Security-Policy: default-src 'self'; connect-src 'self' ws://localhost:3000

# production
Content-Security-Policy: default-src 'self'; connect-src 'self' wss://socket.example.com

Verify

Reload with the console open. The violation line is gone and the Network tab shows the socket with status 101.

Verify
document.addEventListener("securitypolicyviolation", (e) => {
  console.log(e.blockedURI, e.effectiveDirective);
});

Confirmed by w3.org

Did this fix it?

Cause 2 of 2

A middleware sends a default policy that has no connect-src

You seeYou never wrote a CSP, yet the header is there; the message says 'connect-src' was not explicitly set and quotes default-src 'self'

Check

A CSP violation that names default-src when you never wrote a policy means a middleware sent one for you, since security middleware often sets CSP by default. Helmet's default policy for Express, for example, has default-src 'self' and no connect-src, so every WebSocket to another origin falls back to default-src and is refused. Adding a second header by hand does not help: the browser enforces each policy it receives, so the stricter one still blocks.

Check
grep -rn "helmet(" --include=*.js --include=*.ts src/ server/
node --input-type=module -e "import helmet from 'helmet'; console.log(helmet.contentSecurityPolicy.getDefaultDirectives())"

Fix

Change the policy where it is generated. With Helmet, pass a connect-src directive; it is merged into the defaults, so the rest of the policy stays as it was.

Fix
import helmet from "helmet";

app.use(
  helmet({
    contentSecurityPolicy: {
      directives: {
        "connect-src": ["'self'", "ws://localhost:3000"],
      },
    },
  }),
);

Verify

Restart the server and confirm that one policy header comes back and that it lists the socket origin under connect-src.

Verify
curl -sI http://localhost:3000/ | grep -i "content-security-policy"

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Build connect-src from the same setting that holds your socket URL, so both change together per environment.
  • Roll out a stricter policy with Content-Security-Policy-Report-Only first and read the reports before enforcing it.
  • Keep one place that sets CSP for the app, the server code or the proxy, not both.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.