UNPKG

logs-interceptor

Version:

High-performance, production-ready log interceptor for Node.js applications with Loki integration. Built with Clean Architecture principles. Supports Node.js, Browser, and Node-RED.

73 lines 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SanitizationService = void 0; class SanitizationService { constructor(config) { this.config = config; } detectSensitiveData(text) { // Check against configured patterns for (const pattern of this.config.sensitivePatterns) { if (pattern.test(text)) { return true; } } // Check against common patterns for (const pattern of SanitizationService.COMMON_PATTERNS) { if (pattern.test(text)) { return true; } } return false; } sanitize(data) { const sanitized = {}; for (const [key, value] of Object.entries(data)) { // Check if key matches sensitive patterns const isKeySensitive = this.config.sensitivePatterns.some((pattern) => pattern.test(key)); if (isKeySensitive) { sanitized[key] = '[REDACTED]'; continue; } // Handle different value types if (typeof value === 'string') { if (this.detectSensitiveData(value)) { sanitized[key] = '[REDACTED]'; } else { sanitized[key] = value; } } else if (typeof value === 'object' && value !== null) { if (Array.isArray(value)) { sanitized[key] = value.map((item) => { if (typeof item === 'string' && this.detectSensitiveData(item)) { return '[REDACTED]'; } if (typeof item === 'object' && item !== null) { return this.sanitize(item); } return item; }); } else { sanitized[key] = this.sanitize(value); } } else { sanitized[key] = value; } } return sanitized; } } exports.SanitizationService = SanitizationService; SanitizationService.COMMON_PATTERNS = [ /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/, /\b(?:\d{4}[-\s]?){3}\d{4}\b/, /\b\d{3}-\d{2}-\d{4}\b/, /\b\d{3}\.\d{3}\.\d{3}-\d{2}\b/, /Bearer\s+[A-Za-z0-9\-._~+\/]+=*/i, /Basic\s+[A-Za-z0-9+\/]+=*/i, // Basic auth ]; //# sourceMappingURL=SanitizationService.js.map