summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/crypto-check.js
blob: 9d24d3355dce7f3eb0df10f29f008bf009dc8872 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @fileoverview Check that common.hasCrypto is used if crypto, tls,
 * https, or http2 modules are required.
 *
 * This rule can be ignored using // eslint-disable-line crypto-check
 *
 * @author Daniel Bevenius <daniel.bevenius@gmail.com>
 */
'use strict';

const utils = require('./rules-utils.js');

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const msg = 'Please add a hasCrypto check to allow this test to be skipped ' +
            'when Node is built "--without-ssl".';

module.exports = function(context) {
  const missingCheckNodes = [];
  const requireNodes = [];
  var hasSkipCall = false;

  function testCryptoUsage(node) {
    if (utils.isRequired(node, ['crypto', 'tls', 'https', 'http2'])) {
      requireNodes.push(node);
    }
  }

  function testIfStatement(node) {
    if (node.test.argument === undefined) {
      return;
    }
    if (isCryptoCheck(node.test.argument)) {
      checkCryptoCall(node);
    }
  }

  function isCryptoCheck(node) {
    return utils.usesCommonProperty(node, ['hasCrypto', 'hasFipsCrypto']);
  }

  function checkCryptoCall(node) {
    if (utils.inSkipBlock(node)) {
      hasSkipCall = true;
    } else {
      missingCheckNodes.push(node);
    }
  }

  function testMemberExpression(node) {
    if (isCryptoCheck(node)) {
      checkCryptoCall(node);
    }
  }

  function reportIfMissingCheck() {
    if (hasSkipCall) {
      return;
    }

    if (requireNodes.length > 0) {
      if (missingCheckNodes.length > 0) {
        report(missingCheckNodes);
      } else {
        report(requireNodes);
      }
    }
  }

  function report(nodes) {
    nodes.forEach((node) => {
      context.report(node, msg);
    });
  }

  return {
    'CallExpression': (node) => testCryptoUsage(node),
    'IfStatement:exit': (node) => testIfStatement(node),
    'MemberExpression:exit': (node) => testMemberExpression(node),
    'Program:exit': (node) => reportIfMissingCheck(node)
  };
};