UNPKG

ailock

Version:

AI-Proof File Guard - Protect sensitive files from accidental AI modifications

100 lines 3.25 kB
import { SecurityValidator } from './validators/SecurityValidator.js'; import { GlobValidator } from './validators/GlobValidator.js'; import { PathSanitizer } from './validators/PathSanitizer.js'; /** * Unified path validation combining security, glob, and sanitization features * This is a facade that maintains backward compatibility while delegating to specialized validators */ export class SecurePathValidator { securityValidator; globValidator; pathSanitizer; constructor(allowedRootDirs = []) { this.securityValidator = new SecurityValidator(allowedRootDirs); this.globValidator = new GlobValidator(); this.pathSanitizer = new PathSanitizer(); } /** * Validates and sanitizes a file path to prevent security vulnerabilities */ async validateAndSanitizePath(inputPath, rootDir) { // First sanitize the path const sanitized = this.pathSanitizer.sanitizePath(inputPath); // Then validate security this.securityValidator.validatePathSecurity(sanitized); // Resolve the path const resolved = this.pathSanitizer.resolvePath(sanitized, rootDir); // Check if path is allowed if (!this.securityValidator.isPathAllowed(resolved)) { throw new Error(`Path outside allowed directory: ${inputPath}`); } return resolved; } /** * Validate path type (file or directory) */ async validatePathType(filePath, expectedType) { await this.pathSanitizer.validatePathType(filePath, expectedType); } /** * Validate file access permissions */ async validateFileAccess(filePath, mode) { await this.pathSanitizer.validateFileAccess(filePath, mode); } /** * Find files matching glob patterns */ async findMatchingFiles(patterns, options) { return await this.globValidator.findMatchingFiles(patterns, options); } /** * Check if a path matches any of the given patterns */ matchesPattern(filePath, patterns) { return this.globValidator.matchesPattern(filePath, patterns); } /** * Load patterns from a gitignore-style file */ loadPatternsFromFile(filePath) { return this.globValidator.loadPatternsFromFile(filePath); } /** * Create an ignore filter from patterns */ createIgnoreFilter(patterns) { return this.globValidator.createIgnoreFilter(patterns); } /** * Get allowed root directories */ getAllowedRootDirs() { return this.securityValidator.getAllowedRootDirs(); } /** * Parse a path into its components */ parsePath(inputPath) { return this.pathSanitizer.parsePath(inputPath); } /** * Join path segments safely */ joinPath(...segments) { return this.pathSanitizer.joinPath(...segments); } /** * Get relative path between two paths */ getRelativePath(from, to) { return this.pathSanitizer.getRelativePath(from, to); } /** * Check if a path is absolute */ isAbsolutePath(inputPath) { return this.pathSanitizer.isAbsolutePath(inputPath); } } //# sourceMappingURL=PathValidator.js.map