Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(channel): refuse a non-channel id on delete and unsubscribe

Yudhi Armyndharisf0120fa4d8a8
diff --git a/src/modules/channel/channel.controller.ts b/src/modules/channel/channel.controller.ts
index 1ac88e5d0..dc132fa30 100644
--- a/src/modules/channel/channel.controller.ts
+++ b/src/modules/channel/channel.controller.ts
@@ -129,6 +129,7 @@ export class ChannelController {
@ApiResponse({ status: 400, description: 'Session not started' })
@ApiResponse({ status: 403, description: 'The engine refused — not found, or this account does not own it' })
@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })
+ @ApiResponse({ status: 404, description: CHANNEL_NOT_FOUND_404 })
async remove(
@Param('sessionId') sessionId: string,
@Param('channelId') channelId: string,
@@ -289,6 +290,7 @@ export class ChannelController {
})
@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })
@ApiResponse({ status: 403, description: ENGINE_REFUSED_403 })
+ @ApiResponse({ status: 404, description: CHANNEL_NOT_FOUND_404 })
async unsubscribe(@Param('sessionId') sessionId: string, @Param('channelId') channelId: string) {
await this.channelService.unsubscribeFromChannel(sessionId, channelId);
return { success: true };
diff --git a/src/modules/channel/channel.service.spec.ts b/src/modules/channel/channel.service.spec.ts
index fd9131781..ee5ec6942 100644
--- a/src/modules/channel/channel.service.spec.ts
+++ b/src/modules/channel/channel.service.spec.ts
@@ -1,5 +1,6 @@
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { ChannelService } from './channel.service';
+import { ChannelNotFoundError } from '../../common/errors/channel-not-found.error';
import { EngineRegistry } from '../../engine/engine-registry.service';
import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';
@@ -108,4 +109,24 @@ describe('ChannelService', () => {
expect(engineMethod).toHaveBeenCalledWith('ch1@newsletter', '[email protected]');
},
);
+
+ // whatsapp-web.js resolves a non-channel id to an ordinary chat (creating one if needed) before
+ // these calls fail, so a chat id must be refused before it reaches the engine.
+ it.each(['deleteChannel', 'unsubscribeFromChannel'] as const)(
+ '%s refuses an id that is not a channel with 404 and never reaches the engine',
+ method => {
+ const engineCall = jest.fn().mockResolvedValue(undefined);
+ const svc = makeService({ [method]: engineCall });
+ for (const id of ['[email protected]', '[email protected]']) {
+ expect(() => svc[method]('s1', id)).toThrow(ChannelNotFoundError);
+ }
+ expect(engineCall).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each(['deleteChannel', 'unsubscribeFromChannel'] as const)('%s forwards a channel id', async method => {
+ const engineCall = jest.fn().mockResolvedValue(undefined);
+ await makeService({ [method]: engineCall })[method]('s1', '120363000000000000@newsletter');
+ expect(engineCall).toHaveBeenCalledWith('120363000000000000@newsletter');
+ });
});
diff --git a/src/modules/channel/channel.service.ts b/src/modules/channel/channel.service.ts
index b4bdc9a7a..196077ee9 100644
--- a/src/modules/channel/channel.service.ts
+++ b/src/modules/channel/channel.service.ts
@@ -2,6 +2,7 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/comm
import { EngineRegistry } from '../../engine/engine-registry.service';
import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';
import { isAddressableParticipant, toParticipantWid } from '../../engine/identity/wa-id';
+import { ChannelNotFoundError } from '../../common/errors/channel-not-found.error';
/**
* Owns engine access for channel/newsletter operations so the "session not started" guard
@@ -47,9 +48,20 @@ export class ChannelService {
return this.getEngine(sessionId).createChannel(name, description);
}
+ /**
+ * A channel id, or 404. whatsapp-web.js resolves any other id to an ordinary chat, creating one if
+ * needed, before its channel delete or unsubscribe fails with a 500 (the adapter's mute guards the
+ * same way). Every channel id ends in `@newsletter` on both engines.
+ */
+ private requireChannelId(channelId: string): string {
+ if (!channelId.endsWith('@newsletter')) throw new ChannelNotFoundError(channelId);
+ return channelId;
+ }
+
/** Delete a channel this account owns. Irreversible, and its subscribers lose it. */
deleteChannel(sessionId: string, channelId: string) {
- return this.getEngine(sessionId).deleteChannel(channelId);
+ const engine = this.getEngine(sessionId);
+ return engine.deleteChannel(this.requireChannelId(channelId));
}
/** Mute or unmute a channel's notifications. Subscription is untouched either way. */
@@ -97,6 +109,7 @@ export class ChannelService {
}
unsubscribeFromChannel(sessionId: string, channelId: string) {
- return this.getEngine(sessionId).unsubscribeFromChannel(channelId);
+ const engine = this.getEngine(sessionId);
+ return engine.unsubscribeFromChannel(this.requireChannelId(channelId));
}
}