Skip to content

Commit

Permalink
fix(server): check for external urls in array (#1980)
Browse files Browse the repository at this point in the history
* fix: check for external urls in array

* test: move tests to contentBase

* fix: use is-absolute-url & add test case for number type
  • Loading branch information
EslamHiko authored and hiroppy committed Jul 1, 2019
1 parent 7f51859 commit fa78347
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const serveIndex = require('serve-index');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const validateOptions = require('schema-utils');
const isAbsoluteUrl = require('is-absolute-url');
const updateCompiler = require('./utils/updateCompiler');
const createLogger = require('./utils/createLogger');
const getCertificate = require('./utils/getCertificate');
Expand Down Expand Up @@ -356,7 +357,7 @@ class Server {
contentBase.forEach((item) => {
this.app.get('*', express.static(item));
});
} else if (/^(https?:)?\/\//.test(contentBase)) {
} else if (isAbsoluteUrl(String(contentBase))) {
this.log.warn(
'Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.'
);
Expand Down Expand Up @@ -408,8 +409,8 @@ class Server {
this.app.get('*', serveIndex(item));
});
} else if (
!/^(https?:)?\/\//.test(contentBase) &&
typeof contentBase !== 'number'
typeof contentBase !== 'number' &&
!isAbsoluteUrl(String(contentBase))
) {
this.app.get('*', serveIndex(contentBase));
}
Expand All @@ -418,13 +419,13 @@ class Server {
setupWatchStaticFeature() {
const contentBase = this.options.contentBase;

if (
/^(https?:)?\/\//.test(contentBase) ||
typeof contentBase === 'number'
) {
if (isAbsoluteUrl(String(contentBase)) || typeof contentBase === 'number') {
throw new Error('Watching remote files is not supported.');
} else if (Array.isArray(contentBase)) {
contentBase.forEach((item) => {
if (isAbsoluteUrl(String(item))) {
throw new Error('Watching remote files is not supported.');
}
this._watch(item);
});
} else {
Expand Down
5 changes: 3 additions & 2 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const isAbsoluteUrl = require('is-absolute-url');
const defaultTo = require('./defaultTo');

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

if (
!/^(https?:)?\/\//.test(options.publicPath) &&
!isAbsoluteUrl(String(options.publicPath)) &&
options.publicPath[0] !== '/'
) {
options.publicPath = `/${options.publicPath}`;
Expand Down Expand Up @@ -109,7 +110,7 @@ function createConfig(config, argv, { port }) {
options.contentBase = options.contentBase.map((p) => path.resolve(p));
} else if (/^[0-9]$/.test(options.contentBase)) {
options.contentBase = +options.contentBase;
} else if (!/^(https?:)?\/\//.test(options.contentBase)) {
} else if (!isAbsoluteUrl(String(options.contentBase))) {
options.contentBase = path.resolve(options.contentBase);
}
}
Expand Down
50 changes: 50 additions & 0 deletions test/server/contentBase-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,56 @@ describe('contentBase option', () => {
});
});

describe('testing single & multiple external paths', () => {
afterAll((done) => {
testServer.close(() => {
done();
});
});
it('Should throw exception (string)', (done) => {
try {
// eslint-disable-next-line no-unused-vars
server = testServer.start(config, {
contentBase: 'https://example.com/',
watchContentBase: true,
});

expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Watching remote files is not supported.');
done();
}
});
it('Should throw exception (number)', (done) => {
try {
// eslint-disable-next-line no-unused-vars
server = testServer.start(config, {
contentBase: 2,
watchContentBase: true,
});

expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Watching remote files is not supported.');
done();
}
});
it('Should throw exception (array)', (done) => {
try {
// eslint-disable-next-line no-unused-vars
server = testServer.start(config, {
contentBase: [contentBasePublic, 'https://example.com/'],
watchContentBase: true,
});

expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Watching remote files is not supported.');
done();
}
});
});

describe('default to PWD', () => {
beforeAll((done) => {
jest.spyOn(process, 'cwd').mockImplementation(() => contentBasePublic);
Expand Down

0 comments on commit fa78347

Please sign in to comment.