@dexto/tools-filesystem 1.8.12 → 1.9.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/dist/directory-approval.cjs +53 -48
- package/dist/directory-approval.d.ts +3 -5
- package/dist/directory-approval.d.ts.map +1 -1
- package/dist/directory-approval.integration.test.cjs +125 -103
- package/dist/directory-approval.integration.test.js +125 -104
- package/dist/directory-approval.js +51 -48
- package/dist/filesystem-service.cjs +16 -13
- package/dist/filesystem-service.d.ts +2 -1
- package/dist/filesystem-service.d.ts.map +1 -1
- package/dist/filesystem-service.js +16 -13
- package/dist/index.d.cts +109 -108
- package/dist/path-validator.cjs +10 -10
- package/dist/path-validator.d.ts +4 -3
- package/dist/path-validator.d.ts.map +1 -1
- package/dist/path-validator.js +10 -10
- package/dist/path-validator.test.cjs +30 -16
- package/dist/path-validator.test.js +30 -16
- package/dist/tool-factory.cjs +7 -1
- package/dist/tool-factory.d.ts.map +1 -1
- package/dist/tool-factory.js +7 -1
- package/package.json +3 -3
|
@@ -127,7 +127,7 @@ class FileSystemService {
|
|
|
127
127
|
return this.pathValidator.isPathWithinAllowed(filePath);
|
|
128
128
|
}
|
|
129
129
|
async validateReadPath(filePath, mode) {
|
|
130
|
-
const validation = mode === "toolPreview" ? await this.pathValidator.validatePathForPreview(filePath) : await this.pathValidator.validatePath(filePath);
|
|
130
|
+
const validation = mode === "toolPreview" ? await this.pathValidator.validatePathForPreview(filePath) : await this.pathValidator.validatePath(filePath, "read");
|
|
131
131
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
132
132
|
throw FileSystemError.invalidPath(filePath, validation.error || "Unknown error");
|
|
133
133
|
}
|
|
@@ -299,7 +299,7 @@ class FileSystemService {
|
|
|
299
299
|
});
|
|
300
300
|
const validFiles = [];
|
|
301
301
|
for (const file of files) {
|
|
302
|
-
const validation = await this.pathValidator.validatePath(file);
|
|
302
|
+
const validation = await this.pathValidator.validatePath(file, "read");
|
|
303
303
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
304
304
|
this.logger.debug(`Skipping invalid path: ${file}`);
|
|
305
305
|
continue;
|
|
@@ -348,7 +348,7 @@ class FileSystemService {
|
|
|
348
348
|
*/
|
|
349
349
|
async listDirectory(dirPath, options = {}) {
|
|
350
350
|
await this.ensureInitialized();
|
|
351
|
-
const validation = await this.pathValidator.validatePath(dirPath);
|
|
351
|
+
const validation = await this.pathValidator.validatePath(dirPath, "read");
|
|
352
352
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
353
353
|
throw FileSystemError.invalidPath(dirPath, validation.error || "Unknown error");
|
|
354
354
|
}
|
|
@@ -387,7 +387,10 @@ class FileSystemService {
|
|
|
387
387
|
concurrency,
|
|
388
388
|
async (entry) => {
|
|
389
389
|
const entryPath = path.join(normalizedPath, entry.name);
|
|
390
|
-
const entryValidation = await this.pathValidator.validatePath(
|
|
390
|
+
const entryValidation = await this.pathValidator.validatePath(
|
|
391
|
+
entryPath,
|
|
392
|
+
"read"
|
|
393
|
+
);
|
|
391
394
|
if (!entryValidation.isValid || !entryValidation.normalizedPath) {
|
|
392
395
|
return null;
|
|
393
396
|
}
|
|
@@ -498,7 +501,7 @@ class FileSystemService {
|
|
|
498
501
|
*/
|
|
499
502
|
async createDirectory(dirPath, options = {}) {
|
|
500
503
|
await this.ensureInitialized();
|
|
501
|
-
const validation = await this.pathValidator.validatePath(dirPath);
|
|
504
|
+
const validation = await this.pathValidator.validatePath(dirPath, "write");
|
|
502
505
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
503
506
|
throw FileSystemError.invalidPath(dirPath, validation.error || "Unknown error");
|
|
504
507
|
}
|
|
@@ -533,7 +536,7 @@ class FileSystemService {
|
|
|
533
536
|
*/
|
|
534
537
|
async deletePath(targetPath, options = {}) {
|
|
535
538
|
await this.ensureInitialized();
|
|
536
|
-
const validation = await this.pathValidator.validatePath(targetPath);
|
|
539
|
+
const validation = await this.pathValidator.validatePath(targetPath, "write");
|
|
537
540
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
538
541
|
throw FileSystemError.invalidPath(targetPath, validation.error || "Unknown error");
|
|
539
542
|
}
|
|
@@ -560,11 +563,11 @@ class FileSystemService {
|
|
|
560
563
|
*/
|
|
561
564
|
async renamePath(fromPath, toPath) {
|
|
562
565
|
await this.ensureInitialized();
|
|
563
|
-
const fromValidation = await this.pathValidator.validatePath(fromPath);
|
|
566
|
+
const fromValidation = await this.pathValidator.validatePath(fromPath, "write");
|
|
564
567
|
if (!fromValidation.isValid || !fromValidation.normalizedPath) {
|
|
565
568
|
throw FileSystemError.invalidPath(fromPath, fromValidation.error || "Unknown error");
|
|
566
569
|
}
|
|
567
|
-
const toValidation = await this.pathValidator.validatePath(toPath);
|
|
570
|
+
const toValidation = await this.pathValidator.validatePath(toPath, "write");
|
|
568
571
|
if (!toValidation.isValid || !toValidation.normalizedPath) {
|
|
569
572
|
throw FileSystemError.invalidPath(toPath, toValidation.error || "Unknown error");
|
|
570
573
|
}
|
|
@@ -619,7 +622,7 @@ class FileSystemService {
|
|
|
619
622
|
const basePath = options.path || this.config.workingDirectory || process.cwd();
|
|
620
623
|
let searchPath;
|
|
621
624
|
let globPattern;
|
|
622
|
-
const baseValidation = await this.pathValidator.validatePath(basePath);
|
|
625
|
+
const baseValidation = await this.pathValidator.validatePath(basePath, "read");
|
|
623
626
|
if (!baseValidation.isValid || !baseValidation.normalizedPath) {
|
|
624
627
|
throw FileSystemError.invalidPath(basePath, baseValidation.error || "Unknown error");
|
|
625
628
|
}
|
|
@@ -721,7 +724,7 @@ class FileSystemService {
|
|
|
721
724
|
*/
|
|
722
725
|
async writeFile(filePath, content, options = {}) {
|
|
723
726
|
await this.ensureInitialized();
|
|
724
|
-
const validation = await this.pathValidator.validatePath(filePath);
|
|
727
|
+
const validation = await this.pathValidator.validatePath(filePath, "write");
|
|
725
728
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
726
729
|
throw FileSystemError.invalidPath(filePath, validation.error || "Unknown error");
|
|
727
730
|
}
|
|
@@ -761,12 +764,12 @@ class FileSystemService {
|
|
|
761
764
|
*/
|
|
762
765
|
async editFile(filePath, operation, options = {}) {
|
|
763
766
|
await this.ensureInitialized();
|
|
764
|
-
const validation = await this.pathValidator.validatePath(filePath);
|
|
767
|
+
const validation = await this.pathValidator.validatePath(filePath, "edit");
|
|
765
768
|
if (!validation.isValid || !validation.normalizedPath) {
|
|
766
769
|
throw FileSystemError.invalidPath(filePath, validation.error || "Unknown error");
|
|
767
770
|
}
|
|
768
771
|
const normalizedPath = validation.normalizedPath;
|
|
769
|
-
const fileContent = await this.
|
|
772
|
+
const fileContent = await this.readNormalizedFile(normalizedPath);
|
|
770
773
|
const originalContent = fileContent.content;
|
|
771
774
|
const occurrences = (originalContent.match(
|
|
772
775
|
new RegExp(operation.oldString.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g")
|
|
@@ -894,7 +897,7 @@ class FileSystemService {
|
|
|
894
897
|
* Check if a path is allowed (async for non-blocking symlink resolution)
|
|
895
898
|
*/
|
|
896
899
|
async isPathAllowed(filePath) {
|
|
897
|
-
const validation = await this.pathValidator.validatePath(filePath);
|
|
900
|
+
const validation = await this.pathValidator.validatePath(filePath, "read");
|
|
898
901
|
return validation.isValid;
|
|
899
902
|
}
|
|
900
903
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -297,6 +297,114 @@ interface FileSystemConfig {
|
|
|
297
297
|
workingDirectory?: string | undefined;
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
+
/**
|
|
301
|
+
* Path Validator
|
|
302
|
+
*
|
|
303
|
+
* Security-focused path validation for file system operations
|
|
304
|
+
*/
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Callback type for checking if a path is in an approved directory.
|
|
308
|
+
* Used to consult ApprovalManager without creating a direct dependency.
|
|
309
|
+
*/
|
|
310
|
+
type DirectoryApprovalOperation = 'read' | 'write' | 'edit';
|
|
311
|
+
type DirectoryApprovalChecker = (filePath: string, operation: DirectoryApprovalOperation) => boolean;
|
|
312
|
+
/**
|
|
313
|
+
* PathValidator - Validates file paths for security and policy compliance
|
|
314
|
+
*
|
|
315
|
+
* Security checks:
|
|
316
|
+
* 1. Path traversal detection (../, symbolic links)
|
|
317
|
+
* 2. Allowed paths enforcement (whitelist + approved directories)
|
|
318
|
+
* 3. Blocked paths detection (blacklist)
|
|
319
|
+
* 4. File extension restrictions
|
|
320
|
+
* 5. Absolute path normalization
|
|
321
|
+
*
|
|
322
|
+
* PathValidator can optionally consult an external approval checker (e.g., ApprovalManager)
|
|
323
|
+
* to determine if paths outside the config's allowed paths are accessible.
|
|
324
|
+
*/
|
|
325
|
+
declare class PathValidator {
|
|
326
|
+
private config;
|
|
327
|
+
private normalizedAllowedPaths;
|
|
328
|
+
private normalizedBlockedPaths;
|
|
329
|
+
private normalizedBlockedExtensions;
|
|
330
|
+
private logger;
|
|
331
|
+
private directoryApprovalChecker;
|
|
332
|
+
constructor(config: FileSystemConfig, logger: Logger);
|
|
333
|
+
/**
|
|
334
|
+
* Set a callback to check if a path is in an approved directory.
|
|
335
|
+
* This allows PathValidator to consult ApprovalManager without a direct dependency.
|
|
336
|
+
*
|
|
337
|
+
* @param checker Function that returns true if path is in an approved directory
|
|
338
|
+
*/
|
|
339
|
+
setDirectoryApprovalChecker(checker: DirectoryApprovalChecker): void;
|
|
340
|
+
/**
|
|
341
|
+
* Validate a file path for security and policy compliance
|
|
342
|
+
*/
|
|
343
|
+
validatePath(filePath: string, operation: DirectoryApprovalOperation): Promise<PathValidation>;
|
|
344
|
+
/**
|
|
345
|
+
* Validate a file path for preview purposes.
|
|
346
|
+
*
|
|
347
|
+
* This is identical to {@link validatePath} except it does NOT enforce config-allowed roots.
|
|
348
|
+
* It still enforces:
|
|
349
|
+
* - traversal protection
|
|
350
|
+
* - blocked paths (absolute blocked paths only; relative blocked paths are resolved against
|
|
351
|
+
* config-allowed roots and may not match paths outside those roots)
|
|
352
|
+
* - blocked extensions
|
|
353
|
+
*
|
|
354
|
+
* Used for generating UI-only previews (e.g., diffs) before the user approves directory access.
|
|
355
|
+
*/
|
|
356
|
+
validatePathForPreview(filePath: string): Promise<PathValidation>;
|
|
357
|
+
private validatePathInternal;
|
|
358
|
+
/**
|
|
359
|
+
* Check if path contains traversal attempts
|
|
360
|
+
*/
|
|
361
|
+
private hasPathTraversal;
|
|
362
|
+
/**
|
|
363
|
+
* Check if path is within allowed paths (whitelist check)
|
|
364
|
+
* Also consults the directory approval checker if configured.
|
|
365
|
+
* Uses the sync version since the path is already normalized at this point.
|
|
366
|
+
*/
|
|
367
|
+
private isPathAllowed;
|
|
368
|
+
/**
|
|
369
|
+
* Check if path matches blocked patterns (blacklist check)
|
|
370
|
+
*/
|
|
371
|
+
private isPathBlocked;
|
|
372
|
+
/**
|
|
373
|
+
* Quick check if a path is allowed (for internal use)
|
|
374
|
+
* Note: This assumes the path is already normalized/canonicalized
|
|
375
|
+
*/
|
|
376
|
+
isPathAllowedQuick(normalizedPath: string, operation: DirectoryApprovalOperation): boolean;
|
|
377
|
+
/**
|
|
378
|
+
* Synchronous path allowed check (for already-normalized paths)
|
|
379
|
+
* This is used internally when we already have a canonicalized path
|
|
380
|
+
*/
|
|
381
|
+
private isPathAllowedSync;
|
|
382
|
+
/**
|
|
383
|
+
* Check if a file path is within the configured allowed paths (from config only).
|
|
384
|
+
* This method does NOT consult ApprovalManager - it only checks the static config paths.
|
|
385
|
+
*
|
|
386
|
+
* This is used by file tools to determine if a path needs directory approval.
|
|
387
|
+
* Paths within config-allowed directories don't need directory approval prompts.
|
|
388
|
+
*
|
|
389
|
+
* @param filePath The file path to check (can be relative or absolute)
|
|
390
|
+
* @returns true if the path is within config-allowed paths, false otherwise
|
|
391
|
+
*/
|
|
392
|
+
isPathWithinAllowed(filePath: string): Promise<boolean>;
|
|
393
|
+
/**
|
|
394
|
+
* Check if path is within config-allowed paths only (no approval checker).
|
|
395
|
+
* Used for prompting decisions.
|
|
396
|
+
*/
|
|
397
|
+
private isInConfigAllowedPaths;
|
|
398
|
+
/**
|
|
399
|
+
* Get normalized allowed paths
|
|
400
|
+
*/
|
|
401
|
+
getAllowedPaths(): string[];
|
|
402
|
+
/**
|
|
403
|
+
* Get blocked paths
|
|
404
|
+
*/
|
|
405
|
+
getBlockedPaths(): string[];
|
|
406
|
+
}
|
|
407
|
+
|
|
300
408
|
/**
|
|
301
409
|
* FileSystem Service
|
|
302
410
|
*
|
|
@@ -358,7 +466,7 @@ declare class FileSystemService {
|
|
|
358
466
|
*
|
|
359
467
|
* @param checker Function that returns true if path is in an approved directory
|
|
360
468
|
*/
|
|
361
|
-
setDirectoryApprovalChecker(checker: (filePath: string) => boolean): void;
|
|
469
|
+
setDirectoryApprovalChecker(checker: (filePath: string, operation: DirectoryApprovalOperation) => boolean): void;
|
|
362
470
|
/**
|
|
363
471
|
* Update the working directory at runtime (e.g., when workspace changes).
|
|
364
472
|
* Rebuilds the PathValidator so allowed/blocked path roots are recalculated.
|
|
@@ -454,113 +562,6 @@ declare class FileSystemService {
|
|
|
454
562
|
*/
|
|
455
563
|
type FileSystemServiceGetter = (context: ToolExecutionContext) => Promise<FileSystemService>;
|
|
456
564
|
|
|
457
|
-
/**
|
|
458
|
-
* Path Validator
|
|
459
|
-
*
|
|
460
|
-
* Security-focused path validation for file system operations
|
|
461
|
-
*/
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* Callback type for checking if a path is in an approved directory.
|
|
465
|
-
* Used to consult ApprovalManager without creating a direct dependency.
|
|
466
|
-
*/
|
|
467
|
-
type DirectoryApprovalChecker = (filePath: string) => boolean;
|
|
468
|
-
/**
|
|
469
|
-
* PathValidator - Validates file paths for security and policy compliance
|
|
470
|
-
*
|
|
471
|
-
* Security checks:
|
|
472
|
-
* 1. Path traversal detection (../, symbolic links)
|
|
473
|
-
* 2. Allowed paths enforcement (whitelist + approved directories)
|
|
474
|
-
* 3. Blocked paths detection (blacklist)
|
|
475
|
-
* 4. File extension restrictions
|
|
476
|
-
* 5. Absolute path normalization
|
|
477
|
-
*
|
|
478
|
-
* PathValidator can optionally consult an external approval checker (e.g., ApprovalManager)
|
|
479
|
-
* to determine if paths outside the config's allowed paths are accessible.
|
|
480
|
-
*/
|
|
481
|
-
declare class PathValidator {
|
|
482
|
-
private config;
|
|
483
|
-
private normalizedAllowedPaths;
|
|
484
|
-
private normalizedBlockedPaths;
|
|
485
|
-
private normalizedBlockedExtensions;
|
|
486
|
-
private logger;
|
|
487
|
-
private directoryApprovalChecker;
|
|
488
|
-
constructor(config: FileSystemConfig, logger: Logger);
|
|
489
|
-
/**
|
|
490
|
-
* Set a callback to check if a path is in an approved directory.
|
|
491
|
-
* This allows PathValidator to consult ApprovalManager without a direct dependency.
|
|
492
|
-
*
|
|
493
|
-
* @param checker Function that returns true if path is in an approved directory
|
|
494
|
-
*/
|
|
495
|
-
setDirectoryApprovalChecker(checker: DirectoryApprovalChecker): void;
|
|
496
|
-
/**
|
|
497
|
-
* Validate a file path for security and policy compliance
|
|
498
|
-
*/
|
|
499
|
-
validatePath(filePath: string): Promise<PathValidation>;
|
|
500
|
-
/**
|
|
501
|
-
* Validate a file path for preview purposes.
|
|
502
|
-
*
|
|
503
|
-
* This is identical to {@link validatePath} except it does NOT enforce config-allowed roots.
|
|
504
|
-
* It still enforces:
|
|
505
|
-
* - traversal protection
|
|
506
|
-
* - blocked paths (absolute blocked paths only; relative blocked paths are resolved against
|
|
507
|
-
* config-allowed roots and may not match paths outside those roots)
|
|
508
|
-
* - blocked extensions
|
|
509
|
-
*
|
|
510
|
-
* Used for generating UI-only previews (e.g., diffs) before the user approves directory access.
|
|
511
|
-
*/
|
|
512
|
-
validatePathForPreview(filePath: string): Promise<PathValidation>;
|
|
513
|
-
private validatePathInternal;
|
|
514
|
-
/**
|
|
515
|
-
* Check if path contains traversal attempts
|
|
516
|
-
*/
|
|
517
|
-
private hasPathTraversal;
|
|
518
|
-
/**
|
|
519
|
-
* Check if path is within allowed paths (whitelist check)
|
|
520
|
-
* Also consults the directory approval checker if configured.
|
|
521
|
-
* Uses the sync version since the path is already normalized at this point.
|
|
522
|
-
*/
|
|
523
|
-
private isPathAllowed;
|
|
524
|
-
/**
|
|
525
|
-
* Check if path matches blocked patterns (blacklist check)
|
|
526
|
-
*/
|
|
527
|
-
private isPathBlocked;
|
|
528
|
-
/**
|
|
529
|
-
* Quick check if a path is allowed (for internal use)
|
|
530
|
-
* Note: This assumes the path is already normalized/canonicalized
|
|
531
|
-
*/
|
|
532
|
-
isPathAllowedQuick(normalizedPath: string): boolean;
|
|
533
|
-
/**
|
|
534
|
-
* Synchronous path allowed check (for already-normalized paths)
|
|
535
|
-
* This is used internally when we already have a canonicalized path
|
|
536
|
-
*/
|
|
537
|
-
private isPathAllowedSync;
|
|
538
|
-
/**
|
|
539
|
-
* Check if a file path is within the configured allowed paths (from config only).
|
|
540
|
-
* This method does NOT consult ApprovalManager - it only checks the static config paths.
|
|
541
|
-
*
|
|
542
|
-
* This is used by file tools to determine if a path needs directory approval.
|
|
543
|
-
* Paths within config-allowed directories don't need directory approval prompts.
|
|
544
|
-
*
|
|
545
|
-
* @param filePath The file path to check (can be relative or absolute)
|
|
546
|
-
* @returns true if the path is within config-allowed paths, false otherwise
|
|
547
|
-
*/
|
|
548
|
-
isPathWithinAllowed(filePath: string): Promise<boolean>;
|
|
549
|
-
/**
|
|
550
|
-
* Check if path is within config-allowed paths only (no approval checker).
|
|
551
|
-
* Used for prompting decisions.
|
|
552
|
-
*/
|
|
553
|
-
private isInConfigAllowedPaths;
|
|
554
|
-
/**
|
|
555
|
-
* Get normalized allowed paths
|
|
556
|
-
*/
|
|
557
|
-
getAllowedPaths(): string[];
|
|
558
|
-
/**
|
|
559
|
-
* Get blocked paths
|
|
560
|
-
*/
|
|
561
|
-
getBlockedPaths(): string[];
|
|
562
|
-
}
|
|
563
|
-
|
|
564
565
|
/**
|
|
565
566
|
* FileSystem Service Errors
|
|
566
567
|
*
|
package/dist/path-validator.cjs
CHANGED
|
@@ -72,8 +72,8 @@ class PathValidator {
|
|
|
72
72
|
/**
|
|
73
73
|
* Validate a file path for security and policy compliance
|
|
74
74
|
*/
|
|
75
|
-
async validatePath(filePath) {
|
|
76
|
-
return this.validatePathInternal(filePath, { skipAllowedCheck: false });
|
|
75
|
+
async validatePath(filePath, operation) {
|
|
76
|
+
return this.validatePathInternal(filePath, { skipAllowedCheck: false, operation });
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
79
|
* Validate a file path for preview purposes.
|
|
@@ -88,7 +88,7 @@ class PathValidator {
|
|
|
88
88
|
* Used for generating UI-only previews (e.g., diffs) before the user approves directory access.
|
|
89
89
|
*/
|
|
90
90
|
async validatePathForPreview(filePath) {
|
|
91
|
-
return this.validatePathInternal(filePath, { skipAllowedCheck: true });
|
|
91
|
+
return this.validatePathInternal(filePath, { skipAllowedCheck: true, operation: "read" });
|
|
92
92
|
}
|
|
93
93
|
async validatePathInternal(filePath, options) {
|
|
94
94
|
if (!filePath || filePath.trim() === "") {
|
|
@@ -128,7 +128,7 @@ class PathValidator {
|
|
|
128
128
|
error: "Symlink target escapes allowed paths"
|
|
129
129
|
};
|
|
130
130
|
}
|
|
131
|
-
if (!options.skipAllowedCheck && !this.isPathAllowed(normalizedPath)) {
|
|
131
|
+
if (!options.skipAllowedCheck && !this.isPathAllowed(normalizedPath, options.operation)) {
|
|
132
132
|
return {
|
|
133
133
|
isValid: false,
|
|
134
134
|
error: `Path is not within allowed paths. Allowed: ${this.normalizedAllowedPaths.join(", ")}`
|
|
@@ -174,8 +174,8 @@ class PathValidator {
|
|
|
174
174
|
* Also consults the directory approval checker if configured.
|
|
175
175
|
* Uses the sync version since the path is already normalized at this point.
|
|
176
176
|
*/
|
|
177
|
-
isPathAllowed(normalizedPath) {
|
|
178
|
-
return this.isPathAllowedSync(normalizedPath);
|
|
177
|
+
isPathAllowed(normalizedPath, operation) {
|
|
178
|
+
return this.isPathAllowedSync(normalizedPath, operation);
|
|
179
179
|
}
|
|
180
180
|
/**
|
|
181
181
|
* Check if path matches blocked patterns (blacklist check)
|
|
@@ -196,14 +196,14 @@ class PathValidator {
|
|
|
196
196
|
* Quick check if a path is allowed (for internal use)
|
|
197
197
|
* Note: This assumes the path is already normalized/canonicalized
|
|
198
198
|
*/
|
|
199
|
-
isPathAllowedQuick(normalizedPath) {
|
|
200
|
-
return this.isPathAllowedSync(normalizedPath) && !this.isPathBlocked(normalizedPath);
|
|
199
|
+
isPathAllowedQuick(normalizedPath, operation) {
|
|
200
|
+
return this.isPathAllowedSync(normalizedPath, operation) && !this.isPathBlocked(normalizedPath);
|
|
201
201
|
}
|
|
202
202
|
/**
|
|
203
203
|
* Synchronous path allowed check (for already-normalized paths)
|
|
204
204
|
* This is used internally when we already have a canonicalized path
|
|
205
205
|
*/
|
|
206
|
-
isPathAllowedSync(normalizedPath) {
|
|
206
|
+
isPathAllowedSync(normalizedPath, operation) {
|
|
207
207
|
if (this.normalizedAllowedPaths.length === 0) {
|
|
208
208
|
return true;
|
|
209
209
|
}
|
|
@@ -215,7 +215,7 @@ class PathValidator {
|
|
|
215
215
|
return true;
|
|
216
216
|
}
|
|
217
217
|
if (this.directoryApprovalChecker) {
|
|
218
|
-
return this.directoryApprovalChecker(normalizedPath);
|
|
218
|
+
return this.directoryApprovalChecker(normalizedPath, operation);
|
|
219
219
|
}
|
|
220
220
|
return false;
|
|
221
221
|
}
|
package/dist/path-validator.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ import type { Logger } from '@dexto/core/logger';
|
|
|
9
9
|
* Callback type for checking if a path is in an approved directory.
|
|
10
10
|
* Used to consult ApprovalManager without creating a direct dependency.
|
|
11
11
|
*/
|
|
12
|
-
export type
|
|
12
|
+
export type DirectoryApprovalOperation = 'read' | 'write' | 'edit';
|
|
13
|
+
export type DirectoryApprovalChecker = (filePath: string, operation: DirectoryApprovalOperation) => boolean;
|
|
13
14
|
/**
|
|
14
15
|
* PathValidator - Validates file paths for security and policy compliance
|
|
15
16
|
*
|
|
@@ -41,7 +42,7 @@ export declare class PathValidator {
|
|
|
41
42
|
/**
|
|
42
43
|
* Validate a file path for security and policy compliance
|
|
43
44
|
*/
|
|
44
|
-
validatePath(filePath: string): Promise<PathValidation>;
|
|
45
|
+
validatePath(filePath: string, operation: DirectoryApprovalOperation): Promise<PathValidation>;
|
|
45
46
|
/**
|
|
46
47
|
* Validate a file path for preview purposes.
|
|
47
48
|
*
|
|
@@ -74,7 +75,7 @@ export declare class PathValidator {
|
|
|
74
75
|
* Quick check if a path is allowed (for internal use)
|
|
75
76
|
* Note: This assumes the path is already normalized/canonicalized
|
|
76
77
|
*/
|
|
77
|
-
isPathAllowedQuick(normalizedPath: string): boolean;
|
|
78
|
+
isPathAllowedQuick(normalizedPath: string, operation: DirectoryApprovalOperation): boolean;
|
|
78
79
|
/**
|
|
79
80
|
* Synchronous path allowed check (for already-normalized paths)
|
|
80
81
|
* This is used internally when we already have a canonicalized path
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path-validator.d.ts","sourceRoot":"","sources":["../src/path-validator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAGjD;;;GAGG;AACH,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"path-validator.d.ts","sourceRoot":"","sources":["../src/path-validator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAGjD;;;GAGG;AACH,MAAM,MAAM,0BAA0B,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AACnE,MAAM,MAAM,wBAAwB,GAAG,CACnC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,0BAA0B,KACpC,OAAO,CAAC;AAEb;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;IACtB,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,sBAAsB,CAAW;IACzC,OAAO,CAAC,sBAAsB,CAAW;IACzC,OAAO,CAAC,2BAA2B,CAAW;IAC9C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,wBAAwB,CAAuC;gBAE3D,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM;IA0BpD;;;;;OAKG;IACH,2BAA2B,CAAC,OAAO,EAAE,wBAAwB,GAAG,IAAI;IAKpE;;OAEG;IACG,YAAY,CACd,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,0BAA0B,GACtC,OAAO,CAAC,cAAc,CAAC;IAI1B;;;;;;;;;;;OAWG;IACG,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;YAIzD,oBAAoB;IA0FlC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAgBxB;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAIrB;;OAEG;IACH,OAAO,CAAC,aAAa;IAyBrB;;;OAGG;IACH,kBAAkB,CAAC,cAAc,EAAE,MAAM,EAAE,SAAS,EAAE,0BAA0B,GAAG,OAAO;IAM1F;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IA4BzB;;;;;;;;;OASG;IACG,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA+B7D;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAY9B;;OAEG;IACH,eAAe,IAAI,MAAM,EAAE;IAI3B;;OAEG;IACH,eAAe,IAAI,MAAM,EAAE;CAG9B"}
|
package/dist/path-validator.js
CHANGED
|
@@ -39,8 +39,8 @@ class PathValidator {
|
|
|
39
39
|
/**
|
|
40
40
|
* Validate a file path for security and policy compliance
|
|
41
41
|
*/
|
|
42
|
-
async validatePath(filePath) {
|
|
43
|
-
return this.validatePathInternal(filePath, { skipAllowedCheck: false });
|
|
42
|
+
async validatePath(filePath, operation) {
|
|
43
|
+
return this.validatePathInternal(filePath, { skipAllowedCheck: false, operation });
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Validate a file path for preview purposes.
|
|
@@ -55,7 +55,7 @@ class PathValidator {
|
|
|
55
55
|
* Used for generating UI-only previews (e.g., diffs) before the user approves directory access.
|
|
56
56
|
*/
|
|
57
57
|
async validatePathForPreview(filePath) {
|
|
58
|
-
return this.validatePathInternal(filePath, { skipAllowedCheck: true });
|
|
58
|
+
return this.validatePathInternal(filePath, { skipAllowedCheck: true, operation: "read" });
|
|
59
59
|
}
|
|
60
60
|
async validatePathInternal(filePath, options) {
|
|
61
61
|
if (!filePath || filePath.trim() === "") {
|
|
@@ -95,7 +95,7 @@ class PathValidator {
|
|
|
95
95
|
error: "Symlink target escapes allowed paths"
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
|
-
if (!options.skipAllowedCheck && !this.isPathAllowed(normalizedPath)) {
|
|
98
|
+
if (!options.skipAllowedCheck && !this.isPathAllowed(normalizedPath, options.operation)) {
|
|
99
99
|
return {
|
|
100
100
|
isValid: false,
|
|
101
101
|
error: `Path is not within allowed paths. Allowed: ${this.normalizedAllowedPaths.join(", ")}`
|
|
@@ -141,8 +141,8 @@ class PathValidator {
|
|
|
141
141
|
* Also consults the directory approval checker if configured.
|
|
142
142
|
* Uses the sync version since the path is already normalized at this point.
|
|
143
143
|
*/
|
|
144
|
-
isPathAllowed(normalizedPath) {
|
|
145
|
-
return this.isPathAllowedSync(normalizedPath);
|
|
144
|
+
isPathAllowed(normalizedPath, operation) {
|
|
145
|
+
return this.isPathAllowedSync(normalizedPath, operation);
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Check if path matches blocked patterns (blacklist check)
|
|
@@ -163,14 +163,14 @@ class PathValidator {
|
|
|
163
163
|
* Quick check if a path is allowed (for internal use)
|
|
164
164
|
* Note: This assumes the path is already normalized/canonicalized
|
|
165
165
|
*/
|
|
166
|
-
isPathAllowedQuick(normalizedPath) {
|
|
167
|
-
return this.isPathAllowedSync(normalizedPath) && !this.isPathBlocked(normalizedPath);
|
|
166
|
+
isPathAllowedQuick(normalizedPath, operation) {
|
|
167
|
+
return this.isPathAllowedSync(normalizedPath, operation) && !this.isPathBlocked(normalizedPath);
|
|
168
168
|
}
|
|
169
169
|
/**
|
|
170
170
|
* Synchronous path allowed check (for already-normalized paths)
|
|
171
171
|
* This is used internally when we already have a canonicalized path
|
|
172
172
|
*/
|
|
173
|
-
isPathAllowedSync(normalizedPath) {
|
|
173
|
+
isPathAllowedSync(normalizedPath, operation) {
|
|
174
174
|
if (this.normalizedAllowedPaths.length === 0) {
|
|
175
175
|
return true;
|
|
176
176
|
}
|
|
@@ -182,7 +182,7 @@ class PathValidator {
|
|
|
182
182
|
return true;
|
|
183
183
|
}
|
|
184
184
|
if (this.directoryApprovalChecker) {
|
|
185
|
-
return this.directoryApprovalChecker(normalizedPath);
|
|
185
|
+
return this.directoryApprovalChecker(normalizedPath, operation);
|
|
186
186
|
}
|
|
187
187
|
return false;
|
|
188
188
|
}
|
|
@@ -53,7 +53,7 @@ const createMockLogger = () => ({
|
|
|
53
53
|
},
|
|
54
54
|
mockLogger
|
|
55
55
|
);
|
|
56
|
-
const result = await validator.validatePath("");
|
|
56
|
+
const result = await validator.validatePath("", "read");
|
|
57
57
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
58
58
|
(0, import_vitest.expect)(result.error).toBe("Path cannot be empty");
|
|
59
59
|
});
|
|
@@ -70,7 +70,7 @@ const createMockLogger = () => ({
|
|
|
70
70
|
},
|
|
71
71
|
mockLogger
|
|
72
72
|
);
|
|
73
|
-
const result = await validator.validatePath(" ");
|
|
73
|
+
const result = await validator.validatePath(" ", "read");
|
|
74
74
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
75
75
|
(0, import_vitest.expect)(result.error).toBe("Path cannot be empty");
|
|
76
76
|
});
|
|
@@ -89,7 +89,10 @@ const createMockLogger = () => ({
|
|
|
89
89
|
},
|
|
90
90
|
mockLogger
|
|
91
91
|
);
|
|
92
|
-
const result = await validator.validatePath(
|
|
92
|
+
const result = await validator.validatePath(
|
|
93
|
+
"/home/user/project/src/file.ts",
|
|
94
|
+
"read"
|
|
95
|
+
);
|
|
93
96
|
(0, import_vitest.expect)(result.isValid).toBe(true);
|
|
94
97
|
(0, import_vitest.expect)(result.normalizedPath).toBeDefined();
|
|
95
98
|
});
|
|
@@ -106,7 +109,7 @@ const createMockLogger = () => ({
|
|
|
106
109
|
},
|
|
107
110
|
mockLogger
|
|
108
111
|
);
|
|
109
|
-
const result = await validator.validatePath("src/file.ts");
|
|
112
|
+
const result = await validator.validatePath("src/file.ts", "read");
|
|
110
113
|
(0, import_vitest.expect)(result.isValid).toBe(true);
|
|
111
114
|
});
|
|
112
115
|
(0, import_vitest.it)("should expand home-directory shorthand before validation", async () => {
|
|
@@ -122,7 +125,7 @@ const createMockLogger = () => ({
|
|
|
122
125
|
},
|
|
123
126
|
mockLogger
|
|
124
127
|
);
|
|
125
|
-
const result = await validator.validatePath("~/notes/file.ts");
|
|
128
|
+
const result = await validator.validatePath("~/notes/file.ts", "read");
|
|
126
129
|
(0, import_vitest.expect)(result.isValid).toBe(true);
|
|
127
130
|
(0, import_vitest.expect)(result.normalizedPath).toBe(path.join(os.homedir(), "notes", "file.ts"));
|
|
128
131
|
});
|
|
@@ -139,7 +142,7 @@ const createMockLogger = () => ({
|
|
|
139
142
|
},
|
|
140
143
|
mockLogger
|
|
141
144
|
);
|
|
142
|
-
const result = await validator.validatePath("/external/project/file.ts");
|
|
145
|
+
const result = await validator.validatePath("/external/project/file.ts", "read");
|
|
143
146
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
144
147
|
(0, import_vitest.expect)(result.error).toContain("not within allowed paths");
|
|
145
148
|
});
|
|
@@ -156,7 +159,7 @@ const createMockLogger = () => ({
|
|
|
156
159
|
},
|
|
157
160
|
mockLogger
|
|
158
161
|
);
|
|
159
|
-
const result = await validator.validatePath("/anywhere/file.ts");
|
|
162
|
+
const result = await validator.validatePath("/anywhere/file.ts", "read");
|
|
160
163
|
(0, import_vitest.expect)(result.isValid).toBe(true);
|
|
161
164
|
});
|
|
162
165
|
});
|
|
@@ -175,7 +178,8 @@ const createMockLogger = () => ({
|
|
|
175
178
|
mockLogger
|
|
176
179
|
);
|
|
177
180
|
const result = await validator.validatePath(
|
|
178
|
-
"/home/user/project/../../../etc/passwd"
|
|
181
|
+
"/home/user/project/../../../etc/passwd",
|
|
182
|
+
"read"
|
|
179
183
|
);
|
|
180
184
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
181
185
|
(0, import_vitest.expect)(result.error).toBe("Path traversal detected");
|
|
@@ -195,7 +199,10 @@ const createMockLogger = () => ({
|
|
|
195
199
|
},
|
|
196
200
|
mockLogger
|
|
197
201
|
);
|
|
198
|
-
const result = await validator.validatePath(
|
|
202
|
+
const result = await validator.validatePath(
|
|
203
|
+
"/home/user/project/.git/config",
|
|
204
|
+
"read"
|
|
205
|
+
);
|
|
199
206
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
200
207
|
(0, import_vitest.expect)(result.error).toContain("blocked");
|
|
201
208
|
});
|
|
@@ -213,7 +220,8 @@ const createMockLogger = () => ({
|
|
|
213
220
|
mockLogger
|
|
214
221
|
);
|
|
215
222
|
const result = await validator.validatePath(
|
|
216
|
-
"/home/user/project/node_modules/lodash/index.js"
|
|
223
|
+
"/home/user/project/node_modules/lodash/index.js",
|
|
224
|
+
"read"
|
|
217
225
|
);
|
|
218
226
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
219
227
|
(0, import_vitest.expect)(result.error).toContain("blocked");
|
|
@@ -233,7 +241,10 @@ const createMockLogger = () => ({
|
|
|
233
241
|
},
|
|
234
242
|
mockLogger
|
|
235
243
|
);
|
|
236
|
-
const result = await validator.validatePath(
|
|
244
|
+
const result = await validator.validatePath(
|
|
245
|
+
"/home/user/project/malware.exe",
|
|
246
|
+
"read"
|
|
247
|
+
);
|
|
237
248
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
238
249
|
(0, import_vitest.expect)(result.error).toContain(".exe is not allowed");
|
|
239
250
|
});
|
|
@@ -251,7 +262,7 @@ const createMockLogger = () => ({
|
|
|
251
262
|
},
|
|
252
263
|
mockLogger
|
|
253
264
|
);
|
|
254
|
-
const result = await validator.validatePath("/home/user/project/file.exe");
|
|
265
|
+
const result = await validator.validatePath("/home/user/project/file.exe", "read");
|
|
255
266
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
256
267
|
});
|
|
257
268
|
(0, import_vitest.it)("should be case-insensitive for extensions", async () => {
|
|
@@ -267,7 +278,7 @@ const createMockLogger = () => ({
|
|
|
267
278
|
},
|
|
268
279
|
mockLogger
|
|
269
280
|
);
|
|
270
|
-
const result = await validator.validatePath("/home/user/project/file.EXE");
|
|
281
|
+
const result = await validator.validatePath("/home/user/project/file.EXE", "read");
|
|
271
282
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
272
283
|
});
|
|
273
284
|
});
|
|
@@ -285,13 +296,13 @@ const createMockLogger = () => ({
|
|
|
285
296
|
},
|
|
286
297
|
mockLogger
|
|
287
298
|
);
|
|
288
|
-
let result = await validator.validatePath("/external/project/file.ts");
|
|
299
|
+
let result = await validator.validatePath("/external/project/file.ts", "read");
|
|
289
300
|
(0, import_vitest.expect)(result.isValid).toBe(false);
|
|
290
301
|
const approvalChecker = (filePath) => {
|
|
291
302
|
return filePath.startsWith("/external/project");
|
|
292
303
|
};
|
|
293
304
|
validator.setDirectoryApprovalChecker(approvalChecker);
|
|
294
|
-
result = await validator.validatePath("/external/project/file.ts");
|
|
305
|
+
result = await validator.validatePath("/external/project/file.ts", "read");
|
|
295
306
|
(0, import_vitest.expect)(result.isValid).toBe(true);
|
|
296
307
|
});
|
|
297
308
|
(0, import_vitest.it)("should not use approval checker for config-allowed paths", async () => {
|
|
@@ -309,7 +320,10 @@ const createMockLogger = () => ({
|
|
|
309
320
|
mockLogger
|
|
310
321
|
);
|
|
311
322
|
validator.setDirectoryApprovalChecker(approvalChecker);
|
|
312
|
-
const result = await validator.validatePath(
|
|
323
|
+
const result = await validator.validatePath(
|
|
324
|
+
"/home/user/project/src/file.ts",
|
|
325
|
+
"read"
|
|
326
|
+
);
|
|
313
327
|
(0, import_vitest.expect)(result.isValid).toBe(true);
|
|
314
328
|
(0, import_vitest.expect)(approvalChecker).not.toHaveBeenCalled();
|
|
315
329
|
});
|