/
index.ts
182 lines (172 loc) · 6.14 KB
/
index.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { SecretLintRuleContext, SecretLintRuleCreator, SecretLintSourceCode } from "@secretlint/types";
import { matchPatterns } from "@textlint/regexp-string-matcher";
import { SecretLintRuleMessageTranslate } from "@secretlint/types";
const regx = require("regx").default("g");
const matchAll: (
text: string,
regExp: RegExp
) => ReturnType<typeof String.prototype.matchAll> = require("string.prototype.matchall");
export interface Options {
allows?: string[];
}
/**
* These should be ignored by default, because these are used in AWS example.
* https://docs.aws.amazon.com/ja_jp/general/latest/gr/aws-access-keys-best-practices.html
*/
export const BUILTIN_IGNORED = {
AWSAccountID: ["AKIAIOSFODNN7EXAMPLE"],
AWSSecretAccessKey: ["wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"]
};
export const messages = {
AWSAccountID: {
en: "found AWS Account ID: {{ID}}",
ja: "AWS Account ID: {{ID}} がみつかりました"
},
AWSSecretAccessKey: {
en: "found AWS Secret Access Key: {{KEY}}",
ja: "AWS Secret Access Key: {{KEY}} がみつかりました"
},
AWSAccessKeyID: {
en: "found AWS Access Key ID: {{ID}}",
ja: "AWS Access Key Id: {{ID}} がみつかりました"
}
};
/*
local aws="(AWS|aws|Aws)?_?" quote="(\"|')" connect="\s*(:|=>|=)\s*"
local opt_quote="${quote}?"
add_config 'secrets.providers' 'git secrets --aws-provider'
add_config 'secrets.patterns' '(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'
add_config 'secrets.patterns' "${opt_quote}${aws}(SECRET|secret|Secret)?_?(ACCESS|access|Access)?_?(KEY|key|Key)${opt_quote}${connect}${opt_quote}[A-Za-z0-9/\+=]{40}${opt_quote}"
add_config 'secrets.patterns' "${opt_quote}${aws}(ACCOUNT|account|Account)_?(ID|id|Id)?${opt_quote}${connect}${opt_quote}[0-9]{4}\-?[0-9]{4}\-?[0-9]{4}${opt_quote}"
add_config 'secrets.allowed' 'AKIAIOSFODNN7EXAMPLE'
add_config 'secrets.allowed' "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
https://docs.cribl.io/docs/regexesyml
*/
const reportAWSAccessKey = ({
t,
source,
context,
options
}: {
t: SecretLintRuleMessageTranslate<typeof messages>;
source: SecretLintSourceCode;
context: SecretLintRuleContext;
options: Required<Options>;
}) => {
// AWS Access Key ID
// Example) AKIAIOSFODNN7SECRETS
const AWSAccessKeyIDPattern = /(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}/g;
const results = matchAll(source.content, AWSAccessKeyIDPattern);
for (const result of results) {
const index = result.index || 0;
const match = result[0] || "";
const range = [index, index + match.length];
// Built-in ignore
if (BUILTIN_IGNORED.AWSAccountID.includes(match)) {
continue;
}
const allowedResults = matchPatterns(match, options.allows);
if (allowedResults.length > 0) {
continue;
}
context.report({
message: t("AWSAccessKeyID", {
ID: match
}),
range
});
}
};
const reportAWSSecretAccessKey = ({
t,
source,
context,
options
}: {
t: SecretLintRuleMessageTranslate<typeof messages>;
source: SecretLintSourceCode;
context: SecretLintRuleContext;
options: Required<Options>;
}) => {
const AWS = "(AWS|aws|Aws)?_?";
const QUOTE = `("|')?`;
const CONNECT = "\\s*(:|=>|=)\\s*";
const AWSSecretPatten = regx`${QUOTE}${AWS}(SECRET|secret|Secret)?_?(ACCESS|access|Access)?_?(KEY|key|Key)${QUOTE}${CONNECT}${QUOTE}[A-Za-z0-9/\+=]{40}${QUOTE}`;
const results = matchAll(source.content, AWSSecretPatten);
for (const result of results) {
const index = result.index || 0;
const match = result[0] || "";
const range = [index, index + match.length];
// Built-in ignored
if (BUILTIN_IGNORED.AWSSecretAccessKey.includes(match)) {
continue;
}
const allowedResults = matchPatterns(match, options.allows);
if (allowedResults.length > 0) {
continue;
}
context.report({
message: t("AWSSecretAccessKey", {
KEY: match
}),
range
});
}
};
const reportAWSAccountID = ({
t,
source,
context,
options
}: {
t: SecretLintRuleMessageTranslate<typeof messages>;
source: SecretLintSourceCode;
context: SecretLintRuleContext;
options: Required<Options>;
}) => {
const AWS = "(AWS|aws|Aws)?_?";
const QUOTE = `("|')?`;
const CONNECT = "\\s*(:|=>|=)\\s*";
const AWSSecretPatten = regx`${QUOTE}${AWS}(ACCOUNT|account|Account)_?(ID|id|Id)?${QUOTE}${CONNECT}${QUOTE}[0-9]{4}\-?[0-9]{4}\-?[0-9]{4}${QUOTE}`;
const results = matchAll(source.content, AWSSecretPatten);
for (const result of results) {
const index = result.index || 0;
const match = result[0] || "";
const range = [index, index + match.length];
const allowedResults = matchPatterns(match, options.allows);
if (allowedResults.length > 0) {
continue;
}
context.report({
message: t("AWSAccountID", {
ID: match
}),
range
});
}
};
export const creator: SecretLintRuleCreator<Options> = {
meta: {
id: "@secretlint/secretlint-rule-aws",
recommended: true,
type: "scanner",
docs: {
url:
"https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/secretlint-rule-aws/README.md"
}
},
create(context, options) {
const normalizedOptions: Required<Options> = {
allows: options.allows || []
};
const t = context.createTranslator(messages);
return {
file(source: SecretLintSourceCode) {
reportAWSAccessKey({ t, source: source, context: context, options: normalizedOptions });
reportAWSSecretAccessKey({ t, source: source, context: context, options: normalizedOptions });
reportAWSAccountID({ t, source: source, context: context, options: normalizedOptions });
}
};
}
};
export default creator;