-
-
Notifications
You must be signed in to change notification settings - Fork 109
fix: scope websocket upgrades to the proxy prefix #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xianjianlf2
wants to merge
4
commits into
fastify:main
Choose a base branch
from
xianjianlf2:fix/ws-upgrade-respects-prefix-414
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
42565db
fix: scope websocket upgrades to the proxy prefix
xianjianlf2 610ef48
Merge branch 'main' into fix/ws-upgrade-respects-prefix-414
Tony133 7fc29d3
Merge branch 'main' into fix/ws-upgrade-respects-prefix-414
Tony133 508cc3a
test: cover multiple websocket proxy prefixes
xianjianlf2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| 'use strict' | ||
|
|
||
| const { test } = require('node:test') | ||
| const assert = require('node:assert') | ||
| const Fastify = require('fastify') | ||
| const proxy = require('../') | ||
| const WebSocket = require('ws') | ||
| const { createServer } = require('node:http') | ||
| const { promisify } = require('node:util') | ||
| const { once } = require('node:events') | ||
|
|
||
| // Spin up an upstream echo server that prefixes every message it receives, so | ||
| // the proxied payload can be told apart from a locally handled one. | ||
| async function createEchoUpstream (t) { | ||
| const origin = createServer() | ||
| const wss = new WebSocket.Server({ server: origin }) | ||
| wss.on('connection', (ws) => { | ||
| ws.on('message', (message) => ws.send(`proxied:${message}`)) | ||
| }) | ||
| t.after(() => { wss.close() }) | ||
| t.after(() => { origin.close() }) | ||
| await promisify(origin.listen.bind(origin))({ port: 0, host: '127.0.0.1' }) | ||
| return `ws://127.0.0.1:${origin.address().port}` | ||
| } | ||
|
|
||
| // A second, independent WebSocket endpoint mounted directly on the raw server. | ||
| // Its 'upgrade' listener is registered after the proxy's, so in the buggy | ||
| // behaviour the proxy hijacks (and 404s) the socket before this one can run. | ||
| function mountStandalone (t, server, ownedPath) { | ||
| const wss = new WebSocket.Server({ noServer: true }) | ||
| wss.on('connection', (ws) => { | ||
| ws.on('message', (message) => ws.send(`standalone:${message}`)) | ||
| }) | ||
| t.after(() => { wss.close() }) | ||
| server.server.on('upgrade', (rawRequest, socket, head) => { | ||
| if (rawRequest.url.split('?', 1)[0] === ownedPath) { | ||
| wss.handleUpgrade(rawRequest, socket, head, (ws) => { | ||
| wss.emit('connection', ws, rawRequest) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| async function connect (port, path) { | ||
| const ws = new WebSocket(`ws://127.0.0.1:${port}${path}`) | ||
| await once(ws, 'open') | ||
| ws.send('hello') | ||
| const [reply] = await once(ws, 'message') | ||
| ws.close() | ||
| await once(ws, 'close') | ||
| return reply.toString() | ||
| } | ||
|
|
||
| test('does not hijack websocket upgrades outside a prefixed proxy', async (t) => { | ||
| const upstream = await createEchoUpstream(t) | ||
|
|
||
| const server = Fastify() | ||
| server.register(proxy, { prefix: '/proxied', upstream, websocket: true }) | ||
| await server.listen({ port: 0, host: '127.0.0.1' }) | ||
| t.after(() => { server.close() }) | ||
| const port = server.server.address().port | ||
|
|
||
| mountStandalone(t, server, '/standalone') | ||
|
|
||
| // Out of prefix: must reach the standalone handler, untouched by the proxy. | ||
| assert.strictEqual(await connect(port, '/standalone'), 'standalone:hello') | ||
| // In prefix (exact match): still proxied. | ||
| assert.strictEqual(await connect(port, '/proxied'), 'proxied:hello') | ||
| // In prefix (nested path): still proxied. | ||
| assert.strictEqual(await connect(port, '/proxied/nested'), 'proxied:hello') | ||
| }) | ||
|
|
||
| test('a prefix with a trailing slash is scoped correctly', async (t) => { | ||
| const upstream = await createEchoUpstream(t) | ||
|
|
||
| const server = Fastify() | ||
| server.register(proxy, { prefix: '/pub/', upstream, websocket: true }) | ||
| await server.listen({ port: 0, host: '127.0.0.1' }) | ||
| t.after(() => { server.close() }) | ||
| const port = server.server.address().port | ||
|
|
||
| mountStandalone(t, server, '/standalone') | ||
|
|
||
| // Out of prefix: handled by the standalone endpoint. | ||
| assert.strictEqual(await connect(port, '/standalone'), 'standalone:hello') | ||
| // In prefix: proxied to the upstream. | ||
| assert.strictEqual(await connect(port, '/pub/nested'), 'proxied:hello') | ||
| }) | ||
|
|
||
| test('a root proxy still owns upgrades when coexisting with another listener', async (t) => { | ||
| const upstream = await createEchoUpstream(t) | ||
|
|
||
| const server = Fastify() | ||
| server.register(proxy, { upstream, websocket: true }) | ||
| await server.listen({ port: 0, host: '127.0.0.1' }) | ||
| t.after(() => { server.close() }) | ||
| const port = server.server.address().port | ||
|
|
||
| // Present only so the proxy is not the sole 'upgrade' listener; it claims a | ||
| // path the root proxy is never asked about. | ||
| mountStandalone(t, server, '/reserved') | ||
|
|
||
| // The root proxy owns every path, so an arbitrary path is still proxied. | ||
| assert.strictEqual(await connect(port, '/anything'), 'proxied:hello') | ||
| }) | ||
|
|
||
| test('multiple proxies each own their own prefix', async (t) => { | ||
| const upstream = await createEchoUpstream(t) | ||
|
|
||
| const server = Fastify() | ||
| server.register(proxy, { prefix: '/pub', upstream, websocket: true }) | ||
| server.register(proxy, { prefix: '/api', upstream, websocket: true }) | ||
| await server.listen({ port: 0, host: '127.0.0.1' }) | ||
| t.after(() => { server.close() }) | ||
| const port = server.server.address().port | ||
|
|
||
| mountStandalone(t, server, '/standalone') | ||
|
|
||
| assert.strictEqual(await connect(port, '/standalone'), 'standalone:hello') | ||
| assert.strictEqual(await connect(port, '/pub/x'), 'proxied:hello') | ||
| assert.strictEqual(await connect(port, '/api/x'), 'proxied:hello') | ||
| }) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this in
508cc3a, thanks. The new test covers two prefixed websocket proxies on the same Fastify server plus a standalone/standaloneupgrade listener, so/pub/xand/api/xboth stay proxied while the standalone path is left alone.Validation:
node --test test/ws-upgrade-prefix.jsnpm run lint -- test/ws-upgrade-prefix.js