Skip to content

Commit 90c17ff

Browse files
EslamHikoknagaitsev
authored andcommitted
fix(server): check for external urls in array (webpack#1980)
* fix: check for external urls in array * test: move tests to contentBase * fix: use is-absolute-url & add test case for number type
1 parent a45b61b commit 90c17ff

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

lib/Server.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const serveIndex = require('serve-index');
2323
const webpack = require('webpack');
2424
const webpackDevMiddleware = require('webpack-dev-middleware');
2525
const validateOptions = require('schema-utils');
26+
const isAbsoluteUrl = require('is-absolute-url');
2627
const updateCompiler = require('./utils/updateCompiler');
2728
const createLogger = require('./utils/createLogger');
2829
const getCertificate = require('./utils/getCertificate');
@@ -356,7 +357,7 @@ class Server {
356357
contentBase.forEach((item) => {
357358
this.app.get('*', express.static(item));
358359
});
359-
} else if (/^(https?:)?\/\//.test(contentBase)) {
360+
} else if (isAbsoluteUrl(String(contentBase))) {
360361
this.log.warn(
361362
'Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.'
362363
);
@@ -408,8 +409,8 @@ class Server {
408409
this.app.get('*', serveIndex(item));
409410
});
410411
} else if (
411-
!/^(https?:)?\/\//.test(contentBase) &&
412-
typeof contentBase !== 'number'
412+
typeof contentBase !== 'number' &&
413+
!isAbsoluteUrl(String(contentBase))
413414
) {
414415
this.app.get('*', serveIndex(contentBase));
415416
}
@@ -418,13 +419,13 @@ class Server {
418419
setupWatchStaticFeature() {
419420
const contentBase = this.options.contentBase;
420421

421-
if (
422-
/^(https?:)?\/\//.test(contentBase) ||
423-
typeof contentBase === 'number'
424-
) {
422+
if (isAbsoluteUrl(String(contentBase)) || typeof contentBase === 'number') {
425423
throw new Error('Watching remote files is not supported.');
426424
} else if (Array.isArray(contentBase)) {
427425
contentBase.forEach((item) => {
426+
if (isAbsoluteUrl(String(item))) {
427+
throw new Error('Watching remote files is not supported.');
428+
}
428429
this._watch(item);
429430
});
430431
} else {

lib/utils/createConfig.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const isAbsoluteUrl = require('is-absolute-url');
45
const defaultTo = require('./defaultTo');
56

67
function createConfig(config, argv, { port }) {
@@ -60,7 +61,7 @@ function createConfig(config, argv, { port }) {
6061
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';
6162

6263
if (
63-
!/^(https?:)?\/\//.test(options.publicPath) &&
64+
!isAbsoluteUrl(String(options.publicPath)) &&
6465
options.publicPath[0] !== '/'
6566
) {
6667
options.publicPath = `/${options.publicPath}`;
@@ -109,7 +110,7 @@ function createConfig(config, argv, { port }) {
109110
options.contentBase = options.contentBase.map((p) => path.resolve(p));
110111
} else if (/^[0-9]$/.test(options.contentBase)) {
111112
options.contentBase = +options.contentBase;
112-
} else if (!/^(https?:)?\/\//.test(options.contentBase)) {
113+
} else if (!isAbsoluteUrl(String(options.contentBase))) {
113114
options.contentBase = path.resolve(options.contentBase);
114115
}
115116
}

test/server/contentBase-option.test.js

+50
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,56 @@ describe('contentBase option', () => {
255255
});
256256
});
257257

258+
describe('testing single & multiple external paths', () => {
259+
afterAll((done) => {
260+
testServer.close(() => {
261+
done();
262+
});
263+
});
264+
it('Should throw exception (string)', (done) => {
265+
try {
266+
// eslint-disable-next-line no-unused-vars
267+
server = testServer.start(config, {
268+
contentBase: 'https://example.com/',
269+
watchContentBase: true,
270+
});
271+
272+
expect(true).toBe(false);
273+
} catch (e) {
274+
expect(e.message).toBe('Watching remote files is not supported.');
275+
done();
276+
}
277+
});
278+
it('Should throw exception (number)', (done) => {
279+
try {
280+
// eslint-disable-next-line no-unused-vars
281+
server = testServer.start(config, {
282+
contentBase: 2,
283+
watchContentBase: true,
284+
});
285+
286+
expect(true).toBe(false);
287+
} catch (e) {
288+
expect(e.message).toBe('Watching remote files is not supported.');
289+
done();
290+
}
291+
});
292+
it('Should throw exception (array)', (done) => {
293+
try {
294+
// eslint-disable-next-line no-unused-vars
295+
server = testServer.start(config, {
296+
contentBase: [contentBasePublic, 'https://example.com/'],
297+
watchContentBase: true,
298+
});
299+
300+
expect(true).toBe(false);
301+
} catch (e) {
302+
expect(e.message).toBe('Watching remote files is not supported.');
303+
done();
304+
}
305+
});
306+
});
307+
258308
describe('default to PWD', () => {
259309
beforeAll((done) => {
260310
jest.spyOn(process, 'cwd').mockImplementation(() => contentBasePublic);

0 commit comments

Comments
 (0)