Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): scope bulk-batch id uniqueness to the session (#515)
Yudhi Armyndharisc50d74ad6005
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.tsindex 8fe027ff5..bffd5c7d0 100644--- a/src/modules/message/bulk-message.service.spec.ts+++ b/src/modules/message/bulk-message.service.spec.ts@@ -198,12 +198,13 @@ describe('BulkMessageService.processBatch', () => {describe('BulkMessageService.createBatch base64 media cap', () => {let service: BulkMessageService;- let repo: { findOne: jest.Mock; save: jest.Mock };+ let repo: { findOne: jest.Mock; save: jest.Mock; create: jest.Mock };beforeEach(async () => {repo = {findOne: jest.fn().mockResolvedValue(undefined),save: jest.fn().mockImplementation(b => Promise.resolve(b)),+ create: jest.fn().mockImplementation((b: MessageBatch) => b),};const module: TestingModule = await Test.createTestingModule({providers: [@@ -235,4 +236,24 @@ describe('BulkMessageService.createBatch base64 media cap', () => {delete process.env.MEDIA_DOWNLOAD_MAX_BYTES;}});++ it('scopes the batchId uniqueness check to the session (no cross-session collision/oracle)', async () => {+ // Simulate a DB where batchId 'dup' exists only under session 's1'.+ repo.findOne.mockImplementation((opts: { where: { batchId?: string; sessionId?: string } }) => {+ const w = opts.where;+ const existsForS1 = w.batchId === 'dup' && (w.sessionId === undefined || w.sessionId === 's1');+ return Promise.resolve(existsForS1 ? { id: 'b1', batchId: 'dup', sessionId: 's1' } : undefined);+ });++ // A different session reusing the same batchId must succeed — the check is (batchId, sessionId)-scoped,+ // so it neither collides with another tenant's namespace nor leaks that the id is in use elsewhere.+ await expect(+ service.createBatch('s2', {+ messages: [{ chatId: '[email protected]', type: 'text', content: { text: { body: 'hi' } } }],+ batchId: 'dup',+ } as unknown as SendBulkMessageDto),+ ).resolves.toBeDefined();++ expect(repo.findOne).toHaveBeenCalledWith({ where: { batchId: 'dup', sessionId: 's2' } });+ });});diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.tsindex 76a98423b..4c43c83b9 100644--- a/src/modules/message/bulk-message.service.ts+++ b/src/modules/message/bulk-message.service.ts@@ -107,8 +107,10 @@ export class BulkMessageService implements OnApplicationBootstrap {const batchId = dto.batchId || `batch_${randomUUID().split('-')[0]}`;- // Check if batchId already exists- const existing = await this.batchRepository.findOne({ where: { batchId } });+ // Check if this batchId already exists FOR THIS SESSION. Scoping by sessionId (matching how+ // getBatchStatus/cancelBatch already query) makes (sessionId, batchId) the namespace: one session+ // can't deny another a batchId, and the 400-vs-202 difference can't probe another session's ids.+ const existing = await this.batchRepository.findOne({ where: { batchId, sessionId } });if (existing) {throw new BadRequestException(`Batch ID '${batchId}' already exists`);}