Skip to content

fix: scope websocket upgrades to the proxy prefix#475

Open
xianjianlf2 wants to merge 2 commits into
fastify:mainfrom
xianjianlf2:fix/ws-upgrade-respects-prefix-414
Open

fix: scope websocket upgrades to the proxy prefix#475
xianjianlf2 wants to merge 2 commits into
fastify:mainfrom
xianjianlf2:fix/ws-upgrade-respects-prefix-414

Conversation

@xianjianlf2

Copy link
Copy Markdown

Fixes #414.

@fastify/http-proxy registers a single server-wide upgrade listener that dispatches every WebSocket upgrade through fastify.routing, regardless of the prefix the proxy is mounted on. When the proxy shares its server with another upgrade handler (e.g. @fastify/websocket), it consumes and answers upgrades it does not own, corrupting those unrelated WebSocket endpoints (the reporter's "existing handlers stop working").

This scopes the shared upgrade listener: when the proxy coexists with other upgrade listeners, it only handles upgrades whose path falls within a registered proxy prefix and leaves the rest untouched for the owning handler. When the proxy is the sole upgrade listener, behaviour is unchanged — every upgrade is routed exactly as before, so out-of-prefix requests are still rejected normally (existing ws-prefix-rewrite behaviour is preserved).

Added test/ws-upgrade-prefix.js, which fails on main (an out-of-prefix upgrade is answered with a 404 by the proxy) and passes with this change. Full suite green at 100% coverage.

Note for reviewers: @fastify/websocket has the mirror-image global upgrade listener, so a proxy route and a @fastify/websocket route on the same path still conflict — that needs a reciprocal change on their side. This PR fixes the http-proxy side: it no longer steals upgrades outside its own prefix.

Closes #414

The plugin registers a single server-wide `upgrade` listener that dispatches
every WebSocket upgrade through `fastify.routing`, regardless of the prefix the
proxy is mounted on. When the proxy shares the server with another `upgrade`
handler (for example `@fastify/websocket`), it consumes and answers upgrades it
does not own, breaking those unrelated WebSocket endpoints.

Only handle upgrades whose path falls within a registered proxy prefix when the
proxy coexists with other `upgrade` listeners. When the proxy is the only
listener, behaviour is unchanged: every upgrade is routed as before, so
out-of-prefix requests are still rejected normally.

Closes fastify#414

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
mcollina requested review from Eomm and climba03003 July 16, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebSocket proxy handles upgrades regardless prefix

4 participants