Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(message): catch a template length that builds text
Yudhi Armyndharisf55f1cdd22cc
diff --git a/src/common/utils/template-render.spec.ts b/src/common/utils/template-render.spec.tsindex 3f7d5c2f5..6ffd539fe 100644--- a/src/common/utils/template-render.spec.ts+++ b/src/common/utils/template-render.spec.ts@@ -89,11 +89,13 @@ describe('renderedLength', () => {expect(renderedLength(body, vars)).toBe(renderTemplate(body, vars).length);});- it('measures a huge expansion without building it', () => {- // 1365 single-brace placeholders fit in a 4 KiB body; one 393 KB value makes half a gigabyte.+ it('measures an expansion too large to build without building it', () => {+ // 1365 single-brace placeholders fit in a 4 KiB body; a 1 MB value makes 1.4 billion characters, more than a+ // string can hold. A measure that built the text first would throw here instead of answering.const body = '{a}'.repeat(1365);- const vars = { a: 'x'.repeat(393_000) };- expect(renderedLength(body, vars)).toBe(1365 * 393_000);+ const vars = { a: 'x'.repeat(1_000_000) };+ expect(() => renderTemplate(body, vars)).toThrow(RangeError);+ expect(renderedLength(body, vars)).toBe(1365 * 1_000_000);});});diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.tsindex 6a842a768..73f7341f1 100644--- a/src/modules/message/message-send.service.spec.ts+++ b/src/modules/message/message-send.service.spec.ts@@ -639,19 +639,20 @@ describe('MessageSendService', () => {expect(mockEngine.sendTextMessage).not.toHaveBeenCalled();});- it('answers a 400 from the measured length, without building a render of hundreds of megabytes', async () => {- // 341 + 1365 + 341 placeholders fit in a header, body and footer at their size limits. One 393 KB value- // would build ~800 MB (a RangeError at the join, or an out-of-memory abort in a small container).+ it('answers a 400 from the measured length, without building a render too large for a string', async () => {+ // 341 + 1365 + 341 placeholders fit in a header, body and footer at their size limits. One 1 MB value makes+ // the body alone 1.4 billion characters, more than a string can hold: building any part of it would throw+ // a RangeError (a 500) instead of answering 400.(templateService.resolve as jest.Mock).mockResolvedValue(mockTemplate({ header: '{a}'.repeat(341), body: '{a}'.repeat(1365), footer: '{a}'.repeat(341) }),);const error = await service- .sendTemplate('sess-1', { chatId: '[email protected]', templateId: 'tpl-1', vars: { a: 'x'.repeat(393_000) } })+ .sendTemplate('sess-1', { chatId: '[email protected]', templateId: 'tpl-1', vars: { a: 'x'.repeat(1_000_000) } }).catch((e: unknown) => e);expect(error).toBeInstanceOf(BadRequestException);- expect((error as Error).message).toContain(`Rendered template is ${2047 * 393_000 + 4} characters`);+ expect((error as Error).message).toContain(`Rendered template is ${2047 * 1_000_000 + 4} characters`);expect(mockEngine.sendTextMessage).not.toHaveBeenCalled();});