Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): refuse SVG input to the image decoder

Yudhi Armyndharis69e8cdab47a9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 13d955597..2835ddd7d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CI and the weekly security scan run `npm audit` over the JavaScript SDK, and Dependabot watches its lockfile.
- The Baileys linked-device name defaults to `Ubuntu` instead of `OpenWA`, so phone-number pairing works without setting `BAILEYS_BROWSER_NAME`.
- The `/events` WebSocket echoes a `requestId` only when it is a string of at most 128 characters; any other value is dropped from the reply.
+- The Baileys engine refuses SVG input to its image decoder, so a sticker sent as an SVG answers `400` and an outbound image's thumbnail skips it; raster images convert as before.
### Fixed
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 2478179c2..da68b32d9 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -138,6 +138,22 @@ export async function loadSharp() {
}
}
+/**
+ * Refuse SVG input process-wide. OpenWA never needs it, yet a sticker and the thumbnail of every outbound
+ * image hand caller-supplied bytes to sharp, which autodetects SVG and parses it with librsvg: a native
+ * decoder with a memory-safety record that is the largest attack surface on those two routes. The block is
+ * libvips state shared by every importer of sharp, Baileys included, so it has to be set before the first
+ * decode and not by the first sticker. Best effort: a sharp that cannot load has nothing to block, and the
+ * routes that need it report that themselves (see loadSharp).
+ */
+export async function blockSvgDecoding(): Promise<void> {
+ try {
+ (await import('sharp')).default.block({ operation: ['VipsForeignLoadSvg'] });
+ } catch {
+ // sharp could not load, so there is no decoder to block.
+ }
+}
+
/**
* 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
diff --git a/src/engine/adapters/baileys-sticker-webp.spec.ts b/src/engine/adapters/baileys-sticker-webp.spec.ts
index a0fc82386..496a538c9 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, MAX_STICKER_FRAMES, type BaileysMessagingHost } from './baileys-messaging';
+import { BaileysMessaging, MAX_STICKER_FRAMES, blockSvgDecoding, type BaileysMessagingHost } from './baileys-messaging';
import { createLogger } from '../../common/services/logger.service';
/**
@@ -214,3 +214,23 @@ describe('BaileysMessaging.sendStickerMessage — what reaches the socket must r
expect(sock.sendMessage).not.toHaveBeenCalled();
});
});
+
+describe('blockSvgDecoding', () => {
+ const SVG = Buffer.from(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8"/></svg>',
+ );
+ // The block is libvips process state: start from the default and leave it as found, so no other
+ // spec in the same worker sees it.
+ const reset = () => sharp.unblock({ operation: ['VipsForeignLoadSvg'] });
+ beforeEach(reset);
+ afterAll(reset);
+
+ it('refuses SVG input, which a sticker or an outbound image thumbnail would hand to librsvg, and still decodes a raster', async () => {
+ await expect(sharp(SVG).metadata()).resolves.toMatchObject({ format: 'svg' });
+
+ await blockSvgDecoding();
+
+ await expect(sharp(SVG).metadata()).rejects.toThrow();
+ await expect(sharp(PNG).metadata()).resolves.toMatchObject({ format: 'png' });
+ });
+});
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 6795dd128..43feab62f 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -2,6 +2,7 @@ import { EventEmitter } from 'events';
import * as fs from 'fs';
import * as path from 'path';
import * as qrcode from 'qrcode';
+import * as messagingModule from './baileys-messaging';
jest.mock('../../common/media/load-remote-media', () => ({
loadRemoteMediaBuffer: jest.fn(),
@@ -282,6 +283,14 @@ describe('BaileysAdapter lifecycle & status', () => {
expect(adapter.getQRCode()).toBe(rendered);
});
+ it('blocks SVG decoding before the engine starts, whichever route reaches sharp first', async () => {
+ // Sticker conversion and the thumbnail of every outbound image share one process-wide decoder, so the
+ // block cannot wait for the first sticker.
+ const block = jest.spyOn(messagingModule, 'blockSvgDecoding').mockResolvedValue(undefined);
+ await newAdapter().initialize(noopCallbacks({}));
+ expect(block).toHaveBeenCalledTimes(1);
+ });
+
it('captures phone/pushName and fires onReady on connection open', async () => {
const onReady = jest.fn();
const adapter = newAdapter();
diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.ts
index 9c6f8d60f..4e7a5a7bd 100644
--- a/src/engine/adapters/baileys.adapter.ts
+++ b/src/engine/adapters/baileys.adapter.ts
@@ -12,7 +12,7 @@ import { BaileysHistory, toUnixSeconds } from './baileys-history';
import { type BaileysEngineHost } from './baileys-host';
import { OwnSendRegistry } from './baileys-own-sends';
import { BaileysLifecycle } from './baileys-lifecycle';
-import { BaileysMessaging } from './baileys-messaging';
+import { BaileysMessaging, blockSvgDecoding } from './baileys-messaging';
import { BaileysStatus } from './baileys-status';
import {
CallLinkType,
@@ -236,6 +236,7 @@ export class BaileysAdapter implements IWhatsAppEngine {
async initialize(callbacks: EngineEventCallbacks): Promise<void> {
this.callbacks = callbacks;
+ await blockSvgDecoding();
return this.lifecycle.initialize();
}