UNPKG

lever-ui-logger

Version:

Zero-dependency logging library with optional EventBus integration. Built-in PII redaction, multiple transports, and comprehensive logging capabilities.

106 lines 3.48 kB
/** * Error message sanitizer for preventing sensitive data leakage * * This module provides comprehensive sanitization of error messages and other strings * to prevent accidental exposure of tokens, API keys, passwords, and other sensitive data. * Based on security research and production patterns from major security tools. */ /** * Comprehensive regex patterns for detecting various types of sensitive tokens and PII * Based on research from security tools like TruffleHog and industry standards */ export interface TokenPatterns { /** JWT tokens - Base64url encoded with 3 parts */ jwt: RegExp; /** Generic Bearer tokens */ bearer: RegExp; /** Generic API keys with common prefixes */ genericApiKey: RegExp; /** URLs with credentials */ urlWithCredentials: RegExp; /** Email addresses */ email: RegExp; /** Phone numbers */ phone: RegExp; /** Credit card numbers */ creditCard: RegExp; } /** * Configuration for the error sanitizer */ export interface ErrorSanitizerConfig { /** Enable comprehensive token detection (default: true) */ enableTokenDetection?: boolean; /** Replacement strategy for detected sensitive data */ replacementStrategy?: 'mask' | 'redact' | 'hash'; /** Custom token patterns to detect */ customPatterns?: RegExp[]; /** Fields to always redact regardless of content */ sensitiveFields?: string[]; /** Maximum length of original value to show in mask (default: 8) */ maskRevealLength?: number; } /** * Production-grade error message sanitizer */ export declare class ErrorMessageSanitizer { private readonly config; private readonly tokenPatterns; private readonly sensitiveFieldQuotedPattern; private readonly sensitiveFieldUnquotedPattern; constructor(config?: ErrorSanitizerConfig); /** * Sanitize an error message or any string containing potentially sensitive data * * @param input - The string to sanitize * @returns Sanitized string with sensitive data masked/redacted */ sanitize(input: string): string; /** * Sanitize sensitive key-value pairs in strings */ private sanitizeSensitiveFields; /** * Detect and sanitize various token patterns */ private sanitizeTokens; /** * Apply custom user-defined patterns */ private applyCustomPatterns; /** * Apply the configured sanitization strategy to a detected sensitive value */ private applySanitization; /** * Mask a value showing first and last few characters */ private maskValue; /** * Create a hash of the value for logging purposes */ private hashValue; /** * Create comprehensive token detection patterns */ private createTokenPatterns; /** * Check if a string contains any detectable sensitive data * * @param input - String to check * @returns True if sensitive data is detected */ hasSensitiveData(input: string): boolean; /** * Get statistics about what types of sensitive data were found * * @param input - String to analyze * @returns Object with counts of different sensitive data types found */ analyzeSensitiveData(input: string): Record<string, number>; } /** * Default error sanitizer instance for immediate use */ export declare const defaultErrorSanitizer: ErrorMessageSanitizer; //# sourceMappingURL=error-sanitizer.d.ts.map