detect-possible-timing-attacks.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Looks for potential hotspot string comparisons
  3. * @author Adam Baldwin / Jon Lamendola
  4. */
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. var keywords = '((' + [
  9. 'password',
  10. 'secret',
  11. 'api',
  12. 'apiKey',
  13. 'token',
  14. 'auth',
  15. 'pass',
  16. 'hash'
  17. ].join(')|(') + '))';
  18. var re = new RegExp('^' + keywords + '$', 'im');
  19. function containsKeyword (node) {
  20. if (node.type === 'Identifier') {
  21. if (re.test(node.name))
  22. return true;
  23. }
  24. return
  25. }
  26. module.exports = function(context) {
  27. "use strict";
  28. return {
  29. "IfStatement": function(node) {
  30. if (node.test && node.test.type === 'BinaryExpression') {
  31. if (node.test.operator === '==' || node.test.operator === '===' || node.test.operator === '!=' || node.test.operator === '!==') {
  32. var token = context.getTokens(node)[0];
  33. if (node.test.left) {
  34. var left = containsKeyword(node.test.left);
  35. if (left) {
  36. return context.report(node, "Potential timing attack, left side: " + left);
  37. }
  38. }
  39. if (node.test.right) {
  40. var right = containsKeyword(node.test.right);
  41. if (right) {
  42. return context.report(node, "Potential timing attack, right side: " + right);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. };
  49. };