Skip to content

Commit ff0869c

Browse files
authored
fix: invalid host message is missing on client with https (#3997) (#3998)
1 parent d576068 commit ff0869c

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

lib/Server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,9 @@ class Server {
16491649
) {
16501650
this.sendMessage([client], "error", "Invalid Host/Origin header");
16511651

1652-
client.terminate();
1652+
// With https enabled, the sendMessage above is encrypted asynchronously so not yet sent
1653+
// Terminate would prevent it sending, so use close to allow it to be sent
1654+
client.close();
16531655

16541656
return;
16551657
}

test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack4

+24
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,30 @@ Array [
308308

309309
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header ("ws"): page errors 1`] = `Array []`;
310310

311+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("sockjs"): console messages 1`] = `
312+
Array [
313+
"[HMR] Waiting for update signal from WDS...",
314+
"Hey.",
315+
"[webpack-dev-server] Invalid Host/Origin header",
316+
"[webpack-dev-server] Disconnected!",
317+
"[webpack-dev-server] Trying to reconnect...",
318+
]
319+
`;
320+
321+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("sockjs"): page errors 1`] = `Array []`;
322+
323+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("ws"): console messages 1`] = `
324+
Array [
325+
"[HMR] Waiting for update signal from WDS...",
326+
"Hey.",
327+
"[webpack-dev-server] Invalid Host/Origin header",
328+
"[webpack-dev-server] Disconnected!",
329+
"[webpack-dev-server] Trying to reconnect...",
330+
]
331+
`;
332+
333+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("ws"): page errors 1`] = `Array []`;
334+
311335
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("sockjs"): console messages 1`] = `
312336
Array [
313337
"[HMR] Waiting for update signal from WDS...",

test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack5

+24
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,30 @@ Array [
308308

309309
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header ("ws"): page errors 1`] = `Array []`;
310310

311+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("sockjs"): console messages 1`] = `
312+
Array [
313+
"[HMR] Waiting for update signal from WDS...",
314+
"Hey.",
315+
"[webpack-dev-server] Invalid Host/Origin header",
316+
"[webpack-dev-server] Disconnected!",
317+
"[webpack-dev-server] Trying to reconnect...",
318+
]
319+
`;
320+
321+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("sockjs"): page errors 1`] = `Array []`;
322+
323+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("ws"): console messages 1`] = `
324+
Array [
325+
"[HMR] Waiting for update signal from WDS...",
326+
"Hey.",
327+
"[webpack-dev-server] Invalid Host/Origin header",
328+
"[webpack-dev-server] Disconnected!",
329+
"[webpack-dev-server] Trying to reconnect...",
330+
]
331+
`;
332+
333+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("ws"): page errors 1`] = `Array []`;
334+
311335
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("sockjs"): console messages 1`] = `
312336
Array [
313337
"[HMR] Waiting for update signal from WDS...",

test/e2e/allowed-hosts.test.js

+79
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,85 @@ describe("allowed hosts", () => {
8989
await server.stop();
9090
});
9191

92+
it(`should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header when "https" is enabled ("${webSocketServer}")`, async () => {
93+
const devServerHost = "127.0.0.1";
94+
const devServerPort = port1;
95+
const proxyHost = devServerHost;
96+
const proxyPort = port2;
97+
98+
const compiler = webpack(config);
99+
const devServerOptions = {
100+
client: {
101+
webSocketURL: {
102+
port: port2,
103+
protocol: "ws",
104+
},
105+
},
106+
webSocketServer,
107+
port: devServerPort,
108+
host: devServerHost,
109+
allowedHosts: "auto",
110+
https: true,
111+
};
112+
const server = new Server(devServerOptions, compiler);
113+
114+
await server.start();
115+
116+
function startProxy(callback) {
117+
const app = express();
118+
119+
app.use(
120+
"/",
121+
createProxyMiddleware({
122+
// Emulation
123+
onProxyReqWs: (proxyReq) => {
124+
proxyReq.setHeader("host", "my-test-host");
125+
},
126+
target: `https://${devServerHost}:${devServerPort}`,
127+
secure: false,
128+
ws: true,
129+
changeOrigin: true,
130+
logLevel: "warn",
131+
})
132+
);
133+
134+
return app.listen(proxyPort, proxyHost, callback);
135+
}
136+
137+
const proxy = await new Promise((resolve) => {
138+
const proxyCreated = startProxy(() => {
139+
resolve(proxyCreated);
140+
});
141+
});
142+
143+
const { page, browser } = await runBrowser();
144+
145+
const pageErrors = [];
146+
const consoleMessages = [];
147+
148+
page
149+
.on("console", (message) => {
150+
consoleMessages.push(message);
151+
})
152+
.on("pageerror", (error) => {
153+
pageErrors.push(error);
154+
});
155+
156+
await page.goto(`http://${proxyHost}:${proxyPort}/main`, {
157+
waitUntil: "networkidle0",
158+
});
159+
160+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
161+
"console messages"
162+
);
163+
expect(pageErrors).toMatchSnapshot("page errors");
164+
165+
proxy.close();
166+
167+
await browser.close();
168+
await server.stop();
169+
});
170+
92171
it(`should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("${webSocketServer}")`, async () => {
93172
const devServerHost = "127.0.0.1";
94173
const devServerPort = port1;

0 commit comments

Comments
 (0)