@subramanyaks/env-drift 1.0.0 → 1.0.1
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/dist/cli.js +140 -16
- package/dist/index.cjs +44 -7
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +44 -7
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,10 +1,83 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/cli.ts
|
|
4
|
+
import { realpathSync } from "fs";
|
|
4
5
|
import { resolve as resolve2 } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
5
7
|
import { parseArgs } from "util";
|
|
6
8
|
import pc2 from "picocolors";
|
|
7
9
|
|
|
10
|
+
// package.json
|
|
11
|
+
var package_default = {
|
|
12
|
+
name: "@subramanyaks/env-drift",
|
|
13
|
+
version: "1.0.1",
|
|
14
|
+
description: "Zero-config CLI and library to detect environment variable drift, missing keys, dead vars, and accidental public secret exposure.",
|
|
15
|
+
type: "module",
|
|
16
|
+
publishConfig: {
|
|
17
|
+
access: "public"
|
|
18
|
+
},
|
|
19
|
+
main: "./dist/index.cjs",
|
|
20
|
+
module: "./dist/index.js",
|
|
21
|
+
types: "./dist/index.d.ts",
|
|
22
|
+
bin: {
|
|
23
|
+
"env-drift": "./dist/cli.js"
|
|
24
|
+
},
|
|
25
|
+
exports: {
|
|
26
|
+
".": {
|
|
27
|
+
types: "./dist/index.d.ts",
|
|
28
|
+
import: "./dist/index.js",
|
|
29
|
+
require: "./dist/index.cjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
files: [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
engines: {
|
|
38
|
+
node: ">=18.17.0"
|
|
39
|
+
},
|
|
40
|
+
scripts: {
|
|
41
|
+
dev: "tsup --watch",
|
|
42
|
+
build: "tsup",
|
|
43
|
+
typecheck: "tsc --noEmit",
|
|
44
|
+
test: "vitest run",
|
|
45
|
+
"test:watch": "vitest",
|
|
46
|
+
prepublishOnly: "npm run typecheck && npm test && npm run build"
|
|
47
|
+
},
|
|
48
|
+
repository: {
|
|
49
|
+
type: "git",
|
|
50
|
+
url: "https://github.com/SubramanyaKS/env-drift.git"
|
|
51
|
+
},
|
|
52
|
+
bugs: {
|
|
53
|
+
url: "https://github.com/SubramanyaKS/env-drift/issues"
|
|
54
|
+
},
|
|
55
|
+
keywords: [
|
|
56
|
+
"env",
|
|
57
|
+
"dotenv",
|
|
58
|
+
"env-drift",
|
|
59
|
+
"environment",
|
|
60
|
+
"cli",
|
|
61
|
+
"security",
|
|
62
|
+
"audit",
|
|
63
|
+
"typescript",
|
|
64
|
+
"ci",
|
|
65
|
+
"nextjs",
|
|
66
|
+
"vite"
|
|
67
|
+
],
|
|
68
|
+
author: "SubramanyaKS",
|
|
69
|
+
license: "MIT",
|
|
70
|
+
dependencies: {
|
|
71
|
+
picocolors: "^1.1.1"
|
|
72
|
+
},
|
|
73
|
+
devDependencies: {
|
|
74
|
+
"@types/node": "^22.20.7",
|
|
75
|
+
tsup: "^8.5.1",
|
|
76
|
+
typescript: "^5.9.3",
|
|
77
|
+
vitest: "^4.0.18"
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
8
81
|
// src/analyzer.ts
|
|
9
82
|
import { resolve } from "path";
|
|
10
83
|
|
|
@@ -15,8 +88,27 @@ function parseValue(raw) {
|
|
|
15
88
|
const trimmed = raw.trim();
|
|
16
89
|
if (!trimmed) return "";
|
|
17
90
|
const quote = trimmed[0];
|
|
18
|
-
if (quote === '"'
|
|
19
|
-
|
|
91
|
+
if (quote === '"') {
|
|
92
|
+
let result = "";
|
|
93
|
+
for (let i = 1; i < trimmed.length; i++) {
|
|
94
|
+
const ch = trimmed[i];
|
|
95
|
+
if (ch === "\\" && i + 1 < trimmed.length) {
|
|
96
|
+
const next = trimmed[i + 1];
|
|
97
|
+
if (next === "n") result += "\n";
|
|
98
|
+
else if (next === "r") result += "\r";
|
|
99
|
+
else if (next === "t") result += " ";
|
|
100
|
+
else result += next;
|
|
101
|
+
i++;
|
|
102
|
+
} else if (ch === '"') {
|
|
103
|
+
return result;
|
|
104
|
+
} else {
|
|
105
|
+
result += ch;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
if (quote === "'") {
|
|
111
|
+
const closingIdx = trimmed.indexOf("'", 1);
|
|
20
112
|
if (closingIdx !== -1) {
|
|
21
113
|
return trimmed.slice(1, closingIdx);
|
|
22
114
|
}
|
|
@@ -160,7 +252,7 @@ function extractEnvReferences(content, filePath) {
|
|
|
160
252
|
source
|
|
161
253
|
});
|
|
162
254
|
}
|
|
163
|
-
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
255
|
+
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*(?:\?\.)?\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
164
256
|
for (const match of cleaned.matchAll(bracketRegex)) {
|
|
165
257
|
const source = match[1];
|
|
166
258
|
const name = match[2];
|
|
@@ -288,9 +380,13 @@ function auditEnv(options = {}) {
|
|
|
288
380
|
}
|
|
289
381
|
const parsedEnv = parseEnvFile(resolve(rootDir, envFileName));
|
|
290
382
|
const parsedExample = parseEnvFile(resolve(rootDir, exampleFileName));
|
|
383
|
+
let envFileExists = parsedEnv.exists;
|
|
291
384
|
const localEnvKeys = new Set(parsedEnv.keys);
|
|
292
385
|
if (!options.envFile) {
|
|
293
386
|
const parsedLocal = parseEnvFile(resolve(rootDir, ".env.local"));
|
|
387
|
+
if (parsedLocal.exists) {
|
|
388
|
+
envFileExists = true;
|
|
389
|
+
}
|
|
294
390
|
for (const key of parsedLocal.keys) {
|
|
295
391
|
localEnvKeys.add(key);
|
|
296
392
|
}
|
|
@@ -323,6 +419,8 @@ function auditEnv(options = {}) {
|
|
|
323
419
|
return {
|
|
324
420
|
rootDir,
|
|
325
421
|
scannedFiles,
|
|
422
|
+
envFileExists,
|
|
423
|
+
exampleFileExists: parsedExample.exists,
|
|
326
424
|
usedInCode,
|
|
327
425
|
missingInEnv,
|
|
328
426
|
missingInExample,
|
|
@@ -344,12 +442,14 @@ function formatRefs(refs) {
|
|
|
344
442
|
const extra = refs.length > 2 ? ` (+${refs.length - 2} more)` : "";
|
|
345
443
|
return pc.dim(` \u2190 ${shown.join(", ")}${extra}`);
|
|
346
444
|
}
|
|
347
|
-
function hasBlockingIssues(result) {
|
|
348
|
-
|
|
445
|
+
function hasBlockingIssues(result, options = {}) {
|
|
446
|
+
const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
|
|
447
|
+
return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0;
|
|
349
448
|
}
|
|
350
449
|
function formatAuditReport(result, options = {}) {
|
|
351
450
|
const envFile = options.envFile ?? ".env";
|
|
352
451
|
const exampleFile = options.exampleFile ?? ".env.example";
|
|
452
|
+
const skipMissingInEnv = Boolean(options.ci && !result.envFileExists);
|
|
353
453
|
const lines = [];
|
|
354
454
|
lines.push("");
|
|
355
455
|
lines.push(
|
|
@@ -358,9 +458,14 @@ function formatAuditReport(result, options = {}) {
|
|
|
358
458
|
)}`
|
|
359
459
|
);
|
|
360
460
|
lines.push("");
|
|
361
|
-
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;
|
|
461
|
+
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;
|
|
362
462
|
if (totalIssues === 0) {
|
|
363
463
|
lines.push(pc.green("\u2714 No environment drift detected. Everything is in sync!"));
|
|
464
|
+
if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
465
|
+
lines.push(
|
|
466
|
+
pc.dim(` (No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode)`)
|
|
467
|
+
);
|
|
468
|
+
}
|
|
364
469
|
lines.push("");
|
|
365
470
|
return lines.join("\n");
|
|
366
471
|
}
|
|
@@ -375,12 +480,17 @@ function formatAuditReport(result, options = {}) {
|
|
|
375
480
|
}
|
|
376
481
|
lines.push("");
|
|
377
482
|
}
|
|
378
|
-
if (result.missingInEnv.length > 0) {
|
|
483
|
+
if (!skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
379
484
|
lines.push(pc.bold(pc.red(`\u2716 Missing in ${envFile} (${result.missingInEnv.length})`)));
|
|
380
485
|
for (const key of result.missingInEnv) {
|
|
381
486
|
lines.push(` ${pc.red("\u2022")} ${key}${formatRefs(result.usedInCode[key])}`);
|
|
382
487
|
}
|
|
383
488
|
lines.push("");
|
|
489
|
+
} else if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
490
|
+
lines.push(
|
|
491
|
+
pc.dim(`\u2139 No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode`)
|
|
492
|
+
);
|
|
493
|
+
lines.push("");
|
|
384
494
|
}
|
|
385
495
|
if (result.missingInExample.length > 0) {
|
|
386
496
|
lines.push(pc.bold(pc.yellow(`\u26A0 Missing in ${exampleFile} (${result.missingInExample.length})`)));
|
|
@@ -489,7 +599,7 @@ function writeEnvTypes(outputPath, result) {
|
|
|
489
599
|
}
|
|
490
600
|
|
|
491
601
|
// src/cli.ts
|
|
492
|
-
var VERSION =
|
|
602
|
+
var VERSION = package_default.version;
|
|
493
603
|
var HELP_TEXT = `
|
|
494
604
|
${pc2.bold(pc2.cyan("@subramanyaks/env-drift"))} v${VERSION} \u2014 Detect environment variable drift and public secret leaks
|
|
495
605
|
|
|
@@ -557,11 +667,16 @@ function runCli(argv = process.argv.slice(2)) {
|
|
|
557
667
|
if (values.sync && result.missingInExample.length > 0) {
|
|
558
668
|
const examplePath = resolve2(rootDir, exampleFile);
|
|
559
669
|
const synced = syncEnvExample(examplePath, result.missingInExample);
|
|
560
|
-
if (
|
|
561
|
-
const
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
)
|
|
670
|
+
if (synced.added.length > 0) {
|
|
671
|
+
const addedSet = new Set(synced.added);
|
|
672
|
+
result.missingInExample = result.missingInExample.filter((k) => !addedSet.has(k));
|
|
673
|
+
result.exampleFileExists = true;
|
|
674
|
+
if (!values.json) {
|
|
675
|
+
const action = synced.created ? "Created" : "Updated";
|
|
676
|
+
console.log(
|
|
677
|
+
pc2.green(`\u2714 ${action} ${exampleFile} with ${synced.added.length} key(s): ${synced.added.join(", ")}`)
|
|
678
|
+
);
|
|
679
|
+
}
|
|
565
680
|
}
|
|
566
681
|
}
|
|
567
682
|
if (values.types || typeof values["types-out"] === "string") {
|
|
@@ -578,7 +693,8 @@ function runCli(argv = process.argv.slice(2)) {
|
|
|
578
693
|
console.log(
|
|
579
694
|
formatAuditReport(result, {
|
|
580
695
|
envFile: envFile ?? ".env",
|
|
581
|
-
exampleFile
|
|
696
|
+
exampleFile,
|
|
697
|
+
ci: Boolean(values.ci)
|
|
582
698
|
})
|
|
583
699
|
);
|
|
584
700
|
}
|
|
@@ -587,8 +703,16 @@ function runCli(argv = process.argv.slice(2)) {
|
|
|
587
703
|
}
|
|
588
704
|
return 0;
|
|
589
705
|
}
|
|
590
|
-
|
|
591
|
-
|
|
706
|
+
function isDirectExecution() {
|
|
707
|
+
const entry = process.argv[1];
|
|
708
|
+
if (!entry) return false;
|
|
709
|
+
try {
|
|
710
|
+
return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
|
|
711
|
+
} catch {
|
|
712
|
+
return entry.endsWith("cli.js") || entry.endsWith("env-drift") || entry.endsWith("env-drift-cli");
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
if (isDirectExecution()) {
|
|
592
716
|
process.exitCode = runCli();
|
|
593
717
|
}
|
|
594
718
|
export {
|
package/dist/index.cjs
CHANGED
|
@@ -59,8 +59,27 @@ function parseValue(raw) {
|
|
|
59
59
|
const trimmed = raw.trim();
|
|
60
60
|
if (!trimmed) return "";
|
|
61
61
|
const quote = trimmed[0];
|
|
62
|
-
if (quote === '"'
|
|
63
|
-
|
|
62
|
+
if (quote === '"') {
|
|
63
|
+
let result = "";
|
|
64
|
+
for (let i = 1; i < trimmed.length; i++) {
|
|
65
|
+
const ch = trimmed[i];
|
|
66
|
+
if (ch === "\\" && i + 1 < trimmed.length) {
|
|
67
|
+
const next = trimmed[i + 1];
|
|
68
|
+
if (next === "n") result += "\n";
|
|
69
|
+
else if (next === "r") result += "\r";
|
|
70
|
+
else if (next === "t") result += " ";
|
|
71
|
+
else result += next;
|
|
72
|
+
i++;
|
|
73
|
+
} else if (ch === '"') {
|
|
74
|
+
return result;
|
|
75
|
+
} else {
|
|
76
|
+
result += ch;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
if (quote === "'") {
|
|
82
|
+
const closingIdx = trimmed.indexOf("'", 1);
|
|
64
83
|
if (closingIdx !== -1) {
|
|
65
84
|
return trimmed.slice(1, closingIdx);
|
|
66
85
|
}
|
|
@@ -204,7 +223,7 @@ function extractEnvReferences(content, filePath) {
|
|
|
204
223
|
source
|
|
205
224
|
});
|
|
206
225
|
}
|
|
207
|
-
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
226
|
+
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*(?:\?\.)?\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
208
227
|
for (const match of cleaned.matchAll(bracketRegex)) {
|
|
209
228
|
const source = match[1];
|
|
210
229
|
const name = match[2];
|
|
@@ -332,9 +351,13 @@ function auditEnv(options = {}) {
|
|
|
332
351
|
}
|
|
333
352
|
const parsedEnv = parseEnvFile((0, import_node_path2.resolve)(rootDir, envFileName));
|
|
334
353
|
const parsedExample = parseEnvFile((0, import_node_path2.resolve)(rootDir, exampleFileName));
|
|
354
|
+
let envFileExists = parsedEnv.exists;
|
|
335
355
|
const localEnvKeys = new Set(parsedEnv.keys);
|
|
336
356
|
if (!options.envFile) {
|
|
337
357
|
const parsedLocal = parseEnvFile((0, import_node_path2.resolve)(rootDir, ".env.local"));
|
|
358
|
+
if (parsedLocal.exists) {
|
|
359
|
+
envFileExists = true;
|
|
360
|
+
}
|
|
338
361
|
for (const key of parsedLocal.keys) {
|
|
339
362
|
localEnvKeys.add(key);
|
|
340
363
|
}
|
|
@@ -367,6 +390,8 @@ function auditEnv(options = {}) {
|
|
|
367
390
|
return {
|
|
368
391
|
rootDir,
|
|
369
392
|
scannedFiles,
|
|
393
|
+
envFileExists,
|
|
394
|
+
exampleFileExists: parsedExample.exists,
|
|
370
395
|
usedInCode,
|
|
371
396
|
missingInEnv,
|
|
372
397
|
missingInExample,
|
|
@@ -454,12 +479,14 @@ function formatRefs(refs) {
|
|
|
454
479
|
const extra = refs.length > 2 ? ` (+${refs.length - 2} more)` : "";
|
|
455
480
|
return import_picocolors.default.dim(` \u2190 ${shown.join(", ")}${extra}`);
|
|
456
481
|
}
|
|
457
|
-
function hasBlockingIssues(result) {
|
|
458
|
-
|
|
482
|
+
function hasBlockingIssues(result, options = {}) {
|
|
483
|
+
const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
|
|
484
|
+
return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0;
|
|
459
485
|
}
|
|
460
486
|
function formatAuditReport(result, options = {}) {
|
|
461
487
|
const envFile = options.envFile ?? ".env";
|
|
462
488
|
const exampleFile = options.exampleFile ?? ".env.example";
|
|
489
|
+
const skipMissingInEnv = Boolean(options.ci && !result.envFileExists);
|
|
463
490
|
const lines = [];
|
|
464
491
|
lines.push("");
|
|
465
492
|
lines.push(
|
|
@@ -468,9 +495,14 @@ function formatAuditReport(result, options = {}) {
|
|
|
468
495
|
)}`
|
|
469
496
|
);
|
|
470
497
|
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;
|
|
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;
|
|
472
499
|
if (totalIssues === 0) {
|
|
473
500
|
lines.push(import_picocolors.default.green("\u2714 No environment drift detected. Everything is in sync!"));
|
|
501
|
+
if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
502
|
+
lines.push(
|
|
503
|
+
import_picocolors.default.dim(` (No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode)`)
|
|
504
|
+
);
|
|
505
|
+
}
|
|
474
506
|
lines.push("");
|
|
475
507
|
return lines.join("\n");
|
|
476
508
|
}
|
|
@@ -485,12 +517,17 @@ function formatAuditReport(result, options = {}) {
|
|
|
485
517
|
}
|
|
486
518
|
lines.push("");
|
|
487
519
|
}
|
|
488
|
-
if (result.missingInEnv.length > 0) {
|
|
520
|
+
if (!skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
489
521
|
lines.push(import_picocolors.default.bold(import_picocolors.default.red(`\u2716 Missing in ${envFile} (${result.missingInEnv.length})`)));
|
|
490
522
|
for (const key of result.missingInEnv) {
|
|
491
523
|
lines.push(` ${import_picocolors.default.red("\u2022")} ${key}${formatRefs(result.usedInCode[key])}`);
|
|
492
524
|
}
|
|
493
525
|
lines.push("");
|
|
526
|
+
} else if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
527
|
+
lines.push(
|
|
528
|
+
import_picocolors.default.dim(`\u2139 No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode`)
|
|
529
|
+
);
|
|
530
|
+
lines.push("");
|
|
494
531
|
}
|
|
495
532
|
if (result.missingInExample.length > 0) {
|
|
496
533
|
lines.push(import_picocolors.default.bold(import_picocolors.default.yellow(`\u26A0 Missing in ${exampleFile} (${result.missingInExample.length})`)));
|
package/dist/index.d.cts
CHANGED
|
@@ -20,6 +20,8 @@ interface SecretExposureWarning {
|
|
|
20
20
|
interface AuditResult {
|
|
21
21
|
rootDir: string;
|
|
22
22
|
scannedFiles: number;
|
|
23
|
+
envFileExists: boolean;
|
|
24
|
+
exampleFileExists: boolean;
|
|
23
25
|
usedInCode: Record<string, EnvReference[]>;
|
|
24
26
|
missingInEnv: string[];
|
|
25
27
|
missingInExample: string[];
|
|
@@ -106,14 +108,19 @@ declare function writeEnvTypes(outputPath: string, result: AuditResult): {
|
|
|
106
108
|
|
|
107
109
|
/**
|
|
108
110
|
* Returns true if the audit found blocking issues (missing vars or exposed public secrets).
|
|
111
|
+
* When no local `.env` file exists (e.g. in CI where `.env` is gitignored), `missingInEnv`
|
|
112
|
+
* is skipped by default unless `requireEnvFile` is true.
|
|
109
113
|
*/
|
|
110
|
-
declare function hasBlockingIssues(result: AuditResult
|
|
114
|
+
declare function hasBlockingIssues(result: AuditResult, options?: {
|
|
115
|
+
requireEnvFile?: boolean;
|
|
116
|
+
}): boolean;
|
|
111
117
|
/**
|
|
112
118
|
* Formats an AuditResult into a colorized terminal report.
|
|
113
119
|
*/
|
|
114
120
|
declare function formatAuditReport(result: AuditResult, options?: {
|
|
115
121
|
envFile?: string;
|
|
116
122
|
exampleFile?: string;
|
|
123
|
+
ci?: boolean;
|
|
117
124
|
}): string;
|
|
118
125
|
|
|
119
126
|
export { type AuditOptions, type AuditResult, BUILTIN_ENV_VARS, DEFAULT_EXTENSIONS, DEFAULT_IGNORE_DIRS, type EnvReference, PUBLIC_ENV_PREFIXES, type ParsedEnvFile, SENSITIVE_KEYWORDS, type SecretExposureWarning, auditEnv, detectSecretExposure, extractEnvReferences, formatAuditReport, generateEnvTypes, hasBlockingIssues, parseEnvContent, parseEnvFile, scanFiles, syncEnvExample, writeEnvTypes };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,6 +20,8 @@ interface SecretExposureWarning {
|
|
|
20
20
|
interface AuditResult {
|
|
21
21
|
rootDir: string;
|
|
22
22
|
scannedFiles: number;
|
|
23
|
+
envFileExists: boolean;
|
|
24
|
+
exampleFileExists: boolean;
|
|
23
25
|
usedInCode: Record<string, EnvReference[]>;
|
|
24
26
|
missingInEnv: string[];
|
|
25
27
|
missingInExample: string[];
|
|
@@ -106,14 +108,19 @@ declare function writeEnvTypes(outputPath: string, result: AuditResult): {
|
|
|
106
108
|
|
|
107
109
|
/**
|
|
108
110
|
* Returns true if the audit found blocking issues (missing vars or exposed public secrets).
|
|
111
|
+
* When no local `.env` file exists (e.g. in CI where `.env` is gitignored), `missingInEnv`
|
|
112
|
+
* is skipped by default unless `requireEnvFile` is true.
|
|
109
113
|
*/
|
|
110
|
-
declare function hasBlockingIssues(result: AuditResult
|
|
114
|
+
declare function hasBlockingIssues(result: AuditResult, options?: {
|
|
115
|
+
requireEnvFile?: boolean;
|
|
116
|
+
}): boolean;
|
|
111
117
|
/**
|
|
112
118
|
* Formats an AuditResult into a colorized terminal report.
|
|
113
119
|
*/
|
|
114
120
|
declare function formatAuditReport(result: AuditResult, options?: {
|
|
115
121
|
envFile?: string;
|
|
116
122
|
exampleFile?: string;
|
|
123
|
+
ci?: boolean;
|
|
117
124
|
}): string;
|
|
118
125
|
|
|
119
126
|
export { type AuditOptions, type AuditResult, BUILTIN_ENV_VARS, DEFAULT_EXTENSIONS, DEFAULT_IGNORE_DIRS, type EnvReference, PUBLIC_ENV_PREFIXES, type ParsedEnvFile, SENSITIVE_KEYWORDS, type SecretExposureWarning, auditEnv, detectSecretExposure, extractEnvReferences, formatAuditReport, generateEnvTypes, hasBlockingIssues, parseEnvContent, parseEnvFile, scanFiles, syncEnvExample, writeEnvTypes };
|
package/dist/index.js
CHANGED
|
@@ -8,8 +8,27 @@ function parseValue(raw) {
|
|
|
8
8
|
const trimmed = raw.trim();
|
|
9
9
|
if (!trimmed) return "";
|
|
10
10
|
const quote = trimmed[0];
|
|
11
|
-
if (quote === '"'
|
|
12
|
-
|
|
11
|
+
if (quote === '"') {
|
|
12
|
+
let result = "";
|
|
13
|
+
for (let i = 1; i < trimmed.length; i++) {
|
|
14
|
+
const ch = trimmed[i];
|
|
15
|
+
if (ch === "\\" && i + 1 < trimmed.length) {
|
|
16
|
+
const next = trimmed[i + 1];
|
|
17
|
+
if (next === "n") result += "\n";
|
|
18
|
+
else if (next === "r") result += "\r";
|
|
19
|
+
else if (next === "t") result += " ";
|
|
20
|
+
else result += next;
|
|
21
|
+
i++;
|
|
22
|
+
} else if (ch === '"') {
|
|
23
|
+
return result;
|
|
24
|
+
} else {
|
|
25
|
+
result += ch;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
if (quote === "'") {
|
|
31
|
+
const closingIdx = trimmed.indexOf("'", 1);
|
|
13
32
|
if (closingIdx !== -1) {
|
|
14
33
|
return trimmed.slice(1, closingIdx);
|
|
15
34
|
}
|
|
@@ -153,7 +172,7 @@ function extractEnvReferences(content, filePath) {
|
|
|
153
172
|
source
|
|
154
173
|
});
|
|
155
174
|
}
|
|
156
|
-
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
175
|
+
const bracketRegex = /\b(process\.env|import\.meta\.env)\s*(?:\?\.)?\s*\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g;
|
|
157
176
|
for (const match of cleaned.matchAll(bracketRegex)) {
|
|
158
177
|
const source = match[1];
|
|
159
178
|
const name = match[2];
|
|
@@ -281,9 +300,13 @@ function auditEnv(options = {}) {
|
|
|
281
300
|
}
|
|
282
301
|
const parsedEnv = parseEnvFile(resolve(rootDir, envFileName));
|
|
283
302
|
const parsedExample = parseEnvFile(resolve(rootDir, exampleFileName));
|
|
303
|
+
let envFileExists = parsedEnv.exists;
|
|
284
304
|
const localEnvKeys = new Set(parsedEnv.keys);
|
|
285
305
|
if (!options.envFile) {
|
|
286
306
|
const parsedLocal = parseEnvFile(resolve(rootDir, ".env.local"));
|
|
307
|
+
if (parsedLocal.exists) {
|
|
308
|
+
envFileExists = true;
|
|
309
|
+
}
|
|
287
310
|
for (const key of parsedLocal.keys) {
|
|
288
311
|
localEnvKeys.add(key);
|
|
289
312
|
}
|
|
@@ -316,6 +339,8 @@ function auditEnv(options = {}) {
|
|
|
316
339
|
return {
|
|
317
340
|
rootDir,
|
|
318
341
|
scannedFiles,
|
|
342
|
+
envFileExists,
|
|
343
|
+
exampleFileExists: parsedExample.exists,
|
|
319
344
|
usedInCode,
|
|
320
345
|
missingInEnv,
|
|
321
346
|
missingInExample,
|
|
@@ -403,12 +428,14 @@ function formatRefs(refs) {
|
|
|
403
428
|
const extra = refs.length > 2 ? ` (+${refs.length - 2} more)` : "";
|
|
404
429
|
return pc.dim(` \u2190 ${shown.join(", ")}${extra}`);
|
|
405
430
|
}
|
|
406
|
-
function hasBlockingIssues(result) {
|
|
407
|
-
|
|
431
|
+
function hasBlockingIssues(result, options = {}) {
|
|
432
|
+
const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
|
|
433
|
+
return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0;
|
|
408
434
|
}
|
|
409
435
|
function formatAuditReport(result, options = {}) {
|
|
410
436
|
const envFile = options.envFile ?? ".env";
|
|
411
437
|
const exampleFile = options.exampleFile ?? ".env.example";
|
|
438
|
+
const skipMissingInEnv = Boolean(options.ci && !result.envFileExists);
|
|
412
439
|
const lines = [];
|
|
413
440
|
lines.push("");
|
|
414
441
|
lines.push(
|
|
@@ -417,9 +444,14 @@ function formatAuditReport(result, options = {}) {
|
|
|
417
444
|
)}`
|
|
418
445
|
);
|
|
419
446
|
lines.push("");
|
|
420
|
-
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;
|
|
447
|
+
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;
|
|
421
448
|
if (totalIssues === 0) {
|
|
422
449
|
lines.push(pc.green("\u2714 No environment drift detected. Everything is in sync!"));
|
|
450
|
+
if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
451
|
+
lines.push(
|
|
452
|
+
pc.dim(` (No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode)`)
|
|
453
|
+
);
|
|
454
|
+
}
|
|
423
455
|
lines.push("");
|
|
424
456
|
return lines.join("\n");
|
|
425
457
|
}
|
|
@@ -434,12 +466,17 @@ function formatAuditReport(result, options = {}) {
|
|
|
434
466
|
}
|
|
435
467
|
lines.push("");
|
|
436
468
|
}
|
|
437
|
-
if (result.missingInEnv.length > 0) {
|
|
469
|
+
if (!skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
438
470
|
lines.push(pc.bold(pc.red(`\u2716 Missing in ${envFile} (${result.missingInEnv.length})`)));
|
|
439
471
|
for (const key of result.missingInEnv) {
|
|
440
472
|
lines.push(` ${pc.red("\u2022")} ${key}${formatRefs(result.usedInCode[key])}`);
|
|
441
473
|
}
|
|
442
474
|
lines.push("");
|
|
475
|
+
} else if (skipMissingInEnv && result.missingInEnv.length > 0) {
|
|
476
|
+
lines.push(
|
|
477
|
+
pc.dim(`\u2139 No ${envFile} file found \u2014 skipped local ${envFile} check in CI mode`)
|
|
478
|
+
);
|
|
479
|
+
lines.push("");
|
|
443
480
|
}
|
|
444
481
|
if (result.missingInExample.length > 0) {
|
|
445
482
|
lines.push(pc.bold(pc.yellow(`\u26A0 Missing in ${exampleFile} (${result.missingInExample.length})`)));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@subramanyaks/env-drift",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Zero-config CLI and library to detect environment variable drift, missing keys, dead vars, and accidental public secret exposure.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|