@subramanyaks/env-drift 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +137 -2
- package/dist/cli.js +720 -0
- package/dist/index.cjs +589 -0
- package/dist/index.d.cts +126 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.js +537 -0
- package/package.json +67 -4
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
interface EnvReference {
|
|
2
|
+
name: string;
|
|
3
|
+
file: string;
|
|
4
|
+
line: number;
|
|
5
|
+
source: "process.env" | "import.meta.env";
|
|
6
|
+
}
|
|
7
|
+
interface ParsedEnvFile {
|
|
8
|
+
filePath: string;
|
|
9
|
+
exists: boolean;
|
|
10
|
+
keys: string[];
|
|
11
|
+
values: Record<string, string>;
|
|
12
|
+
duplicates: string[];
|
|
13
|
+
}
|
|
14
|
+
interface SecretExposureWarning {
|
|
15
|
+
name: string;
|
|
16
|
+
prefix: string;
|
|
17
|
+
matchedKeyword: string;
|
|
18
|
+
references: EnvReference[];
|
|
19
|
+
}
|
|
20
|
+
interface AuditResult {
|
|
21
|
+
rootDir: string;
|
|
22
|
+
scannedFiles: number;
|
|
23
|
+
envFileExists: boolean;
|
|
24
|
+
exampleFileExists: boolean;
|
|
25
|
+
usedInCode: Record<string, EnvReference[]>;
|
|
26
|
+
missingInEnv: string[];
|
|
27
|
+
missingInExample: string[];
|
|
28
|
+
unusedInEnv: string[];
|
|
29
|
+
unusedInExample: string[];
|
|
30
|
+
secretExposures: SecretExposureWarning[];
|
|
31
|
+
duplicateKeys: {
|
|
32
|
+
env: string[];
|
|
33
|
+
example: string[];
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface AuditOptions {
|
|
37
|
+
rootDir?: string;
|
|
38
|
+
envFile?: string;
|
|
39
|
+
exampleFile?: string;
|
|
40
|
+
ignore?: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare const BUILTIN_ENV_VARS: Set<string>;
|
|
44
|
+
declare const PUBLIC_ENV_PREFIXES: readonly ["NEXT_PUBLIC_", "VITE_", "REACT_APP_", "NUXT_PUBLIC_", "PUBLIC_", "EXPO_PUBLIC_", "GATSBY_"];
|
|
45
|
+
declare const SENSITIVE_KEYWORDS: readonly ["SECRET", "PASSWORD", "PASSWD", "PRIVATE_KEY", "PRIVATE", "DATABASE_URL", "DB_URL", "DB_PASS", "JWT_SECRET", "AUTH_TOKEN", "ACCESS_KEY", "API_SECRET", "SERVICE_ROLE", "CREDENTIALS"];
|
|
46
|
+
/**
|
|
47
|
+
* Detects whether an environment variable name combines a client-exposed prefix
|
|
48
|
+
* with a sensitive keyword (e.g. `NEXT_PUBLIC_DB_PASSWORD`).
|
|
49
|
+
*/
|
|
50
|
+
declare function detectSecretExposure(name: string): {
|
|
51
|
+
prefix: string;
|
|
52
|
+
matchedKeyword: string;
|
|
53
|
+
} | null;
|
|
54
|
+
/**
|
|
55
|
+
* Runs a full environment variable drift and security audit on a project directory.
|
|
56
|
+
*/
|
|
57
|
+
declare function auditEnv(options?: AuditOptions): AuditResult;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Parses the text contents of a .env file without external dependencies.
|
|
61
|
+
*/
|
|
62
|
+
declare function parseEnvContent(content: string): {
|
|
63
|
+
keys: string[];
|
|
64
|
+
values: Record<string, string>;
|
|
65
|
+
duplicates: string[];
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Reads and parses a .env file from disk. Returns `exists: false` if missing.
|
|
69
|
+
*/
|
|
70
|
+
declare function parseEnvFile(filePath: string): ParsedEnvFile;
|
|
71
|
+
/**
|
|
72
|
+
* Safely appends missing keys to `.env.example` as `KEY=` placeholders (never copies real values).
|
|
73
|
+
*/
|
|
74
|
+
declare function syncEnvExample(examplePath: string, missingKeys: string[]): {
|
|
75
|
+
added: string[];
|
|
76
|
+
created: boolean;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
declare const DEFAULT_IGNORE_DIRS: Set<string>;
|
|
80
|
+
declare const DEFAULT_EXTENSIONS: Set<string>;
|
|
81
|
+
/**
|
|
82
|
+
* Extracts all `process.env` and `import.meta.env` references from a source string.
|
|
83
|
+
*/
|
|
84
|
+
declare function extractEnvReferences(content: string, filePath: string): EnvReference[];
|
|
85
|
+
interface ScanOptions {
|
|
86
|
+
ignoreDirs?: string[];
|
|
87
|
+
extensions?: string[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Recursively walks `rootDir` and extracts all environment variable references.
|
|
91
|
+
*/
|
|
92
|
+
declare function scanFiles(rootDir: string, options?: ScanOptions): {
|
|
93
|
+
scannedFiles: number;
|
|
94
|
+
references: EnvReference[];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Generates TypeScript ambient declarations (`env.d.ts`) from an AuditResult.
|
|
99
|
+
*/
|
|
100
|
+
declare function generateEnvTypes(result: AuditResult): string;
|
|
101
|
+
/**
|
|
102
|
+
* Writes the generated `env.d.ts` file to disk.
|
|
103
|
+
*/
|
|
104
|
+
declare function writeEnvTypes(outputPath: string, result: AuditResult): {
|
|
105
|
+
outputPath: string;
|
|
106
|
+
count: number;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
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.
|
|
113
|
+
*/
|
|
114
|
+
declare function hasBlockingIssues(result: AuditResult, options?: {
|
|
115
|
+
requireEnvFile?: boolean;
|
|
116
|
+
}): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Formats an AuditResult into a colorized terminal report.
|
|
119
|
+
*/
|
|
120
|
+
declare function formatAuditReport(result: AuditResult, options?: {
|
|
121
|
+
envFile?: string;
|
|
122
|
+
exampleFile?: string;
|
|
123
|
+
ci?: boolean;
|
|
124
|
+
}): string;
|
|
125
|
+
|
|
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
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
interface EnvReference {
|
|
2
|
+
name: string;
|
|
3
|
+
file: string;
|
|
4
|
+
line: number;
|
|
5
|
+
source: "process.env" | "import.meta.env";
|
|
6
|
+
}
|
|
7
|
+
interface ParsedEnvFile {
|
|
8
|
+
filePath: string;
|
|
9
|
+
exists: boolean;
|
|
10
|
+
keys: string[];
|
|
11
|
+
values: Record<string, string>;
|
|
12
|
+
duplicates: string[];
|
|
13
|
+
}
|
|
14
|
+
interface SecretExposureWarning {
|
|
15
|
+
name: string;
|
|
16
|
+
prefix: string;
|
|
17
|
+
matchedKeyword: string;
|
|
18
|
+
references: EnvReference[];
|
|
19
|
+
}
|
|
20
|
+
interface AuditResult {
|
|
21
|
+
rootDir: string;
|
|
22
|
+
scannedFiles: number;
|
|
23
|
+
envFileExists: boolean;
|
|
24
|
+
exampleFileExists: boolean;
|
|
25
|
+
usedInCode: Record<string, EnvReference[]>;
|
|
26
|
+
missingInEnv: string[];
|
|
27
|
+
missingInExample: string[];
|
|
28
|
+
unusedInEnv: string[];
|
|
29
|
+
unusedInExample: string[];
|
|
30
|
+
secretExposures: SecretExposureWarning[];
|
|
31
|
+
duplicateKeys: {
|
|
32
|
+
env: string[];
|
|
33
|
+
example: string[];
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface AuditOptions {
|
|
37
|
+
rootDir?: string;
|
|
38
|
+
envFile?: string;
|
|
39
|
+
exampleFile?: string;
|
|
40
|
+
ignore?: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare const BUILTIN_ENV_VARS: Set<string>;
|
|
44
|
+
declare const PUBLIC_ENV_PREFIXES: readonly ["NEXT_PUBLIC_", "VITE_", "REACT_APP_", "NUXT_PUBLIC_", "PUBLIC_", "EXPO_PUBLIC_", "GATSBY_"];
|
|
45
|
+
declare const SENSITIVE_KEYWORDS: readonly ["SECRET", "PASSWORD", "PASSWD", "PRIVATE_KEY", "PRIVATE", "DATABASE_URL", "DB_URL", "DB_PASS", "JWT_SECRET", "AUTH_TOKEN", "ACCESS_KEY", "API_SECRET", "SERVICE_ROLE", "CREDENTIALS"];
|
|
46
|
+
/**
|
|
47
|
+
* Detects whether an environment variable name combines a client-exposed prefix
|
|
48
|
+
* with a sensitive keyword (e.g. `NEXT_PUBLIC_DB_PASSWORD`).
|
|
49
|
+
*/
|
|
50
|
+
declare function detectSecretExposure(name: string): {
|
|
51
|
+
prefix: string;
|
|
52
|
+
matchedKeyword: string;
|
|
53
|
+
} | null;
|
|
54
|
+
/**
|
|
55
|
+
* Runs a full environment variable drift and security audit on a project directory.
|
|
56
|
+
*/
|
|
57
|
+
declare function auditEnv(options?: AuditOptions): AuditResult;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Parses the text contents of a .env file without external dependencies.
|
|
61
|
+
*/
|
|
62
|
+
declare function parseEnvContent(content: string): {
|
|
63
|
+
keys: string[];
|
|
64
|
+
values: Record<string, string>;
|
|
65
|
+
duplicates: string[];
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Reads and parses a .env file from disk. Returns `exists: false` if missing.
|
|
69
|
+
*/
|
|
70
|
+
declare function parseEnvFile(filePath: string): ParsedEnvFile;
|
|
71
|
+
/**
|
|
72
|
+
* Safely appends missing keys to `.env.example` as `KEY=` placeholders (never copies real values).
|
|
73
|
+
*/
|
|
74
|
+
declare function syncEnvExample(examplePath: string, missingKeys: string[]): {
|
|
75
|
+
added: string[];
|
|
76
|
+
created: boolean;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
declare const DEFAULT_IGNORE_DIRS: Set<string>;
|
|
80
|
+
declare const DEFAULT_EXTENSIONS: Set<string>;
|
|
81
|
+
/**
|
|
82
|
+
* Extracts all `process.env` and `import.meta.env` references from a source string.
|
|
83
|
+
*/
|
|
84
|
+
declare function extractEnvReferences(content: string, filePath: string): EnvReference[];
|
|
85
|
+
interface ScanOptions {
|
|
86
|
+
ignoreDirs?: string[];
|
|
87
|
+
extensions?: string[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Recursively walks `rootDir` and extracts all environment variable references.
|
|
91
|
+
*/
|
|
92
|
+
declare function scanFiles(rootDir: string, options?: ScanOptions): {
|
|
93
|
+
scannedFiles: number;
|
|
94
|
+
references: EnvReference[];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Generates TypeScript ambient declarations (`env.d.ts`) from an AuditResult.
|
|
99
|
+
*/
|
|
100
|
+
declare function generateEnvTypes(result: AuditResult): string;
|
|
101
|
+
/**
|
|
102
|
+
* Writes the generated `env.d.ts` file to disk.
|
|
103
|
+
*/
|
|
104
|
+
declare function writeEnvTypes(outputPath: string, result: AuditResult): {
|
|
105
|
+
outputPath: string;
|
|
106
|
+
count: number;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
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.
|
|
113
|
+
*/
|
|
114
|
+
declare function hasBlockingIssues(result: AuditResult, options?: {
|
|
115
|
+
requireEnvFile?: boolean;
|
|
116
|
+
}): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Formats an AuditResult into a colorized terminal report.
|
|
119
|
+
*/
|
|
120
|
+
declare function formatAuditReport(result: AuditResult, options?: {
|
|
121
|
+
envFile?: string;
|
|
122
|
+
exampleFile?: string;
|
|
123
|
+
ci?: boolean;
|
|
124
|
+
}): string;
|
|
125
|
+
|
|
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 };
|