Skip to content

Commit fa78347

Browse files
EslamHikohiroppy
authored andcommitted
fix(server): check for external urls in array (#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 7f51859 commit fa78347

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
@@ -267,6 +267,56 @@ describe('contentBase option', () => {
267267
});
268268
});
269269

270+
describe('testing single & multiple external paths', () => {
271+
afterAll((done) => {
272+
testServer.close(() => {
273+
done();
274+
});
275+
});
276+
it('Should throw exception (string)', (done) => {
277+
try {
278+
// eslint-disable-next-line no-unused-vars
279+
server = testServer.start(config, {
280+
contentBase: 'https://example.com/',
281+
watchContentBase: true,
282+
});
283+
284+
expect(true).toBe(false);
285+
} catch (e) {
286+
expect(e.message).toBe('Watching remote files is not supported.');
287+
done();
288+
}
289+
});
290+
it('Should throw exception (number)', (done) => {
291+
try {
292+
// eslint-disable-next-line no-unused-vars
293+
server = testServer.start(config, {
294+
contentBase: 2,
295+
watchContentBase: true,
296+
});
297+
298+
expect(true).toBe(false);
299+
} catch (e) {
300+
expect(e.message).toBe('Watching remote files is not supported.');
301+
done();
302+
}
303+
});
304+
it('Should throw exception (array)', (done) => {
305+
try {
306+
// eslint-disable-next-line no-unused-vars
307+
server = testServer.start(config, {
308+
contentBase: [contentBasePublic, 'https://example.com/'],
309+
watchContentBase: true,
310+
});
311+
312+
expect(true).toBe(false);
313+
} catch (e) {
314+
expect(e.message).toBe('Watching remote files is not supported.');
315+
done();
316+
}
317+
});
318+
});
319+
270320
describe('default to PWD', () => {
271321
beforeAll((done) => {
272322
jest.spyOn(process, 'cwd').mockImplementation(() => contentBasePublic);

0 commit comments

Comments
 (0)