Skip to content

Commit 7f97815

Browse files
committed
fix: Avoid crash when exec() is passed no arguments
Closes #82 Refs #23
1 parent 313c0c6 commit 7f97815

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

rules/detect-child-process.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// Rule Definition
1010
//------------------------------------------------------------------------------
1111

12+
/*
13+
* Stores variable names pointing to child_process to check (child_process).exec()
14+
*/
1215
const names = [];
1316

1417
module.exports = {
@@ -38,7 +41,7 @@ module.exports = {
3841
},
3942
MemberExpression: function (node) {
4043
if (node.property.name === 'exec' && names.indexOf(node.object.name) > -1) {
41-
if (node.parent && node.parent.arguments && node.parent.arguments[0].type !== 'Literal') {
44+
if (node.parent && node.parent.arguments.length && node.parent.arguments[0].type !== 'Literal') {
4245
return context.report(node, 'Found child_process.exec() with non Literal first argument');
4346
}
4447
}

test/detect-child-process.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,22 @@ const RuleTester = require('eslint').RuleTester;
44
const tester = new RuleTester();
55

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

9-
const valid = 'child_process.exec(\'ls\')';
10-
const invalidRequire = 'require(\'child_process\')';
11-
const invalidExec = 'var child = require(\'child_process\'); child.exec(com)';
12-
13-
tester.run(`${ruleName} (require("child_process"))`, Rule, {
14-
valid: [{ code: valid }],
9+
tester.run(ruleName, rule, {
10+
valid: ["child_process.exec('ls')"],
1511
invalid: [
1612
{
17-
code: invalidRequire,
18-
errors: [{ message: 'Found require("child_process")' }]
19-
}
20-
]
21-
});
22-
23-
tester.run(`${ruleName} (child_process.exec() wih non literal 1st arg.)`, Rule, {
24-
valid: [{ code: valid }],
25-
invalid: [
13+
code: "require('child_process')",
14+
errors: [{ message: 'Found require("child_process")' }],
15+
},
16+
{
17+
code: "var child = require('child_process'); child.exec(com)",
18+
errors: [{ message: 'Found require("child_process")' }, { message: 'Found child_process.exec() with non Literal first argument' }],
19+
},
2620
{
27-
code: invalidExec,
28-
errors: [{ message: 'Found require("child_process")' }, { message: 'Found child_process.exec() with non Literal first argument' }]
29-
}
30-
]
21+
code: "var child = require('child_process'); child.exec()",
22+
errors: [{ message: 'Found require("child_process")' }],
23+
},
24+
],
3125
});

0 commit comments

Comments
 (0)