Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): bound sandbox log meta and non-string messages

Yudhi Armyndharisf3cbcdc2e776
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
index 057a6fa01..532b054c3 100644
--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
@@ -502,4 +502,34 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
expect(relayed).toContain('…[truncated]');
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);
+
+ loader.capturedOnLog!('log', ['x'.repeat(6000), 'y'.repeat(6000)] as unknown as string);
+
+ const relayed = logSpy.mock.calls[0][0] as string;
+ expect(relayed.length).toBe('[p1] '.length + 8192 + '…[truncated]'.length);
+ });
+
+ it('replaces an oversized worker log meta with a size marker instead of relaying it', async () => {
+ const loader = makeLoader();
+ seed(loader, { builtIn: false, instance: null });
+ await loader.enablePlugin('p1');
+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);
+
+ loader.capturedOnLog!('log', 'big', { data: 'x'.repeat(20000) });
+ loader.capturedOnLog!('log', 'small', { n: 1 });
+
+ expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] big', {
+ metaTruncated: true,
+ metaLength: 20011,
+ pluginId: 'p1',
+ });
+ expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] small', { n: 1, pluginId: 'p1' });
+ });
});
diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.ts
index 4a4acd4af..2cc13eb90 100644
--- a/src/core/plugins/plugin-sandbox-bridge.ts
+++ b/src/core/plugins/plugin-sandbox-bridge.ts
@@ -576,15 +576,28 @@ 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).
+ const text = typeof message === 'string' ? message : String(message);
const bounded =
- typeof message === 'string' && message.length > SANDBOX_LOG_MAX_MESSAGE_LENGTH
- ? `${message.slice(0, SANDBOX_LOG_MAX_MESSAGE_LENGTH)}…[truncated]`
- : message;
+ text.length > SANDBOX_LOG_MAX_MESSAGE_LENGTH
+ ? `${text.slice(0, SANDBOX_LOG_MAX_MESSAGE_LENGTH)}…[truncated]`
+ : text;
+ let boundedMeta = meta;
+ if (meta !== undefined) {
+ try {
+ const metaLength = JSON.stringify(meta).length;
+ if (metaLength > SANDBOX_LOG_MAX_MESSAGE_LENGTH) boundedMeta = { metaTruncated: true, metaLength };
+ } catch {
+ boundedMeta = undefined; // not serializable (circular or BigInt), so it cannot be logged as JSON anyway
+ }
+ }
// The level comes off the wire unchecked (plugin code can post to parentPort directly), and the
// plugin logger has no method for anything outside PluginLogLevel.
- if (level === 'error') context.logger.error(bounded, undefined, meta);
- else if (level === 'debug' || level === 'warn') context.logger[level](bounded, meta);
- else context.logger.log(bounded, meta);
+ if (level === 'error') context.logger.error(bounded, undefined, boundedMeta);
+ else if (level === 'debug' || level === 'warn') context.logger[level](bounded, boundedMeta);
+ else context.logger.log(bounded, boundedMeta);
};
}