Skip to content

Commit

Permalink
Disallows dynamic require statements
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Dec 12, 2018
1 parent 4ccbabe commit 7b68ec2
Show file tree
Hide file tree
Showing 49 changed files with 88 additions and 119 deletions.
1 change: 1 addition & 0 deletions packages/eslint-config-kibana/.eslintrc.js
Expand Up @@ -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',
}
Expand Down
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-plugin-generator/sao_template/sao.js
Expand Up @@ -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);

Expand Down
11 changes: 4 additions & 7 deletions packages/kbn-plugin-helpers/lib/utils.js
Expand Up @@ -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;
}
Expand All @@ -42,11 +42,8 @@ function resolveKibanaPath(path) {
}

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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-plugin-helpers/tasks/build/create_build.js
Expand Up @@ -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,
Expand Down
Expand Up @@ -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');
});
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/dev/build/lib/config.js
Expand Up @@ -25,7 +25,7 @@ import { createPlatform } from './platform';

export async function getConfig({ isRelease, targetAllPlatforms, versionQualifier }) {
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;

Expand Down
2 changes: 1 addition & 1 deletion src/functional_test_runner/lib/config/read_config_file.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/functional_test_runner/lib/mocha/load_test_files.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/console/api_server/server.js
Expand Up @@ -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();
}
}
Expand Down
Expand Up @@ -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 () {
Expand Down
Expand Up @@ -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],
Expand Down
Expand Up @@ -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');
});
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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) {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -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 () {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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(() => {
Expand Down
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 7b68ec2

Please sign in to comment.