diff --git a/app/utils.js b/app/utils.js index 1ebaa88..6f2d244 100644 --- a/app/utils.js +++ b/app/utils.js @@ -2,31 +2,35 @@ // /app/utils.js /** - * Recursively sanitizes a copy of an object by replacing the value of any `password` - * property with asterisks (*) matching the length of the original password. - * + * Sanitizes an object by replacing sensitive properties with asterisks. * @param {Object} obj - The object to sanitize. - * @returns {Object} - The sanitized copy of the object. + * @param {Array} [properties=['password', 'key', 'secret', 'token']] - The list of properties to sanitize. + * @returns {Object} - The sanitized object. */ -function sanitizeObject(obj) { - if (obj && typeof obj === 'object') { - const copy = Array.isArray(obj) ? [] : Object.assign({}, obj); - +function sanitizeObject( + obj, + properties = ["password", "key", "secret", "token"] +) { + if (obj && typeof obj === "object") { + const copy = Array.isArray(obj) ? [] : Object.assign({}, obj) + for (const key in obj) { - if (obj.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins - if (key === 'password' && typeof obj[key] === 'string') { - copy[key] = '*'.repeat(obj[key].length); - } else if (typeof obj[key] === 'object') { - copy[key] = sanitizeObject(obj[key]); + if (obj.hasOwnProperty(key)) { + // eslint-disable-line no-prototype-builtins + if (properties.includes(key) && typeof obj[key] === "string") { + copy[key] = "*".repeat(obj[key].length) + } else if (typeof obj[key] === "object") { + copy[key] = sanitizeObject(obj[key], properties) } else { - copy[key] = obj[key]; + copy[key] = obj[key] } } } - return copy; + return copy } - return obj; + return obj } -exports.sanitizeObject = sanitizeObject; + +exports.sanitizeObject = sanitizeObject