Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): bound animated sticker conversion (#1787)
Yudhi Armyndharis18a8e68b5254
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ea8d67557..bf0abb252 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Refuse Baileys animated stickers over 500 frames and run at most two sticker conversions at once.- Stop pending webhook deliveries from holding over-cap inline media already omitted from their payload.- Resolve `./data/...` paths from `.env.generated` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs.- Stop `backup.sh` and `restore.sh` on an env line they cannot parse instead of falling back to the default path.@@ -64,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Upgrade notes (behavior changes)+- Baileys answers `400` for an animated sticker with more than 500 frames.- `backup.sh` and `restore.sh` exit with status 2 when `./.env` or `.env.generated` sets a key in a form they cannot parse; fix the line or pass the key in the environment.- With `DATABASE_SSL=true`, `backup.sh` now fails against a PostgreSQL server whose certificate the app would reject; trust a private CA through `NODE_EXTRA_CA_CERTS` or `PGSSLROOTCERT`.diff --git a/docs/29-engine-capability-matrix.md b/docs/29-engine-capability-matrix.mdindex ee43e58e8..4720bca15 100644--- a/docs/29-engine-capability-matrix.md+++ b/docs/29-engine-capability-matrix.md@@ -931,11 +931,11 @@ adapter boundary — none silently stubs.`image/webp` on the payload unconditionally and transcodes nothing(`prepareWAMessageMedia` → `MIMETYPE_MAP.sticker`), so the adapter converts before the socket:WebP passes through byte-identical (preserving its sticker-pack EXIF), other `image/*` input is- re-encoded to a 512×512 WebP with animation retained, and everything else — **including- `video/*`** — is refused with a `400`. So an animated-video sticker works on wwjs and answers- `400` on Baileys. ffmpeg is deliberately not wired in on the Baileys side: the binary ships only- in the Docker image, so depending on it would make the same request succeed or fail depending on- how the gateway was installed.+ re-encoded to a 512×512 WebP with animation retained (up to 500 frames; a longer animation is+ refused with a `400`), and everything else — **including `video/*`** — is refused with a `400`.+ So an animated-video sticker works on wwjs and answers `400` on Baileys. ffmpeg is deliberately+ not wired in on the Baileys side: the binary ships only in the Docker image, so depending on it+ would make the same request succeed or fail depending on how the gateway was installed.- **`deleteStatus` (baileys).** Baileys sends a status stanza, the revoke included, to exactly its`statusJidList`, so the revoke is addressed to the recipients the adapter remembered when it postedthe status. It keeps them in memory for 24 hours, so a status this session did not post in the lastdiff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex b4301450c..f47b8b2d5 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -43,6 +43,7 @@ import { EngineTransportError } from '../../common/errors/engine-transport.errorimport { EngineNotSentError } from '../../common/errors/engine-not-sent.error';import { parseWaId, userPart } from '../identity/wa-id';import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';+import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';/*** Messaging-domain operations extracted from BaileysAdapter. The adapter keeps the public@@ -137,6 +138,19 @@ export async function loadSharp() {}}+/**+ * Animation frames a sticker conversion accepts: a 10-second animation at 50 fps. Each frame is+ * resized and encoded at 512x512 (a few ms apiece), so cost is linear in the frame count, and a+ * GIF of 1x1 frames packs tens of thousands of them into well under a megabyte.+ */+export const MAX_STICKER_FRAMES = 500;++/**+ * Conversions running at once, process-wide. A sharp pipeline holds a libuv threadpool worker+ * (4 by default) for its whole run, and that pool also serves fs, dns.lookup, crypto and zlib.+ */+const stickerConversions = new ConcurrencyLimiter(2);+async function toWebpSticker(data: Buffer, mimetype: string): Promise<Buffer> {if (isWebpBuffer(data)) {return data;@@ -156,11 +170,22 @@ async function toWebpSticker(data: Buffer, mimetype: string): Promise<Buffer> {// deferral the adapters already use for the engine libraries themselves.const sharp = await loadSharp();try {- return await sharp(data, { animated: true })- .resize(512, 512, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })- .webp()- .toBuffer();+ const image = sharp(data, { animated: true });+ // Counting frames only scans the file, so an oversized animation is refused before any decode.+ const { pages = 1 } = await image.metadata();+ if (pages > MAX_STICKER_FRAMES) {+ throw new BadRequestException(+ `The sticker animation has ${pages} frames; at most ${MAX_STICKER_FRAMES} can be converted.`,+ );+ }+ return await stickerConversions.run(() =>+ image+ .resize(512, 512, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })+ .webp()+ .toBuffer(),+ );} catch (error) {+ if (error instanceof BadRequestException) throw error;// Bytes that do not decode as the image they claim to be. Refuse before the socket rather than// ship them mislabelled — that is the whole point.throw new BadRequestException(diff --git a/src/engine/adapters/baileys-sticker-webp.spec.ts b/src/engine/adapters/baileys-sticker-webp.spec.tsindex eeb3501e4..bb462195d 100644--- a/src/engine/adapters/baileys-sticker-webp.spec.ts+++ b/src/engine/adapters/baileys-sticker-webp.spec.ts@@ -1,7 +1,7 @@import type { WASocket } from '@whiskeysockets/baileys';import { BadRequestException } from '@nestjs/common';import sharp from 'sharp';-import { BaileysMessaging, type BaileysMessagingHost } from './baileys-messaging';+import { BaileysMessaging, MAX_STICKER_FRAMES, type BaileysMessagingHost } from './baileys-messaging';import { createLogger } from '../../common/services/logger.service';/**@@ -38,6 +38,18 @@ const ANIMATED_GIF = Buffer.from('base64',);+/** A GIF89a of `frames` 1x1 frames: about 23 bytes each, so a large count stays a small upload. */+function manyFrameGif(frames: number): Buffer {+ const head = Buffer.concat([+ Buffer.from('GIF89a'),+ Buffer.from([1, 0, 1, 0, 0x80, 0, 0, 0, 0, 0, 255, 255, 255, 0x21, 0xff, 0x0b]),+ Buffer.from('NETSCAPE2.0'),+ Buffer.from([3, 1, 0, 0, 0]),+ ]);+ const frame = Buffer.from([0x21, 0xf9, 4, 0, 1, 0, 0, 0, 0x2c, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 0x44, 1, 0]);+ return Buffer.concat([head, ...Array<Buffer>(frames).fill(frame), Buffer.from([0x3b])]);+}+const isWebp = (b: unknown): boolean =>Buffer.isBuffer(b) &&b.length > 12 &&@@ -130,6 +142,58 @@ describe('BaileysMessaging.sendStickerMessage — what reaches the socket must rexpect((await sharp(sent, { animated: true }).metadata()).pages).toBe(2);});+ // Conversion cost is linear in the frame count, and 1x1 frames make that count nearly free to+ // inflate: 25,000 of them fit in under 600 KB and keep a threadpool worker busy for tens of seconds.+ it('an animation with too many frames is refused as a 400 without being converted', async () => {+ const { messaging, sock } = makeMessaging();+ const convert = jest.spyOn(sharp.prototype, 'toBuffer');++ try {+ const send = messaging.sendStickerMessage('[email protected]', {+ data: manyFrameGif(MAX_STICKER_FRAMES + 1),+ mimetype: 'image/gif',+ });++ await expect(send).rejects.toBeInstanceOf(BadRequestException);+ await expect(send).rejects.toThrow(`has ${MAX_STICKER_FRAMES + 1} frames; at most ${MAX_STICKER_FRAMES}`);+ expect(convert).not.toHaveBeenCalled();+ expect(sock.sendMessage).not.toHaveBeenCalled();+ } finally {+ convert.mockRestore();+ }+ });++ // Each pipeline holds a libuv worker for its whole run; unbounded, a handful of concurrent+ // conversions occupy the pool that fs, dns.lookup and crypto also wait on.+ it('runs at most two conversions at once and queues the rest', async () => {+ const pending: Array<(b: Buffer) => void> = [];+ const metadata = jest.spyOn(sharp.prototype, 'metadata').mockResolvedValue({ pages: 1 });+ const convert = jest+ .spyOn(sharp.prototype, 'toBuffer')+ .mockImplementation(() => new Promise<Buffer>(resolve => pending.push(resolve)));+ const settle = () => new Promise(resolve => setImmediate(resolve));++ try {+ const { messaging, sock } = makeMessaging();+ const sends = [1, 2, 3].map(() =>+ messaging.sendStickerMessage('[email protected]', { data: PNG, mimetype: 'image/png' }),+ );+ await settle();+ expect(convert).toHaveBeenCalledTimes(2);++ pending.shift()!(WEBP);+ await settle();+ expect(convert).toHaveBeenCalledTimes(3);++ pending.splice(0).forEach(resolve => resolve(WEBP));+ await Promise.all(sends);+ expect(sock.sendMessage).toHaveBeenCalledTimes(3);+ } finally {+ metadata.mockRestore();+ convert.mockRestore();+ }+ });+it('input that cannot become a sticker is refused BEFORE the socket, as a 400', async () => {const { messaging, sock } = makeMessaging();