Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(contact): validate the id, not just its domain, on addressbook writes
Yudhi Armyndharis01035fb8fe8b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 59acdae9c..e1ea573aa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- The addressbook write guard now checks the id, not just its domain. `parseWaId('NOT A [email protected]').kind` is `'user'`, so free text, letters and an empty user-part cleared the guard, reached the engine and were reported to the caller as a saved contact — for an entry keyed by something that is not a phone number. It now applies `isIndividualWid`, the same predicate the group-participant, channel-admin and message-mention surfaces already use on these very shapes.+- `POST /api/infra/import-data` now answers 400 for a malformed archive instead of 500. A table value that is not an array — a hand-edited or truncated backup — was dereferenced with `.map()` before anything checked it, so the operator was told the server had broken when their file was simply wrong. Checked for every table, not only `sessions`: the rest are read inside the transaction, where the same mistake would fail mid-restore rather than before it opens.- README no longer inverts the shipped MCP posture. It described `MCP_ENABLED=true` as exposing 51 tools including messaging, when the surface is read-only unless `MCP_READONLY=false` — a secure default the code chose deliberately — so an operator wired a client, saw no send or group tools, and had nothing in README pointing at the knob. It now states the 25 read-only tools mounted by default and names the opt-in for all 51. A gate derives both counts from the tool sources, so a tier change fails instead of making the prose wrong again.diff --git a/src/modules/contact/contact.service.spec.ts b/src/modules/contact/contact.service.spec.tsindex a7e0f0520..29ea8bf17 100644--- a/src/modules/contact/contact.service.spec.ts+++ b/src/modules/contact/contact.service.spec.ts@@ -141,5 +141,39 @@ describe('ContactService', () => {expect(upsertContact).not.toHaveBeenCalled();},);++ /**+ * The guard checked the JID DOMAIN only: `parseWaId(id).kind === 'user'` holds for anything+ * ending in `@c.us` / `@s.whatsapp.net` whatever its user-part. So free text reached the engine+ * and the caller was told the contact was saved — while the three sibling surfaces on the same+ * shapes (group participants, channel admin, message mentions) reject exactly these strings+ * through isAddressableParticipant, which also requires the user-part to be numeric.+ */+ it.each([+ ['free text', 'NOT A [email protected]'],+ ['letters', '[email protected]'],+ ['an empty user-part', '@c.us'],+ ['the engine dialect with free text', '[email protected]'],+ ])('refuses %s without reaching the engine', (_label, id) => {+ const upsertContact = jest.fn();+ const deleteContact = jest.fn();+ const svc = makeService({ upsertContact, deleteContact });++ expect(() => svc.upsertContact('s1', id, 'Ada')).toThrow(BadRequestException);+ expect(() => svc.deleteContact('s1', id)).toThrow(BadRequestException);+ expect(upsertContact).not.toHaveBeenCalled();+ expect(deleteContact).not.toHaveBeenCalled();+ });++ // Negative twin: the ids this route exists to serve must still pass.+ it.each([+ ['a phone jid', '[email protected]'],+ ['the engine dialect', '[email protected]'],+ ['a bare number', '628123456789'],+ ])('still accepts %s', (_label, id) => {+ const upsertContact = jest.fn();+ makeService({ upsertContact }).upsertContact('s1', id, 'Ada');+ expect(upsertContact).toHaveBeenCalled();+ });});});diff --git a/src/modules/contact/contact.service.ts b/src/modules/contact/contact.service.tsindex 95223d0be..d1cb8a172 100644--- a/src/modules/contact/contact.service.ts+++ b/src/modules/contact/contact.service.ts@@ -2,7 +2,7 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/commimport { EngineRegistry } from '../../engine/engine-registry.service';import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';import { paginate, ListOptions } from '../../common/utils/paginate';-import { parseWaId } from '../../engine/identity/wa-id';+import { isIndividualWid, parseWaId } from '../../engine/identity/wa-id';/*** Owns engine access for contact operations so the "session not started" guard and@@ -122,7 +122,12 @@ export class ContactService {// Allow-list rather than deny-list: only a user id (or a bare number, which parses as// `unknown`) names a phone. A group/newsletter/broadcast/status id also carries digits that// whatsapp-web.js would happily store as a phone number for a contact that does not exist.- if (kind === 'user' || this.isBareNumber(contactId)) return;+ // The DOMAIN alone is not enough: `parseWaId('NOT A [email protected]').kind` is 'user', so free text+ // cleared this guard and reached the engine, which reported a successful save for an entry+ // keyed by something that is not a phone number. isIndividualWid additionally requires the+ // user-part to be numeric — the same predicate the group-participant, channel-admin and+ // message-mention surfaces already apply to these very shapes.+ if ((kind === 'user' && isIndividualWid(contactId)) || this.isBareNumber(contactId)) return;if (kind === 'lid') {throw new BadRequestException(`Contact ${contactId} is a privacy id (@lid) with no known phone number; the addressbook is keyed by phone number, so pass a phone-based contact id instead`,