@subramanyaks/env-drift 1.0.1 → 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 +394 -52
- package/dist/index.cjs +349 -32
- package/dist/index.d.cts +47 -7
- package/dist/index.d.ts +47 -7
- package/dist/index.js +343 -29
- 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,
|
|
@@ -96,8 +99,10 @@ function parseEnvContent(content) {
|
|
|
96
99
|
const seen = /* @__PURE__ */ new Set();
|
|
97
100
|
const duplicateSet = /* @__PURE__ */ new Set();
|
|
98
101
|
const values = {};
|
|
102
|
+
const keyLines = {};
|
|
99
103
|
const lines = content.split(/\r?\n/);
|
|
100
|
-
for (
|
|
104
|
+
for (let i = 0; i < lines.length; i++) {
|
|
105
|
+
const rawLine = lines[i];
|
|
101
106
|
const line = rawLine.trim();
|
|
102
107
|
if (!line || line.startsWith("#")) continue;
|
|
103
108
|
const match = ENV_LINE_REGEX.exec(line);
|
|
@@ -112,10 +117,12 @@ function parseEnvContent(content) {
|
|
|
112
117
|
keys.push(key);
|
|
113
118
|
}
|
|
114
119
|
values[key] = parseValue(rawValue);
|
|
120
|
+
keyLines[key] = i + 1;
|
|
115
121
|
}
|
|
116
122
|
return {
|
|
117
123
|
keys,
|
|
118
124
|
values,
|
|
125
|
+
lines: keyLines,
|
|
119
126
|
duplicates: Array.from(duplicateSet)
|
|
120
127
|
};
|
|
121
128
|
}
|
|
@@ -126,16 +133,18 @@ function parseEnvFile(filePath) {
|
|
|
126
133
|
exists: false,
|
|
127
134
|
keys: [],
|
|
128
135
|
values: {},
|
|
136
|
+
lines: {},
|
|
129
137
|
duplicates: []
|
|
130
138
|
};
|
|
131
139
|
}
|
|
132
140
|
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8");
|
|
133
|
-
const { keys, values, duplicates } = parseEnvContent(content);
|
|
141
|
+
const { keys, values, lines, duplicates } = parseEnvContent(content);
|
|
134
142
|
return {
|
|
135
143
|
filePath,
|
|
136
144
|
exists: true,
|
|
137
145
|
keys,
|
|
138
146
|
values,
|
|
147
|
+
lines,
|
|
139
148
|
duplicates
|
|
140
149
|
};
|
|
141
150
|
}
|
|
@@ -207,35 +216,48 @@ function getLineNumber(source, index) {
|
|
|
207
216
|
}
|
|
208
217
|
return line;
|
|
209
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
|
+
}
|
|
210
232
|
function extractEnvReferences(content, filePath) {
|
|
233
|
+
const ignoredLines = collectIgnoredLines(content);
|
|
211
234
|
const cleaned = stripCommentsPreservingLines(content);
|
|
212
235
|
const refs = [];
|
|
213
236
|
const normalizedFile = filePath.replace(/\\/g, "/");
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
const source = match[1];
|
|
217
|
-
const name = match[2];
|
|
218
|
-
if (!name || match.index === void 0) continue;
|
|
237
|
+
const addRef = (name, line, source) => {
|
|
238
|
+
if (ignoredLines.has(line)) return;
|
|
219
239
|
refs.push({
|
|
220
240
|
name,
|
|
221
241
|
file: normalizedFile,
|
|
222
|
-
line
|
|
242
|
+
line,
|
|
223
243
|
source
|
|
224
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);
|
|
225
252
|
}
|
|
226
|
-
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*(?:\?\.)?\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;
|
|
227
254
|
for (const match of cleaned.matchAll(bracketRegex)) {
|
|
228
255
|
const source = match[1];
|
|
229
256
|
const name = match[2];
|
|
230
257
|
if (!name || match.index === void 0) continue;
|
|
231
|
-
|
|
232
|
-
name,
|
|
233
|
-
file: normalizedFile,
|
|
234
|
-
line: getLineNumber(cleaned, match.index),
|
|
235
|
-
source
|
|
236
|
-
});
|
|
258
|
+
addRef(name, getLineNumber(cleaned, match.index), source);
|
|
237
259
|
}
|
|
238
|
-
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;
|
|
239
261
|
for (const match of cleaned.matchAll(destructureRegex)) {
|
|
240
262
|
const body = match[1];
|
|
241
263
|
const source = match[2];
|
|
@@ -252,14 +274,15 @@ function extractEnvReferences(content, filePath) {
|
|
|
252
274
|
if (!name) continue;
|
|
253
275
|
const leadingWs = part.indexOf(name);
|
|
254
276
|
const absoluteIdx = bodyStart + partIdx + Math.max(0, leadingWs);
|
|
255
|
-
|
|
256
|
-
name,
|
|
257
|
-
file: normalizedFile,
|
|
258
|
-
line: getLineNumber(cleaned, absoluteIdx),
|
|
259
|
-
source
|
|
260
|
-
});
|
|
277
|
+
addRef(name, getLineNumber(cleaned, absoluteIdx), source);
|
|
261
278
|
}
|
|
262
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
|
+
}
|
|
263
286
|
refs.sort((a, b) => a.line - b.line);
|
|
264
287
|
return refs;
|
|
265
288
|
}
|
|
@@ -327,12 +350,87 @@ var SENSITIVE_KEYWORDS = [
|
|
|
327
350
|
"SERVICE_ROLE",
|
|
328
351
|
"CREDENTIALS"
|
|
329
352
|
];
|
|
330
|
-
|
|
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) {
|
|
331
427
|
const upper = name.toUpperCase();
|
|
332
|
-
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));
|
|
333
431
|
if (!prefix) return null;
|
|
334
432
|
const rest = upper.slice(prefix.length);
|
|
335
|
-
const matchedKeyword =
|
|
433
|
+
const matchedKeyword = keywords.find((kw) => rest.includes(kw));
|
|
336
434
|
if (!matchedKeyword) return null;
|
|
337
435
|
return { prefix, matchedKeyword };
|
|
338
436
|
}
|
|
@@ -341,7 +439,8 @@ function auditEnv(options = {}) {
|
|
|
341
439
|
const envFileName = options.envFile ?? ".env";
|
|
342
440
|
const exampleFileName = options.exampleFile ?? ".env.example";
|
|
343
441
|
const ignoreSet = /* @__PURE__ */ new Set([...BUILTIN_ENV_VARS, ...options.ignore ?? []]);
|
|
344
|
-
const
|
|
442
|
+
const ignoreDirs = options.ignoreDirs?.length ? [...DEFAULT_IGNORE_DIRS, ...options.ignoreDirs] : void 0;
|
|
443
|
+
const { scannedFiles, references } = scanFiles(rootDir, { ignoreDirs });
|
|
345
444
|
const usedInCode = {};
|
|
346
445
|
for (const ref of references) {
|
|
347
446
|
if (!usedInCode[ref.name]) {
|
|
@@ -353,6 +452,7 @@ function auditEnv(options = {}) {
|
|
|
353
452
|
const parsedExample = parseEnvFile((0, import_node_path2.resolve)(rootDir, exampleFileName));
|
|
354
453
|
let envFileExists = parsedEnv.exists;
|
|
355
454
|
const localEnvKeys = new Set(parsedEnv.keys);
|
|
455
|
+
const localEnvValues = { ...parsedEnv.values };
|
|
356
456
|
if (!options.envFile) {
|
|
357
457
|
const parsedLocal = parseEnvFile((0, import_node_path2.resolve)(rootDir, ".env.local"));
|
|
358
458
|
if (parsedLocal.exists) {
|
|
@@ -360,6 +460,7 @@ function auditEnv(options = {}) {
|
|
|
360
460
|
}
|
|
361
461
|
for (const key of parsedLocal.keys) {
|
|
362
462
|
localEnvKeys.add(key);
|
|
463
|
+
localEnvValues[key] = parsedLocal.values[key] ?? "";
|
|
363
464
|
}
|
|
364
465
|
}
|
|
365
466
|
const exampleKeys = new Set(parsedExample.keys);
|
|
@@ -367,6 +468,9 @@ function auditEnv(options = {}) {
|
|
|
367
468
|
const codeKeySet = new Set(codeKeys);
|
|
368
469
|
const missingInEnv = codeKeys.filter((k) => !ignoreSet.has(k) && !localEnvKeys.has(k)).sort();
|
|
369
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();
|
|
370
474
|
const unusedInEnv = parsedEnv.keys.filter((k) => !ignoreSet.has(k) && !codeKeySet.has(k)).sort();
|
|
371
475
|
const unusedInExample = parsedExample.keys.filter((k) => !ignoreSet.has(k) && !codeKeySet.has(k)).sort();
|
|
372
476
|
const allKnownKeys = /* @__PURE__ */ new Set([
|
|
@@ -377,7 +481,10 @@ function auditEnv(options = {}) {
|
|
|
377
481
|
const secretExposures = [];
|
|
378
482
|
for (const key of Array.from(allKnownKeys).sort()) {
|
|
379
483
|
if (ignoreSet.has(key)) continue;
|
|
380
|
-
const hit = detectSecretExposure(key
|
|
484
|
+
const hit = detectSecretExposure(key, {
|
|
485
|
+
publicPrefixes: options.publicPrefixes,
|
|
486
|
+
sensitiveKeywords: options.sensitiveKeywords
|
|
487
|
+
});
|
|
381
488
|
if (hit) {
|
|
382
489
|
secretExposures.push({
|
|
383
490
|
name: key,
|
|
@@ -387,6 +494,19 @@ function auditEnv(options = {}) {
|
|
|
387
494
|
});
|
|
388
495
|
}
|
|
389
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
|
+
}
|
|
390
510
|
return {
|
|
391
511
|
rootDir,
|
|
392
512
|
scannedFiles,
|
|
@@ -395,9 +515,11 @@ function auditEnv(options = {}) {
|
|
|
395
515
|
usedInCode,
|
|
396
516
|
missingInEnv,
|
|
397
517
|
missingInExample,
|
|
518
|
+
emptyInEnv,
|
|
398
519
|
unusedInEnv,
|
|
399
520
|
unusedInExample,
|
|
400
521
|
secretExposures,
|
|
522
|
+
exampleSecretLeaks,
|
|
401
523
|
duplicateKeys: {
|
|
402
524
|
env: parsedEnv.duplicates,
|
|
403
525
|
example: parsedExample.duplicates
|
|
@@ -405,9 +527,60 @@ function auditEnv(options = {}) {
|
|
|
405
527
|
};
|
|
406
528
|
}
|
|
407
529
|
|
|
408
|
-
// src/
|
|
530
|
+
// src/config.ts
|
|
409
531
|
var import_node_fs3 = require("fs");
|
|
410
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");
|
|
411
584
|
function generateEnvTypes(result) {
|
|
412
585
|
const processKeys = /* @__PURE__ */ new Set();
|
|
413
586
|
const importMetaKeys = /* @__PURE__ */ new Set();
|
|
@@ -462,8 +635,8 @@ function generateEnvTypes(result) {
|
|
|
462
635
|
}
|
|
463
636
|
function writeEnvTypes(outputPath, result) {
|
|
464
637
|
const content = generateEnvTypes(result);
|
|
465
|
-
(0,
|
|
466
|
-
(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");
|
|
467
640
|
const uniqueKeys = /* @__PURE__ */ new Set([
|
|
468
641
|
...Object.keys(result.usedInCode).filter((k) => !BUILTIN_ENV_VARS.has(k)),
|
|
469
642
|
...result.unusedInExample.filter((k) => !BUILTIN_ENV_VARS.has(k))
|
|
@@ -481,7 +654,7 @@ function formatRefs(refs) {
|
|
|
481
654
|
}
|
|
482
655
|
function hasBlockingIssues(result, options = {}) {
|
|
483
656
|
const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
|
|
484
|
-
return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0;
|
|
657
|
+
return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0 || result.exampleSecretLeaks.length > 0;
|
|
485
658
|
}
|
|
486
659
|
function formatAuditReport(result, options = {}) {
|
|
487
660
|
const envFile = options.envFile ?? ".env";
|
|
@@ -495,7 +668,7 @@ function formatAuditReport(result, options = {}) {
|
|
|
495
668
|
)}`
|
|
496
669
|
);
|
|
497
670
|
lines.push("");
|
|
498
|
-
const totalIssues = result.secretExposures.length + (skipMissingInEnv ? 0 : 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;
|
|
499
672
|
if (totalIssues === 0) {
|
|
500
673
|
lines.push(import_picocolors.default.green("\u2714 No environment drift detected. Everything is in sync!"));
|
|
501
674
|
if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
@@ -517,6 +690,19 @@ function formatAuditReport(result, options = {}) {
|
|
|
517
690
|
}
|
|
518
691
|
lines.push("");
|
|
519
692
|
}
|
|
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
|
+
}
|
|
520
706
|
if (!skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
521
707
|
lines.push(import_picocolors.default.bold(import_picocolors.default.red(`\u2716 Missing in ${envFile} (${result.missingInEnv.length})`)));
|
|
522
708
|
for (const key of result.missingInEnv) {
|
|
@@ -536,6 +722,13 @@ function formatAuditReport(result, options = {}) {
|
|
|
536
722
|
}
|
|
537
723
|
lines.push("");
|
|
538
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
|
+
}
|
|
539
732
|
if (result.unusedInEnv.length > 0) {
|
|
540
733
|
lines.push(import_picocolors.default.bold(import_picocolors.default.cyan(`\u25CB Unused in ${envFile} (${result.unusedInEnv.length})`)));
|
|
541
734
|
for (const key of result.unusedInEnv) {
|
|
@@ -568,6 +761,127 @@ function formatAuditReport(result, options = {}) {
|
|
|
568
761
|
}
|
|
569
762
|
return lines.join("\n");
|
|
570
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
|
+
}
|
|
571
885
|
// Annotate the CommonJS export names for ESM import in node:
|
|
572
886
|
0 && (module.exports = {
|
|
573
887
|
BUILTIN_ENV_VARS,
|
|
@@ -576,11 +890,14 @@ function formatAuditReport(result, options = {}) {
|
|
|
576
890
|
PUBLIC_ENV_PREFIXES,
|
|
577
891
|
SENSITIVE_KEYWORDS,
|
|
578
892
|
auditEnv,
|
|
893
|
+
detectExampleSecretLeak,
|
|
579
894
|
detectSecretExposure,
|
|
580
895
|
extractEnvReferences,
|
|
581
896
|
formatAuditReport,
|
|
897
|
+
formatGitHubAnnotations,
|
|
582
898
|
generateEnvTypes,
|
|
583
899
|
hasBlockingIssues,
|
|
900
|
+
loadConfig,
|
|
584
901
|
parseEnvContent,
|
|
585
902
|
parseEnvFile,
|
|
586
903
|
scanFiles,
|