Skip to content

Commit ab7f6dd

Browse files
authored
chore(options): change disableHostCheck and allowedHosts to firewall (#2715)
1 parent b45cdeb commit ab7f6dd

16 files changed

+222
-112
lines changed

bin/cli-flags.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ module.exports = {
105105
describe: 'The port',
106106
group: CONNECTION_GROUP,
107107
},
108-
{
109-
name: 'disable-host-check',
110-
type: Boolean,
111-
describe: 'Will not check the host',
112-
group: CONNECTION_GROUP,
113-
},
114108
{
115109
name: 'public',
116110
type: String,
@@ -124,10 +118,10 @@ module.exports = {
124118
group: CONNECTION_GROUP,
125119
},
126120
{
127-
name: 'allowed-hosts',
121+
name: 'firewall',
128122
type: String,
129123
describe:
130-
'A list of hosts that are allowed to access the dev server, separated by spaces',
124+
'Enable/disable firewall, or set hosts that are allowed to access the dev server',
131125
group: CONNECTION_GROUP,
132126
multiple: true,
133127
},

bin/options.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ const options = {
8787
describe: 'The port',
8888
group: CONNECTION_GROUP,
8989
},
90-
'disable-host-check': {
91-
type: 'boolean',
92-
describe: 'Will not check the host',
93-
group: CONNECTION_GROUP,
94-
},
9590
public: {
9691
type: 'string',
9792
describe: 'The public hostname/ip address of the server',
@@ -103,10 +98,10 @@ const options = {
10398
describe: 'The hostname/ip address the server will bind to',
10499
group: CONNECTION_GROUP,
105100
},
106-
'allowed-hosts': {
101+
firewall: {
107102
type: 'string',
108103
describe:
109-
'A comma-delimited string of hosts that are allowed to access the dev server',
104+
'Enable/disable firewall, or set hosts that are allowed to access the dev server',
110105
group: CONNECTION_GROUP,
111106
},
112107
};

examples/cli/public-protocol/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ module.exports = setup({
1010
devServer: {
1111
host: '0.0.0.0',
1212
public: 'https://localhost:8080',
13-
disableHostCheck: true,
13+
firewall: false,
1414
},
1515
});

lib/Server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,9 @@ class Server {
686686
}
687687

688688
checkHeaders(headers, headerToCheck) {
689-
// allow user to opt-out this security check, at own risk
690-
if (this.options.disableHostCheck) {
689+
// allow user to opt out of this security check, at their own risk
690+
// by explicitly disabling firewall
691+
if (!this.options.firewall) {
691692
return true;
692693
}
693694

@@ -728,11 +729,11 @@ class Server {
728729
return true;
729730
}
730731

731-
const allowedHosts = this.options.allowedHosts;
732+
const allowedHosts = this.options.firewall;
732733

733734
// always allow localhost host, for convenience
734735
// allow if hostname is in allowedHosts
735-
if (allowedHosts && allowedHosts.length) {
736+
if (Array.isArray(allowedHosts) && allowedHosts.length) {
736737
for (let hostIdx = 0; hostIdx < allowedHosts.length; hostIdx++) {
737738
const allowedHost = allowedHosts[hostIdx];
738739

lib/options.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
}
5757
},
5858
"properties": {
59-
"allowedHosts": {
60-
"type": "array",
61-
"items": {
62-
"type": "string"
63-
}
64-
},
6559
"bonjour": {
6660
"type": "boolean"
6761
},
@@ -99,8 +93,19 @@
9993
"dev": {
10094
"type": "object"
10195
},
102-
"disableHostCheck": {
103-
"type": "boolean"
96+
"firewall": {
97+
"anyOf": [
98+
{
99+
"type": "boolean"
100+
},
101+
{
102+
"type": "array",
103+
"items": {
104+
"type": "string"
105+
},
106+
"minItems": 1
107+
}
108+
]
104109
},
105110
"headers": {
106111
"type": "object"
@@ -376,12 +381,11 @@
376381
},
377382
"errorMessage": {
378383
"properties": {
379-
"allowedHosts": "should be {Array} (https://webpack.js.org/configuration/dev-server/#devserverallowedhosts)",
380384
"bonjour": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverbonjour)",
381385
"client": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverclient)",
382386
"compress": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservercompress)",
383387
"dev": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverdev-)",
384-
"disableHostCheck": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverdisablehostcheck)",
388+
"firewall": "should be {Boolean|Array} (https://webpack.js.org/configuration/dev-server/#devserverfirewall)",
385389
"headers": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverheaders)",
386390
"historyApiFallback": "should be {Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserverhistoryapifallback)",
387391
"host": "should be {String|Null} (https://webpack.js.org/configuration/dev-server/#devserverhost)",

lib/utils/createConfig.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ function createConfig(config, argv, { port }) {
2424
options.host = 'localhost';
2525
}
2626

27-
if (argv.allowedHosts) {
28-
options.allowedHosts = argv.allowedHosts.split(',');
29-
}
30-
3127
if (argv.public) {
3228
options.public = argv.public;
3329
}
@@ -103,8 +99,16 @@ function createConfig(config, argv, { port }) {
10399
options.compress = true;
104100
}
105101

106-
if (argv.disableHostCheck) {
107-
options.disableHostCheck = true;
102+
if (argv.firewall === '') {
103+
// the user provided --firewall, indicating that they want it enabled
104+
options.firewall = true;
105+
} else if (argv.firewall === false) {
106+
options.firewall = false;
107+
} else if (typeof argv.firewall === 'string') {
108+
options.firewall = [argv.firewall];
109+
} else if (argv.firewall) {
110+
// argv.firewall is an array
111+
options.firewall = argv.firewall;
108112
}
109113

110114
if (argv.openPage) {

lib/utils/normalizeOptions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ function normalizeOptions(compiler, options) {
102102
}`;
103103

104104
options.dev = options.dev || {};
105+
106+
if (typeof options.firewall === 'undefined') {
107+
// firewall is enabled by default
108+
options.firewall = true;
109+
}
105110
}
106111

107112
module.exports = normalizeOptions;

test/Validation.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ describe('Validation', () => {
7777
});
7878
});
7979

80-
it('should always allow any host if options.disableHostCheck is set', () => {
80+
it('should always allow any host if options.firewall is disabled', () => {
8181
const options = {
8282
public: 'test.host:80',
83-
disableHostCheck: true,
83+
firewall: false,
8484
};
8585

8686
const headers = {
@@ -175,10 +175,10 @@ describe('Validation', () => {
175175
}
176176
});
177177

178-
describe('allowedHosts', () => {
179-
it('should allow hosts in allowedHosts', () => {
178+
describe('firewall', () => {
179+
it('should allow hosts in firewall', () => {
180180
const tests = ['test.host', 'test2.host', 'test3.host'];
181-
const options = { allowedHosts: tests };
181+
const options = { firewall: tests };
182182
server = new Server(compiler, options);
183183
tests.forEach((test) => {
184184
const headers = { host: test };
@@ -188,8 +188,8 @@ describe('Validation', () => {
188188
});
189189
});
190190

191-
it('should allow hosts that pass a wildcard in allowedHosts', () => {
192-
const options = { allowedHosts: ['.example.com'] };
191+
it('should allow hosts that pass a wildcard in firewall', () => {
192+
const options = { firewall: ['.example.com'] };
193193
server = new Server(compiler, options);
194194
const tests = [
195195
'www.example.com',

test/__snapshots__/Validation.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ exports[`Validation validation should fail validation for invalid \`static\` con
3939
exports[`Validation validation should fail validation for no additional properties 1`] = `
4040
"Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
4141
- configuration has an unknown property 'additional'. These properties are valid:
42-
object { allowedHosts?, bonjour?, client?, compress?, dev?, disableHostCheck?, headers?, historyApiFallback?, host?, hot?, http2?, https?, injectClient?, injectHot?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, openPage?, overlay?, port?, profile?, progress?, proxy?, public?, static?, transportMode?, useLocalIp? }"
42+
object { bonjour?, client?, compress?, dev?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, injectClient?, injectHot?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, openPage?, overlay?, port?, profile?, progress?, proxy?, public?, static?, transportMode?, useLocalIp? }"
4343
`;

test/e2e/ClientOptions.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('sockjs client proxy', () => {
3030
compress: true,
3131
port: port1,
3232
host: '0.0.0.0',
33-
disableHostCheck: true,
33+
firewall: false,
3434
hot: true,
3535
};
3636
testServer.startAwaitingCompilation(config, options, done);
@@ -102,7 +102,7 @@ describe('ws client proxy', () => {
102102
compress: true,
103103
port: port1,
104104
host: '0.0.0.0',
105-
disableHostCheck: true,
105+
firewall: false,
106106
hot: true,
107107
public: 'myhost',
108108
};

test/fixtures/schema/webpack.config.no-dev-stats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
https: '_https',
1818
historyApiFallback: '_historyApiFallback',
1919
compress: '_compress',
20-
disableHostCheck: '_disableHostCheck',
20+
firewall: '_firewall',
2121
open: '_open',
2222
openPage: '_openPage',
2323
useLocalIp: '_useLocalIp',

test/options.test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ describe('options', () => {
126126
success: [() => {}],
127127
failure: [false],
128128
},
129-
allowedHosts: {
130-
success: [[], ['']],
131-
failure: [[false], false],
132-
},
133129
bonjour: {
134130
success: [false],
135131
failure: [''],
@@ -245,9 +241,9 @@ describe('options', () => {
245241
],
246242
failure: [''],
247243
},
248-
disableHostCheck: {
249-
success: [true],
250-
failure: [''],
244+
firewall: {
245+
success: [true, false, ['']],
246+
failure: ['', []],
251247
},
252248
headers: {
253249
success: [{}],

test/server/utils/__snapshots__/createConfig.test.js.snap

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

3-
exports[`createConfig allowedHosts option (devServer config) 1`] = `
4-
Object {
5-
"allowedHosts": Array [
6-
".host.com",
7-
"host2.com",
8-
],
9-
"dev": Object {},
10-
"host": "localhost",
11-
"hot": true,
12-
"port": 8080,
13-
}
14-
`;
15-
16-
exports[`createConfig allowedHosts option 1`] = `
17-
Object {
18-
"allowedHosts": Array [
19-
".host.com",
20-
"host2.com",
21-
],
22-
"dev": Object {},
23-
"host": "localhost",
24-
"hot": true,
25-
"port": 8080,
26-
}
27-
`;
28-
293
exports[`createConfig bonjour option (devServer config) 1`] = `
304
Object {
315
"bonjour": true,
@@ -102,20 +76,66 @@ Object {
10276
}
10377
`;
10478

105-
exports[`createConfig disableHostCheck option (in devServer config) 1`] = `
79+
exports[`createConfig firewall option (boolean false) 1`] = `
80+
Object {
81+
"dev": Object {},
82+
"firewall": false,
83+
"host": "localhost",
84+
"hot": true,
85+
"port": 8080,
86+
}
87+
`;
88+
89+
exports[`createConfig firewall option (boolean in devServer config) 1`] = `
10690
Object {
10791
"dev": Object {},
108-
"disableHostCheck": true,
92+
"firewall": true,
10993
"host": "localhost",
11094
"hot": true,
11195
"port": 8080,
11296
}
11397
`;
11498

115-
exports[`createConfig disableHostCheck option 1`] = `
99+
exports[`createConfig firewall option (boolean true) 1`] = `
116100
Object {
117101
"dev": Object {},
118-
"disableHostCheck": true,
102+
"firewall": true,
103+
"host": "localhost",
104+
"hot": true,
105+
"port": 8080,
106+
}
107+
`;
108+
109+
exports[`createConfig firewall option (empty string) 1`] = `
110+
Object {
111+
"dev": Object {},
112+
"firewall": true,
113+
"host": "localhost",
114+
"hot": true,
115+
"port": 8080,
116+
}
117+
`;
118+
119+
exports[`createConfig firewall option (string array in devServer config) 1`] = `
120+
Object {
121+
"dev": Object {},
122+
"firewall": Array [
123+
".host.com",
124+
"host2.com",
125+
],
126+
"host": "localhost",
127+
"hot": true,
128+
"port": 8080,
129+
}
130+
`;
131+
132+
exports[`createConfig firewall option (string array) 1`] = `
133+
Object {
134+
"dev": Object {},
135+
"firewall": Array [
136+
".host.com",
137+
"host2.com",
138+
],
119139
"host": "localhost",
120140
"hot": true,
121141
"port": 8080,

0 commit comments

Comments
 (0)