-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecretDetectors.ts
More file actions
400 lines (353 loc) · 13.1 KB
/
secretDetectors.ts
File metadata and controls
400 lines (353 loc) · 13.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { shannonEntropyNormalized } from './entropy.js';
import { isLikelyMinified } from '../helpers/isLikelyMinified.js';
/**
* Severity levels for detected secrets
*/
export type SecretSeverity = 'high' | 'medium' | 'low';
/**
* Represents a secret finding in the source code.
*/
export type SecretFinding = {
file: string;
line: number;
kind: 'pattern' | 'entropy';
message: string;
snippet: string;
severity: SecretSeverity;
};
// Regular expressions for detecting suspicious keys and provider patterns
export const SUSPICIOUS_KEYS =
/\b(pass(word)?|secret|token|apikey|api_key|client_secret|access[_-]?token)\b/i;
// Regular expressions for detecting provider patterns
export const PROVIDER_PATTERNS: RegExp[] = [
/\bAKIA[0-9A-Z]{16}\b/, // AWS access key id
/\bASIA[0-9A-Z]{16}\b/, // AWS temp key
/\bghp_[0-9A-Za-z]{30,}\b/, // GitHub token
/\bsk_live_[0-9a-zA-Z]{24,}\b/, // Stripe live secret
/\bsk_test_[0-9a-zA-Z]{24,}\b/, // Stripe test secret
/\bAIza[0-9A-Za-z\-_]{20,}\b/, // Google API key
/\bya29\.[0-9A-Za-z\-_]+\b/, // Google OAuth access token
/\b[A-Za-z0-9_-]{21}:[A-Za-z0-9_-]{140}\b/, // Firebase token
/\b0x[a-fA-F0-9]{40}\b/, // Ethereum address
/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b/, // JWT token
/\bAC[0-9a-fA-F]{32}\b/, // Twilio Account SID
];
// Regex for detecting long literals
const LONG_LITERAL = /["'`]{1}([A-Za-z0-9+/_\-]{24,})["'`]{1}/g;
// Regex for detecting HTTPS URLs
const HTTPS_PATTERN = /["'`](https:\/\/(?!localhost)[^"'`]*)["'`]/g;
// List of harmless URL patterns to ignore
const HARMLESS_URLS = [
/https?:\/\/(www\.)?placeholder\.com/i,
/https?:\/\/(www\.)?example\.com/i,
/https?:\/\/127\.0\.0\.1(:\d+)?/i,
/http:\/\/www\.w3\.org\/2000\/svg/i,
/xmlns=["']http:\/\/www\.w3\.org\/2000\/svg["']/i, // SVG namespace
];
// Known harmless attribute keys commonly used in UI / analytics
const HARMLESS_ATTRIBUTE_KEYS =
/\b(trackingId|trackingContext|data-testid|data-test|aria-label)\b/i;
// Checks if a line is an HTML text node or tag
function isHtmlTextNode(line: string): boolean {
const trimmed = line.trim();
// Starts with <tag> and ends with </tag> with text inside
// OR is a self-contained HTML tag (even without closing tag on same line)
return (
(/^<[^>]+>[^<]*<\/[^>]+>$/.test(trimmed) &&
!/=["'`][^"'`]*["'`]/.test(trimmed)) || // complete tag without suspicious assignment
/^<[a-z][a-z0-9-]*(?:\s+[a-z-]+(?:=["'][^"']*["'])?)*\s*\/?>$/i.test(
trimmed,
) // opening or self-closing tag
);
}
/**
* Determines the severity of an entropy-based secret finding.
* Note: This function assumes literalLength >= 32 (filtered before calling).
* @param literalLength The length of the literal string
* @returns The severity level of the secret finding
*/
function determineEntropySeverity(literalLength: number): SecretSeverity {
// HIGH: Very high-entropy long strings (48+ chars)
if (literalLength >= 48) {
return 'high';
}
// MEDIUM: Medium high-entropy strings (32-47 chars)
return 'medium';
}
/**
* Checks if a line has an ignore comment
* fx: // dotenv-diff-ignore or /* dotenv-diff-ignore *\/ or <!-- dotenv-diff-ignore -->
* @param line - The line to check
* @returns True if the line should be ignored
*/
export function hasIgnoreComment(line: string): boolean {
const normalized = line.trim();
// Allow mixed casing, extra spaces, and optional dashes
return (
/\/\/.*dotenv[\s-]*diff[\s-]*ignore/i.test(normalized) ||
/\/\*.*dotenv[\s-]*diff[\s-]*ignore.*\*\//i.test(normalized) ||
/<!--.*dotenv[\s-]*diff[\s-]*ignore.*-->/i.test(normalized) ||
/\bdotenv[\s-]*diff[\s-]*ignore\b/i.test(normalized)
);
}
/**
* Checks if a URL should be ignored based on ignoreUrls from config.
* @param url - The URL that might be a potential secret
* @param ignoreUrls - List of URLs to ignore (from config)
* @returns true if the URL matches any ignore pattern
*/
function ignoreUrlsMatch(url: string, ignoreUrls?: string[]): boolean {
if (!ignoreUrls?.length) return false;
// case-insensitive substring match
return ignoreUrls.some((pattern) =>
url.toLowerCase().includes(pattern.toLowerCase()),
);
}
/**
* Checks if a string looks like a character set / alphabet used for ID generation
* or similar utilities (e.g. customAlphabet, nanoid, uuid generation).
*
* A charset string is characterised by:
* - Containing long runs of consecutive ASCII characters (a-z, A-Z, 0-9)
* - Low uniqueness ratio: many repeated character classes, few truly unique chars
* relative to string length
*
* Examples that should pass:
* 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' // dotenv-diff-ignore
* '0123456789abcdef'
* 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567' (base32 alphabet)
*/
function looksLikeCharset(s: string): boolean {
// Must be reasonably long to bother checking
if (s.length < 16) return false;
// Unique character ratio: a charset reuses few characters relative to its length,
// but more importantly its unique chars are a large fraction of the total charset
// space (26 lc + 26 uc + 10 digits = 62). If >50% of the possible alphanumeric
// chars appear, it's almost certainly a charset definition.
const unique = new Set(s.replace(/[^A-Za-z0-9]/g, '')).size;
if (unique >= 61) return true; // covers a-z (26), A-Z (26), 0-9 (10), or combos
// Fallback: detect sequential runs of 6+ consecutive ASCII codes
// e.g. 'abcdef', 'ABCDEF', '012345'
const sequentialRunThreshold = 6;
let maxRun = 1;
let currentRun = 1;
for (let i = 1; i < s.length; i++) {
if (s.charCodeAt(i) === s.charCodeAt(i - 1)! + 1) {
currentRun++;
if (currentRun > maxRun) maxRun = currentRun;
} else {
currentRun = 1;
}
}
return maxRun >= sequentialRunThreshold;
}
/**
* Checks if a string looks like a harmless literal.
* @param s - The string to check.
* @returns True if the string looks harmless, false otherwise.
*/
function looksHarmlessLiteral(s: string): boolean {
return (
/\S+@\S+/.test(s) || // emails
/^data:[a-z]+\/[a-z0-9.+-]+;base64,/i.test(s) || // data URIs
/^\.{0,2}\//.test(s) || // relative paths
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(s) || // UUID
/^[0-9a-f]{32,128}$/i.test(s) || // MD5, SHA1, SHA256, etc.
/^[A-Za-z0-9+/_\-]{16,20}={0,2}$/.test(s) || // short base64
/^[A-Za-z0-9+/_\-]*(_PUBLIC|_PRIVATE|VITE_|NEXT_PUBLIC|VUE_)[A-Za-z0-9+/_\-]*={0,2}$/.test(
s,
) || // env-like keys
/^[MmZzLlHhVvCcSsQqTtAa][0-9eE+.\- ,MmZzLlHhVvCcSsQqTtAa]*$/.test(s) || // SVG path data
/<svg[\s\S]*?>[\s\S]*?<\/svg>/i.test(s) || // SVG markup
HARMLESS_URLS.some((rx) => rx.test(s)) || // Allowlisted URLs
looksLikeCharset(s) // character sets / alphabets used for ID generation
);
}
/**
* Checks if a line looks like a URL construction pattern.
* @param line - The line to check.
* @returns True if the line looks like URL construction, false otherwise.
*/
function looksLikeUrlConstruction(line: string): boolean {
// Check for template literals or string concatenation that looks like URLs
return (
// Template literals with URL-like patterns
/=\s*`[^`]*\$\{[^}]+\}[^`]*\/[^`]*`/.test(line) ||
// String concatenation with slashes
/=\s*["'][^"']*\/[^"']*["']\s*\+/.test(line) ||
// Contains common URL patterns
/=\s*["'`][^"'`]*\/[^"'`]*(auth|api|login|redirect|callback|protocol)[^"'`]*\/[^"'`]*["'`]/.test(
line,
) ||
// Keycloak-specific patterns
/realms\/.*\/protocol\/openid-connect/.test(line)
);
}
/**
* Checks if a file path is probably a test path.
* This is determined by looking for common test folder names and file extensions.
* @param p - The file path to check.
* @returns True if the file path is probably a test path, false otherwise.
*/
function isProbablyTestPath(p: string): boolean {
return (
/\b(__tests__|__mocks__|fixtures|sandbox|samples)\b/i.test(p) ||
/\.(spec|test)\.[jt]sx?$/.test(p)
);
}
/**
* Checks if a string is a pure interpolation template.
* @param s - The string to check.
* @returns True if the string is a pure interpolation template, false otherwise.
*/
function isPureInterpolationTemplate(s: string): boolean {
// Matches templates like `${a}`, `${a}:${b}`, `${a}|${b}|${c}`
// i.e. no meaningful static content
const withoutInterpolations = s.replace(/\$\{[^}]+\}/g, '');
return /^[\s:|,._-]*$/.test(withoutInterpolations);
}
// Threshold is the value between 0 and 1 that determines the sensitivity of the detection.
const DEFAULT_SECRET_THRESHOLD = 0.85 as const;
/**
* Optimized for sveltekit and vite env accessors
* @param line - A line of code to check.
* @returns True if the line is an environment variable accessor, false otherwise.
*/
function isEnvAccessor(line: string): boolean {
return /\b(process\.env|import\.meta\.env|\$env\/(static|dynamic)\/(public|private))\b/.test(
line,
);
}
/**
* Detects secrets in the source code of a file.
* @param file - The file path to check.
* @param source - The source code to scan for secrets.
* @returns An array of secret findings.
*/
export function detectSecretsInSource(
file: string,
source: string,
opts?: { ignoreUrls?: string[] },
): SecretFinding[] {
const threshold = isProbablyTestPath(file) ? 0.95 : DEFAULT_SECRET_THRESHOLD;
const findings: SecretFinding[] = [];
const lines = source.split(/\r?\n/);
let insideIgnoreBlock = false;
for (let i = 0; i < lines.length; i++) {
const lineNo = i + 1;
const line = lines[i] || '';
if (/<!--\s*dotenv[\s-]*diff[\s-]*ignore[\s-]*start\s*-->/i.test(line)) {
insideIgnoreBlock = true;
continue;
}
if (/<!--\s*dotenv[\s-]*diff[\s-]*ignore[\s-]*end\s*-->/i.test(line)) {
insideIgnoreBlock = false;
continue;
}
// Skip if inside ignore block
if (insideIgnoreBlock) continue;
// Skip comments
if (/^\s*\/\//.test(line)) continue;
// Check if line has ignore comment
if (hasIgnoreComment(line)) continue;
// Ignore likely minified / bundled lines before any secret detection
if (isLikelyMinified(line)) continue;
// Check for HTTPS URLs
HTTPS_PATTERN.lastIndex = 0;
let httpsMatch: RegExpExecArray | null;
while ((httpsMatch = HTTPS_PATTERN.exec(line))) {
const url = httpsMatch[1];
if (url && !looksHarmlessLiteral(url)) {
if (ignoreUrlsMatch(url, opts?.ignoreUrls)) continue;
findings.push({
file,
line: lineNo,
kind: 'pattern',
message:
'HTTPS URL detected – consider moving to an environment variable',
snippet: line.trim().slice(0, 180),
severity: 'low',
});
}
}
// 1) Suspicious key literal assignments
if (SUSPICIOUS_KEYS.test(line)) {
// Ignore known harmless UI / analytics attributes
if (HARMLESS_ATTRIBUTE_KEYS.test(line)) continue;
// Ignore HTML text nodes
if (isHtmlTextNode(line)) continue;
// Ignore if inside HTML tag content
if (/<[^>]*>.*<\/[^>]*>/.test(line.trim())) continue;
const m = line.match(/=\s*["'`](.+?)["'`]/);
if (
m &&
m[1] &&
!looksHarmlessLiteral(m[1]) &&
!looksLikeUrlConstruction(line) &&
m[1].length >= 12 &&
!isEnvAccessor(line) &&
!isPureInterpolationTemplate(m[1])
) {
findings.push({
file,
line: lineNo,
kind: 'pattern',
message: 'matches password/secret/token-like literal assignment',
snippet: line.trim().slice(0, 180),
severity: 'medium',
});
}
}
// 2) Provider patterns
for (const rx of PROVIDER_PATTERNS) {
if (rx.test(line)) {
findings.push({
file,
line: lineNo,
kind: 'pattern',
message: 'matches known provider key pattern',
snippet: line.trim().slice(0, 180),
severity: 'high',
});
}
}
// 3) High-entropy long literals
LONG_LITERAL.lastIndex = 0;
let lm: RegExpExecArray | null;
while ((lm = LONG_LITERAL.exec(line))) {
const literal = lm[1]!;
if (looksHarmlessLiteral(literal)) continue;
if (literal.length < 32) continue;
const ent = shannonEntropyNormalized(literal);
if (ent >= threshold) {
const message = `found high-entropy string (len ${literal.length}, H≈${ent.toFixed(2)})`;
findings.push({
file,
line: lineNo,
kind: 'entropy',
message,
snippet: line.trim().slice(0, 180),
severity: determineEntropySeverity(literal.length),
});
}
}
}
const severityRank: Record<SecretSeverity, number> = {
low: 0,
medium: 1,
high: 2,
};
const deduped = new Map<string, SecretFinding>();
for (const finding of findings) {
const key = `${finding.file}|${finding.line}|${finding.snippet}|${finding.kind}`;
const existing = deduped.get(key);
if (!existing) {
deduped.set(key, finding);
continue;
}
if (severityRank[finding.severity] > severityRank[existing.severity]) {
deduped.set(key, finding);
}
}
return Array.from(deduped.values());
}