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

3 files changed

Lines changed: 61 additions & 9 deletions

File tree

‎lib/Server.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const serveIndex = require('serve-index');
2323
constwebpack=require('webpack');
2424
constwebpackDevMiddleware=require('webpack-dev-middleware');
2525
constvalidateOptions=require('schema-utils');
26+
constisAbsoluteUrl=require('is-absolute-url');
2627
constupdateCompiler=require('./utils/updateCompiler');
2728
constcreateLogger=require('./utils/createLogger');
2829
constgetCertificate=require('./utils/getCertificate');
@@ -356,7 +357,7 @@ class Server {
356357
contentBase.forEach((item)=>{
357358
this.app.get('*',express.static(item));
358359
});
359-
}elseif(/^(https?:)?\/\//.test(contentBase)){
360+
}elseif(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
}elseif(
411-
!/^(https?:)?\/\//.test(contentBase)&&
412-
typeofcontentBase!=='number'
412+
typeofcontentBase!=='number'&&
413+
!isAbsoluteUrl(String(contentBase))
413414
){
414415
this.app.get('*',serveIndex(contentBase));
415416
}
@@ -418,13 +419,13 @@ class Server {
418419
setupWatchStaticFeature(){
419420
constcontentBase=this.options.contentBase;
420421

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

‎lib/utils/createConfig.js‎

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

33
constpath=require('path');
4+
constisAbsoluteUrl=require('is-absolute-url');
45
constdefaultTo=require('./defaultTo');
56

67
functioncreateConfig(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
}elseif(/^[0-9]$/.test(options.contentBase)){
111112
options.contentBase=+options.contentBase;
112-
}elseif(!/^(https?:)?\/\//.test(options.contentBase)){
113+
}elseif(!isAbsoluteUrl(String(options.contentBase))){
113114
options.contentBase=path.resolve(options.contentBase);
114115
}
115116
}

‎test/server/contentBase-option.test.js‎

Lines changed: 50 additions & 0 deletions
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)