Skip to content

Commit ef9fff4

Browse files
authored
Merge pull request #589 from mitchellecm7/feat/mask-fields-helper-569
feat: add maskFields helper for redacting sensitive fields in logs - …
2 parents 73ae3f7 + c246c4a commit ef9fff4

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

‎src/utils/log-field-sanitizer.utils.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,22 @@ export function sanitizeLogObject(obj: unknown): unknown {
101101

102102
return obj;
103103
}
104+
105+
/**
106+
* Masks sensitive fields in an object by replacing their values with [REDACTED].
107+
* Works recursively for nested objects. Does not mutate the original object.
108+
*/
109+
export function maskFields(obj: Record<string, unknown>, fields: string[]): Record<string, unknown> {
110+
const fieldSet = new Set(fields);
111+
const result: Record<string, unknown> = {};
112+
for (const [key, value] of Object.entries(obj)) {
113+
if (fieldSet.has(key)) {
114+
result[key] = '[REDACTED]';
115+
} else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
116+
result[key] = maskFields(value as Record<string, unknown>, fields);
117+
} else {
118+
result[key] = value;
119+
}
120+
}
121+
return result;
122+
}

0 commit comments

Comments
 (0)