Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): run the sticker metadata scan inside the conversion limiter (#1849)

Yudhi Armyndharis2bddec29c70d
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 718dab5d0..7bc0e4160 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -2093,7 +2093,7 @@ Send a contact card (vCard).
#### POST /api/sessions/:sessionId/messages/send-sticker
-Send a sticker (by URL or base64; typically webp). Reuses `SendMediaMessageDto`.
+Send a sticker (by URL or base64; typically webp). Reuses `SendMediaMessageDto`. On Baileys a WebP is sent unchanged and any other `image/*` is converted to a 512x512 WebP first, keeping up to 500 animation frames.
**Auth:** API key (OPERATOR)
@@ -2115,7 +2115,7 @@ Send a sticker (by URL or base64; typically webp). Reuses `SendMediaMessageDto`.
{ "messageId": "[email protected]_3EB0ABCD", "timestamp": 1719312000 }
```
-**Errors:** `400` media validation failure / a `url` that answers non-2xx, times out or cannot be reached / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `404` the `quotedMessageId` could not be resolved (see Quoted sends) · `413` base64 or downloaded media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine, or (whatsapp-web.js) a sticker to a channel, `status@broadcast` or a broadcast list · `503` a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault (retryable) · `422` the `Idempotency-Key` was already used for a different request (see [Idempotent sends](#idempotent-sends))
+**Errors:** `400` media validation failure / (Baileys) media that is neither WebP nor an image that converts to one, or an animation with more than 500 frames / a `url` that answers non-2xx, times out or cannot be reached / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `404` the `quotedMessageId` could not be resolved (see Quoted sends) · `413` base64 or downloaded media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine, or (whatsapp-web.js) a sticker to a channel, `status@broadcast` or a broadcast list · `503` a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault (retryable) · `422` the `Idempotency-Key` was already used for a different request (see [Idempotent sends](#idempotent-sends))
#### POST /api/sessions/:sessionId/messages/send-poll
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index f47b8b2d5..2478179c2 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -146,8 +146,9 @@ export async function loadSharp() {
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.
+ * Conversions running at once, process-wide. Each sharp call (the frame scan, then the 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);
@@ -171,19 +172,21 @@ async function toWebpSticker(data: Buffer, mimetype: string): Promise<Buffer> {
const sharp = await loadSharp();
try {
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
+ return await stickerConversions.run(async () => {
+ // Counting frames only scans the file, so an oversized animation is refused before any decode.
+ // The scan still runs on the threadpool and grows faster than the frame count, so it holds the
+ // same slot as the pipeline.
+ 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 image
.resize(512, 512, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.webp()
- .toBuffer(),
- );
+ .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
diff --git a/src/engine/adapters/baileys-sticker-webp.spec.ts b/src/engine/adapters/baileys-sticker-webp.spec.ts
index bb462195d..a0fc82386 100644
--- a/src/engine/adapters/baileys-sticker-webp.spec.ts
+++ b/src/engine/adapters/baileys-sticker-webp.spec.ts
@@ -164,8 +164,9 @@ describe('BaileysMessaging.sendStickerMessage — what reaches the socket must r
});
// 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 () => {
+ // conversions occupy the pool that fs, dns.lookup and crypto also wait on. The frame scan is
+ // threadpool work too, and on a GIF of tens of MB it takes seconds, so it waits for a slot as well.
+ it('runs at most two conversions at once, frame scan included, and queues the rest', async () => {
const pending: Array<(b: Buffer) => void> = [];
const metadata = jest.spyOn(sharp.prototype, 'metadata').mockResolvedValue({ pages: 1 });
const convert = jest
@@ -179,10 +180,12 @@ describe('BaileysMessaging.sendStickerMessage — what reaches the socket must r
messaging.sendStickerMessage('[email protected]', { data: PNG, mimetype: 'image/png' }),
);
await settle();
+ expect(metadata).toHaveBeenCalledTimes(2);
expect(convert).toHaveBeenCalledTimes(2);
pending.shift()!(WEBP);
await settle();
+ expect(metadata).toHaveBeenCalledTimes(3);
expect(convert).toHaveBeenCalledTimes(3);
pending.splice(0).forEach(resolve => resolve(WEBP));