fix: sanitize object no longer mutates original object

This commit is contained in:
Bill Church 2024-08-16 22:47:42 +00:00
parent f14f0bccf5
commit ea017016b7
No known key found for this signature in database

View file

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