Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): relay a non-string log message as a marker

Yudhi Armyndharis13157907f2b9
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
index 1321fe7b1..5ff94785b 100644
--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
@@ -686,17 +686,43 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
expect(relayed.length).toBe('[p1] '.length + 8192 + '…[truncated]'.length);
});
- it('bounds a non-string worker log message like a string one', async () => {
- // Plugin code can post to parentPort directly, so the message is untrusted and may not be a string.
- const loader = makeLoader();
- seed(loader, { builtIn: false, instance: null });
- await loader.enablePlugin('p1');
- const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);
+ describe('a non-string worker log message', () => {
+ // Plugin code can post to parentPort directly, so the message is untrusted and may not be a string. It is not
+ // turned into text, which for an array would write out every shared reference at every use.
+ const sharedArray = (levels: number): unknown[] => {
+ let node: unknown[] = [1];
+ for (let level = 0; level < levels; level++) node = new Array<unknown>(10).fill(node);
+ return node;
+ };
- loader.capturedOnLog!('log', ['x'.repeat(6000), 'y'.repeat(6000)] as unknown as string);
+ it.each([
+ ['an array', ['x'.repeat(6000), 'y'.repeat(6000)]],
+ ['a number', 42],
+ ['an object', { text: 'x'.repeat(10000) }],
+ ['null', null],
+ ])('is relayed as a fixed marker when it is %s', async (_name, message) => {
+ const loader = makeLoader();
+ seed(loader, { builtIn: false, instance: null });
+ await loader.enablePlugin('p1');
+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);
- const relayed = logSpy.mock.calls[0][0] as string;
- expect(relayed.length).toBe('[p1] '.length + 8192 + '…[truncated]'.length);
+ loader.capturedOnLog!('log', message as unknown as string);
+
+ expect(logSpy).toHaveBeenCalledWith('[p1] [non-string log message]', { pluginId: 'p1' });
+ });
+
+ it('is relayed at once when it is an array whose levels share the same child', async () => {
+ const loader = makeLoader();
+ seed(loader, { builtIn: false, instance: null });
+ await loader.enablePlugin('p1');
+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);
+
+ const started = Date.now();
+ loader.capturedOnLog!('log', sharedArray(7) as unknown as string);
+
+ expect(Date.now() - started).toBeLessThan(1000);
+ expect(logSpy).toHaveBeenCalledWith('[p1] [non-string log message]', { pluginId: 'p1' });
+ });
});
it('replaces an oversized worker log meta with a size marker instead of relaying it', async () => {
diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.ts
index 4b49f250c..c787402d3 100644
--- a/src/core/plugins/plugin-sandbox-bridge.ts
+++ b/src/core/plugins/plugin-sandbox-bridge.ts
@@ -577,15 +577,16 @@ export class PluginSandboxBridge {
state.dropped++;
return;
}
- // Like the level below, the message and meta come off the wire unchecked: a non-string message is coerced
- // before the length check, and a meta whose JSON form exceeds the same cap is replaced by a size
- // marker (not a truncated string, which would hide its keys from the logger's secret redaction).
+ // Like the level below, the message and meta come off the wire unchecked: a non-string message is replaced by
+ // a fixed marker (turning an array into text would write a shared reference out at every use), and a meta
+ // whose JSON form exceeds the same cap is replaced by a size marker (not a truncated string, which would hide
+ // its keys from the logger's secret redaction).
// logger.error's reason travels as a string meta.error, so that one key survives the replacement.
const truncate = (value: string): string =>
value.length > SANDBOX_LOG_MAX_MESSAGE_LENGTH
? `${value.slice(0, SANDBOX_LOG_MAX_MESSAGE_LENGTH)}…[truncated]`
: value;
- const bounded = truncate(typeof message === 'string' ? message : String(message));
+ const bounded = truncate(typeof message === 'string' ? message : '[non-string log message]');
let boundedMeta = meta;
if (meta !== undefined) {
const reason = typeof meta?.error === 'string' ? { error: truncate(meta.error) } : undefined;