-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatterns.ts
More file actions
193 lines (178 loc) · 4.67 KB
/
patterns.ts
File metadata and controls
193 lines (178 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
type Pattern = {
name: 'process.env' | 'import.meta.env' | 'sveltekit';
regex: RegExp;
processor?: (match: RegExpExecArray) => string[];
};
/**
* Framework-specific regex patterns for detecting environment variable usage
* across different runtimes and frameworks.
*/
export const ENV_PATTERNS: Pattern[] = [
/**
* Matches process.env.KEY references in source code.
* Supports both dot notation and bracket notation.
*
* Examples:
* process.env.MY_KEY
* process.env["MY_KEY"]
* process.env['MY_KEY']
*/
{
name: 'process.env',
regex:
/process\.env\.([A-Z_][A-Z0-9_]*)|process\.env\[['"]([A-Z_][A-Z0-9_]*)['"]]/g,
processor: (match) => {
// match[1] covers dot notation: process.env.KEY
// match[2] covers bracket notation: process.env['KEY']
const variable = match[1] || match[2];
return variable ? [variable] : [];
},
},
/**
* Matches object destructuring from process.env.
* Captures the full object pattern between braces for further parsing.
*
* Example:
* const { MY_KEY, OTHER_KEY: alias, THIRD_KEY = "fallback" } = process.env
*/
{
name: 'process.env',
regex: /\{([^}]*)\}\s*=\s*process\.env\b/g,
processor: (match) => {
const content = match[1];
if (!content) return [];
return content
.split(',')
.map((part) => part.trim())
.filter(Boolean)
.map((part) => {
// Handle aliases: MY_KEY: alias
// Handle defaults: MY_KEY = "value"
// We want the left-most identifier
const [key] = part.split(/[:=]/);
return key ? key.trim() : '';
})
.filter((key) => /^[A-Z_][A-Z0-9_]*$/.test(key));
},
},
/**
* Matches import.meta.env.KEY references in source code.
* Supports both dot notation and bracket notation.
*
* Examples:
* import.meta.env.MY_KEY
* import.meta.env["MY_KEY"]
* import.meta.env['MY_KEY']
*/
{
name: 'import.meta.env',
regex:
/import\.meta\.env\.([A-Z_][A-Z0-9_]*)|import\.meta\.env\[['"]([A-Z_][A-Z0-9_]*)['"]]/g,
processor: (match) => {
const variable = match[1] || match[2];
return variable ? [variable] : [];
},
},
// SvelteKit static named imports
// import { SECRET } from '$env/static/private';
// import { PUBLIC_URL } from '$env/static/public';
{
name: 'sveltekit' as const,
regex:
/import\s*\{\s*([A-Z_][A-Z0-9_]*)\s*\}\s*from\s*['"]\$env\/static\/(?:private|public)['"]/g,
},
// SvelteKit dynamic env object
// env.SECRET (Only matches .env variables accessed via env.VAR syntax)
{
name: 'sveltekit' as const,
regex: /(?<![.\w])env\.([A-Z_][A-Z0-9_]*)/g,
},
// SvelteKit object destructuring from env
// const { VAR1, VAR2 } = env; (destructured from $env/dynamic/* or $env/static/*)
{
name: 'sveltekit' as const,
regex: /\{([^}]*)\}\s*=\s*env\b/g,
processor: (match) => {
const content = match[1];
if (!content) return [];
return content
.split(',')
.map((part) => part.trim())
.filter(Boolean)
.map((part) => {
// Handle aliases: VAR: alias
// Handle defaults: VAR = "value"
// We want the left-most identifier
const [key] = part.split(/[:=]/);
return key ? key.trim() : '';
})
.filter((key) => /^[A-Z_][A-Z0-9_]*$/.test(key));
},
},
// named import from dynamic is invalid in SvelteKit
// import { env } from '$env/dynamic/private';
{
name: 'sveltekit' as const,
regex:
/import\s*\{\s*([A-Z_][A-Z0-9_]*)\s*\}\s*from\s*['"]\$env\/dynamic\/(?:private|public)['"]/g,
},
// default import from any $env module is invalid in SvelteKit
// import SECRET from '$env/...';
{
name: 'sveltekit' as const,
regex:
/import\s+([A-Z_][A-Z0-9_]*)\s+from\s+['"]\$env\/(?:static|dynamic)\/(?:private|public)['"]/g,
},
];
// Default file extensions to include in scans
export const DEFAULT_INCLUDE_EXTENSIONS = [
'.js',
'.ts',
'.jsx',
'.tsx',
'.vue',
'.svelte',
'.mjs',
'.mts',
'.cjs',
'.cts',
];
// Default patterns to exclude from scans
export const DEFAULT_EXCLUDE_PATTERNS = [
'node_modules',
'.sveltekit',
'.svelte-kit',
'_actions',
'dist',
'build',
'.next',
'.nuxt',
'coverage',
'.git',
'.vscode',
'.idea',
// Tests
'.test.',
'.spec.',
'__tests__',
'__mocks__',
// Common noisy paths
'test',
'tests',
'fixtures',
'fixture',
'examples',
'example',
'samples',
'sandbox',
// Generated / vendored / caches
'.turbo',
'.cache',
'.output',
'.vercel',
'.yarn',
'.pnpm-store',
'.parcel-cache',
'.rollup.cache',
'.DS_Store',
];