Skip to content

Commit

Permalink
fix(detect-child-process): false positives for destructuring spawn (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Dec 14, 2022
1 parent 263bed9 commit fdfe37d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
12 changes: 9 additions & 3 deletions rules/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ module.exports = {
if (node.callee.name === 'require') {
const args = node.arguments[0];
if (args && args.type === 'Literal' && args.value === 'child_process') {
let pattern;
if (node.parent.type === 'VariableDeclarator') {
extractChildProcessIdentifiers(node.parent.id);
pattern = node.parent.id;
} else if (node.parent.type === 'AssignmentExpression' && node.parent.operator === '=') {
extractChildProcessIdentifiers(node.parent.left);
pattern = node.parent.left;
}
if (pattern) {
extractChildProcessIdentifiers(pattern);
}
if (!pattern || pattern.type === 'Identifier') {
return context.report({ node: node, message: 'Found require("child_process")' });
}
return context.report({ node: node, message: 'Found require("child_process")' });
}
}
},
Expand Down
21 changes: 13 additions & 8 deletions test/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ const ruleName = 'detect-child-process';
const rule = require(`../rules/${ruleName}`);

tester.run(ruleName, rule, {
valid: ["child_process.exec('ls')"],
valid: [
"child_process.exec('ls')",
{
code: `
var {} = require('child_process');
var result = /hello/.exec(str);`,
parserOptions: { ecmaVersion: 6 },
},
{
code: "var { spawn } = require('child_process'); spawn(str);",
parserOptions: { ecmaVersion: 6 },
},
],
invalid: [
{
code: "require('child_process')",
Expand All @@ -25,13 +37,6 @@ tester.run(ruleName, rule, {
code: "var child = sinon.stub(require('child_process')); child.exec.returns({});",
errors: [{ message: 'Found require("child_process")' }],
},
{
code: `
var {} = require('child_process');
var result = /hello/.exec(str);`,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: 'Found require("child_process")', line: 2 }],
},
{
code: `
var foo = require('child_process');
Expand Down

0 comments on commit fdfe37d

Please sign in to comment.