Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): define a property named __proto__ in the redacted copy

Yudhi Armyndharis6a9dd88b6c72
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index 54371411f..f824cf9a0 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -280,6 +280,18 @@ describe('LoggerService', () => {
expect(parsed()).toMatchObject({ message: 'saved', metadata: '[Unreadable]' });
});
+ it('writes a property named __proto__ as a field, redacted, without touching any prototype', () => {
+ logger.log(
+ 'saved',
+ JSON.parse('{"__proto__":{"a":1,"password":"value-proto"},"b":2}') as Record<string, unknown>,
+ );
+
+ const line = getRawOutput(consoleSpy);
+ expect(line).toContain('"__proto__":{"a":1,"password":"[REDACTED]"}');
+ expect(line).not.toContain('value-proto');
+ expect(({} as Record<string, unknown>).a).toBeUndefined();
+ });
+
it('never calls a toJSON on the metadata, so it cannot write what the redaction removed', () => {
logger.log('saved', { entity: { id: 1, toJSON: () => ({ password: 'value-json' }) } });
@@ -892,6 +904,33 @@ describe('redactSecrets', () => {
expect(copy.error.stack).toContain('Error: boom');
});
+ // JSON.parse makes a property named __proto__ an ordinary own property; the copy must define it as one too.
+ describe('a property named __proto__', () => {
+ const parsed = (): unknown => JSON.parse('{"__proto__":{"a":1,"password":"value-proto"},"b":2}');
+
+ it('is kept as a field of the copy, with the credentials under it redacted', () => {
+ const copy = redactSecrets(parsed()) as Record<string, unknown>;
+
+ expect(Object.keys(copy)).toEqual(['__proto__', 'b']);
+ expect(JSON.stringify(copy)).toBe('{"__proto__":{"a":1,"password":"[REDACTED]"},"b":2}');
+ expect(Object.getPrototypeOf(copy)).toBe(Object.prototype);
+ expect(copy.a).toBeUndefined();
+ });
+
+ it('is kept at every depth, in an array, and replaced when it names a credential', () => {
+ const deep = JSON.stringify(redactSecrets({ a: { b: { c: { d: { e: [parsed()] } } } } }));
+
+ expect(deep).toContain('"__proto__":{"a":1,"password":"[REDACTED]"}');
+ expect(deep).not.toContain('value-proto');
+ expect(redactSecrets(parsed(), { isSecretKey: key => key === '__proto__', marker: 'M' })).toEqual(
+ expect.objectContaining({ b: 2 }),
+ );
+ expect(JSON.stringify(redactSecrets(parsed(), { isSecretKey: key => key === '__proto__', marker: 'M' }))).toBe(
+ '{"__proto__":"M","b":2}',
+ );
+ });
+ });
+
it('copies an array as a plain array, whatever class it is an instance of', () => {
class Rows extends Array<unknown> {}
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 735216c43..4e05b5180 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -104,7 +104,11 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unkn
const val = source[key];
// JSON.stringify would call an own toJSON on the copy and write whatever it returns, redacted or not.
if (key === 'toJSON' && typeof val === 'function') continue;
- out[key] = isSecretKey(key) ? marker : copy(val);
+ const written = isSecretKey(key) ? marker : copy(val);
+ // Assigning to __proto__ would set the prototype of the copy; an own property of that name is defined instead.
+ if (key === '__proto__')
+ Object.defineProperty(out, key, { value: written, enumerable: true, writable: true, configurable: true });
+ else out[key] = written;
}
return out;
};
diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.ts
index fd8611b9a..c055742a3 100644
--- a/src/engine/adapters/baileys-logger.spec.ts
+++ b/src/engine/adapters/baileys-logger.spec.ts
@@ -195,6 +195,14 @@ describe('createBaileysLogger writes any record the library hands it', () => {
expect(deep).toMatchObject({ a: { b: { c: { d: { e: { file: '[Truncated]' } } } } }, msg: 'x' });
});
+ it('writes a property named __proto__ as a field, with the credentials under it redacted', () => {
+ const line = JSON.stringify(write(JSON.parse('{"__proto__":{"a":1,"password":"value-proto"},"b":2}'), 'm'));
+
+ expect(line).toContain('"__proto__":{"a":1,"password":"[redacted]"}');
+ expect(line).toContain('"b":2');
+ expect(line).not.toContain('value-proto');
+ });
+
it('counts a typed array by the elements it has, not by the length it claims', () => {
const file = Object.defineProperty(new Uint8Array(1_000_001), 'length', { value: 0 });