Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): answer 400 for a picture the decoder cannot read

Yudhi Armyndharisb8e58ccb17d9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2f92c737..8e6094a27 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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.
- A `/events` WebSocket `unsubscribe` whose `sessionId` is neither `*` nor a session id of at most 128 letters, digits and hyphens answers `INVALID_SESSION`, as `subscribe` does.
- 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.
+- The Baileys engine refuses SVG input to its image decoder, so a sticker, profile picture or group picture sent as an SVG answers `400` and an outbound image's thumbnail skips it; raster images convert as before.
### Fixed
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index f3b68a0ac..34d5f16c9 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -2893,7 +2893,7 @@ Set the group's picture. The account must be a group admin.
**Response** `200` — `{ "success": true, "message": "Group picture updated" }`
-**Errors:** `400` the id does not name a group, the session is not active, neither `url` nor `base64` was supplied, or the `url` answers non-2xx, times out or cannot be reached · `401` missing/invalid API key · `403` key lacks OPERATOR role, or the engine refused (admin rights required) · `404` no such group · `409` the session is not connected (engine exists but is not `ready`) · `413` base64 or downloaded media over the media cap (see §6.3) · `503` a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault (retryable), or WhatsApp did not answer within the request budget — the change may or may not have been applied; on Baileys, also when WhatsApp rate-limits or times out the request (code 429 or 408): retry after a pause
+**Errors:** `400` the id does not name a group, the session is not active, neither `url` nor `base64` was supplied, the `url` answers non-2xx, times out or cannot be reached, or the image cannot be read (Baileys refuses an SVG) · `401` missing/invalid API key · `403` key lacks OPERATOR role, or the engine refused (admin rights required) · `404` no such group · `409` the session is not connected (engine exists but is not `ready`) · `413` base64 or downloaded media over the media cap (see §6.3) · `503` a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault (retryable), or WhatsApp did not answer within the request budget — the change may or may not have been applied; on Baileys, also when WhatsApp rate-limits or times out the request (code 429 or 408): retry after a pause
#### DELETE /api/sessions/:sessionId/groups/:groupId/picture
@@ -6615,7 +6615,7 @@ or
**Response** `200` — `{ "success": true, "message": "Profile picture updated" }`
-**Errors:** `400` neither `url` nor `base64` provided / base64 without `mimetype` / a `url` that answers non-2xx, times out or cannot be reached · `401` · `403` the whatsapp-web.js engine refused the change (the Baileys engine has no acceptance signal and answers `200`) · `409` conflict or engine not ready (retryable) · `413` base64 or downloaded image over the media cap (see §6.3) · `503` session not ready, dependency unavailable, or a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault (retryable)
+**Errors:** `400` neither `url` nor `base64` provided / base64 without `mimetype` / a `url` that answers non-2xx, times out or cannot be reached / an image that cannot be read (Baileys refuses an SVG) · `401` · `403` the whatsapp-web.js engine refused the change (the Baileys engine has no acceptance signal and answers `200`) · `409` conflict or engine not ready (retryable) · `413` base64 or downloaded image over the media cap (see §6.3) · `503` session not ready, dependency unavailable, or a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault (retryable)
#### DELETE /api/sessions/:sessionId/profile/picture
diff --git a/openapi.json b/openapi.json
index a0a3e2573..64cee6dd6 100644
--- a/openapi.json
+++ b/openapi.json
@@ -11336,7 +11336,7 @@
}
},
"400": {
- "description": "The id does not name a group, the session is not active, neither url nor base64 was supplied, or the url answers non-2xx, times out or cannot be reached",
+ "description": "The id does not name a group, the session is not active, neither url nor base64 was supplied, the url answers non-2xx, times out or cannot be reached, or the image cannot be read (Baileys refuses an SVG)",
"content": {
"application/json": {
"schema": {
@@ -11933,7 +11933,7 @@
}
},
"400": {
- "description": "Neither url nor base64 provided, base64 without mimetype, session is not started, or the url answers non-2xx, times out or cannot be reached",
+ "description": "Neither url nor base64 provided, base64 without mimetype, session is not started, the url answers non-2xx, times out or cannot be reached, or the image cannot be read (Baileys refuses an SVG)",
"content": {
"application/json": {
"schema": {
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.ts
index 17513fe3c..45f685c05 100644
--- a/src/engine/adapters/baileys-contacts.ts
+++ b/src/engine/adapters/baileys-contacts.ts
@@ -1,6 +1,6 @@
import type { WAMessage, WAMessageKey, WASocket } from '@whiskeysockets/baileys';
import { ChatSummary, Contact, MediaInput } from '../interfaces/whatsapp-engine.interface';
-import { resolveMediaBuffer } from './baileys-messaging';
+import { assertReadableImage, resolveMediaBuffer } from './baileys-messaging';
import { type createLogger } from '../../common/services/logger.service';
import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';
import { EngineTransportError } from '../../common/errors/engine-transport.error';
@@ -209,6 +209,7 @@ export class BaileysContacts {
// updateProfilePicture takes a WAMediaUpload; resolveMediaBuffer covers Buffer | base64 | URL,
// the same conversion the media sends use.
const { data } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());
+ await assertReadableImage(data);
await this.confirmed(this.sock().updateProfilePicture(selfJid, data), 'the profile picture change');
}
diff --git a/src/engine/adapters/baileys-groups.ts b/src/engine/adapters/baileys-groups.ts
index 829293642..bd4ec4140 100644
--- a/src/engine/adapters/baileys-groups.ts
+++ b/src/engine/adapters/baileys-groups.ts
@@ -11,7 +11,7 @@ import {
} from '../interfaces/whatsapp-engine.interface';
import { mapBaileysGroup, mapBaileysGroupInfo } from './baileys-group-mapper';
import { GroupNotFoundError } from '../../common/errors/group-not-found.error';
-import { resolveMediaBuffer } from './baileys-messaging';
+import { assertReadableImage, resolveMediaBuffer } from './baileys-messaging';
import { EngineRefusedError } from '../../common/errors/engine-refused.error';
import { EngineTransportError } from '../../common/errors/engine-transport.error';
import { EngineThrottledError } from '../../common/errors/engine-throttled.error';
@@ -484,6 +484,7 @@ export class BaileysGroups {
this.host.ensureReady();
// Same socket call as the own-account picture, addressed at the group JID.
const { data } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());
+ await assertReadableImage(data);
await this.pictureWrite(groupId, 'Setting the group picture', () =>
this.confirmed(this.sock().updateProfilePicture(groupId, data), 'the group picture change'),
);
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 334eee28c..167bafb39 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -155,6 +155,27 @@ export async function blockSvgDecoding(): Promise<void> {
}
}
+/**
+ * Refuse a picture the image decoder cannot read, before Baileys hands it to that decoder: its own
+ * conversion rejects such a file with an error nothing maps, which reached the client as a 500. An SVG is
+ * one since blockSvgDecoding, and so is a file that is no image at all. Only the header is read. A sharp
+ * that cannot load is left to the library, which reports that itself.
+ */
+export async function assertReadableImage(data: Buffer): Promise<void> {
+ const sharp = await import('sharp').then(
+ module => module.default,
+ () => undefined,
+ );
+ if (!sharp) return;
+ try {
+ await sharp(data).metadata();
+ } catch (error) {
+ throw new BadRequestException(
+ `The picture could not be read as an image (SVG is not accepted): ${error instanceof Error ? error.message : String(error)}`,
+ );
+ }
+}
+
/**
* 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-write-deadline.spec.ts b/src/engine/adapters/baileys-write-deadline.spec.ts
index 1fbb69dd2..eba9cae6f 100644
--- a/src/engine/adapters/baileys-write-deadline.spec.ts
+++ b/src/engine/adapters/baileys-write-deadline.spec.ts
@@ -45,7 +45,14 @@ function contacts(sock: Record<string, jest.Mock>, budgetMs: number): BaileysCon
return new BaileysContacts(host, budgetMs);
}
-const PNG = { mimetype: 'image/png', data: Buffer.from('x') };
+// A real 1x1 PNG: a picture write reads the header of its bytes before it reaches the socket.
+const PNG = {
+ mimetype: 'image/png',
+ data: Buffer.from(
+ 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
+ 'base64',
+ ),
+};
describe('group writes report an unconfirmed write rather than success', () => {
it.each([
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index f7e0a6ee9..ec165fe74 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -242,6 +242,16 @@ const newAdapter = (): BaileysAdapter =>
const noopCallbacks = (over: Partial<EngineEventCallbacks> = {}): EngineEventCallbacks => over;
+// A real 1x1 PNG, and a small SVG: the picture writes read the header of what they upload, so bytes that
+// are no image are refused before they reach the socket.
+const PNG_BYTES = Buffer.from(
+ 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
+ 'base64',
+);
+const SVG_BYTES = Buffer.from(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8"/></svg>',
+);
+
/**
* Every text send now carries send-options, because that is how the SSRF-safe preview generator
* replaces the library's own (see safe-link-preview.ts). Matching on the shape rather than
@@ -5270,7 +5280,7 @@ describe('BaileysAdapter store-backed ops', () => {
});
describe('BaileysAdapter group management', () => {
- const PNG_INPUT = { mimetype: 'image/png', data: 'QUJD' };
+ const PNG_INPUT = { mimetype: 'image/png', data: PNG_BYTES.toString('base64') };
const META = {
subject: 'G',
@@ -5818,8 +5828,18 @@ describe('BaileysAdapter group management', () => {
// fakeSock.user.id is '628999:[email protected]' (see beforeEach) — the own JID the adapter
// normalizes the same way as everywhere else.
const adapter = await ready();
- await adapter.setProfilePicture({ mimetype: 'image/png', data: Buffer.from('IMG').toString('base64') });
- expect(fakeSock.updateProfilePicture).toHaveBeenCalledWith('[email protected]', Buffer.from('IMG'));
+ await adapter.setProfilePicture({ mimetype: 'image/png', data: PNG_BYTES.toString('base64') });
+ expect(fakeSock.updateProfilePicture).toHaveBeenCalledWith('[email protected]', PNG_BYTES);
+ });
+
+ // SVG decoding is blocked process-wide (see blockSvgDecoding), so the library's own conversion rejects
+ // an SVG with an error nothing maps, which answered 500 for a file the client chose.
+ it('setProfilePicture refuses an SVG with a 400 and uploads nothing', async () => {
+ const adapter = await ready();
+ await expect(
+ adapter.setProfilePicture({ mimetype: 'image/svg+xml', data: SVG_BYTES.toString('base64') }),
+ ).rejects.toBeInstanceOf(BadRequestException);
+ expect(fakeSock.updateProfilePicture).not.toHaveBeenCalled();
});
it('setProfilePicture throws when the own JID is not known', async () => {
@@ -7101,8 +7121,16 @@ describe('BaileysAdapter sendSeen + markUnread + deleteChat', () => {
it('setGroupPicture targets the GROUP jid, not the own account', async () => {
const adapter = await readyWithMessage();
- await adapter.setGroupPicture('[email protected]', { mimetype: 'image/png', data: 'QUJD' });
- expect(fakeSock.updateProfilePicture).toHaveBeenCalledWith('[email protected]', expect.any(Buffer));
+ await adapter.setGroupPicture('[email protected]', { mimetype: 'image/png', data: PNG_BYTES.toString('base64') });
+ expect(fakeSock.updateProfilePicture).toHaveBeenCalledWith('[email protected]', PNG_BYTES);
+ });
+
+ it('setGroupPicture refuses an SVG with a 400 and uploads nothing', async () => {
+ const adapter = await readyWithMessage();
+ await expect(
+ adapter.setGroupPicture('[email protected]', { mimetype: 'image/svg+xml', data: SVG_BYTES.toString('base64') }),
+ ).rejects.toBeInstanceOf(BadRequestException);
+ expect(fakeSock.updateProfilePicture).not.toHaveBeenCalled();
});
it('deleteGroupPicture removes by the GROUP jid', async () => {
diff --git a/src/modules/group/group.controller.ts b/src/modules/group/group.controller.ts
index 95d502207..343c6a3e8 100644
--- a/src/modules/group/group.controller.ts
+++ b/src/modules/group/group.controller.ts
@@ -543,8 +543,9 @@ export class GroupController {
@ApiResponse({
status: 400,
description:
- 'The id does not name a group, the session is not active, neither url nor base64 was supplied, or ' +
- 'the url answers non-2xx, times out or cannot be reached',
+ 'The id does not name a group, the session is not active, neither url nor base64 was supplied, ' +
+ 'the url answers non-2xx, times out or cannot be reached, or the image cannot be read (Baileys ' +
+ 'refuses an SVG)',
})
@ApiResponse({ status: 403, description: 'The engine refused the change — admin rights required' })
@ApiResponse({
diff --git a/src/modules/profile/profile.controller.ts b/src/modules/profile/profile.controller.ts
index 55e50b540..79bf0e37d 100644
--- a/src/modules/profile/profile.controller.ts
+++ b/src/modules/profile/profile.controller.ts
@@ -69,8 +69,8 @@ export class ProfileController {
@ApiResponse({
status: 400,
description:
- 'Neither url nor base64 provided, base64 without mimetype, session is not started, or the url ' +
- 'answers non-2xx, times out or cannot be reached',
+ 'Neither url nor base64 provided, base64 without mimetype, session is not started, the url ' +
+ 'answers non-2xx, times out or cannot be reached, or the image cannot be read (Baileys refuses an SVG)',
})
@ApiResponse({
status: 403,