Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): cap the link preview body at its byte budget
Yudhi Armyndharis913ce689aa02
diff --git a/src/engine/adapters/safe-link-preview.spec.ts b/src/engine/adapters/safe-link-preview.spec.tsindex 291b5952c..3b53ca9b2 100644--- a/src/engine/adapters/safe-link-preview.spec.ts+++ b/src/engine/adapters/safe-link-preview.spec.ts@@ -1,3 +1,4 @@+import { Response } from 'undici';import { createServer, type Server } from 'node:http';import type { AddressInfo } from 'node:net';import { generateSafeLinkPreview } from './safe-link-preview';@@ -153,6 +154,46 @@ describe('generateSafeLinkPreview', () => {});});+ describe('the HTML byte prefix', () => {+ const bytes = (...chunks: string[]) => chunks.map(chunk => Buffer.from(chunk));+ it.each([+ ['an oversized first chunk', bytes(' '.repeat(64) + '<title>Beyond</title>'), undefined],+ ['an oversized final chunk', bytes(' '.repeat(48), ' '.repeat(16) + '<title>Beyond</title>'), undefined],+ ['Open Graph beyond the cap', bytes(' '.repeat(64) + '<meta property="og:title" content="Beyond">'), undefined],+ ['valid prefix metadata', bytes('<title>Within</title>' + ' '.repeat(44) + '<title>Beyond</title>'), 'Within'],+ ['metadata ending at the cap', bytes(' '.repeat(44) + '<title>Exact</title>'), 'Exact'],+ [+ 'a split multibyte character',+ [+ Buffer.concat([Buffer.from(' '.repeat(62)), Buffer.from([0xc3])]),+ Buffer.concat([Buffer.from([0xa9]), Buffer.from('<title>Beyond</title>')]),+ ],+ undefined,+ ],+ ] as const)('reads only the allowed bytes for %s', async (_name, chunks, title) => {+ let index = 0;+ const cancel = jest.fn();+ const body = new ReadableStream<Uint8Array>(+ {+ pull(controller) {+ if (index < chunks.length) controller.enqueue(chunks[index++]);+ else controller.close();+ },+ cancel,+ },+ { highWaterMark: 0 },+ );+ const response = new Response(body, { headers: { 'content-type': 'text/html' } });+ jest.spyOn(ssrfGuard, 'withSafeFetch').mockImplementation((_url, _init, use) => Promise.resolve(use(response)));++ const preview = await generateSafeLinkPreview('https://example.com', { maxBytes: 64 });++ expect(preview?.title).toBe(title);+ expect(cancel).toHaveBeenCalledTimes(1);+ expect(index).toBe(chunks.length);+ });+ });+describe('what it extracts', () => {it('prefers Open Graph metadata', async () => {respondWith(diff --git a/src/engine/adapters/safe-link-preview.ts b/src/engine/adapters/safe-link-preview.tsindex bb0244db7..9d28cf71b 100644--- a/src/engine/adapters/safe-link-preview.ts+++ b/src/engine/adapters/safe-link-preview.ts@@ -142,7 +142,8 @@ async function readCapped(response: Response, maxBytes: number): Promise<string>for (;;) {const chunk = await reader.read();if (chunk.done) break;- const value: Uint8Array = chunk.value;+ // The chunk that reaches the cap may run past it: only the bytes up to the cap are kept.+ const value: Uint8Array = chunk.value.subarray(0, Math.max(0, maxBytes - read));read += value.byteLength;out += decoder.decode(value, { stream: true });if (read >= maxBytes) {