Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(group): validate the membership-request participant ids too

Yudhi Armyndharise75056e69d2c
diff --git a/src/modules/group/group.service.spec.ts b/src/modules/group/group.service.spec.ts
index 5a40046d6..ee669c397 100644
--- a/src/modules/group/group.service.spec.ts
+++ b/src/modules/group/group.service.spec.ts
@@ -61,8 +61,10 @@ describe('GroupService', () => {
it('passes participant lists straight through to the engine', async () => {
const addParticipants = jest.fn().mockResolvedValue(undefined);
const svc = makeService({ addParticipants });
- await svc.addParticipants('s1', 'g1', ['[email protected]', '[email protected]']);
- expect(addParticipants).toHaveBeenCalledWith('g1', ['[email protected]', '[email protected]']);
+ // Real-shaped ids: the participant guard requires a numeric user-part, so `[email protected]` would now
+ // fail validation and this would stop testing the pass-through it is named for.
+ await svc.addParticipants('s1', 'g1', ['[email protected]', '[email protected]']);
+ expect(addParticipants).toHaveBeenCalledWith('g1', ['[email protected]', '[email protected]']);
});
it('joinGroupViaInviteCode delegates and returns the group id', async () => {
@@ -365,15 +367,70 @@ describe('GroupService participant id validation (#1220)', () => {
expect(removeParticipants).toHaveBeenCalledWith(GROUP, ['628123456789', '[email protected]', '12345678901234567890@lid']);
});
- it('rejects a malformed participant before it can consume reachout budget', async () => {
- // createGroup and addParticipants are paced; a batch that can never reach WhatsApp must not
- // draw on the cold-reachout budget on its way to a 400.
- const assertReachoutAllowed = jest.fn().mockResolvedValue(undefined);
- const svc = makeService({ addParticipants: jest.fn() }, {
- assertReachoutAllowed,
- } as unknown as SendPacingService);
+ it.each([['addParticipants'], ['createGroup']])(
+ '%s rejects a malformed participant before it can consume reachout budget',
+ async op => {
+ // Both paced writes, not just one: a batch that can never reach WhatsApp must not draw on the
+ // cold-reachout budget on its way to a 400.
+ const assertReachoutAllowed = jest.fn().mockResolvedValue(undefined);
+ const svc = makeService({ [op]: jest.fn() }, { assertReachoutAllowed } as unknown as SendPacingService);
- await expect(call(svc, 'addParticipants', ['NOT A USER'])).rejects.toBeInstanceOf(BadRequestException);
- expect(assertReachoutAllowed).not.toHaveBeenCalled();
+ await expect(call(svc, op, ['NOT A USER'])).rejects.toBeInstanceOf(BadRequestException);
+ expect(assertReachoutAllowed).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([
+ ['NOT A USER'],
+ // A recognised domain alone satisfied the first version of the guard, and the id still reached
+ // the page-side createWid — reproduced as a 500 against a live session.
+ ['NOT A [email protected]'],
+ ['@c.us'],
+ ])('rejects %s on removeParticipants rather than handing it to the engine', async id => {
+ const engineOp = jest.fn();
+ const svc = makeService({ removeParticipants: engineOp });
+
+ await expect(call(svc, 'removeParticipants', [id])).rejects.toBeInstanceOf(BadRequestException);
+ expect(engineOp).not.toHaveBeenCalled();
});
+
+ it.each([['approveGroupMembershipRequests'], ['rejectGroupMembershipRequests']])(
+ '%s validates named requesters too',
+ async op => {
+ // These take the same participant ids as the five writes above and were missed in the first
+ // sweep. whatsapp-web.js feeds them straight to `requesterIds.map(createWid)` (Utils.js), so
+ // free text throws there exactly as it did on the participant routes.
+ const engineOp = jest.fn();
+ const svc = makeService({ [op]: engineOp });
+
+ // Non-async forwarders, so the guard throws synchronously — route it through a promise, as
+ // the participant writes above do.
+ await expect(
+ Promise.resolve().then(() =>
+ (svc as unknown as Record<string, (s: string, g: string, p?: string[]) => Promise<unknown>>)[op](
+ 's1',
+ GROUP,
+ ['NOT A USER'],
+ ),
+ ),
+ ).rejects.toBeInstanceOf(BadRequestException);
+ expect(engineOp).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([['approveGroupMembershipRequests'], ['rejectGroupMembershipRequests']])(
+ '%s still accepts an omitted list, which means every pending request',
+ async op => {
+ const engineOp = jest.fn().mockResolvedValue([]);
+ const svc = makeService({ [op]: engineOp });
+
+ await (svc as unknown as Record<string, (s: string, g: string, p?: string[]) => Promise<unknown>>)[op](
+ 's1',
+ GROUP,
+ undefined,
+ );
+ expect(engineOp).toHaveBeenCalledWith(GROUP, undefined);
+ },
+ );
});
diff --git a/src/modules/group/group.service.ts b/src/modules/group/group.service.ts
index e8cef6b6e..ee23b492b 100644
--- a/src/modules/group/group.service.ts
+++ b/src/modules/group/group.service.ts
@@ -103,13 +103,17 @@ export class GroupService {
/**
* Deliberately NOT paced, unlike addParticipants: the people here asked for the contact
* themselves, so approving (or rejecting) them draws nothing from the cold-reachout budget.
- * `participants` omitted means every pending request.
+ * `participants` omitted means every pending request — so the shape guard is conditional, not
+ * skipped: these routes take the same participant ids as the writes above, and whatsapp-web.js
+ * feeds a named requester straight to `requesterIds.map(createWid)`.
*/
approveGroupMembershipRequests(sessionId: string, groupId: string, participants?: string[]) {
+ if (participants) this.assertAddressableParticipants(participants);
return this.getEngine(sessionId).approveGroupMembershipRequests(groupId, participants);
}
rejectGroupMembershipRequests(sessionId: string, groupId: string, participants?: string[]) {
+ if (participants) this.assertAddressableParticipants(participants);
return this.getEngine(sessionId).rejectGroupMembershipRequests(groupId, participants);
}
diff --git a/src/modules/message/send-pacing.service.spec.ts b/src/modules/message/send-pacing.service.spec.ts
index 428119829..2c97928b1 100644
--- a/src/modules/message/send-pacing.service.spec.ts
+++ b/src/modules/message/send-pacing.service.spec.ts
@@ -368,11 +368,16 @@ describe('send paths consult the governor', () => {
};
const service = new GroupService({ require: () => engine } as never, pacing as never);
- await expect(service.addParticipants('s1', '[email protected]', ['[email protected]', '[email protected]'])).rejects.toBeInstanceOf(HttpException);
- await expect(service.createGroup('s1', 'New', ['[email protected]'])).rejects.toBeInstanceOf(HttpException);
+ // Addressable ids on purpose: the participant guard runs BEFORE pacing, so a placeholder like
+ // `[email protected]` would 400 first and the governor would never be consulted — this test would then
+ // pass for the wrong reason while asserting nothing about pacing.
+ await expect(service.addParticipants('s1', '[email protected]', ['[email protected]', '[email protected]'])).rejects.toBeInstanceOf(
+ HttpException,
+ );
+ await expect(service.createGroup('s1', 'New', ['[email protected]'])).rejects.toBeInstanceOf(HttpException);
- expect(pacing.assertReachoutAllowed).toHaveBeenNthCalledWith(1, 's1', ['[email protected]', '[email protected]']);
- expect(pacing.assertReachoutAllowed).toHaveBeenNthCalledWith(2, 's1', ['[email protected]']);
+ expect(pacing.assertReachoutAllowed).toHaveBeenNthCalledWith(1, 's1', ['[email protected]', '[email protected]']);
+ expect(pacing.assertReachoutAllowed).toHaveBeenNthCalledWith(2, 's1', ['[email protected]']);
expect(engine.addParticipants).not.toHaveBeenCalled();
expect(engine.createGroup).not.toHaveBeenCalled();
});