detect-object-injection.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Tries to detect instances of var[var]
  3. * @author Jon Lamendola
  4. */
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. var Sinks = [];
  9. function getSerialize (fn, decycle) {
  10. var seen = [], keys = [];
  11. decycle = decycle || function(key, value) {
  12. return '[Circular ' + getPath(value, seen, keys) + ']'
  13. };
  14. return function(key, value) {
  15. var ret = value;
  16. if (typeof value === 'object' && value) {
  17. if (seen.indexOf(value) !== -1)
  18. ret = decycle(key, value);
  19. else {
  20. seen.push(value);
  21. keys.push(key);
  22. }
  23. }
  24. if (fn) ret = fn(key, ret);
  25. return ret;
  26. }
  27. }
  28. function getPath (value, seen, keys) {
  29. var index = seen.indexOf(value);
  30. var path = [ keys[index] ];
  31. for (index--; index >= 0; index--) {
  32. if (seen[index][ path[0] ] === value) {
  33. value = seen[index];
  34. path.unshift(keys[index]);
  35. }
  36. }
  37. return '~' + path.join('.');
  38. }
  39. function stringify(obj, fn, spaces, decycle) {
  40. return JSON.stringify(obj, getSerialize(fn, decycle), spaces);
  41. }
  42. stringify.getSerialize = getSerialize;module.exports = function(context) {
  43. "use strict";
  44. var isChanged = false;
  45. return {
  46. "MemberExpression": function(node) {
  47. if (node.computed === true) {
  48. var token = context.getTokens(node)[0];
  49. if (node.property.type === 'Identifier') {
  50. if (node.parent.type === 'VariableDeclarator') {
  51. context.report(node, 'Variable Assigned to Object Injection Sink');
  52. } else if (node.parent.type === 'CallExpression') {
  53. // console.log(node.parent)
  54. context.report(node, 'Function Call Object Injection Sink');
  55. } else {
  56. context.report(node, 'Generic Object Injection Sink');
  57. }
  58. }
  59. }
  60. }
  61. };
  62. }