Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): spread only a plain record as a log meta

Yudhi Armyndharisea01ffb4ab1c
diff --git a/src/core/plugins/plugin-capability-context.ts b/src/core/plugins/plugin-capability-context.ts
index 3a60d5ce0..3d9fe6b86 100644
--- a/src/core/plugins/plugin-capability-context.ts
+++ b/src/core/plugins/plugin-capability-context.ts
@@ -59,11 +59,15 @@ export function dispatchConversationMedia(
/** The trace for a plugin's ctx.logger.error: an Error's message, or the text of whatever else was passed. */
const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error));
-/** A plugin's log meta as the fields of the line: a record as it is, nothing as no fields, anything else as one field. */
+/** A prototype that is the root of a chain (Object.prototype of any realm, or none): what a plain record has. */
+const isRootPrototype = (prototype: object | null): boolean =>
+ prototype === null || Object.getPrototypeOf(prototype) === null;
+
+/** A plugin's log meta as the fields of the line: a plain record as it is, nothing as no fields, else as one field. */
const metaFields = (meta: unknown): Record<string, unknown> =>
meta === undefined || meta === null
? {}
- : typeof meta === 'object' && !Array.isArray(meta)
+ : typeof meta === 'object' && isRootPrototype(Object.getPrototypeOf(meta) as object | null)
? (meta as Record<string, unknown>)
: { meta };
diff --git a/src/core/plugins/plugin-capability-logger.spec.ts b/src/core/plugins/plugin-capability-logger.spec.ts
index 2138883d8..6da47fefd 100644
--- a/src/core/plugins/plugin-capability-logger.spec.ts
+++ b/src/core/plugins/plugin-capability-logger.spec.ts
@@ -94,6 +94,39 @@ describe('the logger a plugin gets', () => {
}
});
+ // Only a plain record is spread into the fields of the line; every other object is one value.
+ it.each([
+ ['a String wrapper', (): unknown => new String('hello')],
+ ['a Uint8Array', (): unknown => new Uint8Array([1, 2, 3])],
+ ['a Map', (): unknown => new Map([[1, 2]])],
+ ['a Date', (): unknown => new Date(0)],
+ ['an Error', (): unknown => new Error('boom')],
+ ['an instance of a class', (): unknown => new (class Model {})()],
+ ])('writes %s as one field', (_name, make) => {
+ const { host, logger } = setup();
+ const meta = make();
+
+ logger.warn('m', asRecord(meta));
+
+ expect(host.warn).toHaveBeenCalledWith('[p1] m', { meta, pluginId: 'p1' });
+ expect(Object.keys(metaOf(host.warn, 1) as object)).toEqual(['meta', 'pluginId']);
+ });
+
+ it.each([
+ ['an object literal', (): unknown => ({ sessionId: 's1' })],
+ [
+ 'an object without a prototype',
+ (): unknown => Object.assign(Object.create(null) as object, { sessionId: 's1' }),
+ ],
+ ['a record made in another realm', (): unknown => structuredClone({ sessionId: 's1' })],
+ ])('keeps %s as the fields of the line', (_name, make) => {
+ const { host, logger } = setup();
+
+ logger.warn('m', asRecord(make()));
+
+ expect(metaOf(host.warn, 1)).toEqual({ sessionId: 's1', pluginId: 'p1' });
+ });
+
it('keeps the fields of a record as they are', () => {
const { host, logger } = setup();
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
index 71c99028b..fd14aa2b2 100644
--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
@@ -746,9 +746,20 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
loader.capturedOnLog!('log', 'string', 'abc' as unknown as Record<string, unknown>);
loader.capturedOnLog!('log', 'array', [1, 2] as unknown as Record<string, unknown>);
+ loader.capturedOnLog!(
+ 'log',
+ 'bytes',
+ structuredClone(new Uint8Array([1, 2])) as unknown as Record<string, unknown>,
+ );
+ loader.capturedOnLog!('log', 'boxed', structuredClone(new String('ab')) as unknown as Record<string, unknown>);
+ loader.capturedOnLog!('log', 'record', structuredClone({ id: 1 }));
expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] string', { meta: 'abc', pluginId: 'p1' });
expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] array', { meta: [1, 2], pluginId: 'p1' });
+ expect(logSpy).toHaveBeenNthCalledWith(3, '[p1] bytes', { meta: new Uint8Array([1, 2]), pluginId: 'p1' });
+ expect(Object.keys(logSpy.mock.calls[3][1] as object)).toEqual(['meta', 'pluginId']);
+ expect(String((logSpy.mock.calls[3][1] as { meta: unknown }).meta)).toBe('ab');
+ expect(logSpy).toHaveBeenNthCalledWith(5, '[p1] record', { id: 1, pluginId: 'p1' });
});
it('keeps the error text of a worker error log whose meta is oversized or not serializable', async () => {