Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(channel): reject a user id the channel-admin writes cannot address
Yudhi Armyndhariscf27ae8cf610
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 23c0d27a7..a0852cff0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Approving or rejecting a join request by bare phone number works on the whatsapp-web.js engine instead of answering `500`: the participant guard accepts a bare number and the three other participant writes qualify it, but these two routes handed it to the engine unqualified, so it reached the page as an unaddressable id. The same request already worked on Baileys. Refs #1220.- A burst of inbound media on the whatsapp-web.js engine no longer loses the media of everything past the eighth, which the Baileys repair in this release left in place on the other engine: its download queue was capped at its active slots too, making admission a constant 8 whatever the batch size. A burst now parks rather than shedding, and only the download waits — a text message never enters the limiter. The wait for a slot is bounded by `MEDIA_DOWNLOAD_TIMEOUT_MS`, so a burst queued behind a hung page still emits its messages without media rather than waiting on a slot that never frees.- A webhook's `filters` is published as nullable, the sibling of the `lastTriggeredAt` repair in this release: the field is stored as `null` for every webhook created without filters and the description offers `null` as an input, but the schema declared neither, so a client generated from the contract rejected a value the route both sends and accepts. An invariant now fails when any property documented as carrying `null` does not publish it.+- The two channel administration routes reject a user id that does not name an individual with `400` instead of handing it to WhatsApp: both bodies accepted any non-empty string, and an id the engine cannot classify was forwarded verbatim, so free text or a group id failed opaquely. A bare phone number is qualified as it is on the group participant writes, and the check sits in the service so the MCP tools are guarded like the REST routes.- The chart's optional ServiceMonitor now selects on a new `openwa.io/scrape-target` label, so it scrapes one target per pod instead of two; series previously collected through the headless target stop appearing, so check anything keyed on the `service` label.- The Helm chart's new startup probe stops the kubelet killing a pod that is still booting, allowing 295s (`periodSeconds: 5`, `failureThreshold: 60`) where the liveness settings allowed 50. It fires immediately, so a fast boot goes live sooner than before, not later — and it is not limited to auto-start, since the database connect retry alone can consume 30 of the old 50 seconds.- A Helm upgrade that changes only `env` or `secretEnv` now restarts the pod through ConfigMap and Secret checksums, so the new configuration reaches the running container. With `existingSecret` set the chart renders no Secret, so that path still needs `kubectl rollout restart`; `values.yaml` notes what publishing the Secret checksum as a pod annotation means.diff --git a/openapi.json b/openapi.jsonindex ccac0da13..631d5bd74 100644--- a/openapi.json+++ b/openapi.json@@ -13747,7 +13747,7 @@"properties": {"userId": {"type": "string",- "description": "WhatsApp ID of the admin to demote back to a subscriber.",+ "description": "WhatsApp ID of the admin to demote back to a subscriber — a phone number, `<phone>@c.us` or `<lid>@lid`; a bare number is qualified for you. Anything that does not name an individual is rejected with 400 rather than handed to WhatsApp.","example": "[email protected]"}},@@ -13760,7 +13760,7 @@"properties": {"newOwnerId": {"type": "string",- "description": "WhatsApp ID of the account that becomes the new owner. This is irreversible: once the transfer lands, this session can no longer take the channel back.",+ "description": "WhatsApp ID of the account that becomes the new owner — a phone number, `<phone>@c.us` or `<lid>@lid`; a bare number is qualified for you. Anything that does not name an individual is rejected with 400 rather than handed to WhatsApp. This is irreversible: once the transfer lands, this session can no longer take the channel back.","example": "[email protected]"}},diff --git a/src/modules/channel/channel.service.spec.ts b/src/modules/channel/channel.service.spec.tsindex 7121bea32..fd9131781 100644--- a/src/modules/channel/channel.service.spec.ts+++ b/src/modules/channel/channel.service.spec.ts@@ -46,25 +46,66 @@ describe('ChannelService', () => {it('demoteChannelAdmin forwards the channel and user to the engine, dropping the sessionId', async () => {const demoteChannelAdmin = jest.fn().mockResolvedValue(undefined);- await makeService({ demoteChannelAdmin }).demoteChannelAdmin('s1', 'ch1@newsletter', '[email protected]');- expect(demoteChannelAdmin).toHaveBeenCalledWith('ch1@newsletter', '[email protected]');+ await makeService({ demoteChannelAdmin }).demoteChannelAdmin('s1', 'ch1@newsletter', '[email protected]');+ expect(demoteChannelAdmin).toHaveBeenCalledWith('ch1@newsletter', '[email protected]');});it('demoteChannelAdmin throws 400 when the session is not started', () => {- expect(() => makeService(undefined).demoteChannelAdmin('s1', 'ch1@newsletter', '[email protected]')).toThrow(+ expect(() => makeService(undefined).demoteChannelAdmin('s1', 'ch1@newsletter', '[email protected]')).toThrow(BadRequestException,);});it('transferChannelOwnership forwards the channel and new owner, dropping the sessionId', async () => {const transferChannelOwnership = jest.fn().mockResolvedValue(undefined);- await makeService({ transferChannelOwnership }).transferChannelOwnership('s1', 'ch1@newsletter', '[email protected]');- expect(transferChannelOwnership).toHaveBeenCalledWith('ch1@newsletter', '[email protected]');+ await makeService({ transferChannelOwnership }).transferChannelOwnership(+ 's1',+ 'ch1@newsletter',+ '[email protected]',+ );+ expect(transferChannelOwnership).toHaveBeenCalledWith('ch1@newsletter', '[email protected]');});it('transferChannelOwnership throws 400 when the session is not started', () => {- expect(() => makeService(undefined).transferChannelOwnership('s1', 'ch1@newsletter', '[email protected]')).toThrow(+ expect(() => makeService(undefined).transferChannelOwnership('s1', 'ch1@newsletter', '[email protected]')).toThrow(BadRequestException,);});++ // Both DTOs accept any non-empty string, and toEngineJid passes anything it cannot classify+ // through verbatim — so an unaddressable id reached the socket and failed opaquely. The group+ // participant writes reject the same input with a naming 400; these two are the routes that+ // shipped without it.+ it.each([+ ['free text', 'NOT A USER'],+ ['a bare @c.us with no user part', '@c.us'],+ ['a number too short to be a WhatsApp id', '123'],+ ['a group id', '[email protected]'],+ ['a channel id', 'ch2@newsletter'],+ ])('demoteChannelAdmin rejects %s with 400 instead of forwarding it', (_label, userId) => {+ const demoteChannelAdmin = jest.fn();+ expect(() => makeService({ demoteChannelAdmin }).demoteChannelAdmin('s1', 'ch1@newsletter', userId)).toThrow(+ BadRequestException,+ );+ expect(demoteChannelAdmin).not.toHaveBeenCalled();+ });++ it('transferChannelOwnership rejects an unaddressable new owner with 400', () => {+ const transferChannelOwnership = jest.fn();+ expect(() =>+ makeService({ transferChannelOwnership }).transferChannelOwnership('s1', 'ch1@newsletter', 'NOT A USER'),+ ).toThrow(BadRequestException);+ expect(transferChannelOwnership).not.toHaveBeenCalled();+ });++ // A bare number is the shape the group routes accept and qualify; accepting it here and then+ // handing it over unqualified would just move the opaque failure one layer down.+ it.each([['demoteChannelAdmin' as const], ['transferChannelOwnership' as const]])(+ '%s qualifies a bare phone number before the engine sees it',+ async method => {+ const engineMethod = jest.fn().mockResolvedValue(undefined);+ await makeService({ [method]: engineMethod })[method]('s1', 'ch1@newsletter', '628123456789');+ expect(engineMethod).toHaveBeenCalledWith('ch1@newsletter', '[email protected]');+ },+ );});diff --git a/src/modules/channel/channel.service.ts b/src/modules/channel/channel.service.tsindex 75ae179e5..2862e1b7d 100644--- a/src/modules/channel/channel.service.ts+++ b/src/modules/channel/channel.service.ts@@ -1,6 +1,7 @@-import { Injectable, NotFoundException } from '@nestjs/common';+import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';import { EngineRegistry } from '../../engine/engine-registry.service';import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';+import { isAddressableParticipant, toParticipantWid } from '../../engine/identity/wa-id';/*** Owns engine access for channel/newsletter operations so the "session not started" guard@@ -56,14 +57,34 @@ export class ChannelService {return this.getEngine(sessionId).muteChannel(channelId, mute);}+ /**+ * The user id both admin writes address, qualified for the engine.+ *+ * Their DTOs only require a non-empty string, and `toEngineJid` passes anything it cannot+ * classify through verbatim — so free text, a group id or a bare `@c.us` reached the socket and+ * failed opaquely, while the group participant writes reject the same input with a naming 400.+ * Guarded here rather than in the DTO so the MCP tools are covered like the REST routes, which is+ * where the group guard sits for the same reason.+ */+ private addressableUser(userId: string): string {+ if (!isAddressableParticipant(userId)) {+ throw new BadRequestException(+ `Not an individual user id: ${userId} — pass a phone number, <phone>@c.us or <lid>@lid`,+ );+ }+ return toParticipantWid(userId);+ }+/** Demote a channel admin back to a subscriber. There is no promote counterpart on either engine. */demoteChannelAdmin(sessionId: string, channelId: string, userId: string) {- return this.getEngine(sessionId).demoteChannelAdmin(channelId, userId);+ const engine = this.getEngine(sessionId);+ return engine.demoteChannelAdmin(channelId, this.addressableUser(userId));}/** Hand the channel to a new owner. Irreversible — the account stops being the owner. */transferChannelOwnership(sessionId: string, channelId: string, newOwnerId: string) {- return this.getEngine(sessionId).transferChannelOwnership(channelId, newOwnerId);+ const engine = this.getEngine(sessionId);+ return engine.transferChannelOwnership(channelId, this.addressableUser(newOwnerId));}subscribeToChannel(sessionId: string, inviteCode: string) {diff --git a/src/modules/channel/dto/demote-channel-admin.dto.ts b/src/modules/channel/dto/demote-channel-admin.dto.tsindex 655f17921..ee45d5173 100644--- a/src/modules/channel/dto/demote-channel-admin.dto.ts+++ b/src/modules/channel/dto/demote-channel-admin.dto.ts@@ -3,7 +3,10 @@ import { IsNotEmpty, IsString } from 'class-validator';export class DemoteChannelAdminDto {@ApiProperty({- description: 'WhatsApp ID of the admin to demote back to a subscriber.',+ description:+ 'WhatsApp ID of the admin to demote back to a subscriber — a phone number, `<phone>@c.us` ' ++ 'or `<lid>@lid`; a bare number is qualified for you. Anything that does not name an ' ++ 'individual is rejected with 400 rather than handed to WhatsApp.',example: '[email protected]',})@IsString()diff --git a/src/modules/channel/dto/transfer-channel-ownership.dto.ts b/src/modules/channel/dto/transfer-channel-ownership.dto.tsindex 7ed382e85..7be0aee48 100644--- a/src/modules/channel/dto/transfer-channel-ownership.dto.ts+++ b/src/modules/channel/dto/transfer-channel-ownership.dto.ts@@ -4,7 +4,9 @@ import { IsNotEmpty, IsString } from 'class-validator';export class TransferChannelOwnershipDto {@ApiProperty({description:- 'WhatsApp ID of the account that becomes the new owner. This is irreversible: once the ' ++ 'WhatsApp ID of the account that becomes the new owner — a phone number, `<phone>@c.us` or ' ++ '`<lid>@lid`; a bare number is qualified for you. Anything that does not name an individual ' ++ 'is rejected with 400 rather than handed to WhatsApp. This is irreversible: once the ' +'transfer lands, this session can no longer take the channel back.',example: '[email protected]',})