@sparkleideas/security 3.0.0-alpha.22 → 3.0.0-alpha.29
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/CVE-REMEDIATION.d.ts +86 -0
- package/dist/CVE-REMEDIATION.d.ts.map +1 -0
- package/dist/CVE-REMEDIATION.js +221 -0
- package/dist/CVE-REMEDIATION.js.map +1 -0
- package/dist/application/index.d.ts +7 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +7 -0
- package/dist/application/index.js.map +1 -0
- package/dist/application/services/security-application-service.d.ts +71 -0
- package/dist/application/services/security-application-service.d.ts.map +1 -0
- package/dist/application/services/security-application-service.js +153 -0
- package/dist/application/services/security-application-service.js.map +1 -0
- package/dist/credential-generator.d.ts +176 -0
- package/dist/credential-generator.d.ts.map +1 -0
- package/dist/credential-generator.js +272 -0
- package/dist/credential-generator.js.map +1 -0
- package/dist/domain/entities/security-context.d.ts +68 -0
- package/dist/domain/entities/security-context.d.ts.map +1 -0
- package/dist/domain/entities/security-context.js +132 -0
- package/dist/domain/entities/security-context.js.map +1 -0
- package/dist/domain/index.d.ts +8 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +8 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/services/security-domain-service.d.ts +71 -0
- package/dist/domain/services/security-domain-service.d.ts.map +1 -0
- package/dist/domain/services/security-domain-service.js +237 -0
- package/dist/domain/services/security-domain-service.js.map +1 -0
- package/dist/index.d.ts +119 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +145 -0
- package/dist/index.js.map +1 -0
- package/dist/input-validator.d.ts +338 -0
- package/dist/input-validator.d.ts.map +1 -0
- package/dist/input-validator.js +393 -0
- package/dist/input-validator.js.map +1 -0
- package/dist/password-hasher.d.ts +128 -0
- package/dist/password-hasher.d.ts.map +1 -0
- package/dist/password-hasher.js +183 -0
- package/dist/password-hasher.js.map +1 -0
- package/dist/path-validator.d.ts +148 -0
- package/dist/path-validator.d.ts.map +1 -0
- package/dist/path-validator.js +421 -0
- package/dist/path-validator.js.map +1 -0
- package/dist/safe-executor.d.ts +173 -0
- package/dist/safe-executor.d.ts.map +1 -0
- package/dist/safe-executor.js +370 -0
- package/dist/safe-executor.js.map +1 -0
- package/dist/token-generator.d.ts +224 -0
- package/dist/token-generator.d.ts.map +1 -0
- package/dist/token-generator.js +351 -0
- package/dist/token-generator.js.map +1 -0
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path Validator - HIGH-2 Remediation
|
|
3
|
+
*
|
|
4
|
+
* Fixes path traversal vulnerabilities by:
|
|
5
|
+
* - Validating all file paths against allowed prefixes
|
|
6
|
+
* - Using path.resolve() for canonicalization
|
|
7
|
+
* - Blocking traversal patterns (../, etc.)
|
|
8
|
+
* - Enforcing path length limits
|
|
9
|
+
*
|
|
10
|
+
* Security Properties:
|
|
11
|
+
* - Path canonicalization
|
|
12
|
+
* - Prefix validation
|
|
13
|
+
* - Symlink resolution (optional)
|
|
14
|
+
* - Traversal pattern detection
|
|
15
|
+
*
|
|
16
|
+
* @module v3/security/path-validator
|
|
17
|
+
*/
|
|
18
|
+
import * as path from 'path';
|
|
19
|
+
import * as fs from 'fs/promises';
|
|
20
|
+
export class PathValidatorError extends Error {
|
|
21
|
+
code;
|
|
22
|
+
path;
|
|
23
|
+
constructor(message, code, path) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.path = path;
|
|
27
|
+
this.name = 'PathValidatorError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Dangerous path patterns that indicate traversal attempts.
|
|
32
|
+
*/
|
|
33
|
+
const TRAVERSAL_PATTERNS = [
|
|
34
|
+
/\.\.\//, // ../
|
|
35
|
+
/\.\.\\/, // ..\
|
|
36
|
+
/\.\./, // .. anywhere
|
|
37
|
+
/%2e%2e/i, // URL-encoded ..
|
|
38
|
+
/%252e%252e/i, // Double URL-encoded ..
|
|
39
|
+
/\.%2e/i, // Mixed encoding
|
|
40
|
+
/%2e\./i, // Mixed encoding
|
|
41
|
+
/\0/, // Null byte
|
|
42
|
+
/%00/, // URL-encoded null
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* Default blocked file extensions (sensitive files).
|
|
46
|
+
*/
|
|
47
|
+
const DEFAULT_BLOCKED_EXTENSIONS = [
|
|
48
|
+
'.env',
|
|
49
|
+
'.pem',
|
|
50
|
+
'.key',
|
|
51
|
+
'.crt',
|
|
52
|
+
'.pfx',
|
|
53
|
+
'.p12',
|
|
54
|
+
'.jks',
|
|
55
|
+
'.keystore',
|
|
56
|
+
'.secret',
|
|
57
|
+
'.credentials',
|
|
58
|
+
];
|
|
59
|
+
/**
|
|
60
|
+
* Default blocked file names (sensitive files).
|
|
61
|
+
*/
|
|
62
|
+
const DEFAULT_BLOCKED_NAMES = [
|
|
63
|
+
'id_rsa',
|
|
64
|
+
'id_dsa',
|
|
65
|
+
'id_ecdsa',
|
|
66
|
+
'id_ed25519',
|
|
67
|
+
'.htpasswd',
|
|
68
|
+
'.htaccess',
|
|
69
|
+
'shadow',
|
|
70
|
+
'passwd',
|
|
71
|
+
'authorized_keys',
|
|
72
|
+
'known_hosts',
|
|
73
|
+
'.git',
|
|
74
|
+
'.gitconfig',
|
|
75
|
+
'.npmrc',
|
|
76
|
+
'.docker',
|
|
77
|
+
];
|
|
78
|
+
/**
|
|
79
|
+
* Path validator that prevents traversal attacks.
|
|
80
|
+
*
|
|
81
|
+
* This class validates file paths to ensure they stay within
|
|
82
|
+
* allowed directories and don't access sensitive files.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const validator = new PathValidator({
|
|
87
|
+
* allowedPrefixes: ['/workspaces/project']
|
|
88
|
+
* });
|
|
89
|
+
*
|
|
90
|
+
* const result = await validator.validate('/workspaces/project/src/file.ts');
|
|
91
|
+
* if (result.isValid) {
|
|
92
|
+
* // Safe to use result.resolvedPath
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export class PathValidator {
|
|
97
|
+
config;
|
|
98
|
+
resolvedPrefixes;
|
|
99
|
+
constructor(config) {
|
|
100
|
+
this.config = {
|
|
101
|
+
allowedPrefixes: config.allowedPrefixes,
|
|
102
|
+
blockedExtensions: config.blockedExtensions ?? DEFAULT_BLOCKED_EXTENSIONS,
|
|
103
|
+
blockedNames: config.blockedNames ?? DEFAULT_BLOCKED_NAMES,
|
|
104
|
+
maxPathLength: config.maxPathLength ?? 4096,
|
|
105
|
+
resolveSymlinks: config.resolveSymlinks ?? true,
|
|
106
|
+
allowNonExistent: config.allowNonExistent ?? true,
|
|
107
|
+
allowHidden: config.allowHidden ?? false,
|
|
108
|
+
};
|
|
109
|
+
if (this.config.allowedPrefixes.length === 0) {
|
|
110
|
+
throw new PathValidatorError('At least one allowed prefix must be specified', 'EMPTY_PREFIXES');
|
|
111
|
+
}
|
|
112
|
+
// Pre-resolve all prefixes
|
|
113
|
+
this.resolvedPrefixes = this.config.allowedPrefixes.map(p => path.resolve(p));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Validates a path against security rules.
|
|
117
|
+
*
|
|
118
|
+
* @param inputPath - The path to validate
|
|
119
|
+
* @returns Validation result with resolved path
|
|
120
|
+
*/
|
|
121
|
+
async validate(inputPath) {
|
|
122
|
+
const errors = [];
|
|
123
|
+
// Check for empty path
|
|
124
|
+
if (!inputPath || inputPath.trim() === '') {
|
|
125
|
+
return {
|
|
126
|
+
isValid: false,
|
|
127
|
+
resolvedPath: '',
|
|
128
|
+
relativePath: '',
|
|
129
|
+
matchedPrefix: '',
|
|
130
|
+
errors: ['Path is empty'],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// Check path length
|
|
134
|
+
if (inputPath.length > this.config.maxPathLength) {
|
|
135
|
+
return {
|
|
136
|
+
isValid: false,
|
|
137
|
+
resolvedPath: '',
|
|
138
|
+
relativePath: '',
|
|
139
|
+
matchedPrefix: '',
|
|
140
|
+
errors: [`Path exceeds maximum length of ${this.config.maxPathLength}`],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// Check for traversal patterns
|
|
144
|
+
for (const pattern of TRAVERSAL_PATTERNS) {
|
|
145
|
+
if (pattern.test(inputPath)) {
|
|
146
|
+
return {
|
|
147
|
+
isValid: false,
|
|
148
|
+
resolvedPath: '',
|
|
149
|
+
relativePath: '',
|
|
150
|
+
matchedPrefix: '',
|
|
151
|
+
errors: ['Path traversal pattern detected'],
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Resolve the path
|
|
156
|
+
let resolvedPath;
|
|
157
|
+
try {
|
|
158
|
+
resolvedPath = path.resolve(inputPath);
|
|
159
|
+
// Optionally resolve symlinks
|
|
160
|
+
if (this.config.resolveSymlinks) {
|
|
161
|
+
try {
|
|
162
|
+
resolvedPath = await fs.realpath(resolvedPath);
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
// Path doesn't exist yet - use resolved path
|
|
166
|
+
if (error.code !== 'ENOENT' || !this.config.allowNonExistent) {
|
|
167
|
+
if (error.code === 'ENOENT') {
|
|
168
|
+
errors.push('Path does not exist');
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
errors.push(`Failed to resolve path: ${error.message}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
return {
|
|
179
|
+
isValid: false,
|
|
180
|
+
resolvedPath: '',
|
|
181
|
+
relativePath: '',
|
|
182
|
+
matchedPrefix: '',
|
|
183
|
+
errors: [`Invalid path: ${error.message}`],
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// Check against allowed prefixes
|
|
187
|
+
let matchedPrefix = '';
|
|
188
|
+
let relativePath = '';
|
|
189
|
+
let prefixMatched = false;
|
|
190
|
+
for (const prefix of this.resolvedPrefixes) {
|
|
191
|
+
if (resolvedPath === prefix || resolvedPath.startsWith(prefix + path.sep)) {
|
|
192
|
+
prefixMatched = true;
|
|
193
|
+
matchedPrefix = prefix;
|
|
194
|
+
relativePath = resolvedPath.slice(prefix.length);
|
|
195
|
+
if (relativePath.startsWith(path.sep)) {
|
|
196
|
+
relativePath = relativePath.slice(1);
|
|
197
|
+
}
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (!prefixMatched) {
|
|
202
|
+
return {
|
|
203
|
+
isValid: false,
|
|
204
|
+
resolvedPath,
|
|
205
|
+
relativePath: '',
|
|
206
|
+
matchedPrefix: '',
|
|
207
|
+
errors: ['Path is outside allowed directories'],
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
// Check for hidden files
|
|
211
|
+
const pathParts = resolvedPath.split(path.sep);
|
|
212
|
+
if (!this.config.allowHidden) {
|
|
213
|
+
for (const part of pathParts) {
|
|
214
|
+
if (part.startsWith('.') && part !== '.' && part !== '..') {
|
|
215
|
+
errors.push('Hidden files/directories are not allowed');
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Check blocked file names
|
|
221
|
+
const basename = path.basename(resolvedPath);
|
|
222
|
+
if (this.config.blockedNames.includes(basename)) {
|
|
223
|
+
errors.push(`File name "${basename}" is blocked`);
|
|
224
|
+
}
|
|
225
|
+
// Check blocked extensions
|
|
226
|
+
const ext = path.extname(resolvedPath).toLowerCase();
|
|
227
|
+
if (this.config.blockedExtensions.includes(ext)) {
|
|
228
|
+
errors.push(`File extension "${ext}" is blocked`);
|
|
229
|
+
}
|
|
230
|
+
// Also check for double extensions (e.g., .tar.gz, .config.json)
|
|
231
|
+
const fullname = basename.toLowerCase();
|
|
232
|
+
for (const blockedExt of this.config.blockedExtensions) {
|
|
233
|
+
if (fullname.endsWith(blockedExt)) {
|
|
234
|
+
errors.push(`File extension "${blockedExt}" is blocked`);
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
isValid: errors.length === 0,
|
|
240
|
+
resolvedPath,
|
|
241
|
+
relativePath,
|
|
242
|
+
matchedPrefix,
|
|
243
|
+
errors,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Validates and returns resolved path, throwing on failure.
|
|
248
|
+
*
|
|
249
|
+
* @param inputPath - The path to validate
|
|
250
|
+
* @returns Resolved path if valid
|
|
251
|
+
* @throws PathValidatorError if validation fails
|
|
252
|
+
*/
|
|
253
|
+
async validateOrThrow(inputPath) {
|
|
254
|
+
const result = await this.validate(inputPath);
|
|
255
|
+
if (!result.isValid) {
|
|
256
|
+
throw new PathValidatorError(result.errors.join('; '), 'VALIDATION_FAILED', inputPath);
|
|
257
|
+
}
|
|
258
|
+
return result.resolvedPath;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Synchronous validation (without symlink resolution).
|
|
262
|
+
*
|
|
263
|
+
* @param inputPath - The path to validate
|
|
264
|
+
* @returns Validation result
|
|
265
|
+
*/
|
|
266
|
+
validateSync(inputPath) {
|
|
267
|
+
const errors = [];
|
|
268
|
+
if (!inputPath || inputPath.trim() === '') {
|
|
269
|
+
return {
|
|
270
|
+
isValid: false,
|
|
271
|
+
resolvedPath: '',
|
|
272
|
+
relativePath: '',
|
|
273
|
+
matchedPrefix: '',
|
|
274
|
+
errors: ['Path is empty'],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (inputPath.length > this.config.maxPathLength) {
|
|
278
|
+
return {
|
|
279
|
+
isValid: false,
|
|
280
|
+
resolvedPath: '',
|
|
281
|
+
relativePath: '',
|
|
282
|
+
matchedPrefix: '',
|
|
283
|
+
errors: [`Path exceeds maximum length of ${this.config.maxPathLength}`],
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
for (const pattern of TRAVERSAL_PATTERNS) {
|
|
287
|
+
if (pattern.test(inputPath)) {
|
|
288
|
+
return {
|
|
289
|
+
isValid: false,
|
|
290
|
+
resolvedPath: '',
|
|
291
|
+
relativePath: '',
|
|
292
|
+
matchedPrefix: '',
|
|
293
|
+
errors: ['Path traversal pattern detected'],
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const resolvedPath = path.resolve(inputPath);
|
|
298
|
+
let matchedPrefix = '';
|
|
299
|
+
let relativePath = '';
|
|
300
|
+
let prefixMatched = false;
|
|
301
|
+
for (const prefix of this.resolvedPrefixes) {
|
|
302
|
+
if (resolvedPath === prefix || resolvedPath.startsWith(prefix + path.sep)) {
|
|
303
|
+
prefixMatched = true;
|
|
304
|
+
matchedPrefix = prefix;
|
|
305
|
+
relativePath = resolvedPath.slice(prefix.length);
|
|
306
|
+
if (relativePath.startsWith(path.sep)) {
|
|
307
|
+
relativePath = relativePath.slice(1);
|
|
308
|
+
}
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (!prefixMatched) {
|
|
313
|
+
return {
|
|
314
|
+
isValid: false,
|
|
315
|
+
resolvedPath,
|
|
316
|
+
relativePath: '',
|
|
317
|
+
matchedPrefix: '',
|
|
318
|
+
errors: ['Path is outside allowed directories'],
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const pathParts = resolvedPath.split(path.sep);
|
|
322
|
+
if (!this.config.allowHidden) {
|
|
323
|
+
for (const part of pathParts) {
|
|
324
|
+
if (part.startsWith('.') && part !== '.' && part !== '..') {
|
|
325
|
+
errors.push('Hidden files/directories are not allowed');
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const basename = path.basename(resolvedPath);
|
|
331
|
+
if (this.config.blockedNames.includes(basename)) {
|
|
332
|
+
errors.push(`File name "${basename}" is blocked`);
|
|
333
|
+
}
|
|
334
|
+
const ext = path.extname(resolvedPath).toLowerCase();
|
|
335
|
+
if (this.config.blockedExtensions.includes(ext)) {
|
|
336
|
+
errors.push(`File extension "${ext}" is blocked`);
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
isValid: errors.length === 0,
|
|
340
|
+
resolvedPath,
|
|
341
|
+
relativePath,
|
|
342
|
+
matchedPrefix,
|
|
343
|
+
errors,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Securely joins path segments within allowed directories.
|
|
348
|
+
*
|
|
349
|
+
* @param prefix - Base directory (must be in allowedPrefixes)
|
|
350
|
+
* @param segments - Path segments to join
|
|
351
|
+
* @returns Validated resolved path
|
|
352
|
+
*/
|
|
353
|
+
async securePath(prefix, ...segments) {
|
|
354
|
+
// Join the segments
|
|
355
|
+
const joined = path.join(prefix, ...segments);
|
|
356
|
+
// Validate the result
|
|
357
|
+
return this.validateOrThrow(joined);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Adds a prefix to the allowed list at runtime.
|
|
361
|
+
*
|
|
362
|
+
* @param prefix - Prefix to add
|
|
363
|
+
*/
|
|
364
|
+
addPrefix(prefix) {
|
|
365
|
+
const resolved = path.resolve(prefix);
|
|
366
|
+
if (!this.resolvedPrefixes.includes(resolved)) {
|
|
367
|
+
this.config.allowedPrefixes.push(prefix);
|
|
368
|
+
this.resolvedPrefixes.push(resolved);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Returns the current allowed prefixes.
|
|
373
|
+
*/
|
|
374
|
+
getAllowedPrefixes() {
|
|
375
|
+
return [...this.resolvedPrefixes];
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Checks if a path is within allowed prefixes (quick check).
|
|
379
|
+
*/
|
|
380
|
+
isWithinAllowed(inputPath) {
|
|
381
|
+
try {
|
|
382
|
+
const resolved = path.resolve(inputPath);
|
|
383
|
+
return this.resolvedPrefixes.some(prefix => resolved === prefix || resolved.startsWith(prefix + path.sep));
|
|
384
|
+
}
|
|
385
|
+
catch {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Factory function to create a path validator for a project directory.
|
|
392
|
+
*
|
|
393
|
+
* @param projectRoot - Root directory of the project
|
|
394
|
+
* @returns Configured PathValidator
|
|
395
|
+
*/
|
|
396
|
+
export function createProjectPathValidator(projectRoot) {
|
|
397
|
+
const srcDir = path.join(projectRoot, 'src');
|
|
398
|
+
const testDir = path.join(projectRoot, 'tests');
|
|
399
|
+
const docsDir = path.join(projectRoot, 'docs');
|
|
400
|
+
return new PathValidator({
|
|
401
|
+
allowedPrefixes: [srcDir, testDir, docsDir],
|
|
402
|
+
allowHidden: false,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Factory function to create a path validator for the entire project.
|
|
407
|
+
*
|
|
408
|
+
* @param projectRoot - Root directory of the project
|
|
409
|
+
* @returns Configured PathValidator
|
|
410
|
+
*/
|
|
411
|
+
export function createFullProjectPathValidator(projectRoot) {
|
|
412
|
+
return new PathValidator({
|
|
413
|
+
allowedPrefixes: [projectRoot],
|
|
414
|
+
allowHidden: true, // Allow .gitignore, etc.
|
|
415
|
+
blockedNames: [
|
|
416
|
+
...DEFAULT_BLOCKED_NAMES,
|
|
417
|
+
'node_modules', // Block access to node_modules
|
|
418
|
+
],
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
//# sourceMappingURL=path-validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-validator.js","sourceRoot":"","sources":["../src/path-validator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAsDlC,MA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AC,QAAQ,KAAK,MAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CACxE,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CAAC,WAAmB;IAC5D,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAC7C,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IAE/C,OAAO,IAAI,aAAa,CAAC;QACvB,eAAe,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC;QAC3C,WAAW,EAAE,KAAK;KACnB,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,8BAA8B,CAAC,WAAmB;IAChE,OAAO,IAAI,aAAa,CAAC;QACvB,eAAe,EAAE,CAAC,WAAW,CAAC;QAC9B,WAAW,EAAE,IAAI,EAAE,yBAAyB;QAC5C,YAAY,EAAE;YACZ,GAAG,qBAAqB;YACxB,cAAc,EAAE,+BAA+B;SAChD;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe Executor - HIGH-1 Remediation
|
|
3
|
+
*
|
|
4
|
+
* Fixes command injection vulnerabilities by:
|
|
5
|
+
* - Using execFile instead of exec with shell
|
|
6
|
+
* - Validating all command arguments
|
|
7
|
+
* - Implementing command allowlist
|
|
8
|
+
* - Sanitizing command inputs
|
|
9
|
+
*
|
|
10
|
+
* Security Properties:
|
|
11
|
+
* - No shell interpretation
|
|
12
|
+
* - Argument validation
|
|
13
|
+
* - Command allowlist enforcement
|
|
14
|
+
* - Timeout controls
|
|
15
|
+
* - Resource limits
|
|
16
|
+
*
|
|
17
|
+
* @module v3/security/safe-executor
|
|
18
|
+
*/
|
|
19
|
+
import { ChildProcess } from 'child_process';
|
|
20
|
+
export interface ExecutorConfig {
|
|
21
|
+
/**
|
|
22
|
+
* Allowed commands (allowlist).
|
|
23
|
+
* Only commands in this list can be executed.
|
|
24
|
+
*/
|
|
25
|
+
allowedCommands: string[];
|
|
26
|
+
/**
|
|
27
|
+
* Blocked argument patterns (regex strings).
|
|
28
|
+
* Arguments matching these patterns are rejected.
|
|
29
|
+
*/
|
|
30
|
+
blockedPatterns?: string[];
|
|
31
|
+
/**
|
|
32
|
+
* Maximum execution timeout in milliseconds.
|
|
33
|
+
* Default: 30000 (30 seconds)
|
|
34
|
+
*/
|
|
35
|
+
timeout?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Maximum buffer size for stdout/stderr.
|
|
38
|
+
* Default: 10MB
|
|
39
|
+
*/
|
|
40
|
+
maxBuffer?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Working directory for command execution.
|
|
43
|
+
* Default: process.cwd()
|
|
44
|
+
*/
|
|
45
|
+
cwd?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Environment variables to include.
|
|
48
|
+
* Default: process.env
|
|
49
|
+
*/
|
|
50
|
+
env?: NodeJS.ProcessEnv;
|
|
51
|
+
/**
|
|
52
|
+
* Whether to allow sudo commands.
|
|
53
|
+
* Default: false
|
|
54
|
+
*/
|
|
55
|
+
allowSudo?: boolean;
|
|
56
|
+
}
|
|
57
|
+
export interface ExecutionResult {
|
|
58
|
+
stdout: string;
|
|
59
|
+
stderr: string;
|
|
60
|
+
exitCode: number;
|
|
61
|
+
command: string;
|
|
62
|
+
args: string[];
|
|
63
|
+
duration: number;
|
|
64
|
+
}
|
|
65
|
+
export interface StreamingExecutor {
|
|
66
|
+
process: ChildProcess;
|
|
67
|
+
stdout: NodeJS.ReadableStream | null;
|
|
68
|
+
stderr: NodeJS.ReadableStream | null;
|
|
69
|
+
promise: Promise<ExecutionResult>;
|
|
70
|
+
}
|
|
71
|
+
export declare class SafeExecutorError extends Error {
|
|
72
|
+
readonly code: string;
|
|
73
|
+
readonly command?: string | undefined;
|
|
74
|
+
readonly args?: string[] | undefined;
|
|
75
|
+
constructor(message: string, code: string, command?: string | undefined, args?: string[] | undefined);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Safe command executor that prevents command injection.
|
|
79
|
+
*
|
|
80
|
+
* This class replaces unsafe exec() and spawn({shell: true}) calls
|
|
81
|
+
* with validated execFile() calls.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const executor = new SafeExecutor({
|
|
86
|
+
* allowedCommands: ['git', 'npm', 'node']
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* const result = await executor.execute('git', ['status']);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare class SafeExecutor {
|
|
93
|
+
private readonly config;
|
|
94
|
+
private readonly blockedPatterns;
|
|
95
|
+
constructor(config: ExecutorConfig);
|
|
96
|
+
/**
|
|
97
|
+
* Escapes special regex characters.
|
|
98
|
+
*/
|
|
99
|
+
private escapeRegExp;
|
|
100
|
+
/**
|
|
101
|
+
* Validates executor configuration.
|
|
102
|
+
*/
|
|
103
|
+
private validateConfig;
|
|
104
|
+
/**
|
|
105
|
+
* Validates a command against the allowlist.
|
|
106
|
+
*
|
|
107
|
+
* @param command - Command to validate
|
|
108
|
+
* @throws SafeExecutorError if command is not allowed
|
|
109
|
+
*/
|
|
110
|
+
private validateCommand;
|
|
111
|
+
/**
|
|
112
|
+
* Validates command arguments for injection patterns.
|
|
113
|
+
*
|
|
114
|
+
* @param args - Arguments to validate
|
|
115
|
+
* @throws SafeExecutorError if arguments contain dangerous patterns
|
|
116
|
+
*/
|
|
117
|
+
private validateArguments;
|
|
118
|
+
/**
|
|
119
|
+
* Sanitizes a single argument.
|
|
120
|
+
*
|
|
121
|
+
* @param arg - Argument to sanitize
|
|
122
|
+
* @returns Sanitized argument
|
|
123
|
+
*/
|
|
124
|
+
sanitizeArgument(arg: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Executes a command safely.
|
|
127
|
+
*
|
|
128
|
+
* @param command - Command to execute (must be in allowlist)
|
|
129
|
+
* @param args - Command arguments
|
|
130
|
+
* @returns Execution result
|
|
131
|
+
* @throws SafeExecutorError on validation failure or execution error
|
|
132
|
+
*/
|
|
133
|
+
execute(command: string, args?: string[]): Promise<ExecutionResult>;
|
|
134
|
+
/**
|
|
135
|
+
* Executes a command with streaming output.
|
|
136
|
+
*
|
|
137
|
+
* @param command - Command to execute
|
|
138
|
+
* @param args - Command arguments
|
|
139
|
+
* @returns Streaming executor with process handles
|
|
140
|
+
*/
|
|
141
|
+
executeStreaming(command: string, args?: string[]): StreamingExecutor;
|
|
142
|
+
/**
|
|
143
|
+
* Adds a command to the allowlist at runtime.
|
|
144
|
+
*
|
|
145
|
+
* @param command - Command to add
|
|
146
|
+
*/
|
|
147
|
+
allowCommand(command: string): void;
|
|
148
|
+
/**
|
|
149
|
+
* Checks if a command is allowed.
|
|
150
|
+
*
|
|
151
|
+
* @param command - Command to check
|
|
152
|
+
* @returns True if command is allowed
|
|
153
|
+
*/
|
|
154
|
+
isCommandAllowed(command: string): boolean;
|
|
155
|
+
/**
|
|
156
|
+
* Returns the current allowlist.
|
|
157
|
+
*/
|
|
158
|
+
getAllowedCommands(): readonly string[];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Factory function to create a safe executor for common development tasks.
|
|
162
|
+
*
|
|
163
|
+
* @returns Configured SafeExecutor for git, npm, and node
|
|
164
|
+
*/
|
|
165
|
+
export declare function createDevelopmentExecutor(): SafeExecutor;
|
|
166
|
+
/**
|
|
167
|
+
* Factory function to create a read-only executor.
|
|
168
|
+
* Only allows commands that read without modifying.
|
|
169
|
+
*
|
|
170
|
+
* @returns Configured SafeExecutor for read operations
|
|
171
|
+
*/
|
|
172
|
+
export declare function createReadOnlyExecutor(): SafeExecutor;
|
|
173
|
+
//# sourceMappingURL=safe-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-executor.d.ts","sourceRoot":"","sources":["../src/safe-executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAmB,YAAY,EAAE,MAAM,eAAe,CAAC;AAM9D,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,eAAe,EAAE,MAAM,EAAE,CAAC;IAE1B;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAE3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IAExB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,YAAY,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC,cAAc,GAAG,IAAI,CAAC;IACrC,MAAM,EAAE,MAAM,CAAC,cAAc,GAAG,IAAI,CAAC;IACrC,OAAO,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;CACnC;AAED,qBAAa,iBAAkB,SAAQ,KAAK;aAGxB,IAAI,EAAE,MAAM;aACZ,OAAO,CAAC,EAAE,MAAM;aAChB,IAAI,CAA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