/** * Utility functions for secure logging operations. * * This module provides utilities for sanitizing data before logging to prevent * sensitive information leakage. It handles circular references, prototype pollution, * and provides protection against DoS attacks through depth limiting. * * @module */ /** * Options for customizing data sanitization behavior. */ export type SanitizeOptions = { /** * Additional key patterns to treat as sensitive and redact. * These are added to the default list of sensitive keys. */ readonly additionalSensitiveKeys?: readonly string[]; }; /** * Sanitizes data for secure logging by redacting sensitive information. * * This function deeply traverses objects, arrays, and errors to redact sensitive * fields while preserving structure for debugging. It provides multiple layers of * protection against common security and reliability issues: * * **Security Features:** * - Redacts sensitive keys (token, password, secret, auth, credential) * - Prevents prototype pollution by skipping dangerous keys * - Uses case-insensitive pattern matching for sensitive data detection * * **Reliability Features:** * - Handles circular references using WeakSet tracking * - Limits recursion depth to prevent stack overflow (max 100 levels) * - Preserves non-serializable types (functions, symbols, bigints) * * **Type Support:** * - Primitives: returned as-is (strings, numbers, booleans, null, undefined) * - Functions: converted to `[Function: name]` strings * - Symbols: converted to string representation * - BigInts: converted to string with 'n' suffix * - Errors: extracts name, message, stack, and other properties * - Arrays: recursively sanitized element by element * - Maps: converted to object with `__type` and `entries`; if key is sensitive, both key and value are redacted * - Sets: converted to object with `__type` and `values` * - Objects: recursively sanitized key by key with sensitive data redacted * * @param data - The data to sanitize (any type) * @param options - Optional configuration for sanitization * @returns A new sanitized version with sensitive information redacted * * @example * Basic usage with sensitive data: * ```typescript * const data = { * username: 'john', * apiToken: 'secret-key-123', * settings: { theme: 'dark' } * }; * * const sanitized = sanitizeDataForLogging(data); * // Result: { username: 'john', apiToken: '***', settings: { theme: 'dark' } } * ``` * * @example * Usage with custom sensitive keys: * ```typescript * const data = { * apiKey: 'secret', * ssn: '123-45-6789' * }; * * const sanitized = sanitizeDataForLogging(data, { * additionalSensitiveKeys: ['ssn'] * }); * // Result: { apiKey: '***', ssn: '***' } // 'apiKey' contains 'key' which might not match default, but 'ssn' will match. * ``` * * @example * Handling circular references: * ```typescript * const obj: any = { name: 'test' }; * obj.self = obj; * * const sanitized = sanitizeDataForLogging(obj); * // Result: { name: 'test', self: '[Circular]' } * ``` * * @example * Error object sanitization: * ```typescript * const error = new Error('Failed to authenticate'); * error.apiToken = 'secret'; * * const sanitized = sanitizeDataForLogging(error); * // Result: { name: 'Error', message: 'Failed...', stack: '...', apiToken: '***' } * ``` */ export declare function sanitizeDataForLogging(data: unknown, options?: SanitizeOptions): unknown; //# sourceMappingURL=logger-utils.d.ts.map