[6.x] Prevents dynamic import/require statements (#27515) (#27701)

Backports the following commits to 6.x:
 - Prevents dynamic import/require statements  (#27515)
This commit is contained in:
Larry Gregory 2018-12-21 12:00:00 -05:00 committed by GitHub
parent a230e184fc
commit b786f08781
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 90 additions and 119 deletions

View file

@ -171,6 +171,7 @@ module.exports = {
'import/no-named-as-default': 'error',
'import/no-named-as-default-member': 'error',
'import/no-duplicates': 'error',
'import/no-dynamic-require': 'error',
'prefer-object-spread/prefer-object-spread': 'error',
}

View file

@ -55,7 +55,7 @@ exports.getPlugins = function(config, kibanaPath, projectRoot) {
return pluginsFromMap.concat(
glob.sync(globPatterns).map(pkgJsonPath => {
const path = dirname(pkgJsonPath);
const pkg = require(pkgJsonPath);
const pkg = require(pkgJsonPath); // eslint-disable-line import/no-dynamic-require
return {
name: pkg.name,
directory: path,

View file

@ -57,7 +57,7 @@ export const populateServerRegistries = types => {
global.canvas.register = d => registries[type].register(d);
paths.forEach(path => {
require(path);
require(path); // eslint-disable-line import/no-dynamic-require
});
Object.keys(global).forEach(key => {

View file

@ -27,7 +27,7 @@ const chalk = require('chalk');
const pkg = require('../package.json');
const kibanaPkgPath = require.resolve('../../../package.json');
const kibanaPkg = require(kibanaPkgPath);
const kibanaPkg = require(kibanaPkgPath); // eslint-disable-line import/no-dynamic-require
const KBN_DIR = dirname(kibanaPkgPath);

View file

@ -26,10 +26,10 @@ function babelRegister() {
try {
// add support for moved babel-register source: https://github.com/elastic/kibana/pull/13973
require(resolve(plugin.kibanaRoot, 'src/setup_node_env/babel_register'));
require(resolve(plugin.kibanaRoot, 'src/setup_node_env/babel_register')); // eslint-disable-line import/no-dynamic-require
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
require(resolve(plugin.kibanaRoot, 'src/optimize/babel/register'));
require(resolve(plugin.kibanaRoot, 'src/optimize/babel/register')); // eslint-disable-line import/no-dynamic-require
} else {
throw error;
}
@ -43,13 +43,15 @@ function resolveKibanaPath(path) {
function createToolingLog(level) {
// The tooling log location changed in 6.1.0, see https://github.com/elastic/kibana/pull/14890
const utils = require(resolveKibanaPath('src/utils'));
const utils = require(resolveKibanaPath('src/utils')); // eslint-disable-line import/no-dynamic-require
if (utils.createToolingLog) return utils.createToolingLog(level);
return require(resolveKibanaPath('src/dev')).createToolingLog(level);
return require(resolveKibanaPath('src/dev')) // eslint-disable-line import/no-dynamic-require
.createToolingLog(level);
}
function readFtrConfigFile(log, path, settingOverrides) {
return require(resolveKibanaPath('src/functional_test_runner')).readConfigFile(log, path, settingOverrides);
return require(resolveKibanaPath('src/functional_test_runner')) // eslint-disable-line import/no-dynamic-require
.readConfigFile(log, path, settingOverrides);
}
module.exports = {

View file

@ -51,7 +51,7 @@ function removeSymlinkDependencies(root) {
// parse a ts config file
function parseTsconfig(pluginSourcePath, configPath) {
const ts = require(path.join(pluginSourcePath, 'node_modules', 'typescript'));
const ts = require(path.join(pluginSourcePath, 'node_modules', 'typescript')); // eslint-disable-line import/no-dynamic-require
const { error, config } = ts.parseConfigFileTextToJson(
configPath,

View file

@ -42,7 +42,7 @@ describe('creating the build', () => {
await createBuild(PLUGIN, buildTarget, buildVersion, kibanaVersion, buildFiles);
const pkg = require(resolve(PLUGIN_BUILD_TARGET, 'package.json'));
const pkg = require(resolve(PLUGIN_BUILD_TARGET, 'package.json')); // eslint-disable-line import/no-dynamic-require
expect(pkg).not.toHaveProperty('scripts');
expect(pkg).not.toHaveProperty('devDependencies');
});
@ -52,7 +52,7 @@ describe('creating the build', () => {
await createBuild(PLUGIN, buildTarget, buildVersion, kibanaVersion, buildFiles);
const pkg = require(resolve(PLUGIN_BUILD_TARGET, 'package.json'));
const pkg = require(resolve(PLUGIN_BUILD_TARGET, 'package.json')); // eslint-disable-line import/no-dynamic-require
expect(pkg).toHaveProperty('build');
expect(pkg.build.git).not.toBeUndefined();
expect(pkg.build.date).not.toBeUndefined();

View file

@ -25,7 +25,7 @@ import { createPlatform } from './platform';
export async function getConfig({ isRelease, targetAllPlatforms }) {
const pkgPath = resolve(__dirname, '../../../../package.json');
const pkg = require(pkgPath);
const pkg = require(pkgPath); // eslint-disable-line import/no-dynamic-require
const repoRoot = dirname(pkgPath);
const nodeVersion = pkg.engines.node;

View file

@ -25,7 +25,7 @@ import { transformDeprecations } from './transform_deprecations';
const cache = new WeakMap();
async function getSettingsFromFile(log, path, settingOverrides) {
const configModule = require(path);
const configModule = require(path); // eslint-disable-line import/no-dynamic-require
const configProvider = configModule.__esModule
? configModule.default
: configModule;

View file

@ -48,7 +48,7 @@ export const loadTestFiles = ({ mocha, log, lifecycle, providers, paths, exclude
loadTracer(path, `testFile[${path}]`, () => {
log.verbose('Loading test file %s', path);
const testModule = require(path);
const testModule = require(path); // eslint-disable-line import/no-dynamic-require
const testProvider = testModule.__esModule
? testModule.default
: testModule;

View file

@ -27,7 +27,7 @@ export function resolveApi(senseVersion, apis, h) {
{
if (KNOWN_APIS.includes(name)) {
// for now we ignore sense_version. might add it in the api name later
const api = require('./' + name);
const api = require('./' + name); // eslint-disable-line import/no-dynamic-require
result[name] = api.asJson();
}
}

View file

@ -17,13 +17,12 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../average`);
import moment from 'moment';
const expect = require('chai').expect;
import _ from 'lodash';
describe(filename, function () {
describe('average.js', function () {
describe('average', function () {
it('fills holes in the data', function () {

View file

@ -17,13 +17,12 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../carry`);
import moment from 'moment';
const expect = require('chai').expect;
import _ from 'lodash';
describe(filename, function () {
describe('carry.js', function () {
it('fills holes in the data', function () {
const data = [
[moment.utc('1980', 'YYYY').valueOf(), 10],

View file

@ -17,12 +17,11 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../load_functions`);
const expect = require('chai').expect;
describe(filename, () => {
describe('load_functions.js', () => {
it('exports a function', () => {
expect(fn).to.be.a('function');
});

View file

@ -25,7 +25,7 @@ import processFunctionDefinition from './process_function_definition';
export default function (directory) {
function getTuple(directory, name) {
return [name, require('../' + directory + '/' + name)];
return [name, require('../' + directory + '/' + name)]; // eslint-disable-line import/no-dynamic-require
}
// Get a list of all files and use the filename as the object key

View file

@ -17,15 +17,14 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../abs`);
import _ from 'lodash';
const expect = require('chai').expect;
const seriesList = require('./fixtures/seriesList.js')();
import invoke from './helpers/invoke_series_fn.js';
describe(filename, function () {
describe('abs.js', function () {
it('should return the positive value of every value', function () {
return invoke(fn, [seriesList]).then(function (result) {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../bars`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('bars.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../color`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('color.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,15 +17,14 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../condition`);
import moment from 'moment';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
import getSeriesList from './helpers/get_single_series_list';
import _ from 'lodash';
describe(filename, function () {
describe('condition.js', function () {
let comparable;
let seriesList;

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../cusum`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('cusum.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../derivative`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('derivative.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../divide`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('divide.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../first`);
const expect = require('chai').expect;
const seriesList = require('./fixtures/seriesList.js')();
import invoke from './helpers/invoke_series_fn.js';
describe(filename, function () {
describe('first.js', function () {
it('should return exactly the data input', function () {
return invoke(fn, [seriesList]).then(function (result) {
expect(result.input[0]).to.eql(result.output);

View file

@ -17,15 +17,14 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../fit`);
import moment from 'moment';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
import getSeriesList from './helpers/get_single_series_list';
import _ from 'lodash';
describe(filename, function () {
describe('fit.js', function () {
describe('should not filter out zeros', function () {
it('all zeros', function () {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../hide`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('hide.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../label`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('label.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,13 +17,12 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../legend`);
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('legend.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,13 +17,12 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../lines`);
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('lines.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../log`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('log.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../max`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('max.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../min`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('min.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,8 +17,7 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../movingaverage`);
const expect = require('chai').expect;
import moment from 'moment';
@ -34,7 +33,7 @@ function getFivePointSeries() {
]);
}
describe(filename, () => {
describe('movingaverage.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,8 +17,7 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../movingstd`);
import moment from 'moment';
const expect = require('chai').expect;
@ -26,7 +25,7 @@ import invoke from './helpers/invoke_series_fn.js';
import getSeries from './helpers/get_series';
import getSeriesList from './helpers/get_series_list';
describe(filename, () => {
describe('movingstd.js', () => {
it('computes the moving standard deviation of a list', async () => {
const points = [

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../multiply`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('multiply.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,15 +17,14 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../points`);
import _ from 'lodash';
import assert from 'chai';
const expect = assert.expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('points.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../precision`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('precision.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../range`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('range.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../scale_interval`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('scale_interval.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../static`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('static.js', () => {
it('returns a series in which all numbers are the same', () => {
return invoke(fn, [5]).then((r) => {
expect(_.unique(_.map(r.output.list[0].data, 1))).to.eql([5]);

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../subtract`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('subtract.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../sum`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('sum.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../title`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('title.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,14 +17,13 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../trim`);
import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('trim.js', () => {
let seriesList;
beforeEach(() => {

View file

@ -17,13 +17,12 @@
* under the License.
*/
const filename = require('path').basename(__filename);
const fn = require(`../${filename}`);
const fn = require(`../yaxis`);
import Promise from 'bluebird';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';
describe(filename, () => {
describe('yaxis.js', () => {
let seriesList;
beforeEach(() => {
seriesList = require('./fixtures/seriesList.js')();

View file

@ -22,7 +22,7 @@ import { map, catchError } from 'rxjs/operators';
import { createInvalidPackError } from '../errors';
function createPack(packageJson) {
let provider = require(packageJson.directoryPath);
let provider = require(packageJson.directoryPath); // eslint-disable-line import/no-dynamic-require
if (provider.__esModule) {
provider = provider.default;
}

View file

@ -23,7 +23,7 @@ import * as kbnTestServer from '../../../test_utils/kbn_server';
const src = resolve.bind(null, __dirname, '../../../../src');
const versionHeader = 'kbn-version';
const version = require(src('../package.json')).version;
const version = require(src('../package.json')).version; // eslint-disable-line import/no-dynamic-require
describe('version_check request filter', function () {
let root;

View file

@ -27,7 +27,7 @@ const xsrfHeader = 'kbn-xsrf';
const versionHeader = 'kbn-version';
const testPath = '/xsrf/test/route';
const whitelistedTestPath = '/xsrf/test/route/whitelisted';
const actualVersion = require(src('../package.json')).version;
const actualVersion = require(src('../package.json')).version; // eslint-disable-line import/no-dynamic-require
describe('xsrf request filter', () => {
let root;

View file

@ -20,7 +20,7 @@ async function listFiles() {
const pattern = resolve(__dirname, './apis/*/index.js');
const files = await scan(pattern);
files.forEach(file => {
const { name, description } = require(file);
const { name, description } = require(file); // eslint-disable-line import/no-dynamic-require
console.log(' ' + bold(`node ${argv.$0} ${name}`));
console.log(` ${description}`);
});

View file

@ -56,7 +56,7 @@ function prettyPrintJson(json) {
export async function requestFromApi(argv, requestType) {
const pattern = resolve(__dirname, `./apis/${requestType}/index.js`);
const { method, path, body } = require(pattern);
const { method, path, body } = require(pattern); // eslint-disable-line import/no-dynamic-require
const { host, auth, basePath } = getRequestParams(argv);
if (argv.k || !argv.ssl) {
setIgnoreSSLErrors();

View file

@ -15,5 +15,5 @@ exports.runGulpTask = function(name) {
exports.runKibanaScript = function(name, args = []) {
process.chdir(resolve(__dirname, '../../../..'));
process.argv.splice(2, 0, ...args);
require('../../../../scripts/' + name);
require('../../../../scripts/' + name); // eslint-disable-line import/no-dynamic-require
};

View file

@ -28,7 +28,7 @@ async function exportTypesRegistryFn(server) {
files.forEach(file => {
server.log(['reporting', 'debug', 'exportTypes'], `Found exportType at ${file}`);
const { register } = require(file);
const { register } = require(file); // eslint-disable-line import/no-dynamic-require
register(exportTypesRegistry);
});
return exportTypesRegistry;