Skip to content

Commit c280fbe

Browse files
committed
fix: invalid host message is missing on clientwith https (#3997)
1 parent 6d060ed commit c280fbe

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

lib/Server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,9 @@ class Server {
16331633
) {
16341634
this.sendMessage([client], "error", "Invalid Host/Origin header");
16351635

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

16381640
return;
16391641
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,30 @@ Array [
278278

279279
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 []`;
280280

281+
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`] = `
282+
Array [
283+
"[HMR] Waiting for update signal from WDS...",
284+
"Hey.",
285+
"[webpack-dev-server] Invalid Host/Origin header",
286+
"[webpack-dev-server] Disconnected!",
287+
"[webpack-dev-server] Trying to reconnect...",
288+
]
289+
`;
290+
291+
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 []`;
292+
293+
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`] = `
294+
Array [
295+
"[HMR] Waiting for update signal from WDS...",
296+
"Hey.",
297+
"[webpack-dev-server] Invalid Host/Origin header",
298+
"[webpack-dev-server] Disconnected!",
299+
"[webpack-dev-server] Trying to reconnect...",
300+
]
301+
`;
302+
303+
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 []`;
304+
281305
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`] = `
282306
Array [
283307
"[HMR] Waiting for update signal from WDS...",

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,30 @@ Array [
278278

279279
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 []`;
280280

281+
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`] = `
282+
Array [
283+
"[HMR] Waiting for update signal from WDS...",
284+
"Hey.",
285+
"[webpack-dev-server] Invalid Host/Origin header",
286+
"[webpack-dev-server] Disconnected!",
287+
"[webpack-dev-server] Trying to reconnect...",
288+
]
289+
`;
290+
291+
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 []`;
292+
293+
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`] = `
294+
Array [
295+
"[HMR] Waiting for update signal from WDS...",
296+
"Hey.",
297+
"[webpack-dev-server] Invalid Host/Origin header",
298+
"[webpack-dev-server] Disconnected!",
299+
"[webpack-dev-server] Trying to reconnect...",
300+
]
301+
`;
302+
303+
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 []`;
304+
281305
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`] = `
282306
Array [
283307
"[HMR] Waiting for update signal from WDS...",

test/e2e/allowed-hosts.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,85 @@ describe("allowed hosts", () => {
8888
await server.stop();
8989
});
9090

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

0 commit comments

Comments
 (0)