Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(identity): recognize Meta-hosted ids instead of refusing them

Yudhi Armyndharis03810a6de450
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c833754d2..0137f21c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+
+- Meta-hosted ids (`@hosted`, `@hosted.lid`) normalize to the dialect they name. They parsed as unknown before, so a chat surfaced with kind `unknown` and the same id was then refused with a `400` on any write.
+
## [0.21.0] - 2026-08-18
### Fixed
diff --git a/docs/03-system-architecture.md b/docs/03-system-architecture.md
index 2d818e6c8..c183b539c 100644
--- a/docs/03-system-architecture.md
+++ b/docs/03-system-architecture.md
@@ -180,7 +180,9 @@ chat `id`) is reduced to one small **neutral dialect**:
Never `@s.whatsapp.net`, never a `:device` suffix. **Resolution rule:** prefer `@c.us` (resolve a lid
to its phone when the mapping is known), and fall back to `@lid` only when it can't be resolved - an
-unresolved lid is never faked into a phone number.
+unresolved lid is never faked into a phone number. WhatsApp's Meta-hosted dialects fold in here
+too: `<n>@hosted` is the same account as `<n>@c.us` and normalizes to it, and `<lid>@hosted.lid`
+normalizes like any other lid. Baileys makes the same fold itself on every inbound message.
The shared implementation lives in `src/engine/identity/wa-id.ts` (`parseWaId` / `toNeutralJid`); the
contract is documented on the `IWhatsAppEngine` interface.
diff --git a/docs/21-glossary.md b/docs/21-glossary.md
index 20bcf4839..4bfe5654b 100644
--- a/docs/21-glossary.md
+++ b/docs/21-glossary.md
@@ -134,7 +134,7 @@ Data stored in RAM. Fast but non-persistent — lost on restart. Independent of
### JID (Jabber ID)
-WhatsApp's id format, inherited from XMPP; the user-facing "Chat ID" is a JID. The same entity can be addressed in more than one dialect: `<phone>@c.us` (whatsapp-web.js, and OpenWA's neutral form), `<phone>@s.whatsapp.net` (Baileys' raw form for the same user), `<id>@g.us` (a group), or `<lid>@lid` (a LID, a privacy id). OpenWA normalizes engine ids to a single neutral dialect at the engine boundary - see _System Architecture > WhatsApp Identity Contract_.
+WhatsApp's id format, inherited from XMPP; the user-facing "Chat ID" is a JID. The same entity can be addressed in more than one dialect: `<phone>@c.us` (whatsapp-web.js, and OpenWA's neutral form), `<phone>@s.whatsapp.net` (Baileys' raw form for the same user), `<id>@g.us` (a group), or `<lid>@lid` (a LID, a privacy id). WhatsApp also issues Meta-hosted dialects of the first and last of these (`@hosted`, `@hosted.lid`); they name the same account and normalize to the same neutral form. OpenWA normalizes engine ids to a single neutral dialect at the engine boundary - see _System Architecture > WhatsApp Identity Contract_.
### Job Queue
diff --git a/src/engine/identity/wa-id.spec.ts b/src/engine/identity/wa-id.spec.ts
index ae03c6845..b44cbfebe 100644
--- a/src/engine/identity/wa-id.spec.ts
+++ b/src/engine/identity/wa-id.spec.ts
@@ -2,6 +2,7 @@ import {
chatKind,
isAddressableParticipant,
isChannelJid,
+ isIndividualWid,
parseWaId,
toNeutralJid,
toParticipantWid,
@@ -154,4 +155,50 @@ describe('wa-id', () => {
},
);
});
+
+ /**
+ * WhatsApp issues Meta-hosted dialects of a phone id and a lid, and Baileys decodes them off the
+ * wire (`WAJIDDomains.HOSTED` = 128, `HOSTED_LID` = 129). They used to parse as `unknown` here, so
+ * an id we had emitted on GET /chats was refused with a 400 when it was handed back to us.
+ *
+ * They fold into `user` and `lid` rather than becoming kinds of their own, because the user-part
+ * names the SAME account: Baileys rewrites `<n>@hosted` to `<n>@s.whatsapp.net` on every inbound
+ * message (`cleanMessage`). A separate kind would split one person's history in two.
+ */
+ describe('Meta-hosted dialects', () => {
+ it('folds each hosted dialect into the entity it names', () => {
+ expect(parseWaId('628111@hosted')).toMatchObject({ kind: 'user', userPart: '628111' });
+ expect(parseWaId('[email protected]')).toMatchObject({ kind: 'lid', userPart: '4707' });
+ expect(parseWaId('628111:3@hosted')).toMatchObject({ kind: 'user', userPart: '628111', device: '3' });
+ expect(parseWaId('[email protected]')).toMatchObject({ kind: 'lid', userPart: 'abc' });
+ });
+
+ it('does not confuse @hosted.lid with @hosted: the domain is the whole suffix', () => {
+ // Same user-part, different entity: one is a phone number, the other a privacy id.
+ expect(parseWaId('[email protected]').kind).toBe('lid');
+ expect(parseWaId('4707@hosted').kind).toBe('user');
+ });
+
+ it('accepts them as individuals, which is what the guards refused before', () => {
+ expect(isIndividualWid('628123456789@hosted')).toBe(true);
+ expect(isIndividualWid('[email protected]')).toBe(true);
+ expect(isAddressableParticipant('628123456789@hosted')).toBe(true);
+ // The user-part still has to look like an id: the domain alone was never enough.
+ expect(isIndividualWid('NOT A USER@hosted')).toBe(false);
+ expect(isIndividualWid('[email protected]')).toBe(false);
+ });
+
+ it('normalizes to the neutral dialect of the same account, so history stays in one place', () => {
+ expect(toNeutralJid('628111@hosted')).toBe('[email protected]');
+ expect(toNeutralJid('628111:3@hosted')).toBe('[email protected]');
+ // An unresolved hosted lid keeps the lid dialect, exactly like a plain @lid.
+ expect(toNeutralJid('[email protected]')).toBe('4707@lid');
+ expect(toNeutralJid('[email protected]', () => '628111')).toBe('[email protected]');
+ });
+
+ it('surfaces as an individual chat, not unknown', () => {
+ expect(chatKind('628111@hosted')).toBe('individual');
+ expect(chatKind('[email protected]')).toBe('individual');
+ });
+ });
});
diff --git a/src/engine/identity/wa-id.ts b/src/engine/identity/wa-id.ts
index 209eaa9fe..548d78b2f 100644
--- a/src/engine/identity/wa-id.ts
+++ b/src/engine/identity/wa-id.ts
@@ -25,8 +25,30 @@
export type WaIdKind = 'user' | 'group' | 'lid' | 'status' | 'newsletter' | 'broadcast' | 'unknown';
-/** Domains that denote a phone-addressed user (the two are the same entity, different dialects). */
-const USER_DOMAINS = new Set(['c.us', 's.whatsapp.net']);
+/**
+ * Domain to kind. `c.us` and `s.whatsapp.net` are one entity in two dialects.
+ *
+ * `hosted` and `hosted.lid` are two more dialects of the same two entities, not entities of their
+ * own: they are the Meta-hosted forms WhatsApp issues and Baileys decodes off the wire
+ * (`WAJIDDomains.HOSTED` = 128, `HOSTED_LID` = 129). The user-part is the SAME account either way,
+ * which the library states by folding them itself on every inbound message (`cleanMessage`:
+ * `<n>@hosted` becomes `<n>@s.whatsapp.net`, `<lid>@hosted.lid` becomes `<lid>@lid`) and by treating
+ * `isHostedPnUser` as `isPnUser` throughout its history handling.
+ *
+ * So they fold here too. A separate kind would split one person's history in two: rows arrive under
+ * the plain dialect (Baileys already rewrote them) while a caller filtering by the hosted id we
+ * published would match none of them.
+ */
+const DOMAIN_KINDS = new Map<string, WaIdKind>([
+ ['c.us', 'user'],
+ ['s.whatsapp.net', 'user'],
+ ['lid', 'lid'],
+ ['hosted', 'user'],
+ ['hosted.lid', 'lid'],
+ ['g.us', 'group'],
+ ['newsletter', 'newsletter'],
+ ['broadcast', 'broadcast'],
+]);
export interface ParsedWaId {
kind: WaIdKind;
@@ -56,17 +78,7 @@ export function parseWaId(jid: string): ParsedWaId {
}
const domain = lower.slice(at + 1);
const [local, device] = lower.slice(0, at).split(':');
- const kind: WaIdKind = USER_DOMAINS.has(domain)
- ? 'user'
- : domain === 'g.us'
- ? 'group'
- : domain === 'lid'
- ? 'lid'
- : domain === 'newsletter'
- ? 'newsletter'
- : domain === 'broadcast'
- ? 'broadcast'
- : 'unknown';
+ const kind: WaIdKind = DOMAIN_KINDS.get(domain) ?? 'unknown';
return { kind, userPart: local, device, raw };
}
@@ -78,7 +90,7 @@ const NUMERIC_ID = /^\d{5,}$/;
/**
* Whether a string names an individual in a qualified dialect: `<phone>@c.us` (and its raw-protocol
- * twin `@s.whatsapp.net`) or `<lid>@lid`. A `:<device>` suffix is fine — {@link parseWaId} strips it.
+ * twin `@s.whatsapp.net`), `<lid>@lid`, or the Meta-hosted dialect of either. A `:<device>` suffix is fine — {@link parseWaId} strips it.
*
* **The domain alone is not enough.** `parseWaId` classifies anything ending in `@c.us` as a user, so
* a check that stops at the kind accepts `NOT A [email protected]` and `@c.us`. Those still reach WhatsApp
diff --git a/src/modules/contact/contact.service.spec.ts b/src/modules/contact/contact.service.spec.ts
index 013f2e9d3..aa15e0af1 100644
--- a/src/modules/contact/contact.service.spec.ts
+++ b/src/modules/contact/contact.service.spec.ts
@@ -127,6 +127,23 @@ describe('ContactService', () => {
expect(unblockContact).not.toHaveBeenCalled();
});
+ /**
+ * A Meta-hosted id names the same account as its plain twin, so the write must reach the engine
+ * rather than be refused. It is neutralized on the way there: whatsapp-web.js knows no `@hosted`
+ * domain, so forwarding the suffix verbatim would fail inside the page instead of blocking anyone.
+ */
+ it('accepts a Meta-hosted id and hands the engine the neutral dialect', () => {
+ const blockContact = jest.fn();
+ const unblockContact = jest.fn();
+ const svc = makeService({ blockContact, unblockContact });
+
+ svc.blockContact('s1', '628123456789@hosted');
+ svc.unblockContact('s1', '[email protected]');
+
+ expect(blockContact).toHaveBeenCalledWith('[email protected]');
+ expect(unblockContact).toHaveBeenCalledWith('12345678901234567890@lid');
+ });
+
/**
* 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
diff --git a/src/modules/contact/contact.service.ts b/src/modules/contact/contact.service.ts
index 8365ccb1d..de88de1b5 100644
--- a/src/modules/contact/contact.service.ts
+++ b/src/modules/contact/contact.service.ts
@@ -3,7 +3,7 @@ import { EngineRegistry } from '../../engine/engine-registry.service';
import { createLogger } from '../../common/services/logger.service';
import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';
import { paginate, ListOptions } from '../../common/utils/paginate';
-import { isIndividualWid, parseWaId } from '../../engine/identity/wa-id';
+import { isIndividualWid, parseWaId, toNeutralJid } from '../../engine/identity/wa-id';
/**
* Owns engine access for contact operations so the "session not started" guard and
@@ -200,7 +200,12 @@ export class ContactService {
* qualification is a no-op there.
*/
private toAddressableId(contactId: string): string {
- return this.isBareNumber(contactId) ? `${contactId.trim()}@c.us` : contactId;
+ const trimmed = contactId.trim();
+ // Neutralized rather than passed through: a Meta-hosted id names the same account as its plain
+ // twin but whatsapp-web.js knows no such domain, so forwarding the suffix verbatim would fail
+ // inside the page instead of acting on the person. toNeutralJid is idempotent on ids already in
+ // the neutral dialect, and the Baileys adapter re-encodes to its own dialect on the way out.
+ return this.isBareNumber(trimmed) ? `${trimmed}@c.us` : toNeutralJid(trimmed);
}
upsertContact(sessionId: string, contactId: string, firstName: string, lastName?: string) {
diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.ts
index 16aa517c6..dbc84c141 100644
--- a/src/modules/message/message.service.spec.ts
+++ b/src/modules/message/message.service.spec.ts
@@ -219,6 +219,23 @@ describe('MessageService', () => {
expect(messages.map(m => m.id)).toEqual(['m-dm']); // only the @c.us DM matches
});
+
+ // `<n>@hosted` is the Meta-hosted dialect of the SAME phone account, which is why Baileys
+ // rewrites it to `<n>@s.whatsapp.net` on every inbound message. Rows therefore land under the
+ // plain dialect while a chat id we published may carry the hosted suffix, so a filter given the
+ // hosted form has to expand to the phone dialects or it returns none of the person's history.
+ it('expands a hosted id into the phone dialects, so it finds rows stored under @c.us', async () => {
+ const qb = makeFilteringQb();
+ (repository.createQueryBuilder as jest.Mock).mockReturnValue(qb);
+
+ const { messages } = await service.getMessages('sess-1', { from: '628999@hosted' });
+
+ expect(messages.map(m => m.id)).toEqual(['m-dm']);
+ expect(lidMappingStore.lidsForPhone).toHaveBeenCalledWith('628999');
+ const calls = qb.andWhere.mock.calls as Array<[string, { froms?: string[] }?]>;
+ const froms = calls.find(c => c[1]?.froms)?.[1]?.froms;
+ expect(froms).toEqual(expect.arrayContaining(['628999@hosted', '[email protected]', '[email protected]']));
+ });
});
// ── getMessages from-filter matches the group author ──────────────