fix: scope websocket upgrades to the proxy prefix#475
Open
xianjianlf2 wants to merge 2 commits into
Open
Conversation
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
climba03003
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #414.
@fastify/http-proxyregisters a single server-wideupgradelistener that dispatches every WebSocket upgrade throughfastify.routing, regardless of the prefix the proxy is mounted on. When the proxy shares its server with anotherupgradehandler (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
upgradelisteners, 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 soleupgradelistener, behaviour is unchanged — every upgrade is routed exactly as before, so out-of-prefix requests are still rejected normally (existingws-prefix-rewritebehaviour is preserved).Added
test/ws-upgrade-prefix.js, which fails onmain(an out-of-prefix upgrade is answered with a404by the proxy) and passes with this change. Full suite green at 100% coverage.Note for reviewers:
@fastify/websockethas the mirror-image global upgrade listener, so a proxy route and a@fastify/websocketroute 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