Skip to content

Commit 0b7bb65

Browse files
committed
fix: add auto and all option in allowedHosts
1 parent a20867a commit 0b7bb65

16 files changed

+295
-108
lines changed

bin/cli-flags.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,17 +398,12 @@ module.exports = {
398398
name: 'allowed-hosts',
399399
type: [Boolean, String],
400400
configs: [
401-
{
402-
type: 'boolean',
403-
},
404401
{
405402
type: 'string',
406403
},
407404
],
408405
description: 'Set hosts that are allowed to access the dev server.',
409-
negatedDescription: 'Allow any host to access dev server.',
410406
multiple: true,
411-
negative: true,
412407
},
413408
{
414409
name: 'watch-files',

examples/cli/web-socket-url/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ module.exports = setup({
1212
client: {
1313
webSocketURL: 'ws://localhost:8080',
1414
},
15-
allowedHosts: true,
15+
allowedHosts: 'all',
1616
},
1717
});

lib/Server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ class Server {
900900
checkHeaders(headers, headerToCheck) {
901901
// allow user to opt out of this security check, at their own risk
902902
// by explicitly enabling allowedHosts
903-
if (this.options.allowedHosts) {
903+
if (this.options.allowedHosts.includes('auto')) {
904904
return true;
905905
}
906906

lib/options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340
"allowedHosts": {
341341
"anyOf": [
342342
{
343-
"type": "boolean"
343+
"enum": ["auto", "all"]
344344
},
345345
{
346346
"type": "string"

lib/utils/normalizeOptions.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ function normalizeOptions(compiler, options, logger) {
129129
options.devMiddleware = options.devMiddleware || {};
130130

131131
if (typeof options.allowedHosts === 'undefined') {
132-
// allowedHosts is disabled by default
133-
options.allowedHosts = false;
134-
} else if (typeof options.allowedHosts === 'string') {
132+
// allowedHosts allows some default hosts picked from
133+
// `options.host` or `webSocketURL.host` and `localhost`
134+
options.allowedHosts = 'auto';
135+
}
136+
if (typeof options.allowedHosts === 'string') {
135137
// we store allowedHosts as array when supplied as string
136138
options.allowedHosts = [options.allowedHosts];
137139
}

test/__snapshots__/validate-options.test.js.snap.webpack4

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

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

8-
exports[`options validate should throw an error on the "allowedHosts" option with '123' value 1`] = `
16+
exports[`options validate should throw an error on the "allowedHosts" option with 'false' value 1`] = `
17+
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
18+
- configuration.allowedHosts should be one of these:
19+
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
20+
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
21+
Details:
22+
* configuration.allowedHosts should be one of these:
23+
\\"auto\\" | \\"all\\"
24+
* configuration.allowedHosts should be a string.
25+
* configuration.allowedHosts should be an array:
26+
[string, ...] (should not have fewer than 1 item)"
27+
`;
28+
29+
exports[`options validate should throw an error on the "allowedHosts" option with 'true' value 1`] = `
930
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
1031
- configuration.allowedHosts should be one of these:
11-
boolean | string | [string, ...] (should not have fewer than 1 item)
32+
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
1233
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
1334
Details:
14-
* configuration.allowedHosts should be a boolean.
35+
* configuration.allowedHosts should be one of these:
36+
\\"auto\\" | \\"all\\"
1537
* configuration.allowedHosts should be a string.
1638
* configuration.allowedHosts should be an array:
1739
[string, ...] (should not have fewer than 1 item)"

test/__snapshots__/validate-options.test.js.snap.webpack5

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

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

8-
exports[`options validate should throw an error on the "allowedHosts" option with '123' value 1`] = `
16+
exports[`options validate should throw an error on the "allowedHosts" option with 'false' value 1`] = `
17+
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
18+
- configuration.allowedHosts should be one of these:
19+
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
20+
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
21+
Details:
22+
* configuration.allowedHosts should be one of these:
23+
\\"auto\\" | \\"all\\"
24+
* configuration.allowedHosts should be a string.
25+
* configuration.allowedHosts should be an array:
26+
[string, ...] (should not have fewer than 1 item)"
27+
`;
28+
29+
exports[`options validate should throw an error on the "allowedHosts" option with 'true' value 1`] = `
930
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
1031
- configuration.allowedHosts should be one of these:
11-
boolean | string | [string, ...] (should not have fewer than 1 item)
32+
\\"auto\\" | \\"all\\" | string | [string, ...] (should not have fewer than 1 item)
1233
-> Defines routes which are enabled by default, on by default and allows localhost. https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
1334
Details:
14-
* configuration.allowedHosts should be a boolean.
35+
* configuration.allowedHosts should be one of these:
36+
\\"auto\\" | \\"all\\"
1537
* configuration.allowedHosts should be a string.
1638
* configuration.allowedHosts should be an array:
1739
[string, ...] (should not have fewer than 1 item)"

test/cli/__snapshots__/cli.test.js.snap.webpack4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ Options:
273273
--no-compress Disable gzip compression.
274274
--allowed-hosts [value...] Set hosts that are allowed to access the dev
275275
server.
276-
--no-allowed-hosts Allow any host to access dev server.
277276
--watch-files <value...> Watch static files for file changes.
278277

279278
Global options:

test/cli/__snapshots__/cli.test.js.snap.webpack5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ Options:
274274
--no-compress Disable gzip compression.
275275
--allowed-hosts [value...] Set hosts that are allowed to access the dev
276276
server.
277-
--no-allowed-hosts Allow any host to access dev server.
278277
--watch-files <value...> Watch static files for file changes.
279278

280279
Global options:

test/cli/cli.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,17 +655,17 @@ describe('CLI', () => {
655655
});
656656

657657
describe('allowed-hosts', () => {
658-
it('--allowed-hosts', (done) => {
659-
testBin('--allowed-hosts')
658+
it('--allowed-hosts auto', (done) => {
659+
testBin(['--allowed-hosts', 'auto'])
660660
.then((output) => {
661661
expect(output.exitCode).toEqual(0);
662662
done();
663663
})
664664
.catch(done);
665665
});
666666

667-
it('--no-allowed-hosts', (done) => {
668-
testBin('--no-allowed-hosts')
667+
it('--allowed-hosts all', (done) => {
668+
testBin(['--allowed-hosts', 'all'])
669669
.then((output) => {
670670
expect(output.exitCode).toEqual(0);
671671
done();

test/e2e/web-socket-server-and-url.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ for (const webSocketServerType of webSocketServerTypes) {
6565
webSocketServer: webSocketServerType,
6666
port: devServerPort,
6767
host: devServerHost,
68-
allowedHosts: true,
68+
allowedHosts: 'all',
6969
hot: true,
7070
};
7171

@@ -132,7 +132,7 @@ for (const webSocketServerType of webSocketServerTypes) {
132132
webSocketServer: webSocketServerType,
133133
port: devServerPort,
134134
host: devServerHost,
135-
allowedHosts: true,
135+
allowedHosts: 'all',
136136
hot: true,
137137
};
138138

@@ -204,7 +204,7 @@ for (const webSocketServerType of webSocketServerTypes) {
204204
port: devServerPort,
205205
host: devServerHost,
206206
webSocketServer: webSocketServerType,
207-
allowedHosts: true,
207+
allowedHosts: 'all',
208208
hot: true,
209209
static: true,
210210
};

test/server/allowedHosts-option.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('allowedHosts', () => {
2525
client: {
2626
webSocketURL: 'ws://test.host:80',
2727
},
28-
allowedHosts: true,
28+
allowedHosts: 'all',
2929
};
3030

3131
const headers = {

0 commit comments

Comments
 (0)