@@ -415,7 +415,7 @@ class Server {
415415 // Keep track of websocket proxies for external websocket upgrade.
416416 /**
417417 * @private
418- * @type {RequestHandler[] }
418+ * @type {(() => RequestHandler | undefined) [] }
419419 */
420420 this . webSocketProxies = [ ] ;
421421 /**
@@ -1755,7 +1755,7 @@ class Server {
17551755 // Proxy WebSocket without the initial http request
17561756 // https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
17571757 const webSocketProxies =
1758- /** @type {RequestHandler[] } */
1758+ /** @type {(() => RequestHandler | undefined) [] } */
17591759 ( this . webSocketProxies ) ;
17601760
17611761 const hmrPath =
@@ -1768,13 +1768,15 @@ class Server {
17681768 ( this . options . webSocketServer ) . options
17691769 ) . path ;
17701770
1771- for ( const webSocketProxy of webSocketProxies ) {
1772- const proxyUpgrade =
1773- /** @type {RequestHandler & { upgrade: NonNullable<RequestHandler["upgrade"]> } } */
1774- ( webSocketProxy ) . upgrade ;
1775-
1771+ for ( const getWebSocketProxy of webSocketProxies ) {
17761772 /** @type {S } */
17771773 ( this . server ) . on ( "upgrade" , ( req , socket , head ) => {
1774+ const webSocketProxy = getWebSocketProxy ( ) ;
1775+
1776+ if ( ! webSocketProxy ) {
1777+ return ;
1778+ }
1779+
17781780 if ( hmrPath && typeof req . url === "string" ) {
17791781 // Match the configured HMR path exactly the same way the underlying
17801782 // WebSocket server (`ws`) does in `WebSocketServer#shouldHandle`: a
@@ -1789,7 +1791,31 @@ class Server {
17891791 return ;
17901792 }
17911793 }
1792- proxyUpgrade ( req , socket , head ) ;
1794+ // Upgrade requests are dispatched from the raw HTTP server and never
1795+ // enter the middleware chain, so `allowedHosts` and the cross-origin
1796+ // checks do not otherwise run for a proxied WebSocket. Mirror the same
1797+ // host/origin validation the HMR socket performs (see the "connection"
1798+ // handler) so a cross-origin page cannot open a WebSocket through a
1799+ // `ws: true` proxy to a service the developer proxies.
1800+ const headers =
1801+ /** @type {{ [key: string]: string | undefined } } */
1802+ ( req . headers ) ;
1803+
1804+ if (
1805+ ! headers ||
1806+ ! this . isValidHost ( headers , "host" ) ||
1807+ ! this . isValidHost ( headers , "origin" ) ||
1808+ ! this . isSameOrigin ( headers )
1809+ ) {
1810+ if ( ! socket . destroyed ) {
1811+ socket . write ( "HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n" ) ;
1812+ socket . destroy ( ) ;
1813+ }
1814+
1815+ return ;
1816+ }
1817+
1818+ webSocketProxy . upgrade ( req , socket , head ) ;
17931819 } ) ;
17941820 }
17951821 }
@@ -2196,6 +2222,9 @@ class Server {
21962222 return createProxyMiddleware ( {
21972223 ...proxyConfig ,
21982224 pathFilter : /** @type {string } */ ( context ) ,
2225+ // Handle upgrades ourselves so an HTTP request cannot register an
2226+ // additional listener that bypasses our host/origin and HMR checks.
2227+ ws : false ,
21992228 } ) ;
22002229 } ;
22012230
@@ -2231,8 +2260,12 @@ class Server {
22312260 /** @type {RequestHandler } */
22322261 ( getProxyMiddleware ( proxyConfig ) ) ;
22332262
2234- if ( proxyConfig . ws ) {
2235- this . webSocketProxies . push ( proxyMiddleware ) ;
2263+ if ( proxyConfig . ws || typeof proxyConfigOrCallback === "function" ) {
2264+ // Callbacks can replace the middleware or enable WebSockets after
2265+ // an HTTP request. Resolve the current configuration on each upgrade.
2266+ this . webSocketProxies . push ( ( ) =>
2267+ proxyConfig . ws ? proxyMiddleware : undefined ,
2268+ ) ;
22362269 }
22372270
22382271 /**
0 commit comments