UNPKG

@energica-city/shared-amplify-utils

Version:

Shared utilities for AWS Amplify projects

81 lines 2.51 kB
/** * Shared sanitization utilities for middleware logging */ /** * Checks if a field name indicates sensitive data */ function isSensitiveField(fieldName) { const lowerKey = fieldName.toLowerCase(); return (lowerKey.includes('password') || lowerKey.includes('token') || lowerKey.includes('secret') || lowerKey.includes('key') || lowerKey.includes('auth')); } /** * Sanitizes an array by recursively sanitizing each item */ function sanitizeArray(obj, config, currentDepth) { return obj.map(item => sanitizeObject(item, config, currentDepth + 1)); } /** * Sanitizes an object's properties */ function sanitizeObjectProperties(obj, config, currentDepth) { const { excludeFields = [] } = config; const sanitized = {}; for (const [key, value] of Object.entries(obj)) { if (excludeFields.includes(key)) { sanitized[key] = '[REDACTED]'; } else if (isSensitiveField(key)) { sanitized[key] = '[REDACTED]'; } else if (key === 'body' && typeof value === 'string') { try { const parsedBody = JSON.parse(value); const sanitizedBody = sanitizeObject(parsedBody, config, currentDepth + 1); sanitized[key] = JSON.stringify(sanitizedBody); } catch { // Not a valid JSON string, treat as a regular field sanitized[key] = sanitizeObject(value, config, currentDepth + 1); } } else { sanitized[key] = sanitizeObject(value, config, currentDepth + 1); } } return sanitized; } /** * Checks if a value is a primitive type */ function isPrimitive(obj) { return (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean'); } /** * Sanitizes an object by removing sensitive fields and limiting depth */ export function sanitizeObject(obj, config = {}, currentDepth = 0) { const { maxDepth = 3 } = config; if (currentDepth >= maxDepth) { return '[Object: max depth reached]'; } if (obj === null || obj === undefined) { return obj; } if (isPrimitive(obj)) { return obj; } if (Array.isArray(obj)) { return sanitizeArray(obj, config, currentDepth); } if (typeof obj === 'object') { return sanitizeObjectProperties(obj, config, currentDepth); } return '[Unknown type]'; } //# sourceMappingURL=sanitization.js.map