-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
142 lines (131 loc) · 3.97 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const {
getIdentifierName,
shannonEntropy,
checkOptions,
HIGH_ENTROPY,
PATTERN_MATCH,
isModulePathString
} = require("./utils");
const STANDARD_PATTERNS = require("./regexes");
function isNonEmptyString(value) {
return value && typeof value === "string";
}
function checkRegexes(value, patterns) {
return Object.keys(patterns)
.map(name => {
const pattern = patterns[name];
const m = value.match(pattern);
if (!m || !m[0]) return m;
return { name, match: m[0] };
})
.filter(payload => !!payload);
}
function shouldIgnore(value, toIgnore) {
for (let i = 0; i < toIgnore.length; i++) {
if (value.match(toIgnore[i])) return true;
}
return false;
}
module.exports = {
meta: {
name: "eslint-plugin-no-secrets",
version: "1.0.0-eslint9"
},
rules: {
"no-secrets": {
meta: {
schema: false,
messages: {
[HIGH_ENTROPY]: `Found a string with entropy {{ entropy }} : "{{ token }}"`,
[PATTERN_MATCH]: `Found a string that matches "{{ name }}" : "{{ match }}"`
},
docs: {
description: "An eslint rule that looks for possible leftover secrets in code",
category: "Best Practices"
}
},
create(context) {
const {
tolerance,
additionalRegexes,
ignoreContent,
ignoreModules,
ignoreIdentifiers,
additionalDelimiters,
ignoreCase
} = checkOptions(context.options[0] || {});
const sourceCode = context.getSourceCode();
const allPatterns = Object.assign({}, STANDARD_PATTERNS, additionalRegexes);
const allDelimiters = additionalDelimiters.concat([" "]);
function splitIntoTokens(value) {
let tokens = [value];
allDelimiters.forEach(delimiter => {
tokens = tokens.map(token => token.split(delimiter));
//flatten
tokens = [].concat.apply([], tokens);
});
return tokens;
}
function checkEntropy(value) {
value = ignoreCase ? value.toLowerCase() : value;
const tokens = splitIntoTokens(value);
return tokens
.map(token => {
const entropy = shannonEntropy(token);
return { token, entropy };
})
.filter(payload => tolerance <= payload.entropy);
}
function entropyReport(data, node) {
//Easier to read numbers
data.entropy = Math.round(data.entropy * 100) / 100;
context.report({
node,
data,
messageId: HIGH_ENTROPY
});
}
function patternReport(data, node) {
context.report({
node,
data,
messageId: PATTERN_MATCH
});
}
function checkString(value, node) {
const idName = getIdentifierName(node);
if (idName && shouldIgnore(idName, ignoreIdentifiers)) return;
if (!isNonEmptyString(value)) return;
if (ignoreModules && isModulePathString(node)) {
return;
}
if (shouldIgnore(value, ignoreContent)) return;
checkEntropy(value).forEach(payload => {
entropyReport(payload, node);
});
checkRegexes(value, allPatterns).forEach(payload => {
patternReport(payload, node);
});
}
//Check all comments
const comments = sourceCode.getAllComments();
comments.forEach(comment => checkString(comment.value,comment));
return {
Literal(node) {
const { value } = node;
checkString(value, node);
},
TemplateElement(node) {
if (!node.value) return;
const value = node.value.cooked;
checkString(value, node);
},
JSONLiteral(node){
const { value } = node;
checkString(value, node);
}
};
}
}
}
};