UNPKG

mdsaad-cli

Version:

🚀 A powerful CLI toolkit with 20+ commands: AI chat, weather, math calculations, ASCII art, currency conversion, and more - all without API keys required!

402 lines (292 loc) • 11.7 kB
# Task 19 - Security Measures Implementation ## Overview Implemented comprehensive security measures to protect the CLI application, user data, and API communications. This includes input validation, encrypted API key storage, secure network communications, and comprehensive security management tools. ## Implementation Summary ### 1. Input Validation Service (`src/services/input-validator.js`) **Purpose**: Comprehensive input validation and sanitization for all user inputs **Key Features**: - **Pattern Validation**: Email, URL, API key, expression, and city name validation - **Sanitization**: HTML, SQL injection, XSS prevention, path traversal protection - **Rate Limiting**: Per-user request throttling with configurable limits - **Security Checks**: Dangerous function detection, balanced parentheses validation - **Type Validation**: Strong typing with custom validation rules **Security Methods**: ```javascript // Input validation with type checking validate(input, type, (options = {})); // Multi-layer sanitization sanitize(input, type); // Rate limiting protection checkRateLimit(identifier, limit, windowMs); // Complete input processing process(input, type, (options = {})); ``` **Validation Types Supported**: - `email`: RFC-compliant email validation - `url`: HTTP/HTTPS URL validation - `apiKey`: Alphanumeric and safe character validation - `expression`: Mathematical expression with security checks - `cityName`: Geographic name validation - `currencyCode`: ISO currency code validation - `command`: CLI command validation - `path`: File path validation with traversal protection ### 2. Security Manager (`src/services/security-manager.js`) **Purpose**: Centralized security management with encrypted API key storage and authentication **Key Features**: - **AES-256-GCM Encryption**: Military-grade encryption for sensitive data - **Secure API Key Storage**: Encrypted key management with metadata - **Authentication System**: Password hashing with salt using PBKDF2 - **Login Protection**: Rate limiting for failed login attempts - **Security Auditing**: Comprehensive security reporting and monitoring - **Token Generation**: Cryptographically secure random tokens **Core Security Methods**: ```javascript // Encryption/Decryption encrypt(text); decrypt(encryptedData); // API Key Management storeApiKey(service, apiKey); getApiKey(service); removeApiKey(service); listApiKeys(); // Authentication hashPassword(password); verifyPassword(password, hash, salt); checkLoginAttempts(identifier); // Security Operations generateToken((length = 32)); generateSecurityReport(); cleanupExpiredKeys(); ``` **Security Features**: - **Key Rotation**: Automatic key aging and rotation recommendations - **Secure Storage**: OS-appropriate secure directories with proper permissions - **Audit Logging**: Security event tracking and monitoring - **Cleanup Operations**: Automatic cleanup of expired or unused keys ### 3. Network Security (`src/services/network-security.js`) **Purpose**: Secure HTTP communications and network security measures **Key Features**: - **TLS Security**: Enforced TLS 1.2+ with proper certificate validation - **Certificate Pinning**: Server identity verification and certificate checking - **Rate Limiting**: Network request throttling per hostname - **Request Signing**: HMAC-based request/response integrity verification - **Response Sanitization**: Automatic sanitization of API responses - **Secure Headers**: Automatic security header management **Network Security Methods**: ```javascript // Secure HTTP Agent createSecureAgent(); // Header Security validateHeaders(headers); // Rate Limiting checkRateLimit(hostname, (limit = 50), (window = 60000)); // Request/Response Security signRequest(data); verifyResponse(data, signature); sanitizeResponse(response); // WebSocket Security createSecureWebSocket(url, (options = {})); ``` **Security Protocols**: - **TLS Configuration**: Secure protocols, cipher suites, and certificate validation - **Request Validation**: Automatic header sanitization and security header injection - **Response Protection**: Content sanitization and structure validation - **Rate Limiting**: Per-hostname request throttling with customizable limits ### 4. Security Command Interface (`src/commands/security.js`) **Purpose**: User-friendly command-line interface for security management **Key Features**: - **Security Status**: Real-time security configuration and status display - **Security Audit**: Comprehensive security assessment and recommendations - **API Key Management**: Interactive key management with secure operations - **Input Validation**: Command-line input validation and testing - **Security Reporting**: Detailed security reports with actionable insights - **Cleanup Operations**: Automated maintenance and cleanup tasks **Available Commands**: ```bash # Security status and overview mdsaad security status [--verbose] # Comprehensive security audit mdsaad security audit [--detailed] # Generate security report mdsaad security report [--format json|text] # API key management mdsaad security keys list mdsaad security keys set <service> [--key <key>] mdsaad security keys remove <service> # Input validation testing mdsaad security validate <input> --type <type> # Security cleanup mdsaad security cleanup [--force] ``` **Interactive Features**: - **Secure Prompts**: Password-masked input for sensitive data - **Confirmation Dialogs**: Safety confirmations for destructive operations - **Progress Indicators**: Visual feedback for long-running security operations - **Color-coded Output**: Security level indicators and status visualization ## Security Architecture ### Multi-Layer Security Model 1. **Input Layer**: All user inputs validated and sanitized before processing 2. **Application Layer**: Secure API key storage and authentication 3. **Network Layer**: TLS encryption and secure communication protocols 4. **Storage Layer**: Encrypted data at rest with proper file permissions ### Security Standards Compliance - **Encryption**: AES-256-GCM encryption for data at rest - **Hashing**: PBKDF2 with SHA-512 for password hashing - **TLS**: Minimum TLS 1.2 for network communications - **Validation**: Input validation following OWASP guidelines - **Rate Limiting**: DoS protection with configurable thresholds ### Configuration Management **Security Settings** (`~/.config/mdsaad/security.json`): ```json { "encryption": { "algorithm": "aes-256-gcm", "keyLength": 32, "ivLength": 16, "tagLength": 16 }, "rateLimit": { "default": 50, "window": 60000, "maxAttempts": 5 }, "network": { "timeout": 30000, "userAgent": "mdsaad-cli/1.0.0", "secureProtocol": "TLSv1_2_method" }, "validation": { "maxLength": 1000, "strictMode": true, "sanitizeHtml": true } } ``` ## Security Testing ### Comprehensive Test Coverage **Unit Tests** (`tests/unit/security.test.js`): - Input validation with edge cases and attack vectors - Encryption/decryption with various data types - Network security with malicious requests - Authentication system with brute force protection - API key management with lifecycle testing **Test Categories**: - **Input Validation Tests**: SQL injection, XSS, path traversal, code injection - **Encryption Tests**: Data integrity, key rotation, algorithm validation - **Network Security Tests**: TLS verification, certificate pinning, rate limiting - **Authentication Tests**: Password security, login protection, token generation - **Integration Tests**: End-to-end security workflows ### Security Test Results ```bash npm test -- --testPathPattern=security ``` **Expected Coverage**: - Input Validation: 100% code coverage - Security Manager: 100% code coverage - Network Security: 100% code coverage - Security Command: 95%+ code coverage ## Usage Examples ### Basic Security Operations ```bash # Check security status mdsaad security status # Perform security audit mdsaad security audit --detailed # Store API key securely mdsaad security keys set weather-api # Validate user input mdsaad security validate "user@example.com" --type email ``` ### Advanced Security Management ```bash # Generate comprehensive security report mdsaad security report --format json > security-report.json # Clean up expired keys and security data mdsaad security cleanup # List all stored API keys with metadata mdsaad security keys list --verbose # Test mathematical expression security mdsaad security validate "2+2*3" --type expression ``` ### Programmatic API Usage ```javascript const { InputValidator, SecurityManager, NetworkSecurity, } = require('./src/services'); // Initialize security services const validator = new InputValidator(); const security = new SecurityManager(); const network = new NetworkSecurity(); await security.initialize(); // Secure input processing const email = validator.process(userInput, 'email'); // Secure API key storage await security.storeApiKey('service', apiKey); // Secure network request const agent = network.createSecureAgent(); const headers = network.validateHeaders(requestHeaders); ``` ## Security Best Practices ### For Users 1. **API Key Security**: - Store API keys using the built-in secure storage - Regularly rotate API keys - Never share or commit API keys to version control 2. **Input Validation**: - Always validate user inputs before processing - Use the built-in validation types for common formats - Be cautious with mathematical expressions and file paths 3. **Network Security**: - Always use HTTPS for API communications - Verify SSL certificates in production - Monitor rate limiting and adjust as needed ### For Developers 1. **Security Integration**: - Use security services in all user-facing commands - Implement proper error handling for security failures - Follow the principle of least privilege 2. **Testing Security**: - Include security tests for all new features - Test with malicious inputs and edge cases - Verify encryption and validation in unit tests 3. **Security Monitoring**: - Regularly review security reports - Monitor for security vulnerabilities in dependencies - Keep security configurations up to date ## Security Maintenance ### Regular Security Tasks 1. **Weekly**: - Review security audit reports - Check for expired API keys - Monitor rate limiting patterns 2. **Monthly**: - Update security configurations - Review and rotate API keys - Update TLS certificates if needed 3. **Quarterly**: - Comprehensive security assessment - Update security policies and procedures - Review and update security tests ### Security Updates The security system is designed to be maintainable and updatable: - Configuration-driven security settings - Modular security services for easy updates - Comprehensive testing to prevent regressions - Clear documentation for security procedures ## Security Incident Response ### If Security Issues Are Detected 1. **Immediate Actions**: - Run security audit to assess scope - Check security logs for suspicious activity - Rotate potentially compromised API keys 2. **Investigation**: - Review recent security reports - Check input validation logs for attack attempts - Verify network security configurations 3. **Recovery**: - Update security configurations as needed - Clean up any compromised data - Strengthen security measures based on findings The security implementation provides comprehensive protection while maintaining usability and performance for the CLI application.