UNPKG

ailock

Version:

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

94 lines 2.99 kB
import { access, chmod, stat, constants } from 'fs/promises'; import { SecureCommandExecutor } from '../../security/CommandExecutor.js'; import { SecurePathValidator } from '../../security/PathValidator.js'; import { SecureErrorHandler } from '../../security/ErrorHandler.js'; /** * Base abstract class for platform-specific file locking adapters */ export class BasePlatformAdapter { commandExecutor; pathValidator; fileManager; errorHandler; constructor() { this.commandExecutor = new SecureCommandExecutor(); this.pathValidator = new SecurePathValidator(); this.errorHandler = new SecureErrorHandler(); // Remove circular dependency - AtomicFileManager should not be created here // This will be injected or created when needed to avoid circular references } /** * Validate file security settings */ async validateSecurity(filePath) { try { // Validate path security // Validate path exists and is accessible await this.pathValidator.validateAndSanitizePath(filePath); // Check if file exists await access(filePath, constants.F_OK); // Check if file is locked const locked = await this.isLocked(filePath); return locked; } catch { return false; } } /** * Get security information about a file */ async getSecurityInfo(filePath) { const stats = await stat(filePath); const isReadOnly = (stats.mode & 0o200) === 0; const isImmutable = await this.checkImmutable(filePath); return { isReadOnly, isImmutable, permissions: (stats.mode & parseInt('777', 8)).toString(8), platform: this.platformType, lastModified: stats.mtime }; } /** * Common method to make a file read-only using chmod */ async makeReadOnly(filePath) { const stats = await stat(filePath); const newMode = stats.mode & ~0o200; // Remove write permission await chmod(filePath, newMode); } /** * Common method to make a file writable using chmod */ async makeWritable(filePath) { const stats = await stat(filePath); const newMode = stats.mode | 0o200; // Add write permission await chmod(filePath, newMode); } /** * Common method to check if a file is read-only */ async isReadOnly(filePath) { try { const stats = await stat(filePath); return (stats.mode & 0o200) === 0; } catch { return false; } } /** * Verify file access permissions */ async verifyAccess(filePath, mode) { try { await access(filePath, mode); return true; } catch { return false; } } } //# sourceMappingURL=BasePlatformAdapter.js.map