Skip to content

Commit 326ed56

Browse files
authored
fix: handle `--allowed-hosts all" correctly (#3720)
1 parent 4eb1ab8 commit 326ed56

6 files changed

+132
-11
lines changed

lib/Server.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,24 @@ class Server {
190190
};
191191

192192
if (typeof options.allowedHosts === "undefined") {
193-
// allowedHosts allows some default hosts picked from
194-
// `options.host` or `webSocketURL.hostname` and `localhost`
193+
// AllowedHosts allows some default hosts picked from `options.host` or `webSocketURL.hostname` and `localhost`
195194
options.allowedHosts = "auto";
196195
}
197-
198-
if (
196+
// We store allowedHosts as array when supplied as string
197+
else if (
199198
typeof options.allowedHosts === "string" &&
200199
options.allowedHosts !== "auto" &&
201200
options.allowedHosts !== "all"
202201
) {
203-
// we store allowedHosts as array when supplied as string
204202
options.allowedHosts = [options.allowedHosts];
205203
}
204+
// CLI pass options as array, we should normalize them
205+
else if (
206+
Array.isArray(this.options.allowedHosts) &&
207+
this.options.allowedHosts.includes("all")
208+
) {
209+
options.allowedHosts = "all";
210+
}
206211

207212
if (typeof options.bonjour === "undefined") {
208213
options.bonjour = false;

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

+22
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ Array [
6666

6767
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value ("ws"): page errors 1`] = `Array []`;
6868

69+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("sockjs"): console messages 1`] = `
70+
Array [
71+
"[HMR] Waiting for update signal from WDS...",
72+
"Hey.",
73+
"[webpack-dev-server] Hot Module Replacement enabled.",
74+
"[webpack-dev-server] Live Reloading enabled.",
75+
]
76+
`;
77+
78+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("sockjs"): page errors 1`] = `Array []`;
79+
80+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("ws"): console messages 1`] = `
81+
Array [
82+
"[HMR] Waiting for update signal from WDS...",
83+
"Hey.",
84+
"[webpack-dev-server] Hot Module Replacement enabled.",
85+
"[webpack-dev-server] Live Reloading enabled.",
86+
]
87+
`;
88+
89+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("ws"): page errors 1`] = `Array []`;
90+
6991
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("sockjs"): console messages 1`] = `
7092
Array [
7193
"[HMR] Waiting for update signal from WDS...",

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

+22
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ Array [
6666

6767
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value ("ws"): page errors 1`] = `Array []`;
6868

69+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("sockjs"): console messages 1`] = `
70+
Array [
71+
"[HMR] Waiting for update signal from WDS...",
72+
"Hey.",
73+
"[webpack-dev-server] Hot Module Replacement enabled.",
74+
"[webpack-dev-server] Live Reloading enabled.",
75+
]
76+
`;
77+
78+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("sockjs"): page errors 1`] = `Array []`;
79+
80+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("ws"): console messages 1`] = `
81+
Array [
82+
"[HMR] Waiting for update signal from WDS...",
83+
"Hey.",
84+
"[webpack-dev-server] Hot Module Replacement enabled.",
85+
"[webpack-dev-server] Live Reloading enabled.",
86+
]
87+
`;
88+
89+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value in array ("ws"): page errors 1`] = `Array []`;
90+
6991
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("sockjs"): console messages 1`] = `
7092
Array [
7193
"[HMR] Waiting for update signal from WDS...",

test/e2e/allowed-hosts.test.js

+76
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,82 @@ describe("allowed hosts", () => {
456456
await server.stop();
457457
});
458458

459+
it(`should connect web socket client using custom hostname to web socket server with the "all" value in array ("${webSocketServer}")`, async () => {
460+
const devServerHost = "127.0.0.1";
461+
const devServerPort = port1;
462+
const proxyHost = devServerHost;
463+
const proxyPort = port2;
464+
465+
const compiler = webpack(config);
466+
const devServerOptions = {
467+
client: {
468+
webSocketURL: {
469+
port: port2,
470+
},
471+
},
472+
webSocketServer,
473+
port: devServerPort,
474+
host: devServerHost,
475+
allowedHosts: ["all"],
476+
};
477+
const server = new Server(devServerOptions, compiler);
478+
479+
await server.start();
480+
481+
function startProxy(callback) {
482+
const app = express();
483+
484+
app.use(
485+
"/",
486+
createProxyMiddleware({
487+
// Emulation
488+
onProxyReqWs: (proxyReq) => {
489+
proxyReq.setHeader("origin", "http://my-test-origin.com/");
490+
},
491+
target: `http://${devServerHost}:${devServerPort}`,
492+
ws: true,
493+
changeOrigin: true,
494+
logLevel: "warn",
495+
})
496+
);
497+
498+
return app.listen(proxyPort, proxyHost, callback);
499+
}
500+
501+
const proxy = await new Promise((resolve) => {
502+
const proxyCreated = startProxy(() => {
503+
resolve(proxyCreated);
504+
});
505+
});
506+
507+
const { page, browser } = await runBrowser();
508+
509+
const pageErrors = [];
510+
const consoleMessages = [];
511+
512+
page
513+
.on("console", (message) => {
514+
consoleMessages.push(message);
515+
})
516+
.on("pageerror", (error) => {
517+
pageErrors.push(error);
518+
});
519+
520+
await page.goto(`http://${proxyHost}:${proxyPort}/main`, {
521+
waitUntil: "networkidle0",
522+
});
523+
524+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
525+
"console messages"
526+
);
527+
expect(pageErrors).toMatchSnapshot("page errors");
528+
529+
proxy.close();
530+
531+
await browser.close();
532+
await server.stop();
533+
});
534+
459535
it(`should connect web socket client using custom hostname to web socket server with the custom hostname value ("${webSocketServer}")`, async () => {
460536
const devServerHost = "127.0.0.1";
461537
const devServerPort = port1;

test/server/__snapshots__/Server.test.js.snap.webpack4

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ exports[`Server Server.getFreePort should throws the error when the port isn't f
5858

5959
exports[`Server normalizeOptions allowedHosts is array 1`] = `
6060
Object {
61-
"allowedHosts": Array [
62-
"all",
63-
],
61+
"allowedHosts": "all",
6462
"bonjour": false,
6563
"client": Object {
6664
"logging": "info",

test/server/__snapshots__/Server.test.js.snap.webpack5

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ exports[`Server Server.getFreePort should throws the error when the port isn't f
5858

5959
exports[`Server normalizeOptions allowedHosts is array 1`] = `
6060
Object {
61-
"allowedHosts": Array [
62-
"all",
63-
],
61+
"allowedHosts": "all",
6462
"bonjour": false,
6563
"client": Object {
6664
"logging": "info",

0 commit comments

Comments
 (0)