detect-non-literal-fs-filename.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Tries to detect calls to fs functions that take a non Literal value as the filename parameter
  3. * @author Adam Baldwin
  4. */
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. var names = [];
  9. var fsMetaData = require('./data/fsFunctionData.json');
  10. var funcNames = Object.keys(fsMetaData);
  11. module.exports = function(context) {
  12. "use strict";
  13. return {
  14. "MemberExpression": function (node) {
  15. var result = [];
  16. if (funcNames.indexOf(node.property.name) !== -1) {
  17. var meta = fsMetaData[node.property.name];
  18. var args = node.parent.arguments;
  19. meta.forEach(function (i) {
  20. if (args && args.length > i) {
  21. if (args[i].type !== 'Literal') {
  22. result.push(i);
  23. }
  24. }
  25. });
  26. }
  27. if (result.length > 0) {
  28. var token = context.getTokens(node)[0];
  29. return context.report(node, 'Found fs.' + node.property.name + ' with non literal argument at index ' + result.join(','));
  30. }
  31. /*
  32. if (node.parent && node.parent.arguments && node.parent.arguments[index].value) {
  33. return context.report(node, 'found Buffer.' + node.property.name + ' with noAssert flag set true');
  34. }
  35. */
  36. }
  37. };
  38. };