Skip to content

Commit

Permalink
fix: invalid host message is missing on client with https (#3997) (#3998
Browse files Browse the repository at this point in the history
)
  • Loading branch information
driskell committed Nov 13, 2021
1 parent d576068 commit ff0869c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Server.js
Expand Up @@ -1649,7 +1649,9 @@ class Server {
) {
this.sendMessage([client], "error", "Invalid Host/Origin header");

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

return;
}
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack4
Expand Up @@ -308,6 +308,30 @@ Array [

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Invalid Host/Origin header",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Trying to reconnect...",
]
`;

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Invalid Host/Origin header",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Trying to reconnect...",
]
`;

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack5
Expand Up @@ -308,6 +308,30 @@ Array [

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Invalid Host/Origin header",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Trying to reconnect...",
]
`;

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Invalid Host/Origin header",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Trying to reconnect...",
]
`;

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
79 changes: 79 additions & 0 deletions test/e2e/allowed-hosts.test.js
Expand Up @@ -89,6 +89,85 @@ describe("allowed hosts", () => {
await server.stop();
});

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 () => {
const devServerHost = "127.0.0.1";
const devServerPort = port1;
const proxyHost = devServerHost;
const proxyPort = port2;

const compiler = webpack(config);
const devServerOptions = {
client: {
webSocketURL: {
port: port2,
protocol: "ws",
},
},
webSocketServer,
port: devServerPort,
host: devServerHost,
allowedHosts: "auto",
https: true,
};
const server = new Server(devServerOptions, compiler);

await server.start();

function startProxy(callback) {
const app = express();

app.use(
"/",
createProxyMiddleware({
// Emulation
onProxyReqWs: (proxyReq) => {
proxyReq.setHeader("host", "my-test-host");
},
target: `https://${devServerHost}:${devServerPort}`,
secure: false,
ws: true,
changeOrigin: true,
logLevel: "warn",
})
);

return app.listen(proxyPort, proxyHost, callback);
}

const proxy = await new Promise((resolve) => {
const proxyCreated = startProxy(() => {
resolve(proxyCreated);
});
});

const { page, browser } = await runBrowser();

const pageErrors = [];
const consoleMessages = [];

page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://${proxyHost}:${proxyPort}/main`, {
waitUntil: "networkidle0",
});

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);
expect(pageErrors).toMatchSnapshot("page errors");

proxy.close();

await browser.close();
await server.stop();
});

it(`should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("${webSocketServer}")`, async () => {
const devServerHost = "127.0.0.1";
const devServerPort = port1;
Expand Down

0 comments on commit ff0869c

Please sign in to comment.