Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

A Node.js WebSocket client gets 403 Forbidden from a server behind Cloudflare

Your error

Error: Unexpected server response: 4033 more lines it can show

Fix it fast

Most common cause, 1 of 3

Cloudflare answers the upgrade request with a challenge

If you own the zone, add a custom rule with the Skip action for the socket path, and skip the feature that challenges it (Super Bot Fight Mode, managed rules or rate limiting).

Fix
# Security rules > Create rule > Custom rules
# Expression:
(http.host eq "example.com" and starts_with(http.request.uri.path, "/socket"))
# Action: Skip, then tick the features that challenged the request
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

"Unexpected server response: 403" from ws 8 behind Cloudflare is rarely about headers. The upgrade request passes the WAF, and a Node.js client cannot solve a JavaScript challenge. Read the failed response: cf-mitigated: challenge is a challenge to skip for the socket path, an Error 1020 page is a block rule, and neither means your own server refused the Origin.

  • Applies to ws 8.23 on Node.js 22 and 24, Cloudflare WAF (2026 dashboard), gorilla/websocket v1.5.3 as an example origin
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Catch the failed handshake with the ws client's unexpected-response event and print the status, the Cloudflare headers and the start of the body. The answer decides which cause below applies.

Run this first
import WebSocket from "ws";

const ws = new WebSocket("wss://example.com/socket");
ws.on("unexpected-response", (req, res) => {
  console.log(res.statusCode, {
    mitigated: res.headers["cf-mitigated"],
    ray: res.headers["cf-ray"],
    type: res.headers["content-type"],
  });
  res.on("data", (chunk) => console.log(chunk.toString().slice(0, 300)));
});
ws.on("error", console.error);

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

  1. 1Cloudflare answers the upgrade request with a challengeThe failed response carries cf-mitigated: challenge and an HTML body, even though you asked for a WebSocket
  2. 2A Cloudflare security rule blocks the request outrightThe body is the Error 1020: Access denied page and there is no cf-mitigated header
  3. 3Your own server refuses the Origin header the client sendsNo cf-mitigated header, no 1020 page; the body is your application's own text, for example websocket: request origin not allowed by Upgrader.CheckOrigin

What does the error look like?

  • Error: Unexpected server response: 403
  • Error during WebSocket handshake: Unexpected response code: 403
  • cf-mitigated: challenge
  • Error 1020: Access denied

Cause 1 of 3

Cloudflare answers the upgrade request with a challenge

You seeThe failed response carries cf-mitigated: challenge and an HTML body, even though you asked for a WebSocket

Check

A failed handshake whose response has cf-mitigated: challenge means Cloudflare challenged the WebSocket upgrade itself, because WAF rules, rate limits and other security features run on the HTTP request that opens a socket. Challenges need a browser that executes JavaScript; Cloudflare lists curl, wget and similar tools as unable to solve them, and a Node.js socket client is in the same position. No header set will get past it.

Check
curl -s --http1.1 -o /dev/null -D - -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" https://example.com/socket | grep -i -E "^HTTP|cf-mitigated|cf-ray"

Fix

If you own the zone, add a custom rule with the Skip action for the socket path, and skip the feature that challenges it (Super Bot Fight Mode, managed rules or rate limiting). Bot Fight Mode on the Free plan cannot be skipped, so it must be turned off if it is the one challenging. If you do not own the site, ask the owner for an allowed client or use the API they publish for programs.

Fix
# Security rules > Create rule > Custom rules
# Expression:
(http.host eq "example.com" and starts_with(http.request.uri.path, "/socket"))
# Action: Skip, then tick the features that challenged the request

Verify

Run the client again. The handshake returns 101 and the open event fires.

Verify
node client.mjs

Confirmed by developers.cloudflare.com

Did this fix it?

Cause 2 of 3

A Cloudflare security rule blocks the request outright

You seeThe body is the Error 1020: Access denied page and there is no cf-mitigated header

Check

A 403 that returns the Error 1020 page means a Cloudflare security rule with the Block action matched the upgrade request and has no custom response. Unlike a challenge, nothing on the client side can pass it; only the zone owner can change the rule.

Check
curl -s --http1.1 -D - -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" https://example.com/socket | grep -i -E "^cf-ray|1020"

Fix

Find the request in the Security Events log by the Ray ID from the cf-ray header, see which rule matched, then narrow that rule or exclude the socket path and your client from it.

Fix
# Security Events log: filter by the Ray ID
# Open the rule named in the event and add an exception, for example:
... and not starts_with(http.request.uri.path, "/socket")

Verify

A new attempt no longer shows up as blocked in Security Events, and the client logs open.

Verify
node client.mjs

Confirmed by developers.cloudflare.com

Did this fix it?

Cause 3 of 3

Your own server refuses the Origin header the client sends

You seeNo cf-mitigated header, no 1020 page; the body is your application's own text, for example websocket: request origin not allowed by Upgrader.CheckOrigin

Check

A 403 whose body comes from your own application means the socket server refused the Origin header, since many socket servers compare it with their own host. gorilla/websocket, by default, rejects with 403 any request whose Origin is present and points at another host. Copying a browser's headers into a script often copies an Origin that belongs to a different site. The ws client sends an Origin only when you pass the origin option.

Check
grep -rn "Origin\|origin:" client.mjs

Fix

Send the Origin the server expects, or drop it, and allow only the origins you trust on the server rather than accepting all of them.

Fix
// client
const ws = new WebSocket("wss://example.com/socket", { origin: "https://example.com" });

// Go server with gorilla/websocket
var upgrader = websocket.Upgrader{
    CheckOrigin: func(r *http.Request) bool {
        return r.Header.Get("Origin") == "https://example.com"
    },
}

Verify

The server log shows the upgrade accepted, and the client receives its first message.

Verify
node client.mjs

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Give programs their own hostname or path for sockets, with security rules written for machine clients.
  • Log the cf-ray value of every failed handshake, so a block can be found in Security Events in seconds.
  • Add a CI smoke test that opens the socket from a server-side client through the public hostname, so a new security rule that blocks it fails the build.

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: TaxiDex, Workforce.

Next step

Want this handled for good?

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