Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): keep the error text when bounding sandbox log meta
Yudhi Armyndharisf4519c276cf7
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.tsindex 532b054c3..74a529e9d 100644--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts@@ -532,4 +532,31 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {});expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] small', { n: 1, pluginId: 'p1' });});++ it('keeps the error text of a worker error log whose meta is oversized or not serializable', async () => {+ // logger.error's reason travels as meta.error, and the host error call gets no trace argument.+ const loader = makeLoader();+ seed(loader, { builtIn: false, instance: null });+ await loader.enablePlugin('p1');+ const errorSpy = jest+ .spyOn((loader as unknown as { logger: { error: jest.Mock } }).logger, 'error')+ .mockImplementation(() => undefined);++ loader.capturedOnLog!('error', 'big', { data: 'x'.repeat(20000), error: 'boom' });+ loader.capturedOnLog!('error', 'bigint', { n: 1n, error: 'boom' });+ loader.capturedOnLog!('error', 'long', { error: 'e'.repeat(10000) });+ loader.capturedOnLog!('error', 'nullmeta', null as unknown as Record<string, unknown>);++ expect(errorSpy).toHaveBeenNthCalledWith(1, '[p1] big', 'undefined', {+ error: 'boom',+ metaTruncated: true,+ metaLength: 20026,+ pluginId: 'p1',+ });+ expect(errorSpy).toHaveBeenNthCalledWith(2, '[p1] bigint', 'undefined', { error: 'boom', pluginId: 'p1' });+ const longMeta = errorSpy.mock.calls[2][2] as { error: string; metaTruncated: boolean };+ expect(longMeta.metaTruncated).toBe(true);+ expect(longMeta.error).toBe(`${'e'.repeat(8192)}…[truncated]`);+ expect(errorSpy).toHaveBeenNthCalledWith(4, '[p1] nullmeta', 'undefined', { pluginId: 'p1' });+ });});diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.tsindex 2cc13eb90..7eae891ec 100644--- a/src/core/plugins/plugin-sandbox-bridge.ts+++ b/src/core/plugins/plugin-sandbox-bridge.ts@@ -579,18 +579,20 @@ export class PluginSandboxBridge {// 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 =- text.length > SANDBOX_LOG_MAX_MESSAGE_LENGTH- ? `${text.slice(0, SANDBOX_LOG_MAX_MESSAGE_LENGTH)}…[truncated]`- : text;+ // 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));let boundedMeta = meta;if (meta !== undefined) {+ const reason = typeof meta?.error === 'string' ? { error: truncate(meta.error) } : undefined;try {const metaLength = JSON.stringify(meta).length;- if (metaLength > SANDBOX_LOG_MAX_MESSAGE_LENGTH) boundedMeta = { metaTruncated: true, metaLength };+ if (metaLength > SANDBOX_LOG_MAX_MESSAGE_LENGTH) boundedMeta = { ...reason, metaTruncated: true, metaLength };} catch {- boundedMeta = undefined; // not serializable (circular or BigInt), so it cannot be logged as JSON anyway+ boundedMeta = reason; // 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