| const fs = require('fs'); |
| const crypto = require('crypto'); |
| const path = require('path'); |
|
|
| |
| |
| |
| |
|
|
| class SecurityValidator { |
| constructor() { |
| this.checksumFile = path.join(__dirname, '..', 'checksums.json'); |
| this.checksums = this._loadChecksums(); |
| } |
|
|
| |
| |
| |
|
|
| validateChecksums() { |
| console.log(' Validating file checksums...'); |
| |
| if (!fs.existsSync(this.checksumFile)) { |
| console.warn(' Warning: checksums.json not found. Skipping checksum validation.'); |
| return true; |
| } |
|
|
| const criticalFiles = [ |
| 'main.js', |
| 'preload.js', |
| 'engines/js_scanner/junk-detector.js', |
| 'security/security-validator.js' |
| ]; |
|
|
| for (const file of criticalFiles) { |
| const filePath = path.join(__dirname, '..', file); |
| if (!fs.existsSync(filePath)) { |
| console.error(` Critical file missing: ${file}`); |
| return false; |
| } |
|
|
| const expectedChecksum = this.checksums[file]; |
| const actualChecksum = this._calculateChecksum(filePath); |
|
|
| if (expectedChecksum && actualChecksum !== expectedChecksum) { |
| console.error(` Checksum mismatch for ${file}`); |
| console.error(` Expected: ${expectedChecksum}`); |
| console.error(` Actual: ${actualChecksum}`); |
| return false; |
| } |
| } |
|
|
| console.log(' All checksums validated successfully'); |
| return true; |
| } |
|
|
| generateChecksums() { |
| console.log(' Generating file checksums...'); |
| |
| const criticalFiles = [ |
| 'main.js', |
| 'preload.js', |
| 'junk-sweeper-cli.js', |
| 'engines/js_scanner/junk-detector.js', |
| 'security/security-validator.js' |
| ]; |
|
|
| const newChecksums = {}; |
| const appDir = path.join(__dirname, '..'); |
|
|
| for (const file of criticalFiles) { |
| const filePath = path.join(appDir, file); |
| if (fs.existsSync(filePath)) { |
| newChecksums[file] = this._calculateChecksum(filePath); |
| } |
| } |
|
|
| fs.writeFileSync(this.checksumFile, JSON.stringify(newChecksums, null, 2)); |
| console.log(' Checksums generated and saved'); |
| return newChecksums; |
| } |
|
|
| _calculateChecksum(filePath) { |
| const content = fs.readFileSync(filePath, 'utf-8'); |
| return crypto.createHash('sha256').update(content).digest('hex'); |
| } |
|
|
| _loadChecksums() { |
| if (fs.existsSync(this.checksumFile)) { |
| try { |
| return JSON.parse(fs.readFileSync(this.checksumFile, 'utf-8')); |
| } catch (error) { |
| console.warn(' Failed to load checksums.json'); |
| return {}; |
| } |
| } |
| return {}; |
| } |
|
|
| |
| |
| |
|
|
| applySecurityConfig() { |
| console.log(' Applying security configuration...'); |
|
|
| |
| if (process.env.NODE_ENV === 'production') { |
| |
| console.log(' Production security mode enabled'); |
| } |
|
|
| |
| process.env.NODE_ENV = process.env.NODE_ENV || 'production'; |
| |
| console.log(' Security configuration applied'); |
| } |
|
|
| |
| |
| |
|
|
| validatePath(userPath) { |
| const normalized = path.normalize(userPath); |
| const resolved = path.resolve(userPath); |
|
|
| |
| if (normalized !== path.normalize(userPath)) { |
| throw new Error('Invalid path: directory traversal detected'); |
| } |
|
|
| |
| const forbidden = [ |
| /^[A-Z]:\\Windows\\/i, |
| /^[A-Z]:\\Program Files\\/i, |
| /^\/etc\//, |
| /^\/usr\/bin\//, |
| /^\/System\// |
| ]; |
|
|
| for (const pattern of forbidden) { |
| if (pattern.test(resolved)) { |
| throw new Error(`Access denied: cannot access ${userPath}`); |
| } |
| } |
|
|
| return resolved; |
| } |
|
|
| |
| |
| |
|
|
| performSecurityAudit() { |
| console.log(' Performing security audit...'); |
|
|
| const audit = { |
| timestamp: new Date().toISOString(), |
| checks: { |
| checksumValidation: this.validateChecksums(), |
| pathValidation: true, |
| sandboxing: process.env.NODE_ENV === 'production', |
| contextIsolation: true |
| }, |
| status: 'PASSED' |
| }; |
|
|
| const allPassed = Object.values(audit.checks).every(check => check === true); |
| if (!allPassed) { |
| audit.status = 'FAILED'; |
| console.error(' Security audit failed'); |
| } else { |
| console.log(' Security audit passed'); |
| } |
|
|
| return audit; |
| } |
| } |
|
|
| module.exports = SecurityValidator; |
|
|