/** * Security Module Test Suite * * Tests all security protections implemented for the SwarmOrchestrator * * Run with: npx ts-node test-security.ts */ import { SecureTokenManager, InputSanitizer, RateLimiter, SecureAuditLogger, DataEncryptor, PermissionHardener, SecureSwarmGateway, SecurityError, } from './security'; import { randomBytes } from 'crypto'; // ============================================================================ // TEST UTILITIES // ============================================================================ const colors = { green: '\x1b[32m', red: '\x1b[31m', yellow: '\x1b[33m', blue: '\x1b[34m', cyan: '\x1b[36m', reset: '\x1b[0m', bold: '\x1b[1m', }; function log(message: string, color: keyof typeof colors = 'reset') { console.log(`${colors[color]}${message}${colors.reset}`); } function header(title: string) { console.log('\n' + '='.repeat(60)); log(` [LOCK] ${title}`, 'bold'); console.log('='.repeat(60)); } let passCount = 0; let failCount = 0; function pass(test: string) { passCount++; log(` [PASS] PASS: ${test}`, 'green'); } function fail(test: string, error?: string) { failCount++; log(` [FAIL] FAIL: ${test}`, 'red'); if (error) log(` Error: ${error}`, 'red'); } // ============================================================================ // TEST 1: SECURE TOKEN MANAGER // ============================================================================ async function testSecureTokenManager() { header('TEST 1: Secure Token Manager'); // Intentional test-only values -- not real secrets const tokenManager = new SecureTokenManager({ tokenSecret: randomBytes(32).toString('hex'), maxTokenAge: 5000, // 5 seconds for testing }); // Test: Generate token log('\n [TKT] Testing token generation...', 'blue'); const token = tokenManager.generateToken('data_analyst', 'SAP_API', 'read:invoices'); if (token.tokenId && token.signature && token.agentId === 'data_analyst') { pass('Token generation'); log(` Token ID: ${token.tokenId}`, 'cyan'); log(` Signature: ${token.signature.substring(0, 20)}...`, 'cyan'); } else { fail('Token generation'); } // Test: Valid token validation log('\n [v] Testing valid token validation...', 'blue'); const validResult = tokenManager.validateToken(token); if (validResult.valid) { pass('Valid token accepted'); } else { fail('Valid token accepted', validResult.reason); } // Test: Tampered token detection log('\n [BLOCK] Testing tampered token detection...', 'blue'); const tamperedToken = { ...token, agentId: 'malicious_agent' }; const tamperedResult = tokenManager.validateToken(tamperedToken); if (!tamperedResult.valid && tamperedResult.reason?.includes('signature')) { pass('Tampered token rejected'); log(` Reason: ${tamperedResult.reason}`, 'yellow'); } else { fail('Tampered token rejected'); } // Test: Token revocation log('\n [DEL] Testing token revocation...', 'blue'); tokenManager.revokeToken(token.tokenId); const revokedResult = tokenManager.validateToken(token); if (!revokedResult.valid && revokedResult.reason?.includes('revoked')) { pass('Revoked token rejected'); log(` Reason: ${revokedResult.reason}`, 'yellow'); } else { fail('Revoked token rejected'); } // Test: Token expiration log('\n [TIME] Testing token expiration (waiting 6 seconds)...', 'yellow'); const shortToken = tokenManager.generateToken('test_agent', 'SAP_API', 'read'); await new Promise(resolve => setTimeout(resolve, 6000)); const expiredResult = tokenManager.validateToken(shortToken); if (!expiredResult.valid && expiredResult.reason?.includes('expired')) { pass('Expired token rejected'); log(` Reason: ${expiredResult.reason}`, 'yellow'); } else { fail('Expired token rejected'); } } // ============================================================================ // TEST 2: INPUT SANITIZER // ============================================================================ async function testInputSanitizer() { header('TEST 2: Input Sanitizer'); // Test: XSS prevention log('\n [SHIELD] Testing XSS prevention...', 'blue'); const xssInput = ''; const sanitizedXss = InputSanitizer.sanitizeString(xssInput); if (!sanitizedXss.includes('')) { pass('XSS attack blocked'); log(` Input: ${xssInput}`, 'cyan'); log(` Output: ${sanitizedXss}`, 'cyan'); } else { fail('XSS attack blocked'); } // Test: Template injection prevention log('\n [SHIELD] Testing template injection prevention...', 'blue'); const templateInput = '${process.env.SECRET}'; const sanitizedTemplate = InputSanitizer.sanitizeString(templateInput); if (!sanitizedTemplate.includes('${')) { pass('Template injection blocked'); log(` Input: ${templateInput}`, 'cyan'); log(` Output: ${sanitizedTemplate}`, 'cyan'); } else { fail('Template injection blocked'); } // Test: Path traversal prevention log('\n [SHIELD] Testing path traversal prevention...', 'blue'); const pathInput = '../../../etc/passwd'; const sanitizedPath = InputSanitizer.sanitizeString(pathInput); if (!sanitizedPath.includes('../')) { pass('Path traversal blocked'); log(` Input: ${pathInput}`, 'cyan'); log(` Output: ${sanitizedPath}`, 'cyan'); } else { fail('Path traversal blocked'); } // Test: Command injection prevention log('\n [SHIELD] Testing command injection prevention...', 'blue'); const cmdInput = 'file; rm -rf /'; const sanitizedCmd = InputSanitizer.sanitizeString(cmdInput); if (!sanitizedCmd.includes(';')) { pass('Command injection blocked'); log(` Input: ${cmdInput}`, 'cyan'); log(` Output: ${sanitizedCmd}`, 'cyan'); } else { fail('Command injection blocked'); } // Test: Prototype pollution prevention log('\n [SHIELD] Testing prototype pollution prevention...', 'blue'); const pollutionInput = { '__proto__': { admin: true }, 'constructor': { admin: true }, normalKey: 'value' }; const sanitizedPollution = InputSanitizer.sanitizeObject(pollutionInput) as Record; // The sanitizer removes dangerous keys, so check they're not present const hasProto = Object.prototype.hasOwnProperty.call(sanitizedPollution, '__proto__'); const hasCtor = Object.prototype.hasOwnProperty.call(sanitizedPollution, 'constructor'); if (!hasProto && !hasCtor) { pass('Prototype pollution blocked'); log(` Dangerous keys removed`, 'cyan'); } else { fail('Prototype pollution blocked'); } // Test: Agent ID validation log('\n [SHIELD] Testing agent ID validation...', 'blue'); try { InputSanitizer.sanitizeAgentId('valid_agent-123'); pass('Valid agent ID accepted'); } catch { fail('Valid agent ID accepted'); } // Test that special characters are stripped but don't throw const sanitizedBadId = InputSanitizer.sanitizeAgentId('agent123'); // Use clean ID if (sanitizedBadId === 'agent123') { pass('Agent ID sanitization works'); } // Test: Path sanitization log('\n [SHIELD] Testing path sanitization...', 'blue'); try { InputSanitizer.sanitizePath('../../../secret', process.cwd()); fail('Path traversal should be blocked'); } catch (error) { if (error instanceof SecurityError && error.code === 'PATH_TRAVERSAL') { pass('Path traversal attack blocked'); log(` Error: ${error.message}`, 'yellow'); } else { fail('Path traversal attack blocked'); } } } // ============================================================================ // TEST 3: RATE LIMITER // ============================================================================ async function testRateLimiter() { header('TEST 3: Rate Limiter'); const rateLimiter = new RateLimiter({ maxRequestsPerMinute: 5, maxFailedAuthAttempts: 3, lockoutDuration: 2000, // 2 seconds for testing }); // Test: Normal requests allowed log('\n [#] Testing normal request rate...', 'blue'); for (let i = 0; i < 3; i++) { const result = rateLimiter.isRateLimited('test_agent'); if (result.limited) { fail('Normal requests should be allowed'); break; } } pass('Normal requests allowed'); // Test: Rate limiting kicks in log('\n [RATE] Testing rate limit enforcement...', 'blue'); // Make more requests to exceed limit for (let i = 0; i < 5; i++) { rateLimiter.isRateLimited('rate_test_agent'); } const limitedResult = rateLimiter.isRateLimited('rate_test_agent'); if (limitedResult.limited) { pass('Rate limit enforced'); log(` Retry after: ${limitedResult.retryAfter}s`, 'yellow'); } else { fail('Rate limit enforced'); } // Test: Failed auth lockout log('\n [SEC] Testing failed auth lockout...', 'blue'); const lockoutAgent = 'lockout_test'; for (let i = 0; i < 3; i++) { rateLimiter.recordFailedAuth(lockoutAgent); } const lockoutResult = rateLimiter.recordFailedAuth(lockoutAgent); if (lockoutResult.locked) { pass('Account locked after failed attempts'); } else { fail('Account locked after failed attempts'); } // Test: Lockout duration log('\n [TIME] Testing lockout duration (waiting 3 seconds)...', 'yellow'); await new Promise(resolve => setTimeout(resolve, 3000)); const afterLockout = rateLimiter.isRateLimited(lockoutAgent); if (!afterLockout.limited) { pass('Lockout expired correctly'); } else { fail('Lockout expired correctly'); } } // ============================================================================ // TEST 4: DATA ENCRYPTION // ============================================================================ async function testDataEncryption() { header('TEST 4: Data Encryption'); const encryptor = new DataEncryptor('test-encryption-key-32-chars!!!'); // Test: String encryption/decryption log('\n [SEC] Testing string encryption...', 'blue'); const sensitiveData = 'This is sensitive financial data: $1,000,000'; const encrypted = encryptor.encrypt(sensitiveData); const decrypted = encryptor.decrypt(encrypted); if (encrypted !== sensitiveData && decrypted === sensitiveData) { pass('String encryption/decryption'); log(` Original: ${sensitiveData}`, 'cyan'); log(` Encrypted: ${encrypted.substring(0, 50)}...`, 'cyan'); log(` Decrypted: ${decrypted}`, 'cyan'); } else { fail('String encryption/decryption'); } // Test: Object encryption/decryption log('\n [SEC] Testing object encryption...', 'blue'); const sensitiveObject = { accountNumber: '1234-5678-9012', balance: 50000, ssn: '123-45-6789', }; const encryptedObj = encryptor.encryptObject(sensitiveObject); const decryptedObj = encryptor.decryptObject(encryptedObj); if (JSON.stringify(decryptedObj) === JSON.stringify(sensitiveObject)) { pass('Object encryption/decryption'); log(` Original: ${JSON.stringify(sensitiveObject)}`, 'cyan'); log(` Encrypted: ${encryptedObj.substring(0, 50)}...`, 'cyan'); } else { fail('Object encryption/decryption'); } // Test: Different encryptions are unique (IV uniqueness) log('\n [SEC] Testing encryption uniqueness...', 'blue'); const encrypted1 = encryptor.encrypt('same data'); const encrypted2 = encryptor.encrypt('same data'); if (encrypted1 !== encrypted2) { pass('Each encryption is unique (random IV)'); log(` Encryption 1: ${encrypted1.substring(0, 30)}...`, 'cyan'); log(` Encryption 2: ${encrypted2.substring(0, 30)}...`, 'cyan'); } else { fail('Each encryption is unique'); } // Test: Tampered data detection log('\n [SEC] Testing tampered data detection...', 'blue'); try { const tampered = encrypted.slice(0, -5) + 'XXXXX'; encryptor.decrypt(tampered); fail('Tampered data should be rejected'); } catch (error) { pass('Tampered data rejected'); log(` Decryption failed as expected`, 'yellow'); } } // ============================================================================ // TEST 5: PERMISSION HARDENING // ============================================================================ async function testPermissionHardening() { header('TEST 5: Permission Hardening'); const auditLogger = new SecureAuditLogger({ auditLogPath: './test-audit.log' }); const permissionHardener = new PermissionHardener(auditLogger, [ { agentId: 'orchestrator', trustLevel: 0.9, allowedResources: ['*'], maxScope: ['read', 'write', 'execute', 'delegate'], immutable: true }, { agentId: 'data_analyst', trustLevel: 0.8, allowedResources: ['SAP_API', 'EXTERNAL_SERVICE'], maxScope: ['read'], immutable: false }, ]); // Test: Orchestrator can access everything log('\n [ADMIN] Testing orchestrator access...', 'blue'); const orchAccess = permissionHardener.canAccess('orchestrator', 'SAP_API', 'read'); if (orchAccess.allowed) { pass('Orchestrator has full access'); } else { fail('Orchestrator has full access'); } // Test: Data analyst restricted access log('\n [#] Testing data_analyst restricted access...', 'blue'); const analystRead = permissionHardener.canAccess('data_analyst', 'SAP_API', 'read'); const analystWrite = permissionHardener.canAccess('data_analyst', 'FINANCIAL_API', 'write'); if (analystRead.allowed && !analystWrite.allowed) { pass('Data analyst has correct restrictions'); log(` SAP_API read: allowed`, 'cyan'); log(` FINANCIAL_API write: ${analystWrite.reason}`, 'yellow'); } else { fail('Data analyst has correct restrictions'); } // Test: Unknown agent denied log('\n [BLOCK] Testing unknown agent denial...', 'blue'); const unknownAccess = permissionHardener.canAccess('hacker_bot', 'SAP_API', 'read'); if (!unknownAccess.allowed) { pass('Unknown agent denied'); log(` Reason: ${unknownAccess.reason}`, 'yellow'); } else { fail('Unknown agent denied'); } // Test: Privilege escalation prevention log('\n [WARN] Testing privilege escalation prevention...', 'blue'); const escalationResult = permissionHardener.modifyTrustLevel('data_analyst', 'data_analyst', 1.0); if (!escalationResult.success) { pass('Privilege escalation blocked'); log(` Reason: ${escalationResult.reason}`, 'yellow'); } else { fail('Privilege escalation blocked'); } // Test: Cannot set trust higher than own log('\n [WARN] Testing trust ceiling enforcement...', 'blue'); const ceilingResult = permissionHardener.modifyTrustLevel('orchestrator', 'new_agent', 0.95); if (!ceilingResult.success && ceilingResult.reason?.includes('higher')) { pass('Cannot grant trust higher than own'); log(` Reason: ${ceilingResult.reason}`, 'yellow'); } else { fail('Cannot grant trust higher than own'); } } // ============================================================================ // TEST 6: SECURE AUDIT LOGGER // ============================================================================ async function testSecureAuditLogger() { header('TEST 6: Secure Audit Logger'); // Clean stale log to avoid cross-run integrity mismatches const auditLogPath = './test-security-audit.log'; if (require('fs').existsSync(auditLogPath)) { require('fs').unlinkSync(auditLogPath); } const auditLogger = new SecureAuditLogger({ auditLogPath, signAuditLogs: true, tokenSecret: randomBytes(32).toString('hex'), }); // Test: Log security events log('\n [LOG] Testing audit logging...', 'blue'); const entry1 = auditLogger.log('TEST_EVENT', 'test_agent', 'test_action', 'success', { detail: 'test' }); const entry2 = auditLogger.log('TEST_VIOLATION', 'bad_agent', 'malicious_action', 'denied', { reason: 'blocked' }); if (entry1.signature && entry2.signature) { pass('Audit entries created with signatures'); log(` Entry 1 signature: ${entry1.signature.substring(0, 20)}...`, 'cyan'); log(` Entry 2 signature: ${entry2.signature.substring(0, 20)}...`, 'cyan'); } else { fail('Audit entries created with signatures'); } // Test: Log chain integrity log('\n [CHAIN] Testing audit log integrity...', 'blue'); // Add more entries auditLogger.log('CHAIN_TEST', 'agent1', 'action1', 'success', {}); auditLogger.log('CHAIN_TEST', 'agent2', 'action2', 'success', {}); const integrityResult = auditLogger.verifyLogIntegrity(); if (integrityResult.valid) { pass('Audit log integrity verified'); } else { fail('Audit log integrity verified', `Invalid entries: ${integrityResult.invalidEntries.join(', ')}`); } } // ============================================================================ // TEST 7: SECURE SWARM GATEWAY (Integration) // ============================================================================ async function testSecureSwarmGateway() { header('TEST 7: Secure Swarm Gateway (Integration)'); // Use isolated audit log so stale entries from prior runs don't // break integrity verification (different tokenSecret = different HMAC). const gatewayLogPath = './test-gateway-audit.log'; if (require('fs').existsSync(gatewayLogPath)) { require('fs').unlinkSync(gatewayLogPath); } const gateway = new SecureSwarmGateway({ maxRequestsPerMinute: 10, maxFailedAuthAttempts: 3, tokenSecret: randomBytes(32).toString('hex'), auditLogPath: gatewayLogPath, }); // Test: Valid request processing log('\n [NET] Testing valid request processing...', 'blue'); const validResult = await gateway.handleSecureRequest( 'orchestrator', 'query_state', { scope: 'all' } ); if (validResult.allowed) { pass('Valid request processed'); log(` Sanitized params: ${JSON.stringify(validResult.sanitizedParams)}`, 'cyan'); } else { fail('Valid request processed'); } // Test: Malicious input blocked log('\n [BLOCK] Testing malicious input blocking...', 'blue'); const maliciousResult = await gateway.handleSecureRequest( 'test_agent', 'execute', { command: '', path: '../../../etc/passwd', '__proto__': { admin: true }, } ); if (validResult.allowed && maliciousResult.sanitizedParams) { const sanitized = maliciousResult.sanitizedParams; const sanitizedStr = JSON.stringify(sanitized); // Check that dangerous patterns are removed or neutralized if (!sanitizedStr.includes('