Skip to content

Commit

Permalink
fix: add auto and all option in allowedHosts
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed May 27, 2021
1 parent a20867a commit 0b7bb65
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 108 deletions.
5 changes: 0 additions & 5 deletions bin/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,12 @@ module.exports = {
name: 'allowed-hosts',
type: [Boolean, String],
configs: [
{
type: 'boolean',
},
{
type: 'string',
},
],
description: 'Set hosts that are allowed to access the dev server.',
negatedDescription: 'Allow any host to access dev server.',
multiple: true,
negative: true,
},
{
name: 'watch-files',
Expand Down
2 changes: 1 addition & 1 deletion examples/cli/web-socket-url/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = setup({
client: {
webSocketURL: 'ws://localhost:8080',
},
allowedHosts: true,
allowedHosts: 'all',
},
});
2 changes: 1 addition & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ class Server {
checkHeaders(headers, headerToCheck) {
// allow user to opt out of this security check, at their own risk
// by explicitly enabling allowedHosts
if (this.options.allowedHosts) {
if (this.options.allowedHosts.includes('auto')) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
"allowedHosts": {
"anyOf": [
{
"type": "boolean"
"enum": ["auto", "all"]
},
{
"type": "string"
Expand Down
8 changes: 5 additions & 3 deletions lib/utils/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ function normalizeOptions(compiler, options, logger) {
options.devMiddleware = options.devMiddleware || {};

if (typeof options.allowedHosts === 'undefined') {
// allowedHosts is disabled by default
options.allowedHosts = false;
} else if (typeof options.allowedHosts === 'string') {
// allowedHosts allows some default hosts picked from
// `options.host` or `webSocketURL.host` and `localhost`
options.allowedHosts = 'auto';
}
if (typeof options.allowedHosts === 'string') {
// we store allowedHosts as array when supplied as string
options.allowedHosts = [options.allowedHosts];
}
Expand Down
32 changes: 27 additions & 5 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`options validate should throw an error on the "allowedHosts" option with '[]' value 1`] = `
exports[`options validate should throw an error on the "allowedHosts" option with '123' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.allowedHosts should be an non-empty array."
- configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
Details:
* configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\"
* configuration.allowedHosts should be a string.
* configuration.allowedHosts should be an array:
[string, ...] (should not have fewer than 1 item)"
`;

exports[`options validate should throw an error on the "allowedHosts" option with '123' value 1`] = `
exports[`options validate should throw an error on the "allowedHosts" option with 'false' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
Details:
* configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\"
* configuration.allowedHosts should be a string.
* configuration.allowedHosts should be an array:
[string, ...] (should not have fewer than 1 item)"
`;

exports[`options validate should throw an error on the "allowedHosts" option with 'true' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.allowedHosts should be one of these:
boolean | string | [string, ...] (should not have fewer than 1 item)
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
Details:
* configuration.allowedHosts should be a boolean.
* configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\"
* configuration.allowedHosts should be a string.
* configuration.allowedHosts should be an array:
[string, ...] (should not have fewer than 1 item)"
Expand Down
32 changes: 27 additions & 5 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`options validate should throw an error on the "allowedHosts" option with '[]' value 1`] = `
exports[`options validate should throw an error on the "allowedHosts" option with '123' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.allowedHosts should be an non-empty array."
- configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
Details:
* configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\"
* configuration.allowedHosts should be a string.
* configuration.allowedHosts should be an array:
[string, ...] (should not have fewer than 1 item)"
`;

exports[`options validate should throw an error on the "allowedHosts" option with '123' value 1`] = `
exports[`options validate should throw an error on the "allowedHosts" option with 'false' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
Details:
* configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\"
* configuration.allowedHosts should be a string.
* configuration.allowedHosts should be an array:
[string, ...] (should not have fewer than 1 item)"
`;

exports[`options validate should throw an error on the "allowedHosts" option with 'true' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.allowedHosts should be one of these:
boolean | string | [string, ...] (should not have fewer than 1 item)
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
Details:
* configuration.allowedHosts should be a boolean.
* configuration.allowedHosts should be one of these:
\\"auto\\" | \\"all\\"
* configuration.allowedHosts should be a string.
* configuration.allowedHosts should be an array:
[string, ...] (should not have fewer than 1 item)"
Expand Down
1 change: 0 additions & 1 deletion test/cli/__snapshots__/cli.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ Options:
--no-compress Disable gzip compression.
--allowed-hosts [value...] Set hosts that are allowed to access the dev
server.
--no-allowed-hosts Allow any host to access dev server.
--watch-files <value...> Watch static files for file changes.

Global options:
Expand Down
1 change: 0 additions & 1 deletion test/cli/__snapshots__/cli.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ Options:
--no-compress Disable gzip compression.
--allowed-hosts [value...] Set hosts that are allowed to access the dev
server.
--no-allowed-hosts Allow any host to access dev server.
--watch-files <value...> Watch static files for file changes.

Global options:
Expand Down
8 changes: 4 additions & 4 deletions test/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,17 +655,17 @@ describe('CLI', () => {
});

describe('allowed-hosts', () => {
it('--allowed-hosts', (done) => {
testBin('--allowed-hosts')
it('--allowed-hosts auto', (done) => {
testBin(['--allowed-hosts', 'auto'])
.then((output) => {
expect(output.exitCode).toEqual(0);
done();
})
.catch(done);
});

it('--no-allowed-hosts', (done) => {
testBin('--no-allowed-hosts')
it('--allowed-hosts all', (done) => {
testBin(['--allowed-hosts', 'all'])
.then((output) => {
expect(output.exitCode).toEqual(0);
done();
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/web-socket-server-and-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ for (const webSocketServerType of webSocketServerTypes) {
webSocketServer: webSocketServerType,
port: devServerPort,
host: devServerHost,
allowedHosts: true,
allowedHosts: 'all',
hot: true,
};

Expand Down Expand Up @@ -132,7 +132,7 @@ for (const webSocketServerType of webSocketServerTypes) {
webSocketServer: webSocketServerType,
port: devServerPort,
host: devServerHost,
allowedHosts: true,
allowedHosts: 'all',
hot: true,
};

Expand Down Expand Up @@ -204,7 +204,7 @@ for (const webSocketServerType of webSocketServerTypes) {
port: devServerPort,
host: devServerHost,
webSocketServer: webSocketServerType,
allowedHosts: true,
allowedHosts: 'all',
hot: true,
static: true,
};
Expand Down
2 changes: 1 addition & 1 deletion test/server/allowedHosts-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('allowedHosts', () => {
client: {
webSocketURL: 'ws://test.host:80',
},
allowedHosts: true,
allowedHosts: 'all',
};

const headers = {
Expand Down

0 comments on commit 0b7bb65

Please sign in to comment.