Skip to content

Commit

Permalink
fix: Avoid crash when exec() is passed no arguments
Browse files Browse the repository at this point in the history
Closes #82
Refs #23
  • Loading branch information
nzakas committed Apr 18, 2022
1 parent 313c0c6 commit 7f97815
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
5 changes: 4 additions & 1 deletion rules/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// Rule Definition
//------------------------------------------------------------------------------

/*
* Stores variable names pointing to child_process to check (child_process).exec()
*/
const names = [];

module.exports = {
Expand Down Expand Up @@ -38,7 +41,7 @@ module.exports = {
},
MemberExpression: function (node) {
if (node.property.name === 'exec' && names.indexOf(node.object.name) > -1) {
if (node.parent && node.parent.arguments && node.parent.arguments[0].type !== 'Literal') {
if (node.parent && node.parent.arguments.length && node.parent.arguments[0].type !== 'Literal') {
return context.report(node, 'Found child_process.exec() with non Literal first argument');
}
}
Expand Down
34 changes: 14 additions & 20 deletions test/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@ const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester();

const ruleName = 'detect-child-process';
const Rule = require(`../rules/${ruleName}`);
const rule = require(`../rules/${ruleName}`);

const valid = 'child_process.exec(\'ls\')';
const invalidRequire = 'require(\'child_process\')';
const invalidExec = 'var child = require(\'child_process\'); child.exec(com)';

tester.run(`${ruleName} (require("child_process"))`, Rule, {
valid: [{ code: valid }],
tester.run(ruleName, rule, {
valid: ["child_process.exec('ls')"],
invalid: [
{
code: invalidRequire,
errors: [{ message: 'Found require("child_process")' }]
}
]
});

tester.run(`${ruleName} (child_process.exec() wih non literal 1st arg.)`, Rule, {
valid: [{ code: valid }],
invalid: [
code: "require('child_process')",
errors: [{ message: 'Found require("child_process")' }],
},
{
code: "var child = require('child_process'); child.exec(com)",
errors: [{ message: 'Found require("child_process")' }, { message: 'Found child_process.exec() with non Literal first argument' }],
},
{
code: invalidExec,
errors: [{ message: 'Found require("child_process")' }, { message: 'Found child_process.exec() with non Literal first argument' }]
}
]
code: "var child = require('child_process'); child.exec()",
errors: [{ message: 'Found require("child_process")' }],
},
],
});

0 comments on commit 7f97815

Please sign in to comment.