Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): redact the value under a boxed string Map key

Yudhi Armyndharis98a035cbb7a2
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index e0593f368..19b01cc55 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -1061,6 +1061,20 @@ describe('redactSecrets', () => {
expect(JSON.stringify(redactSecrets(new Map<unknown, unknown>([[unprintable, 'v']])))).toBe('[[{"id":1},"v"]]');
});
+ // A boxed string key is written as its text, so the value under one that names a credential is replaced too.
+ it('tests a boxed string key by its text, read through the built-in method', () => {
+ const forged = Object.assign(new String('id'), { valueOf: () => 'password', toString: () => 'password' });
+ const map = new Map<unknown, unknown>([
+ [new String('password'), 'value-boxed'],
+ [forged, 'kept'],
+ [new Proxy(new String('token'), {}), 'proxied'],
+ ]);
+
+ expect(JSON.stringify(redactSecrets(map))).toBe(
+ '[["password","[REDACTED]"],["id","kept"],[{"0":"t","1":"o","2":"k","3":"e","4":"n"},"proxied"]]',
+ );
+ });
+
// A Proxy is copied by its own properties, whatever its traps answer; a value made from one of these prototypes
// alone lacks the data the built-in methods read.
it('copies a Proxy, or a value that only has one of these prototypes, by its own properties', () => {
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 9becc7bf3..6baf4f094 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -123,9 +123,11 @@ export function writtenForm(node: object, form = formOf(node), isSecretKey = isS
if (form === 'map' || form === 'set') {
const items: unknown[] = [];
if (form === 'map')
- Map.prototype.forEach.call(node, (item: unknown, key: unknown) =>
- items.push([key, typeof key === 'string' && isSecretKey(key) ? marker : item]),
- );
+ Map.prototype.forEach.call(node, (item: unknown, key: unknown) => {
+ // A boxed string key is written as its text, so it is tested as that text.
+ const name = typeof key === 'object' && key !== null && formOf(key) === 'boxed' ? boxedValue(key) : key;
+ items.push([key, typeof name === 'string' && isSecretKey(name) ? marker : item]);
+ });
else Set.prototype.forEach.call(node, (item: unknown) => items.push(item));
return items;
}
diff --git a/src/core/plugins/sandbox-log-meta.spec.ts b/src/core/plugins/sandbox-log-meta.spec.ts
index fc6dc7970..50c5e53f9 100644
--- a/src/core/plugins/sandbox-log-meta.spec.ts
+++ b/src/core/plugins/sandbox-log-meta.spec.ts
@@ -218,6 +218,7 @@ describe('metaText', () => {
},
],
['a Set holding a credential key', { s: new Set([1, { secret: 's' }]) }],
+ ['a Map with a boxed credential key', { m: new Map([[new String('token'), 'v']]) }],
[
'a typed array, a Buffer and a DataView',
{ u: new Uint8Array(3), b: Buffer.from('ab'), v: new DataView(new ArrayBuffer(4)) },