chore: utils function sanitizeObject Recursively sanitizes an object by replacing the value of any password
property with asterisks (*) matching the length of the original password.
This commit is contained in:
parent
1fc35f74da
commit
aec8be86b4
1 changed files with 29 additions and 0 deletions
29
app/utils.js
Normal file
29
app/utils.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
// server
|
||||||
|
// /app/utils.js
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively sanitizes an object by replacing the value of any `password`
|
||||||
|
* property with asterisks (*) matching the length of the original password.
|
||||||
|
*
|
||||||
|
* @param {Object} obj - The object to sanitize.
|
||||||
|
* @returns {Object} - The sanitized object.
|
||||||
|
*/
|
||||||
|
function sanitizeObject(obj) {
|
||||||
|
// Check if the input is an object or array
|
||||||
|
if (obj && typeof obj === 'object') {
|
||||||
|
// Iterate over each key in the object
|
||||||
|
for (const key in obj) {
|
||||||
|
if (obj.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
|
||||||
|
if (key === 'password' && typeof obj[key] === 'string') {
|
||||||
|
// Replace password value with asterisks
|
||||||
|
obj[key] = '*'.repeat(obj[key].length);
|
||||||
|
} else if (typeof obj[key] === 'object') {
|
||||||
|
// Recursively sanitize nested objects
|
||||||
|
sanitizeObject(obj[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
exports.sanitizeObject = sanitizeObject;
|
Loading…
Reference in a new issue