Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(detect-child-process): false positive for destructuring with exec #102

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 25 additions & 8 deletions rules/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
// Rule Definition
//------------------------------------------------------------------------------

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

module.exports = {
meta: {
type: 'error',
Expand All @@ -25,22 +20,44 @@ module.exports = {
},
},
create: function (context) {
/*
* Stores variable identifiers pointing to child_process to check (child_process).exec()
*/
const childProcessIdentifiers = new Set();

/**
* Extract identifiers assigned the expression `require("child_process")`.
* @param {Pattern} node
*/
function extractChildProcessIdentifiers(node) {
if (node.type !== 'Identifier') {
return;
}
const variable = context.getScope().set.get(node.name);
if (!variable) {
return;
}
for (const reference of variable.references) {
childProcessIdentifiers.add(reference.identifier);
}
}

return {
CallExpression: function (node) {
if (node.callee.name === 'require') {
const args = node.arguments[0];
if (args && args.type === 'Literal' && args.value === 'child_process') {
if (node.parent.type === 'VariableDeclarator') {
names.push(node.parent.id.name);
extractChildProcessIdentifiers(node.parent.id);
} else if (node.parent.type === 'AssignmentExpression' && node.parent.operator === '=') {
names.push(node.parent.left.name);
extractChildProcessIdentifiers(node.parent.left);
}
return context.report({ node: node, message: 'Found require("child_process")' });
}
}
},
MemberExpression: function (node) {
if (node.property.name === 'exec' && names.indexOf(node.object.name) > -1) {
if (node.property.name === 'exec' && childProcessIdentifiers.has(node.object)) {
if (node.parent && node.parent.arguments && node.parent.arguments.length && node.parent.arguments[0].type !== 'Literal') {
return context.report({ node: node, message: 'Found child_process.exec() with non Literal first argument' });
}
Expand Down
27 changes: 27 additions & 0 deletions test/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,32 @@ 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');
function fn () {
var result = foo.exec(str);
}`,
errors: [
{ message: 'Found require("child_process")', line: 2 },
{ message: 'Found child_process.exec() with non Literal first argument', line: 4 },
],
},
{
code: `
var foo = require('child_process');
function fn () {
var foo = /hello/;
var result = foo.exec(str);
}`,
errors: [{ message: 'Found require("child_process")', line: 2 }],
},
],
});