Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): check each distinct bulk recipient once
Yudhi Armyndharis470c993dcc17
diff --git a/src/modules/auth/guards/api-key.guard.spec.ts b/src/modules/auth/guards/api-key.guard.spec.tsindex 5ceba8c66..9e5a4e31b 100644--- a/src/modules/auth/guards/api-key.guard.spec.ts+++ b/src/modules/auth/guards/api-key.guard.spec.ts@@ -589,6 +589,23 @@ describe('ApiKeyGuard', () => {expect(await guard.canActivate(context)).toBe(true);});+ it('expands each distinct bulk recipient once, however often it repeats', async () => {+ const apiKey = createMockApiKey({ allowedChats: [ALLOWED_GROUP, ALLOWED_CONTACT] });+ (authService.validateApiKey as jest.Mock).mockResolvedValue(apiKey);+ const allows = jest.spyOn(ChatScopeService.prototype, 'allows');+ try {+ const messages = Array.from({ length: 100 }, (_, i) => ({+ chatId: i % 2 ? ALLOWED_GROUP : ALLOWED_CONTACT,+ type: 'text',+ }));+ const context = createMockContext({ 'x-api-key': 'key' }, {}, '127.0.0.1', { messages });+ expect(await guard.canActivate(context)).toBe(true);+ expect(allows).toHaveBeenCalledTimes(2);+ } finally {+ allows.mockRestore();+ }+ });+it('rejects an oversized bulk body before any per-entry lookup', async () => {const apiKey = createMockApiKey({ allowedChats: [ALLOWED_GROUP] });(authService.validateApiKey as jest.Mock).mockResolvedValue(apiKey);diff --git a/src/modules/auth/guards/api-key.guard.ts b/src/modules/auth/guards/api-key.guard.tsindex 30023ce4c..82eccaec0 100644--- a/src/modules/auth/guards/api-key.guard.ts+++ b/src/modules/auth/guards/api-key.guard.ts@@ -189,12 +189,15 @@ export class ApiKeyGuard implements CanActivate {* two lid-table lookups each), so a route that names no chat costs nothing.*/private async assertChatsAllowed(request: Request, apiKey: ApiKey, context: ExecutionContext): Promise<void> {+ // A bulk send repeats the same chat freely; each distinct id is expanded once.+ const checked = new Set<string>();for (const [field, value] of this.chatIdsIn(request)) {if (value === undefined || value === null) continue;if (typeof value !== 'string') {throw new BadRequestException(`${field} must be a single chat id string`);}- if (value.length === 0) continue;+ if (value.length === 0 || checked.has(value)) continue;+ checked.add(value);if (!(await this.chatScope.allows(apiKey, value))) {throw new ForbiddenException('API key not authorized for this chat');}