detect-child-process.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Tries to detect instances of child_process
  3. * @author Adam Baldwin
  4. */
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. var names = [];
  9. module.exports = function(context) {
  10. "use strict";
  11. return {
  12. "CallExpression": function (node) {
  13. var token = context.getTokens(node)[0];
  14. if (node.callee.name === 'require') {
  15. var args = node.arguments[0];
  16. if (args && args.type === 'Literal' && args.value === 'child_process') {
  17. if (node.parent.type === 'VariableDeclarator') {
  18. names.push(node.parent.id.name);
  19. } else if (node.parent.type === 'AssignmentExpression' && node.parent.operator === '=') {
  20. names.push(node.parent.left.name);
  21. }
  22. return context.report(node, 'Found require("child_process")');
  23. }
  24. }
  25. },
  26. "MemberExpression": function (node) {
  27. var token = context.getTokens(node)[0];
  28. if (node.property.name === 'exec' && names.indexOf(node.object.name) > -1) {
  29. if (node.parent && node.parent.arguments && node.parent.arguments[0].type !== 'Literal') {
  30. return context.report(node, 'Found child_process.exec() with non Literal first argument');
  31. }
  32. }
  33. }
  34. };
  35. };