Skip to content

Commit 0c8717e

Browse files
Merge remote-tracking branch 'origin/main' into feat/multi-compiler-plugin-mode
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
2 parents 662404d + 9915359 commit 0c8717e

7 files changed

Lines changed: 780 additions & 470 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-dev-server": patch
3+
---
4+
5+
Validate the `Host` and `Origin` headers and enforce same-origin checks on WebSocket upgrade requests handled by proxies with `ws: true`. This prevents cross-origin pages from connecting to proxied WebSocket services through the development server.

‎lib/Server.js‎

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)