@subramanyaks/env-drift 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/README.md +29 -8
- package/dist/cli.js +529 -63
- package/dist/index.cjs +390 -36
- package/dist/index.d.cts +55 -8
- package/dist/index.d.ts +55 -8
- package/dist/index.js +384 -33
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -36,11 +36,14 @@ __export(src_exports, {
|
|
|
36
36
|
PUBLIC_ENV_PREFIXES: () => PUBLIC_ENV_PREFIXES,
|
|
37
37
|
SENSITIVE_KEYWORDS: () => SENSITIVE_KEYWORDS,
|
|
38
38
|
auditEnv: () => auditEnv,
|
|
39
|
+
detectExampleSecretLeak: () => detectExampleSecretLeak,
|
|
39
40
|
detectSecretExposure: () => detectSecretExposure,
|
|
40
41
|
extractEnvReferences: () => extractEnvReferences,
|
|
41
42
|
formatAuditReport: () => formatAuditReport,
|
|
43
|
+
formatGitHubAnnotations: () => formatGitHubAnnotations,
|
|
42
44
|
generateEnvTypes: () => generateEnvTypes,
|
|
43
45
|
hasBlockingIssues: () => hasBlockingIssues,
|
|
46
|
+
loadConfig: () => loadConfig,
|
|
44
47
|
parseEnvContent: () => parseEnvContent,
|
|
45
48
|
parseEnvFile: () => parseEnvFile,
|
|
46
49
|
scanFiles: () => scanFiles,
|
|
@@ -59,8 +62,27 @@ function parseValue(raw) {
|
|
|
59
62
|
const trimmed = raw.trim();
|
|
60
63
|
if (!trimmed) return "";
|
|
61
64
|
const quote = trimmed[0];
|
|
62
|
-
if (quote === '"'
|
|
63
|
-
|
|
65
|
+
if (quote === '"') {
|
|
66
|
+
let result = "";
|
|
67
|
+
for (let i = 1; i < trimmed.length; i++) {
|
|
68
|
+
const ch = trimmed[i];
|
|
69
|
+
if (ch === "\\" && i + 1 < trimmed.length) {
|
|
70
|
+
const next = trimmed[i + 1];
|
|
71
|
+
if (next === "n") result += "\n";
|
|
72
|
+
else if (next === "r") result += "\r";
|
|
73
|
+
else if (next === "t") result += " ";
|
|
74
|
+
else result += next;
|
|
75
|
+
i++;
|
|
76
|
+
} else if (ch === '"') {
|
|
77
|
+
return result;
|
|
78
|
+
} else {
|
|
79
|
+
result += ch;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
if (quote === "'") {
|
|
85
|
+
const closingIdx = trimmed.indexOf("'", 1);
|
|
64
86
|
if (closingIdx !== -1) {
|
|
65
87
|
return trimmed.slice(1, closingIdx);
|
|
66
88
|
}
|
|
@@ -77,8 +99,10 @@ function parseEnvContent(content) {
|
|
|
77
99
|
const seen = /* @__PURE__ */ new Set();
|
|
78
100
|
const duplicateSet = /* @__PURE__ */ new Set();
|
|
79
101
|
const values = {};
|
|
102
|
+
const keyLines = {};
|
|
80
103
|
const lines = content.split(/\r?\n/);
|
|
81
|
-
for (
|
|
104
|
+
for (let i = 0; i < lines.length; i++) {
|
|
105
|
+
const rawLine = lines[i];
|
|
82
106
|
const line = rawLine.trim();
|
|
83
107
|
if (!line || line.startsWith("#")) continue;
|
|
84
108
|
const match = ENV_LINE_REGEX.exec(line);
|
|
@@ -93,10 +117,12 @@ function parseEnvContent(content) {
|
|
|
93
117
|
keys.push(key);
|
|
94
118
|
}
|
|
95
119
|
values[key] = parseValue(rawValue);
|
|
120
|
+
keyLines[key] = i + 1;
|
|
96
121
|
}
|
|
97
122
|
return {
|
|
98
123
|
keys,
|
|
99
124
|
values,
|
|
125
|
+
lines: keyLines,
|
|
100
126
|
duplicates: Array.from(duplicateSet)
|
|
101
127
|
};
|
|
102
128
|
}
|
|
@@ -107,16 +133,18 @@ function parseEnvFile(filePath) {
|
|
|
107
133
|
exists: false,
|
|
108
134
|
keys: [],
|
|
109
135
|
values: {},
|
|
136
|
+
lines: {},
|
|
110
137
|
duplicates: []
|
|
111
138
|
};
|
|
112
139
|
}
|
|
113
140
|
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8");
|
|
114
|
-
const { keys, values, duplicates } = parseEnvContent(content);
|
|
141
|
+
const { keys, values, lines, duplicates } = parseEnvContent(content);
|
|
115
142
|
return {
|
|
116
143
|
filePath,
|
|
117
144
|
exists: true,
|
|
118
145
|
keys,
|
|
119
146
|
values,
|
|
147
|
+
lines,
|
|
120
148
|
duplicates
|
|
121
149
|
};
|
|
122
150
|
}
|
|
@@ -188,35 +216,48 @@ function getLineNumber(source, index) {
|
|
|
188
216
|
}
|
|
189
217
|
return line;
|
|
190
218
|
}
|
|
219
|
+
function collectIgnoredLines(source) {
|
|
220
|
+
const ignored = /* @__PURE__ */ new Set();
|
|
221
|
+
const lines = source.split(/\r?\n/);
|
|
222
|
+
for (let i = 0; i < lines.length; i++) {
|
|
223
|
+
const line = lines[i];
|
|
224
|
+
if (/(?:\/\/|\/\*)\s*env-drift-ignore-next-line\b/.test(line)) {
|
|
225
|
+
ignored.add(i + 2);
|
|
226
|
+
} else if (/(?:\/\/|\/\*)\s*env-drift-ignore\b/.test(line)) {
|
|
227
|
+
ignored.add(i + 1);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return ignored;
|
|
231
|
+
}
|
|
191
232
|
function extractEnvReferences(content, filePath) {
|
|
233
|
+
const ignoredLines = collectIgnoredLines(content);
|
|
192
234
|
const cleaned = stripCommentsPreservingLines(content);
|
|
193
235
|
const refs = [];
|
|
194
236
|
const normalizedFile = filePath.replace(/\\/g, "/");
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
const source = match[1];
|
|
198
|
-
const name = match[2];
|
|
199
|
-
if (!name || match.index === void 0) continue;
|
|
237
|
+
const addRef = (name, line, source) => {
|
|
238
|
+
if (ignoredLines.has(line)) return;
|
|
200
239
|
refs.push({
|
|
201
240
|
name,
|
|
202
241
|
file: normalizedFile,
|
|
203
|
-
line
|
|
242
|
+
line,
|
|
204
243
|
source
|
|
205
244
|
});
|
|
245
|
+
};
|
|
246
|
+
const dotRegex = /\b(process\.env|import\.meta\.env|Bun\.env)\s*\??\.\s*([A-Za-z_][A-Za-z0-9_]*)\b/g;
|
|
247
|
+
for (const match of cleaned.matchAll(dotRegex)) {
|
|
248
|
+
const source = match[1];
|
|
249
|
+
const name = match[2];
|
|
250
|
+
if (!name || match.index === void 0) continue;
|
|
251
|
+
addRef(name, getLineNumber(cleaned, match.index), source);
|
|
206
252
|
}
|
|
207
|
-
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
253
|
+
const bracketRegex = /\b(process\.env|import\.meta\.env|Bun\.env)\s*(?:\?\.)?\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
208
254
|
for (const match of cleaned.matchAll(bracketRegex)) {
|
|
209
255
|
const source = match[1];
|
|
210
256
|
const name = match[2];
|
|
211
257
|
if (!name || match.index === void 0) continue;
|
|
212
|
-
|
|
213
|
-
name,
|
|
214
|
-
file: normalizedFile,
|
|
215
|
-
line: getLineNumber(cleaned, match.index),
|
|
216
|
-
source
|
|
217
|
-
});
|
|
258
|
+
addRef(name, getLineNumber(cleaned, match.index), source);
|
|
218
259
|
}
|
|
219
|
-
const destructureRegex = /\{([^}]+)\}\s*=\s*(process\.env|import\.meta\.env)\b/g;
|
|
260
|
+
const destructureRegex = /\{([^}]+)\}\s*=\s*(process\.env|import\.meta\.env|Bun\.env)\b/g;
|
|
220
261
|
for (const match of cleaned.matchAll(destructureRegex)) {
|
|
221
262
|
const body = match[1];
|
|
222
263
|
const source = match[2];
|
|
@@ -233,14 +274,15 @@ function extractEnvReferences(content, filePath) {
|
|
|
233
274
|
if (!name) continue;
|
|
234
275
|
const leadingWs = part.indexOf(name);
|
|
235
276
|
const absoluteIdx = bodyStart + partIdx + Math.max(0, leadingWs);
|
|
236
|
-
|
|
237
|
-
name,
|
|
238
|
-
file: normalizedFile,
|
|
239
|
-
line: getLineNumber(cleaned, absoluteIdx),
|
|
240
|
-
source
|
|
241
|
-
});
|
|
277
|
+
addRef(name, getLineNumber(cleaned, absoluteIdx), source);
|
|
242
278
|
}
|
|
243
279
|
}
|
|
280
|
+
const denoRegex = /\bDeno\.env\s*\??\.\s*get\s*\(\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\)/g;
|
|
281
|
+
for (const match of cleaned.matchAll(denoRegex)) {
|
|
282
|
+
const name = match[1];
|
|
283
|
+
if (!name || match.index === void 0) continue;
|
|
284
|
+
addRef(name, getLineNumber(cleaned, match.index), "Deno.env");
|
|
285
|
+
}
|
|
244
286
|
refs.sort((a, b) => a.line - b.line);
|
|
245
287
|
return refs;
|
|
246
288
|
}
|
|
@@ -308,12 +350,87 @@ var SENSITIVE_KEYWORDS = [
|
|
|
308
350
|
"SERVICE_ROLE",
|
|
309
351
|
"CREDENTIALS"
|
|
310
352
|
];
|
|
311
|
-
|
|
353
|
+
var PLACEHOLDER_VALUE_PATTERNS = [
|
|
354
|
+
/^your[_-]/i,
|
|
355
|
+
/^my[_-]/i,
|
|
356
|
+
/^change[_-]?me$/i,
|
|
357
|
+
/^replace[_-]?me$/i,
|
|
358
|
+
/^placeholder$/i,
|
|
359
|
+
/^example$/i,
|
|
360
|
+
/^dummy$/i,
|
|
361
|
+
/^test$/i,
|
|
362
|
+
/^todo$/i,
|
|
363
|
+
/^<[^>]+>$/,
|
|
364
|
+
/^\$\{[^}]+\}$/,
|
|
365
|
+
/^[xX*]{4,}$/
|
|
366
|
+
];
|
|
367
|
+
var PLACEHOLDER_URL_PASSWORDS = /* @__PURE__ */ new Set([
|
|
368
|
+
"password",
|
|
369
|
+
"pass",
|
|
370
|
+
"passwd",
|
|
371
|
+
"postgres",
|
|
372
|
+
"root",
|
|
373
|
+
"admin",
|
|
374
|
+
"changeme",
|
|
375
|
+
"change_me",
|
|
376
|
+
"secret",
|
|
377
|
+
"example",
|
|
378
|
+
"dummy",
|
|
379
|
+
"test",
|
|
380
|
+
"1234",
|
|
381
|
+
"123456",
|
|
382
|
+
"your_password",
|
|
383
|
+
"<password>"
|
|
384
|
+
]);
|
|
385
|
+
var EXAMPLE_SECRET_PATTERNS = [
|
|
386
|
+
{ pattern: /^sk_live_[A-Za-z0-9]{16,}$/, reason: "Stripe live secret key" },
|
|
387
|
+
{ pattern: /^rk_live_[A-Za-z0-9]{16,}$/, reason: "Stripe restricted live key" },
|
|
388
|
+
{ pattern: /^whsec_[A-Za-z0-9]{16,}$/, reason: "Stripe webhook secret" },
|
|
389
|
+
{ pattern: /^gh[pous]_[A-Za-z0-9]{20,}$/, reason: "GitHub access token" },
|
|
390
|
+
{ pattern: /^github_pat_[A-Za-z0-9_]{20,}$/, reason: "GitHub fine-grained token" },
|
|
391
|
+
{ pattern: /^AKIA[0-9A-Z]{16}$/, reason: "AWS Access Key ID" },
|
|
392
|
+
{ pattern: /^sk-(?:proj|ant)-[A-Za-z0-9_-]{20,}$/, reason: "AI provider API key" },
|
|
393
|
+
{ pattern: /^sk-[A-Za-z0-9]{32,}$/, reason: "Secret API key (sk-...)" },
|
|
394
|
+
{ pattern: /^xox[baprs]-[A-Za-z0-9-]{10,}$/, reason: "Slack token" },
|
|
395
|
+
{ pattern: /^AIza[0-9A-Za-z_-]{35}$/, reason: "Google API key" },
|
|
396
|
+
{
|
|
397
|
+
pattern: /^eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}$/,
|
|
398
|
+
reason: "JWT token"
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/,
|
|
402
|
+
reason: "PEM private key"
|
|
403
|
+
}
|
|
404
|
+
];
|
|
405
|
+
var CONNECTION_URL_PASSWORD_REGEX = /^(?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|rediss|amqp|amqps):\/\/[^:\s/]+:([^@\s/]+)@/i;
|
|
406
|
+
function detectExampleSecretLeak(_name, value) {
|
|
407
|
+
const trimmed = value.trim();
|
|
408
|
+
if (!trimmed) return null;
|
|
409
|
+
if (PLACEHOLDER_VALUE_PATTERNS.some((re) => re.test(trimmed))) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
for (const { pattern, reason } of EXAMPLE_SECRET_PATTERNS) {
|
|
413
|
+
if (pattern.test(trimmed)) {
|
|
414
|
+
return reason;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const urlMatch = CONNECTION_URL_PASSWORD_REGEX.exec(trimmed);
|
|
418
|
+
if (urlMatch) {
|
|
419
|
+
const password = urlMatch[1] ?? "";
|
|
420
|
+
if (password && !PLACEHOLDER_URL_PASSWORDS.has(password.toLowerCase()) && !PLACEHOLDER_VALUE_PATTERNS.some((re) => re.test(password))) {
|
|
421
|
+
return "Connection URL with embedded password";
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
function detectSecretExposure(name, options) {
|
|
312
427
|
const upper = name.toUpperCase();
|
|
313
|
-
const
|
|
428
|
+
const prefixes = options?.publicPrefixes?.length ? [...PUBLIC_ENV_PREFIXES, ...options.publicPrefixes.map((p) => p.toUpperCase())] : PUBLIC_ENV_PREFIXES;
|
|
429
|
+
const keywords = options?.sensitiveKeywords?.length ? [...SENSITIVE_KEYWORDS, ...options.sensitiveKeywords.map((k) => k.toUpperCase())] : SENSITIVE_KEYWORDS;
|
|
430
|
+
const prefix = prefixes.find((p) => upper.startsWith(p));
|
|
314
431
|
if (!prefix) return null;
|
|
315
432
|
const rest = upper.slice(prefix.length);
|
|
316
|
-
const matchedKeyword =
|
|
433
|
+
const matchedKeyword = keywords.find((kw) => rest.includes(kw));
|
|
317
434
|
if (!matchedKeyword) return null;
|
|
318
435
|
return { prefix, matchedKeyword };
|
|
319
436
|
}
|
|
@@ -322,7 +439,8 @@ function auditEnv(options = {}) {
|
|
|
322
439
|
const envFileName = options.envFile ?? ".env";
|
|
323
440
|
const exampleFileName = options.exampleFile ?? ".env.example";
|
|
324
441
|
const ignoreSet = /* @__PURE__ */ new Set([...BUILTIN_ENV_VARS, ...options.ignore ?? []]);
|
|
325
|
-
const
|
|
442
|
+
const ignoreDirs = options.ignoreDirs?.length ? [...DEFAULT_IGNORE_DIRS, ...options.ignoreDirs] : void 0;
|
|
443
|
+
const { scannedFiles, references } = scanFiles(rootDir, { ignoreDirs });
|
|
326
444
|
const usedInCode = {};
|
|
327
445
|
for (const ref of references) {
|
|
328
446
|
if (!usedInCode[ref.name]) {
|
|
@@ -332,11 +450,17 @@ function auditEnv(options = {}) {
|
|
|
332
450
|
}
|
|
333
451
|
const parsedEnv = parseEnvFile((0, import_node_path2.resolve)(rootDir, envFileName));
|
|
334
452
|
const parsedExample = parseEnvFile((0, import_node_path2.resolve)(rootDir, exampleFileName));
|
|
453
|
+
let envFileExists = parsedEnv.exists;
|
|
335
454
|
const localEnvKeys = new Set(parsedEnv.keys);
|
|
455
|
+
const localEnvValues = { ...parsedEnv.values };
|
|
336
456
|
if (!options.envFile) {
|
|
337
457
|
const parsedLocal = parseEnvFile((0, import_node_path2.resolve)(rootDir, ".env.local"));
|
|
458
|
+
if (parsedLocal.exists) {
|
|
459
|
+
envFileExists = true;
|
|
460
|
+
}
|
|
338
461
|
for (const key of parsedLocal.keys) {
|
|
339
462
|
localEnvKeys.add(key);
|
|
463
|
+
localEnvValues[key] = parsedLocal.values[key] ?? "";
|
|
340
464
|
}
|
|
341
465
|
}
|
|
342
466
|
const exampleKeys = new Set(parsedExample.keys);
|
|
@@ -344,6 +468,9 @@ function auditEnv(options = {}) {
|
|
|
344
468
|
const codeKeySet = new Set(codeKeys);
|
|
345
469
|
const missingInEnv = codeKeys.filter((k) => !ignoreSet.has(k) && !localEnvKeys.has(k)).sort();
|
|
346
470
|
const missingInExample = codeKeys.filter((k) => !ignoreSet.has(k) && !exampleKeys.has(k)).sort();
|
|
471
|
+
const emptyInEnv = codeKeys.filter(
|
|
472
|
+
(k) => !ignoreSet.has(k) && localEnvKeys.has(k) && (localEnvValues[k] ?? "").trim() === ""
|
|
473
|
+
).sort();
|
|
347
474
|
const unusedInEnv = parsedEnv.keys.filter((k) => !ignoreSet.has(k) && !codeKeySet.has(k)).sort();
|
|
348
475
|
const unusedInExample = parsedExample.keys.filter((k) => !ignoreSet.has(k) && !codeKeySet.has(k)).sort();
|
|
349
476
|
const allKnownKeys = /* @__PURE__ */ new Set([
|
|
@@ -354,7 +481,10 @@ function auditEnv(options = {}) {
|
|
|
354
481
|
const secretExposures = [];
|
|
355
482
|
for (const key of Array.from(allKnownKeys).sort()) {
|
|
356
483
|
if (ignoreSet.has(key)) continue;
|
|
357
|
-
const hit = detectSecretExposure(key
|
|
484
|
+
const hit = detectSecretExposure(key, {
|
|
485
|
+
publicPrefixes: options.publicPrefixes,
|
|
486
|
+
sensitiveKeywords: options.sensitiveKeywords
|
|
487
|
+
});
|
|
358
488
|
if (hit) {
|
|
359
489
|
secretExposures.push({
|
|
360
490
|
name: key,
|
|
@@ -364,15 +494,32 @@ function auditEnv(options = {}) {
|
|
|
364
494
|
});
|
|
365
495
|
}
|
|
366
496
|
}
|
|
497
|
+
const exampleSecretLeaks = [];
|
|
498
|
+
for (const key of [...parsedExample.keys].sort()) {
|
|
499
|
+
if (ignoreSet.has(key)) continue;
|
|
500
|
+
const reason = detectExampleSecretLeak(key, parsedExample.values[key] ?? "");
|
|
501
|
+
if (reason) {
|
|
502
|
+
exampleSecretLeaks.push({
|
|
503
|
+
name: key,
|
|
504
|
+
reason,
|
|
505
|
+
file: exampleFileName,
|
|
506
|
+
line: parsedExample.lines[key] ?? 1
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
367
510
|
return {
|
|
368
511
|
rootDir,
|
|
369
512
|
scannedFiles,
|
|
513
|
+
envFileExists,
|
|
514
|
+
exampleFileExists: parsedExample.exists,
|
|
370
515
|
usedInCode,
|
|
371
516
|
missingInEnv,
|
|
372
517
|
missingInExample,
|
|
518
|
+
emptyInEnv,
|
|
373
519
|
unusedInEnv,
|
|
374
520
|
unusedInExample,
|
|
375
521
|
secretExposures,
|
|
522
|
+
exampleSecretLeaks,
|
|
376
523
|
duplicateKeys: {
|
|
377
524
|
env: parsedEnv.duplicates,
|
|
378
525
|
example: parsedExample.duplicates
|
|
@@ -380,9 +527,60 @@ function auditEnv(options = {}) {
|
|
|
380
527
|
};
|
|
381
528
|
}
|
|
382
529
|
|
|
383
|
-
// src/
|
|
530
|
+
// src/config.ts
|
|
384
531
|
var import_node_fs3 = require("fs");
|
|
385
532
|
var import_node_path3 = require("path");
|
|
533
|
+
function toStringArray(val) {
|
|
534
|
+
if (!Array.isArray(val)) return void 0;
|
|
535
|
+
return val.filter((item) => typeof item === "string" && item.trim().length > 0);
|
|
536
|
+
}
|
|
537
|
+
function normalizeConfig(raw) {
|
|
538
|
+
if (!raw || typeof raw !== "object") return {};
|
|
539
|
+
const obj = raw;
|
|
540
|
+
const config = {};
|
|
541
|
+
if (typeof obj.envFile === "string" && obj.envFile.trim()) {
|
|
542
|
+
config.envFile = obj.envFile.trim();
|
|
543
|
+
}
|
|
544
|
+
if (typeof obj.exampleFile === "string" && obj.exampleFile.trim()) {
|
|
545
|
+
config.exampleFile = obj.exampleFile.trim();
|
|
546
|
+
}
|
|
547
|
+
const ignore = toStringArray(obj.ignore);
|
|
548
|
+
if (ignore) config.ignore = ignore;
|
|
549
|
+
const ignoreDirs = toStringArray(obj.ignoreDirs);
|
|
550
|
+
if (ignoreDirs) config.ignoreDirs = ignoreDirs;
|
|
551
|
+
const publicPrefixes = toStringArray(obj.publicPrefixes);
|
|
552
|
+
if (publicPrefixes) config.publicPrefixes = publicPrefixes;
|
|
553
|
+
const sensitiveKeywords = toStringArray(obj.sensitiveKeywords);
|
|
554
|
+
if (sensitiveKeywords) config.sensitiveKeywords = sensitiveKeywords;
|
|
555
|
+
return config;
|
|
556
|
+
}
|
|
557
|
+
function loadConfig(rootDir) {
|
|
558
|
+
const rcPath = (0, import_node_path3.resolve)(rootDir, ".envdriftrc.json");
|
|
559
|
+
if ((0, import_node_fs3.existsSync)(rcPath)) {
|
|
560
|
+
try {
|
|
561
|
+
const parsed = JSON.parse((0, import_node_fs3.readFileSync)(rcPath, "utf-8"));
|
|
562
|
+
return normalizeConfig(parsed);
|
|
563
|
+
} catch (err) {
|
|
564
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
565
|
+
throw new Error(`Failed to parse .envdriftrc.json: ${msg}`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const pkgPath = (0, import_node_path3.resolve)(rootDir, "package.json");
|
|
569
|
+
if ((0, import_node_fs3.existsSync)(pkgPath)) {
|
|
570
|
+
try {
|
|
571
|
+
const parsed = JSON.parse((0, import_node_fs3.readFileSync)(pkgPath, "utf-8"));
|
|
572
|
+
if (parsed && typeof parsed === "object" && parsed["env-drift"]) {
|
|
573
|
+
return normalizeConfig(parsed["env-drift"]);
|
|
574
|
+
}
|
|
575
|
+
} catch {
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return {};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/typegen.ts
|
|
582
|
+
var import_node_fs4 = require("fs");
|
|
583
|
+
var import_node_path4 = require("path");
|
|
386
584
|
function generateEnvTypes(result) {
|
|
387
585
|
const processKeys = /* @__PURE__ */ new Set();
|
|
388
586
|
const importMetaKeys = /* @__PURE__ */ new Set();
|
|
@@ -437,8 +635,8 @@ function generateEnvTypes(result) {
|
|
|
437
635
|
}
|
|
438
636
|
function writeEnvTypes(outputPath, result) {
|
|
439
637
|
const content = generateEnvTypes(result);
|
|
440
|
-
(0,
|
|
441
|
-
(0,
|
|
638
|
+
(0, import_node_fs4.mkdirSync)((0, import_node_path4.dirname)(outputPath), { recursive: true });
|
|
639
|
+
(0, import_node_fs4.writeFileSync)(outputPath, content, "utf-8");
|
|
442
640
|
const uniqueKeys = /* @__PURE__ */ new Set([
|
|
443
641
|
...Object.keys(result.usedInCode).filter((k) => !BUILTIN_ENV_VARS.has(k)),
|
|
444
642
|
...result.unusedInExample.filter((k) => !BUILTIN_ENV_VARS.has(k))
|
|
@@ -454,12 +652,14 @@ function formatRefs(refs) {
|
|
|
454
652
|
const extra = refs.length > 2 ? ` (+${refs.length - 2} more)` : "";
|
|
455
653
|
return import_picocolors.default.dim(` \u2190 ${shown.join(", ")}${extra}`);
|
|
456
654
|
}
|
|
457
|
-
function hasBlockingIssues(result) {
|
|
458
|
-
|
|
655
|
+
function hasBlockingIssues(result, options = {}) {
|
|
656
|
+
const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
|
|
657
|
+
return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0 || result.exampleSecretLeaks.length > 0;
|
|
459
658
|
}
|
|
460
659
|
function formatAuditReport(result, options = {}) {
|
|
461
660
|
const envFile = options.envFile ?? ".env";
|
|
462
661
|
const exampleFile = options.exampleFile ?? ".env.example";
|
|
662
|
+
const skipMissingInEnv = Boolean(options.ci && !result.envFileExists);
|
|
463
663
|
const lines = [];
|
|
464
664
|
lines.push("");
|
|
465
665
|
lines.push(
|
|
@@ -468,9 +668,14 @@ function formatAuditReport(result, options = {}) {
|
|
|
468
668
|
)}`
|
|
469
669
|
);
|
|
470
670
|
lines.push("");
|
|
471
|
-
const totalIssues = result.secretExposures.length + result.missingInEnv.length + result.missingInExample.length + result.unusedInEnv.length + result.unusedInExample.length + result.duplicateKeys.env.length + result.duplicateKeys.example.length;
|
|
671
|
+
const totalIssues = result.secretExposures.length + result.exampleSecretLeaks.length + (skipMissingInEnv ? 0 : result.missingInEnv.length) + result.missingInExample.length + result.emptyInEnv.length + result.unusedInEnv.length + result.unusedInExample.length + result.duplicateKeys.env.length + result.duplicateKeys.example.length;
|
|
472
672
|
if (totalIssues === 0) {
|
|
473
673
|
lines.push(import_picocolors.default.green("\u2714 No environment drift detected. Everything is in sync!"));
|
|
674
|
+
if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
675
|
+
lines.push(
|
|
676
|
+
import_picocolors.default.dim(` (No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode)`)
|
|
677
|
+
);
|
|
678
|
+
}
|
|
474
679
|
lines.push("");
|
|
475
680
|
return lines.join("\n");
|
|
476
681
|
}
|
|
@@ -485,12 +690,30 @@ function formatAuditReport(result, options = {}) {
|
|
|
485
690
|
}
|
|
486
691
|
lines.push("");
|
|
487
692
|
}
|
|
488
|
-
if (result.
|
|
693
|
+
if (result.exampleSecretLeaks.length > 0) {
|
|
694
|
+
lines.push(
|
|
695
|
+
import_picocolors.default.bold(import_picocolors.default.red(`\u2716 Possible Real Secret in ${exampleFile} (${result.exampleSecretLeaks.length})`))
|
|
696
|
+
);
|
|
697
|
+
for (const item of result.exampleSecretLeaks) {
|
|
698
|
+
lines.push(
|
|
699
|
+
` ${import_picocolors.default.red("\u2022")} ${import_picocolors.default.bold(item.name)} ${import_picocolors.default.dim(
|
|
700
|
+
`(${item.reason}) \u2190 ${item.file}:${item.line}`
|
|
701
|
+
)}`
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
lines.push("");
|
|
705
|
+
}
|
|
706
|
+
if (!skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
489
707
|
lines.push(import_picocolors.default.bold(import_picocolors.default.red(`\u2716 Missing in ${envFile} (${result.missingInEnv.length})`)));
|
|
490
708
|
for (const key of result.missingInEnv) {
|
|
491
709
|
lines.push(` ${import_picocolors.default.red("\u2022")} ${key}${formatRefs(result.usedInCode[key])}`);
|
|
492
710
|
}
|
|
493
711
|
lines.push("");
|
|
712
|
+
} else if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
713
|
+
lines.push(
|
|
714
|
+
import_picocolors.default.dim(`\u2139 No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode`)
|
|
715
|
+
);
|
|
716
|
+
lines.push("");
|
|
494
717
|
}
|
|
495
718
|
if (result.missingInExample.length > 0) {
|
|
496
719
|
lines.push(import_picocolors.default.bold(import_picocolors.default.yellow(`\u26A0 Missing in ${exampleFile} (${result.missingInExample.length})`)));
|
|
@@ -499,6 +722,13 @@ function formatAuditReport(result, options = {}) {
|
|
|
499
722
|
}
|
|
500
723
|
lines.push("");
|
|
501
724
|
}
|
|
725
|
+
if (result.emptyInEnv.length > 0) {
|
|
726
|
+
lines.push(import_picocolors.default.bold(import_picocolors.default.yellow(`\u26A0 Empty Value in ${envFile} (${result.emptyInEnv.length})`)));
|
|
727
|
+
for (const key of result.emptyInEnv) {
|
|
728
|
+
lines.push(` ${import_picocolors.default.yellow("\u2022")} ${key}${formatRefs(result.usedInCode[key])}`);
|
|
729
|
+
}
|
|
730
|
+
lines.push("");
|
|
731
|
+
}
|
|
502
732
|
if (result.unusedInEnv.length > 0) {
|
|
503
733
|
lines.push(import_picocolors.default.bold(import_picocolors.default.cyan(`\u25CB Unused in ${envFile} (${result.unusedInEnv.length})`)));
|
|
504
734
|
for (const key of result.unusedInEnv) {
|
|
@@ -531,6 +761,127 @@ function formatAuditReport(result, options = {}) {
|
|
|
531
761
|
}
|
|
532
762
|
return lines.join("\n");
|
|
533
763
|
}
|
|
764
|
+
function escapeGitHubData(value) {
|
|
765
|
+
return value.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
|
|
766
|
+
}
|
|
767
|
+
function escapeGitHubProperty(value) {
|
|
768
|
+
return escapeGitHubData(value).replace(/:/g, "%3A").replace(/,/g, "%2C");
|
|
769
|
+
}
|
|
770
|
+
function buildAnnotation(level, title, message, file, line) {
|
|
771
|
+
const props = [];
|
|
772
|
+
if (file) props.push(`file=${escapeGitHubProperty(file)}`);
|
|
773
|
+
if (line !== void 0) props.push(`line=${line}`);
|
|
774
|
+
props.push(`title=${escapeGitHubProperty(title)}`);
|
|
775
|
+
return `::${level} ${props.join(",")}::${escapeGitHubData(message)}`;
|
|
776
|
+
}
|
|
777
|
+
function formatGitHubAnnotations(result, options = {}) {
|
|
778
|
+
const envFile = options.envFile ?? ".env";
|
|
779
|
+
const exampleFile = options.exampleFile ?? ".env.example";
|
|
780
|
+
const skipMissingInEnv = Boolean(options.ci && !result.envFileExists);
|
|
781
|
+
const lines = [];
|
|
782
|
+
for (const item of result.secretExposures) {
|
|
783
|
+
const firstRef = item.references[0];
|
|
784
|
+
lines.push(
|
|
785
|
+
buildAnnotation(
|
|
786
|
+
"error",
|
|
787
|
+
"Public Secret Exposure",
|
|
788
|
+
`${item.name} uses client-exposed prefix "${item.prefix}" with sensitive keyword "${item.matchedKeyword}"`,
|
|
789
|
+
firstRef?.file,
|
|
790
|
+
firstRef?.line
|
|
791
|
+
)
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
for (const leak of result.exampleSecretLeaks) {
|
|
795
|
+
lines.push(
|
|
796
|
+
buildAnnotation(
|
|
797
|
+
"error",
|
|
798
|
+
`Secret Leak in ${exampleFile}`,
|
|
799
|
+
`${leak.name} appears to contain a real secret (${leak.reason}). Replace it with an empty placeholder.`,
|
|
800
|
+
leak.file,
|
|
801
|
+
leak.line
|
|
802
|
+
)
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
if (!skipMissingInEnv) {
|
|
806
|
+
for (const key of result.missingInEnv) {
|
|
807
|
+
const firstRef = result.usedInCode[key]?.[0];
|
|
808
|
+
lines.push(
|
|
809
|
+
buildAnnotation(
|
|
810
|
+
"error",
|
|
811
|
+
`Missing in ${envFile}`,
|
|
812
|
+
`Environment variable ${key} is referenced in code but missing in ${envFile}`,
|
|
813
|
+
firstRef?.file,
|
|
814
|
+
firstRef?.line
|
|
815
|
+
)
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
for (const key of result.missingInExample) {
|
|
820
|
+
const firstRef = result.usedInCode[key]?.[0];
|
|
821
|
+
lines.push(
|
|
822
|
+
buildAnnotation(
|
|
823
|
+
"error",
|
|
824
|
+
`Missing in ${exampleFile}`,
|
|
825
|
+
`Environment variable ${key} is referenced in code but missing in ${exampleFile}. Run \`env-drift --sync\` to update.`,
|
|
826
|
+
firstRef?.file,
|
|
827
|
+
firstRef?.line
|
|
828
|
+
)
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
for (const key of result.emptyInEnv) {
|
|
832
|
+
const firstRef = result.usedInCode[key]?.[0];
|
|
833
|
+
lines.push(
|
|
834
|
+
buildAnnotation(
|
|
835
|
+
"warning",
|
|
836
|
+
`Empty Value in ${envFile}`,
|
|
837
|
+
`Environment variable ${key} is defined in ${envFile} with an empty value`,
|
|
838
|
+
firstRef?.file,
|
|
839
|
+
firstRef?.line
|
|
840
|
+
)
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
for (const key of result.unusedInEnv) {
|
|
844
|
+
lines.push(
|
|
845
|
+
buildAnnotation(
|
|
846
|
+
"warning",
|
|
847
|
+
`Unused in ${envFile}`,
|
|
848
|
+
`Environment variable ${key} is defined in ${envFile} but not referenced in code`,
|
|
849
|
+
envFile
|
|
850
|
+
)
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
for (const key of result.unusedInExample) {
|
|
854
|
+
lines.push(
|
|
855
|
+
buildAnnotation(
|
|
856
|
+
"warning",
|
|
857
|
+
`Unused in ${exampleFile}`,
|
|
858
|
+
`Environment variable ${key} is defined in ${exampleFile} but not referenced in code`,
|
|
859
|
+
exampleFile
|
|
860
|
+
)
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
for (const key of result.duplicateKeys.env) {
|
|
864
|
+
lines.push(
|
|
865
|
+
buildAnnotation(
|
|
866
|
+
"warning",
|
|
867
|
+
`Duplicate Key in ${envFile}`,
|
|
868
|
+
`Environment variable ${key} is defined multiple times in ${envFile}`,
|
|
869
|
+
envFile
|
|
870
|
+
)
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
for (const key of result.duplicateKeys.example) {
|
|
874
|
+
lines.push(
|
|
875
|
+
buildAnnotation(
|
|
876
|
+
"warning",
|
|
877
|
+
`Duplicate Key in ${exampleFile}`,
|
|
878
|
+
`Environment variable ${key} is defined multiple times in ${exampleFile}`,
|
|
879
|
+
exampleFile
|
|
880
|
+
)
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
return lines.join("\n");
|
|
884
|
+
}
|
|
534
885
|
// Annotate the CommonJS export names for ESM import in node:
|
|
535
886
|
0 && (module.exports = {
|
|
536
887
|
BUILTIN_ENV_VARS,
|
|
@@ -539,11 +890,14 @@ function formatAuditReport(result, options = {}) {
|
|
|
539
890
|
PUBLIC_ENV_PREFIXES,
|
|
540
891
|
SENSITIVE_KEYWORDS,
|
|
541
892
|
auditEnv,
|
|
893
|
+
detectExampleSecretLeak,
|
|
542
894
|
detectSecretExposure,
|
|
543
895
|
extractEnvReferences,
|
|
544
896
|
formatAuditReport,
|
|
897
|
+
formatGitHubAnnotations,
|
|
545
898
|
generateEnvTypes,
|
|
546
899
|
hasBlockingIssues,
|
|
900
|
+
loadConfig,
|
|
547
901
|
parseEnvContent,
|
|
548
902
|
parseEnvFile,
|
|
549
903
|
scanFiles,
|