Skip to content

Commit 033de4c

Browse files
fix: logic
1 parent 4d3ee50 commit 033de4c

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

lib/utils/normalizeOptions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ function normalizeOptions(compiler, options, logger) {
133133
// `options.host` or `webSocketURL.host` and `localhost`
134134
options.allowedHosts = 'auto';
135135
}
136-
if (typeof options.allowedHosts === 'string') {
136+
if (
137+
typeof options.allowedHosts === 'string' &&
138+
options.allowedHosts !== 'auto' &&
139+
options.allowedHosts !== 'all'
140+
) {
137141
// we store allowedHosts as array when supplied as string
138142
options.allowedHosts = [options.allowedHosts];
139143
}

test/server/allowedHosts-option.test.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const internalIp = require('internal-ip');
34
const webpack = require('webpack');
45
const Server = require('../../lib/Server');
56
const config = require('../fixtures/simple-config/webpack.config');
@@ -20,14 +21,59 @@ describe('allowedHosts', () => {
2021
});
2122
});
2223

23-
it('should always allow any host if options.allowedHosts is enabled', () => {
24+
it('should always allow `localhost` if options.allowedHosts is auto', () => {
2425
const options = {
26+
allowedHosts: 'auto',
27+
};
28+
const headers = {
29+
host: 'localhost',
30+
};
31+
32+
server = createServer(compiler, options);
33+
34+
if (!server.checkHost(headers)) {
35+
throw new Error("Validation didn't fail");
36+
}
37+
});
38+
39+
it('should always allow value from the `host` options if options.allowedHosts is auto', () => {
40+
const networkIP = internalIp.v4.sync();
41+
const options = {
42+
host: networkIP,
43+
};
44+
const headers = {
45+
host: networkIP,
46+
};
47+
48+
server = createServer(compiler, options);
49+
50+
if (!server.checkHost(headers)) {
51+
throw new Error("Validation didn't fail");
52+
}
53+
});
54+
55+
it('should always allow value of the `host` option from the `client.webSocketURL` option if options.allowedHosts is auto', () => {
56+
const options = {
57+
allowedHosts: 'auto',
2558
client: {
2659
webSocketURL: 'ws://test.host:80',
2760
},
28-
allowedHosts: 'all',
61+
};
62+
const headers = {
63+
host: 'test.host',
2964
};
3065

66+
server = createServer(compiler, options);
67+
68+
if (!server.checkHost(headers)) {
69+
throw new Error("Validation didn't fail");
70+
}
71+
});
72+
73+
it('should always allow any host if options.allowedHosts is all', () => {
74+
const options = {
75+
allowedHosts: 'all',
76+
};
3177
const headers = {
3278
host: 'bad.host',
3379
};
@@ -42,9 +88,11 @@ describe('allowedHosts', () => {
4288
it('should allow hosts in allowedHosts', () => {
4389
const tests = ['test.host', 'test2.host', 'test3.host'];
4490
const options = { allowedHosts: tests };
91+
4592
server = createServer(compiler, options);
4693
tests.forEach((test) => {
4794
const headers = { host: test };
95+
4896
if (!server.checkHost(headers)) {
4997
throw new Error("Validation didn't fail");
5098
}
@@ -53,7 +101,9 @@ describe('allowedHosts', () => {
53101

54102
it('should allow hosts that pass a wildcard in allowedHosts', () => {
55103
const options = { allowedHosts: ['.example.com'] };
104+
56105
server = createServer(compiler, options);
106+
57107
const tests = [
58108
'www.example.com',
59109
'subdomain.example.com',
@@ -62,8 +112,10 @@ describe('allowedHosts', () => {
62112
'example.com:80',
63113
'subdomain.example.com:80',
64114
];
115+
65116
tests.forEach((test) => {
66117
const headers = { host: test };
118+
67119
if (!server.checkHost(headers)) {
68120
throw new Error("Validation didn't fail");
69121
}

0 commit comments

Comments
 (0)