-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.mjs
More file actions
285 lines (268 loc) · 11.3 KB
/
Copy pathrules.mjs
File metadata and controls
285 lines (268 loc) · 11.3 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Detection ruleset for broomsticks.
// Patterns derived from gitleaks (MIT), secretlint (MIT), and detect-secrets (Apache-2.0).
//
// Each rule:
// pattern — RegExp with `g` and `d` flags (d gives match.indices for precise offsets)
// secretGroup — which capture group is the secret (default: 0 = whole match)
// entropy — minimum Shannon bits/char; match is skipped if below threshold
/**
* @typedef {'critical'|'high'|'medium'|'low'} Severity
* @typedef {{ id:string, title:string, severity:Severity, pattern:RegExp, secretGroup?:number, entropy?:number, deny?:RegExp }} Rule
*
* `deny` (optional): if the captured secret matches this RegExp in full, the
* match is discarded. Used to drop well-known placeholder values that clear the
* entropy gate (e.g. `your-password`).
*/
/**
* Shannon entropy over every character in the string (bits per character).
* Ported from detect-secrets (Apache-2.0); thresholds: base64 ≥ 4.5, hex ≥ 3.0, generic ≥ 3.5.
* @param {string} str
* @returns {number}
*/
export function shannonEntropy(str) {
if (!str) return 0
const freq = new Map()
for (const ch of str) freq.set(ch, (freq.get(ch) ?? 0) + 1)
let e = 0
for (const count of freq.values()) {
const p = count / str.length
e -= p * Math.log2(p)
}
return e
}
/** @type {Rule[]} */
export const RULES = [
// ── Private / Cryptographic Keys ──────────────────────────────────────────
// Matches PEM blocks: RSA, EC, OpenSSH, PGP, PKCS#8, encrypted variants.
{
id: 'private-key',
title: 'Private key (PEM block)',
severity: 'critical',
pattern: /-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY(?: BLOCK)?-----[\s\S]{0,8192}?-----END[ A-Z0-9_-]{0,100}(?:PRIVATE KEY|KEY BLOCK)-----/gd,
},
// ── AWS ───────────────────────────────────────────────────────────────────
// Covers AKIA (long-term), ASIA (session), ABIA (billing), ACCA, A3T* families.
{
id: 'aws-access-key',
title: 'AWS access key ID',
severity: 'high',
pattern: /\b((?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16})\b/gd,
secretGroup: 1,
entropy: 3,
},
// 40-char base64 value paired with a secret-key variable name.
{
id: 'aws-secret-key',
title: 'AWS secret access key',
severity: 'high',
pattern: /(?:aws_?secret_?access_?key|aws_?secret)\s*[:=]\s*["']?([A-Za-z0-9/+=]{40})["']?/gid,
secretGroup: 1,
entropy: 3.5,
},
// ── Anthropic ─────────────────────────────────────────────────────────────
// Covers api03 and api04 formats; always ends with AA (secretlint pattern).
{
id: 'anthropic-key',
title: 'Anthropic API key',
severity: 'high',
pattern: /\b(sk-ant-api0[34]-[A-Za-z0-9_-]{90,128}AA)\b/gd,
secretGroup: 1,
},
{
id: 'anthropic-admin-key',
title: 'Anthropic admin API key',
severity: 'high',
pattern: /\b(sk-ant-admin01-[A-Za-z0-9_-]{93}AA)\b/gd,
secretGroup: 1,
},
// ── OpenAI ────────────────────────────────────────────────────────────────
// New-format keys embed "T3BlbkFJ" (base64 for "OpenAI") as a fixed anchor —
// this dramatically cuts false positives compared to a bare sk- prefix match.
{
id: 'openai-key',
title: 'OpenAI API key',
severity: 'high',
pattern: /\b(sk-(?:proj|svcacct|admin)-[A-Za-z0-9_-]{58,74}T3BlbkFJ[A-Za-z0-9_-]{58,74})\b/gd,
secretGroup: 1,
},
// Legacy 51-char keys, also anchored by T3BlbkFJ.
{
id: 'openai-key-legacy',
title: 'OpenAI API key (legacy sk- format)',
severity: 'high',
pattern: /\b(sk-[A-Za-z0-9]{20}T3BlbkFJ[A-Za-z0-9]{20})\b/gd,
secretGroup: 1,
},
// ── Hugging Face ──────────────────────────────────────────────────────────
{
id: 'huggingface-token',
title: 'Hugging Face user access token',
severity: 'high',
pattern: /\b(hf_[A-Za-z0-9]{34})\b/gd,
secretGroup: 1,
entropy: 2,
},
{
id: 'huggingface-org-token',
title: 'Hugging Face organization API token',
severity: 'high',
pattern: /\b(api_org_[A-Za-z0-9]{34})\b/gd,
secretGroup: 1,
entropy: 2,
},
// ── GitHub ────────────────────────────────────────────────────────────────
// Classic tokens — each type has a distinct 3-letter prefix + 36 alphanumeric chars.
{
id: 'github-pat',
title: 'GitHub personal access token',
severity: 'high',
pattern: /\b(ghp_[0-9A-Za-z]{36})\b/gd,
secretGroup: 1,
entropy: 3,
},
{
id: 'github-oauth',
title: 'GitHub OAuth token',
severity: 'high',
pattern: /\b(gho_[0-9A-Za-z]{36})\b/gd,
secretGroup: 1,
entropy: 3,
},
{
id: 'github-app-token',
title: 'GitHub app installation / server-to-server token',
severity: 'high',
pattern: /\b(gh[us]_[0-9A-Za-z]{36})\b/gd,
secretGroup: 1,
entropy: 3,
},
{
id: 'github-refresh-token',
title: 'GitHub refresh token',
severity: 'high',
pattern: /\b(ghr_[0-9A-Za-z]{36})\b/gd,
secretGroup: 1,
entropy: 3,
},
// Fine-grained PATs: `github_pat_` + exactly 82 word chars.
// Uses Unicode property escape \p{L} for tighter boundary — requires `u` flag.
{
id: 'github-fine-grained-pat',
title: 'GitHub fine-grained personal access token',
severity: 'high',
pattern: /(?<!\p{L})(github_pat_\w{82})(?!\w)/gud,
secretGroup: 1,
},
// ── Google ────────────────────────────────────────────────────────────────
{
id: 'google-api-key',
title: 'Google API key',
severity: 'high',
pattern: /\b(AIza[0-9A-Za-z_-]{35})\b/gd,
secretGroup: 1,
entropy: 4,
},
// ── Stripe ────────────────────────────────────────────────────────────────
// Covers secret keys (sk_) and restricted keys (rk_) in live/test/prod.
{
id: 'stripe-key',
title: 'Stripe secret or restricted key',
severity: 'high',
pattern: /\b((?:sk|rk)_(?:live|test|prod)_[A-Za-z0-9]{10,99})\b/gd,
secretGroup: 1,
entropy: 2,
},
// ── Slack ─────────────────────────────────────────────────────────────────
{
id: 'slack-bot-token',
title: 'Slack bot token',
severity: 'high',
pattern: /\b(xoxb-[0-9]{10,13}-[0-9]{10,13}-[A-Za-z0-9]{24,28})\b/gd,
secretGroup: 1,
entropy: 3,
},
{
id: 'slack-user-token',
title: 'Slack user token',
severity: 'high',
pattern: /\b(xoxp-[0-9]{10,13}-[0-9]{10,13}-[0-9]{10,13}-[A-Za-z0-9]{32})\b/gd,
secretGroup: 1,
entropy: 3,
},
{
id: 'slack-app-token',
title: 'Slack app-level token',
severity: 'high',
pattern: /\b(xapp-\d-[A-Z0-9]+-\d+-[a-z0-9]+)\b/gd,
secretGroup: 1,
entropy: 2,
},
{
id: 'slack-webhook',
title: 'Slack incoming webhook URL',
severity: 'medium',
pattern: /(https?:\/\/hooks\.slack\.com\/(?:services|workflows|triggers)\/[A-Za-z0-9/_-]{20,})/gd,
secretGroup: 1,
},
// ── JSON Web Tokens ───────────────────────────────────────────────────────
// Three dot-separated base64url segments; first two begin with eyJ (= '{"' in base64).
{
id: 'jwt',
title: 'JSON Web Token',
severity: 'medium',
pattern: /\b(ey[A-Za-z0-9_-]{10,}\.ey[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})\b/gd,
secretGroup: 1,
entropy: 3,
},
// ── Database connection strings ───────────────────────────────────────────
// Matches scheme://user:pass@host for common databases. Requires at least
// user:pass@ (the @ is the discriminator — bare scheme://host has no credential).
{
id: 'db-url',
title: 'Database connection string with inline credentials',
severity: 'high',
pattern: /((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|rediss|amqp|amqps):\/\/[^:@\s]{1,128}:[^@\s]{1,256}@[^\s'"`,)]{8,256})/gd,
secretGroup: 1,
},
// ── Labeled password in prose (scoped, low floor) ─────────────────────────
// Pasting a credential as "Password: <value>" is the single most common human
// leak, and it slips under the generic rule's 16-char floor. Here the label
// itself is the signal, so we run a lower length floor (8) with only a modest
// entropy gate (3.0) — enough to drop fixed-char placeholders (xxxxxxxx → 0
// bits) but not to require token-grade randomness.
//
// `pwd` is deliberately NOT a keyword: transcripts routinely contain
// `printenv`/`env` output, and `PWD=/home/...` (a path that clears the entropy
// gate) would be a systematic false positive. `password|passwd|passphrase`
// only.
//
// Genuinely weak human passwords (changeme ≈ 2.75, hunter2 ≈ 2.81) are left
// uncaught on purpose: they sit in the same entropy band as — and below —
// common placeholders (your-password ≈ 3.24), so no threshold separates them.
// The `deny` list removes the placeholder strings that do clear the gate.
{
id: 'labeled-password',
title: 'Labeled password or passphrase',
severity: 'medium',
pattern: /\b(?:password|passwd|passphrase)\s*[:=]\s*["']?([A-Za-z0-9+/=_\-!@#$%^&*]{8,128})["']?/gid,
secretGroup: 1,
entropy: 3,
deny: /^(?:your[-_]?password(?:[-_]?here)?|password\d*|examplepassword|example|placeholder|changeme\d*|redacted|<.*>|\*+|x+)$/i,
},
// ── Generic secret assignment (entropy-gated, runs last) ─────────────────
// Catches unknown key formats assigned to recognisably secret-named variables.
// Only fires when the value's Shannon entropy ≥ 3.5 bits/char, which eliminates
// placeholder strings, UUIDs with low randomness, and most word-like values.
{
id: 'generic-secret',
title: 'Generic secret assignment',
severity: 'medium',
// Value charset includes common password special chars (!@#$%^&*) in addition
// to base64url chars so we catch real passwords, not just token-shaped strings.
// The entropy gate (≥3.5 bits/char) prevents false positives on phrases and UUIDs.
pattern: /(?:api[_\-.]?key|api[_\-.]?secret|auth[_\-.]?token|access[_\-.]?token|secret[_\-.]?key|private[_\-.]?key|client[_\-.]?secret|password|passwd|token|credential)\s*[:=]\s*["']?([A-Za-z0-9+/=_\-!@#$%^&*]{16,})["']?/gid,
secretGroup: 1,
entropy: 3.5,
},
]
export default RULES