Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(contact): accept a privacy id on block and unblock
Yudhi Armyndhariseb507bdcfb4a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b23861c4d..a875dbfb3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- block and unblock accept a privacy id (`@lid`), the only id a contact without a known phone number has. The blocklist read answers those ids verbatim, so refusing them on the write left such a contact listed as blocked with no way to unblock it; ids that name no individual (group, newsletter, broadcast, free text) are still refused with `400`.- Follow-ups from the accessibility review: the plugin ConfigField derives a per-field id (a hardcoded id collided on any schema with two booleans, making the second label toggle the first checkbox), the 10 codemod-touched files are Prettier-clean (9 failed the dashboard format gate), and six more multi-line labels in MessageTester are wired.- Dashboard accessibility: 55 form labels are associated with their controls (`htmlFor`/`id`, no duplicates or orphans), the muted-text token meets AA on both themes (4.76:1 light, 5.71:1 dark), and the primary button uses dark text on the green (9.0:1, from 1.98:1).- Follow-ups from the batch review: the chain-boot e2e sets MAIN_DATABASE_SYNCHRONIZE=false explicitly (the env's absence defaults to synchronize=true, so the main chain was never exercised), the drift gate classifies statements against the two known shape families (column-type rebuild, index-name drift) instead of blanket-filtering every index/DROP statement, the shared delivery recorder strips the raw error before the persistence spread, and the ignore-pattern list deduplicates.diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 11030962b..7d332a041 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -2442,7 +2442,7 @@ This route is annotated `@HttpCode(200)`, so it returns `200` rather than the PO{ "success": true, "message": "Contact blocked" }```-**Errors:** `400` session is not started, or the id is not a phone-based contact id (group/newsletter/lid/free text are refused on both engines) · `401` missing/invalid API key · `403` key role below OPERATOR · `409` conflict or engine not ready (retryable) · `503` session not ready or dependency unavailable (retryable)+**Errors:** `400` session is not started, or the id does not name an individual (group/newsletter/broadcast/free text are refused on both engines; a phone-based or privacy `@lid` id is accepted, because the blocklist read answers both shapes) · `401` missing/invalid API key · `403` key role below OPERATOR · `409` conflict or engine not ready (retryable) · `503` session not ready or dependency unavailable (retryable)#### DELETE /api/sessions/:sessionId/contacts/:contactId/block@@ -2467,7 +2467,7 @@ No `@HttpCode` override is present, so this DELETE returns the NestJS default `2{ "success": true, "message": "Contact unblocked" }```-**Errors:** `400` session is not started, or the id is not a phone-based contact id (group/newsletter/lid/free text are refused on both engines) · `401` missing/invalid API key · `403` key role below OPERATOR · `409` conflict or engine not ready (retryable) · `503` session not ready or dependency unavailable (retryable)+**Errors:** `400` session is not started, or the id does not name an individual (group/newsletter/broadcast/free text are refused on both engines; a phone-based or privacy `@lid` id is accepted, because the blocklist read answers both shapes) · `401` missing/invalid API key · `403` key role below OPERATOR · `409` conflict or engine not ready (retryable) · `503` session not ready or dependency unavailable (retryable)### 6.4.4 Groupsdiff --git a/src/modules/contact/contact.service.spec.ts b/src/modules/contact/contact.service.spec.tsindex 0f780ee8d..013f2e9d3 100644--- a/src/modules/contact/contact.service.spec.ts+++ b/src/modules/contact/contact.service.spec.ts@@ -114,7 +114,9 @@ describe('ContactService', () => {it.each([['blockContact', (svc: ContactService) => svc.blockContact('s1', '[email protected]')],['unblockContact', (svc: ContactService) => svc.unblockContact('s1', '[email protected]')],- ['blockContact (lid)', (svc: ContactService) => svc.blockContact('s1', '159442138038327@lid')],+ ['blockContact (newsletter)', (svc: ContactService) => svc.blockContact('s1', '1200000000@newsletter')],+ ['blockContact (broadcast)', (svc: ContactService) => svc.blockContact('s1', '1200000000@broadcast')],+ ['blockContact (status)', (svc: ContactService) => svc.blockContact('s1', 'status@broadcast')],['blockContact (free text)', (svc: ContactService) => svc.blockContact('s1', '[email protected]')],])('%s refuses the id with a 400 before the engine is called', (_name, call) => {const blockContact = jest.fn();@@ -125,6 +127,38 @@ describe('ContactService', () => {expect(unblockContact).not.toHaveBeenCalled();});+ /**+ * A privacy-id contact has no phone number at all, and the blocklist READ answers those ids+ * verbatim (Baileys maps each blocked jid through toNeutralJid, which leaves an unresolved lid+ * as `<lid>@lid`; whatsapp-web.js returns the wid as-is). Refusing them here made the ids the+ * API itself hands out unusable for the matching write, so a privacy-id contact could be listed+ * as blocked and never unblocked. Blocking acts on the identity, not on an addressbook row, so+ * neither engine needs a phone here: Baileys passes the jid straight to updateBlockStatus, and+ * whatsapp-web.js only short-circuits (`return false`) for a group.+ */+ it.each([+ ['blockContact', (svc: ContactService) => svc.blockContact('s1', '159442138038327@lid')],+ ['unblockContact', (svc: ContactService) => svc.unblockContact('s1', '159442138038327@lid')],+ ])('%s forwards a privacy id (@lid) to the engine unchanged', async (_name, call) => {+ const blockContact = jest.fn().mockResolvedValue(undefined);+ const unblockContact = jest.fn().mockResolvedValue(undefined);+ await call(makeService({ blockContact, unblockContact }));+ const called = blockContact.mock.calls.length ? blockContact : unblockContact;+ expect(called).toHaveBeenCalledWith('159442138038327@lid');+ });++ it('accepts every id shape the blocklist read can return', async () => {+ // The read half answers ids only, so whatever it lists must be feedable back to unblock.+ const getBlockedContacts = jest.fn().mockResolvedValue(['[email protected]', '159442138038327@lid']);+ const unblockContact = jest.fn().mockResolvedValue(undefined);+ const svc = makeService({ getBlockedContacts, unblockContact });+ for (const id of await svc.getBlockedContacts('s1')) {+ await svc.unblockContact('s1', id);+ }+ expect(unblockContact).toHaveBeenCalledWith('[email protected]');+ expect(unblockContact).toHaveBeenCalledWith('159442138038327@lid');+ });+it('qualifies a bare number and forwards it to the engine as a @c.us id', async () => {const blockContact = jest.fn().mockResolvedValue(undefined);await makeService({ blockContact }).blockContact('s1', '628123');diff --git a/src/modules/contact/contact.service.ts b/src/modules/contact/contact.service.tsindex fa8427557..8365ccb1d 100644--- a/src/modules/contact/contact.service.ts+++ b/src/modules/contact/contact.service.ts@@ -125,17 +125,36 @@ export class ContactService {}/**- * Guarded like the addressbook writes: whatsapp-web.js's Contact.block()/unblock() silently- * return false for a group id (nothing blocked, reported as success), and Baileys passes the id- * to updateBlockStatus, whose Boom for an unresolvable jid has no HttpException mapping (opaque- * 500). A group/newsletter/lid id does not name a person, so refuse it here on both engines- * with the same 400 the addressbook surfaces use.+ * Guarded because whatsapp-web.js's Contact.block()/unblock() silently return false for a group id+ * (nothing blocked, reported as success), and Baileys passes the id to updateBlockStatus, whose+ * Boom for an unresolvable jid has no HttpException mapping (opaque 500). See `assertBlockable`+ * for why this guard is wider than the addressbook one.*/blockContact(sessionId: string, contactId: string) {- this.assertAddressable(contactId);+ this.assertBlockable(contactId);return this.getEngine(sessionId).blockContact(this.toAddressableId(contactId));}+ /**+ * Blocking acts on an IDENTITY, not on an addressbook row, so unlike the addressbook writes it+ * accepts a privacy id (`@lid`) as well as a phone-based one. It has to: a privacy-id contact has+ * no phone number, and the blocklist READ answers ids verbatim (Baileys maps each blocked jid+ * through `toNeutralJid`, which leaves an unresolved lid as `<lid>@lid`; whatsapp-web.js returns+ * the wid as-is), so refusing them made the very ids this API hands out unusable for the matching+ * write and left such a contact listed as blocked with no way to unblock it.+ *+ * Neither engine needs a phone here: Baileys passes the jid straight to `updateBlockStatus`, and+ * whatsapp-web.js only short-circuits (`Contact.block()` returns false without acting) for a+ * group. What must still be refused is an id that names no individual at all, which is what made+ * whatsapp-web.js answer 200 "blocked" while nothing was blocked.+ */+ private assertBlockable(contactId: string): void {+ if (isIndividualWid(contactId) || this.isBareNumber(contactId)) return;+ throw new BadRequestException(+ `Contact ${contactId} does not name an individual; block and unblock act on a person, so pass a phone-based or privacy (@lid) contact id instead`,+ );+ }+/*** An addressbook entry is keyed by a PHONE NUMBER, so a privacy-id (`@lid`) contact cannot be* saved or removed: the lid's digits are not a phone number, and whatsapp-web.js — which takes a@@ -195,7 +214,7 @@ export class ContactService {}unblockContact(sessionId: string, contactId: string) {- this.assertAddressable(contactId);+ this.assertBlockable(contactId);return this.getEngine(sessionId).unblockContact(this.toAddressableId(contactId));}}