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: normalize for allowedHosts #3720

Merged
merged 3 commits into from
Aug 23, 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
15 changes: 10 additions & 5 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,24 @@ class Server {
};

if (typeof options.allowedHosts === "undefined") {
// allowedHosts allows some default hosts picked from
// `options.host` or `webSocketURL.hostname` and `localhost`
// AllowedHosts allows some default hosts picked from `options.host` or `webSocketURL.hostname` and `localhost`
options.allowedHosts = "auto";
}

if (
// We store allowedHosts as array when supplied as string
else if (
typeof options.allowedHosts === "string" &&
options.allowedHosts !== "auto" &&
options.allowedHosts !== "all"
) {
// we store allowedHosts as array when supplied as string
options.allowedHosts = [options.allowedHosts];
}
// CLI pass options as array, we should normalize them
else if (
Array.isArray(this.options.allowedHosts) &&
this.options.allowedHosts.includes("all")
) {
options.allowedHosts = "all";
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also account for allowedHosts: ['host1.com', 'host2.com', 'all']:

if (this.options.allowedHosts === "all") {

- if (this.options.allowedHosts === "all") { 
+ if (
+ this.options.allowedHosts === "all" || 
+ (Array.isArray(this.options.allowedHosts) && this.options.allowedHosts.includes("all"))
+ ) { 

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all and other hosts are mutually exclusive, I don't think there would be a use case for specifying other hosts with all 🤔 Maybe this would be the case for auto when we need to allow localhost along with other hosts

Copy link
Member

@snitin315 snitin315 Aug 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hear more opinions here 👍🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @alexander-akait need slight attention 

is this okay or should we handle 'auto' too here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look at this in near future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add tests once you confirm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was mistake adding auto (because we already allow to handle from localhost/ip/etc), we should revisit this in the next major release, I will finish PR

if (typeof options.bonjour === "undefined") {
options.bonjour = false;
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ Array [

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

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

exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ Array [

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

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

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`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

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

exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
76 changes: 76 additions & 0 deletions test/e2e/allowed-hosts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,82 @@ describe("allowed hosts", () => {
await server.stop();
});

it(`should connect web socket client using custom hostname to web socket server with the "all" value in array ("${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,
},
},
webSocketServer,
port: devServerPort,
host: devServerHost,
allowedHosts: ["all"],
};
const server = new Server(devServerOptions, compiler);

await server.start();

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

app.use(
"/",
createProxyMiddleware({
// Emulation
onProxyReqWs: (proxyReq) => {
proxyReq.setHeader("origin", "http://my-test-origin.com/");
},
target: `http://${devServerHost}:${devServerPort}`,
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 connect web socket client using custom hostname to web socket server with the custom hostname value ("${webSocketServer}")`, async () => {
const devServerHost = "127.0.0.1";
const devServerPort = port1;
Expand Down
4 changes: 1 addition & 3 deletions test/server/__snapshots__/Server.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ exports[`Server Server.getFreePort should throws the error when the port isn't f

exports[`Server normalizeOptions allowedHosts is array 1`] = `
Object {
"allowedHosts": Array [
"all",
],
"allowedHosts": "all",
"bonjour": false,
"client": Object {
"logging": "info",
Expand Down
4 changes: 1 addition & 3 deletions test/server/__snapshots__/Server.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ exports[`Server Server.getFreePort should throws the error when the port isn't f

exports[`Server normalizeOptions allowedHosts is array 1`] = `
Object {
"allowedHosts": Array [
"all",
],
"allowedHosts": "all",
"bonjour": false,
"client": Object {
"logging": "info",
Expand Down