-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathdetect-child-process.js
70 lines (64 loc) · 2.24 KB
/
detect-child-process.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Tries to detect instances of child_process
* @author Adam Baldwin
*/
'use strict';
const { getImportAccessPath } = require('../utils/import-utils');
const { isStaticExpression } = require('../utils/is-static-expression');
const childProcessPackageNames = ['child_process', 'node:child_process'];
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'error',
docs: {
description: 'Detects instances of "child_process" & non-literal "exec()" calls.',
category: 'Possible Security Vulnerability',
recommended: true,
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-child-process.md',
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();
return {
CallExpression: function (node) {
if (node.callee.name === 'require') {
const args = node.arguments[0];
if (
args &&
args.type === 'Literal' &&
childProcessPackageNames.includes(args.value) &&
node.parent.type !== 'VariableDeclarator' &&
node.parent.type !== 'AssignmentExpression' &&
node.parent.type !== 'MemberExpression'
) {
context.report({ node: node, message: 'Found require("' + args.value + '")' });
}
return;
}
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
// Reports non-literal `exec()` calls.
if (
!node.arguments.length ||
isStaticExpression({
node: node.arguments[0],
scope,
})
) {
return;
}
const pathInfo = getImportAccessPath({
node: node.callee,
scope,
packageNames: childProcessPackageNames,
});
const fnName = pathInfo && pathInfo.path.length === 1 && pathInfo.path[0];
if (fnName !== 'exec') {
return;
}
context.report({ node: node, message: 'Found child_process.exec() with non Literal first argument' });
},
};
},
};