Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): check for external urls in array #1980

Merged
merged 4 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -347,7 +348,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 @@ -399,8 +400,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 @@ -409,13 +410,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