Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid host message is missing on clientwith https (#3997) #3998

Merged
merged 1 commit into from Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/Server.js
Expand Up @@ -1633,7 +1633,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 @@ -278,6 +278,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 @@ -278,6 +278,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 @@ -88,6 +88,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