eslint-plugin-no-credentials
Version:
An ESlint plugin checking for any hardcoded credentials
143 lines (122 loc) • 4.53 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.rules = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _lodash = _interopRequireDefault(require("lodash"));
var _calculateStrongEntropy = _interopRequireDefault(require("./calculateStrongEntropy"));
var _splitStringIntoWords = _interopRequireDefault(require("./splitStringIntoWords"));
var inspectNode = function inspectNode(_ref) {
var node = _ref.node,
value = _ref.value,
context = _ref.context;
if (typeof value !== 'string') {
return;
}
var _ref2 = context.options[0] || {},
delimiters = _ref2.delimiters,
minimumWordLength = _ref2.minimumWordLength,
minimumNumberOfWords = _ref2.minimumNumberOfWords,
shouldSplitCamelCase = _ref2.shouldSplitCamelCase,
_ref2$maximumEntropy = _ref2.maximumEntropy,
maximumEntropy = _ref2$maximumEntropy === void 0 ? 5 : _ref2$maximumEntropy;
var words = (0, _splitStringIntoWords["default"])(value, {
delimiters: delimiters,
minimumNumberOfWords: minimumNumberOfWords,
minimumWordLength: minimumWordLength,
shouldSplitCamelCase: shouldSplitCamelCase
});
var entropies = _lodash["default"].zipObject(words, words.map(function (word) {
return (0, _calculateStrongEntropy["default"])(word);
}));
var tooLowEntropies = Object.entries(entropies).filter(function (_ref3) {
var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
entropy = _ref4[1];
return entropy > maximumEntropy;
});
tooLowEntropies.forEach(function (_ref5) {
var _ref6 = (0, _slicedToArray2["default"])(_ref5, 2),
word = _ref6[0],
entropy = _ref6[1];
context.report({
data: {
entropy: entropy,
word: word
},
messageId: 'tooHighEntropy',
node: node
});
});
};
var rules = {
'no-credentials': {
create: function create(context) {
var sourceCode = context.getSourceCode();
return {
Literal: function Literal(node) {
return inspectNode({
context: context,
node: node,
value: node.value
});
},
Program: function Program() {
var comments = sourceCode.getAllComments();
var _ref7 = context.options[0] || {},
delimiters = _ref7.delimiters,
_ref7$maximumEntropy = _ref7.maximumEntropy,
maximumEntropy = _ref7$maximumEntropy === void 0 ? 5 : _ref7$maximumEntropy,
minimumNumberOfWords = _ref7.minimumNumberOfWords,
minimumWordLength = _ref7.minimumWordLength,
shouldSplitCamelCase = _ref7.shouldSplitCamelCase;
comments.forEach(function (comment) {
var words = (0, _splitStringIntoWords["default"])(comment.value, {
delimiters: delimiters,
minimumNumberOfWords: minimumNumberOfWords,
minimumWordLength: minimumWordLength,
shouldSplitCamelCase: shouldSplitCamelCase
});
var entropies = _lodash["default"].zipObject(words, words.map(function (word) {
return (0, _calculateStrongEntropy["default"])(word);
}));
var tooLowEntropies = Object.entries(entropies).filter(function (_ref8) {
var _ref9 = (0, _slicedToArray2["default"])(_ref8, 2),
entropy = _ref9[1];
return entropy > maximumEntropy;
});
tooLowEntropies.forEach(function (_ref10) {
var _ref11 = (0, _slicedToArray2["default"])(_ref10, 2),
word = _ref11[0],
entropy = _ref11[1];
context.report({
data: {
entropy: entropy,
word: word
},
loc: comment.loc,
messageId: 'tooHighEntropy'
});
});
});
},
TemplateElement: function TemplateElement(node) {
return inspectNode({
context: context,
node: node,
value: node.value.raw
});
}
};
},
meta: {
messages: {
tooHighEntropy: 'too high entropy {{entropy}} : {{word}}'
}
}
}
};
exports.rules = rules;
var _default = null;
exports["default"] = _default;
;