Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(group): refuse a non-group id on the group-picture routes
Yudhi Armyndharis2e282cb47514
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8f3b910a1..8c795e24e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- The three group-picture routes now refuse an id that does not name a group. They reuse the account's own profile-picture primitives, and Baileys omits the `target` attribute whenever the jid is the account's own — so `PUT` or `DELETE /sessions/:id/groups/<own-jid>/picture` replaced or permanently deleted the WhatsApp account's own avatar and answered `{success: true}`, while whatsapp-web.js refused the same input with a 404. Both engines now reject it with a 400 before the id reaches either adapter.+- `GET /sessions/:sessionId/messages` and the MCP `MessageList` tool now bound the inline media they return, via `MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES` (8 MiB of encoded base64 by default). The row count was clamped to 1..100, but a row is not a bounded object: each carries its base64 in `metadata.media.data`, so a hundred media rows serialised to well over a hundred megabytes, and a large enough page pushed `JSON.stringify` past its ceiling and failed the read outright — permanently for the dashboard, which requests the maximum page size with no way to ask for less. The budget is spent newest-first; past it a payload is replaced with the `{ omitted: true, sizeBytes }` marker the engine already emits for skipped inbound media, and remains fetchable from the per-message media route.- Bulk send now caps its rendered result at `TEMPLATE_RENDER_MAX_CHARS` (default 64 KiB), the same limit the single-send template path already enforced. `content.text` is length-validated before substitution, so caller-supplied variables inflated each item without bound while the request body stayed far below the in-flight body budget — a few hundred KiB of variables became hundreds of megabytes of heap per item, then went to the engine and the `messages.body` column. An over-cap render now fails that item with a message naming the limit, exactly as a pacing refusal does, rather than being truncated silently or sent.diff --git a/src/modules/group/group.service.spec.ts b/src/modules/group/group.service.spec.tsindex ee669c397..a484b6f4e 100644--- a/src/modules/group/group.service.spec.ts+++ b/src/modules/group/group.service.spec.ts@@ -17,6 +17,53 @@ describe('GroupService', () => {return new GroupService(engines, pacing);};+ // On Baileys the id reaches sock.updateProfilePicture/removeProfilePicture verbatim, and Baileys+ // omits the `target` attribute whenever the jid is the account's own — so a 1:1 id passed where a+ // group id belongs replaced or PERMANENTLY DELETED the account's own profile picture and answered+ // 200. whatsapp-web.js refused the same input via requireGroupChat, so the two engines disagreed+ // destructively on a documented route. Guarded in the service so both agree, and so the id never+ // reaches an engine at all.+ describe('group-picture routes reject an id that does not name a group', () => {+ const engine = () => ({+ setGroupPicture: jest.fn().mockResolvedValue(undefined),+ deleteGroupPicture: jest.fn().mockResolvedValue(undefined),+ getProfilePicture: jest.fn().mockResolvedValue('https://cdn/x.jpg'),+ });++ it.each([+ ["the account's own 1:1 jid", '[email protected]'],+ ['a lid', '99887766@lid'],+ ['a channel', '120363000000000000@newsletter'],+ ])('refuses %s', (_label, id) => {+ const e = engine();+ const svc = makeService(e);++ // Thrown synchronously, matching this service's existing guards; the controller methods are+ // async, so it still surfaces as a 400 at the HTTP layer.+ expect(() => svc.setGroupPicture('s1', id, { base64: 'AAA=', mimetype: 'image/jpeg' })).toThrow(+ BadRequestException,+ );+ expect(() => svc.deleteGroupPicture('s1', id)).toThrow(BadRequestException);+ expect(() => svc.getGroupPicture('s1', id)).toThrow(BadRequestException);++ expect(e.setGroupPicture).not.toHaveBeenCalled();+ expect(e.deleteGroupPicture).not.toHaveBeenCalled();+ expect(e.getProfilePicture).not.toHaveBeenCalled();+ });++ // Negative twin: the guard must not refuse the ids the routes exist to serve.+ it('still forwards a real group id', async () => {+ const e = engine();+ const svc = makeService(e);++ await svc.deleteGroupPicture('s1', '[email protected]');+ await svc.getGroupPicture('s1', '[email protected]');++ expect(e.deleteGroupPicture).toHaveBeenCalledWith('[email protected]');+ expect(e.getProfilePicture).toHaveBeenCalledWith('[email protected]');+ });+ });+it('throws 400 "Session is not started" when the engine is missing (guard preserved)', () => {// The guard throws synchronously; the controller methods are `async`, so this still surfaces// as a rejected promise → 400 at the HTTP layer.diff --git a/src/modules/group/group.service.ts b/src/modules/group/group.service.tsindex ee23b492b..76f4bffc1 100644--- a/src/modules/group/group.service.ts+++ b/src/modules/group/group.service.ts@@ -2,6 +2,7 @@ import { Injectable, BadRequestException, HttpException, HttpStatus, NotFoundExcimport { EngineRegistry } from '../../engine/engine-registry.service';import { GroupMemberAddMode, IWhatsAppEngine, MediaInput } from '../../engine/interfaces/whatsapp-engine.interface';import { assertBase64WithinMediaCap, stripBase64DataUri } from '../message/media-cap.util';+import { chatKind } from '../../engine/identity/wa-id';import { isAddressableParticipant } from '../../engine/identity/wa-id';import { SetGroupPictureDto } from './dto/group.dto';import { paginate, ListOptions } from '../../common/utils/paginate';@@ -153,12 +154,29 @@ export class GroupService {return this.getEngine(sessionId).joinGroupViaInviteCode(inviteCode);}+ /**+ * Refuse an id that does not name a group before it reaches an engine.+ *+ * These three routes reuse the ACCOUNT's profile-picture primitives, and Baileys omits the `target`+ * attribute whenever the jid it is handed is the account's own — so a 1:1 id passed where a group id+ * belongs replaced or permanently deleted the account's own avatar and answered 200, while+ * whatsapp-web.js refused the same input through requireGroupChat. Guarded here rather than in either+ * adapter so both engines agree, and so no engine ever sees the wrong kind of id.+ */+ private assertGroupId(groupId: string): void {+ if (chatKind(groupId) !== 'group') {+ throw new BadRequestException(`${groupId} is not a group id`);+ }+ }+/** Read the group's picture URL, or null when it has none. Groups reuse the profile-picture read. */getGroupPicture(sessionId: string, groupId: string): Promise<string | null> {+ this.assertGroupId(groupId);return this.getEngine(sessionId).getProfilePicture(groupId);}setGroupPicture(sessionId: string, groupId: string, dto: SetGroupPictureDto): Promise<void> {+ this.assertGroupId(groupId);const base64 = stripBase64DataUri(dto.base64);if (!dto.url && !base64) {throw new BadRequestException('Either url or base64 must be provided');@@ -176,6 +194,7 @@ export class GroupService {}deleteGroupPicture(sessionId: string, groupId: string): Promise<void> {+ this.assertGroupId(groupId);return this.getEngine(sessionId).deleteGroupPicture(groupId);}