Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(identity): require a numeric user-part in an individual WID

Yudhi Armyndharisba00fa45aba8
diff --git a/src/engine/identity/wa-id.spec.ts b/src/engine/identity/wa-id.spec.ts
index b97a0f2d5..ae03c6845 100644
--- a/src/engine/identity/wa-id.spec.ts
+++ b/src/engine/identity/wa-id.spec.ts
@@ -115,6 +115,18 @@ describe('wa-id', () => {
['status@broadcast', false],
['abc@newsletter', false],
['abc@weird', false],
+ // A recognised domain is not enough: the user-part has to look like a WhatsApp id, or the
+ // string still reaches the page-side createWid and throws there — the very failure the guard
+ // exists to prevent. Reproduced against a live session before this row was added.
+ ['NOT A [email protected]', false],
+ ['[email protected]', false],
+ ['[email protected]', false],
+ ['abc@lid', false],
+ ['@c.us', false],
+ ['x [email protected]', false],
+ ['[email protected]', false],
+ ['[email protected]', false],
+ ['[email protected]', false],
])('classifies %s as %s', (value, expected) => {
expect(isAddressableParticipant(value)).toBe(expected);
});
@@ -131,10 +143,15 @@ describe('wa-id', () => {
expect(toParticipantWid('[email protected]')).toBe('[email protected]');
});
- it('does not append a second domain to a string that already carries one', () => {
- // The difference from the old `p.includes('@')` test: an unrecognised domain must pass
- // through rather than become `abc@[email protected]`.
- expect(toParticipantWid('abc@weird')).toBe('abc@weird');
- });
+ it.each([['abc@weird'], ['abc'], ['NOT A USER'], ['']])(
+ 'leaves %s alone rather than minting a nonsense id from it',
+ value => {
+ // The old rule was `p.includes('@') ? p : p + '@c.us'`, which turned every un-domained
+ // string into one — `abc` became `[email protected]`, a well-formed id naming nobody. Keying on the
+ // bare-number shape instead means only a number is ever qualified. (`abc@weird` alone would
+ // NOT show this: the old rule passed it through too, so it discriminates nothing.)
+ expect(toParticipantWid(value)).toBe(value);
+ },
+ );
});
});
diff --git a/src/engine/identity/wa-id.ts b/src/engine/identity/wa-id.ts
index 0afe59733..209eaa9fe 100644
--- a/src/engine/identity/wa-id.ts
+++ b/src/engine/identity/wa-id.ts
@@ -70,36 +70,47 @@ export function parseWaId(jid: string): ParsedWaId {
return { kind, userPart: local, device, raw };
}
-/** A digits-only id, e.g. `628123456789` — accepted for convenience and qualified by {@link toParticipantWid}. */
-const BARE_NUMBER = /^\d{5,}$/;
+/**
+ * The user-part of every id that names a person: a phone number or a lid, both digits-only. The
+ * floor rules out `0`, `-1` and the empty string, none of which is a WhatsApp id.
+ */
+const NUMERIC_ID = /^\d{5,}$/;
/**
- * Whether a string can address an individual participant of a group.
+ * 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.
*
- * A participant names a person, so only the individual dialects qualify: `<phone>@c.us` (and its
- * raw-protocol twin `@s.whatsapp.net`), `<lid>@lid`, or a bare phone number. A group id is rejected —
- * a group cannot be a member of a group — as is anything else.
- *
- * Nothing validated the shape before, so free text reached whatsapp-web.js's page-side `createWid`,
- * which threw a minified `t: t` and left the caller with an undiagnosable 500 (#1220). The same
- * failure was diagnosed once already for message mentions (#1068, see is-mention-wid.validator.ts).
+ * **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
+ * Web's page-side `createWid`, which throws an error the minified bundle reports without a name, and
+ * the caller gets the undiagnosable 500 this check exists to prevent — reproduced against a live
+ * session. The user-part has to look like a WhatsApp id too (#1220, and #1068 before it).
+ */
+export function isIndividualWid(value: string): boolean {
+ const { kind, userPart } = parseWaId(value.trim());
+ return (kind === 'user' || kind === 'lid') && NUMERIC_ID.test(userPart);
+}
+
+/**
+ * Whether a string can address an individual participant of a group: an individual WID, or a bare
+ * phone number accepted for convenience and qualified by {@link toParticipantWid}. A group id is
+ * rejected — a group cannot be a member of a group — as is anything else.
*/
export function isAddressableParticipant(value: string): boolean {
const trimmed = value.trim();
- const { kind } = parseWaId(trimmed);
- return kind === 'user' || kind === 'lid' || (kind === 'unknown' && BARE_NUMBER.test(trimmed));
+ return isIndividualWid(trimmed) || NUMERIC_ID.test(trimmed);
}
/**
* Qualify a bare number to the neutral `@c.us` dialect. Anything else passes through verbatim.
*
- * Deliberately keyed on the bare-number shape rather than on the absence of `@`: the old test
- * (`p.includes('@') ? p : p + '@c.us'`) agrees for every valid input, but this form cannot turn an
- * unrecognised domain into a double-qualified `abc@[email protected]`.
+ * Deliberately keyed on the bare-number shape rather than on the absence of `@`: the old rule
+ * (`p.includes('@') ? p : p + '@c.us'`) minted an id out of ANY un-domained string, so `abc` became
+ * `[email protected]` — a well-formed id naming nobody.
*/
export function toParticipantWid(value: string): string {
const trimmed = value.trim();
- return BARE_NUMBER.test(trimmed) ? `${trimmed}@c.us` : trimmed;
+ return NUMERIC_ID.test(trimmed) ? `${trimmed}@c.us` : trimmed;
}
/**
diff --git a/src/modules/message/dto/is-mention-wid.validator.ts b/src/modules/message/dto/is-mention-wid.validator.ts
index ac37b3449..ee25c9c01 100644
--- a/src/modules/message/dto/is-mention-wid.validator.ts
+++ b/src/modules/message/dto/is-mention-wid.validator.ts
@@ -1,12 +1,12 @@
import { ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
-import { parseWaId } from '../../../engine/identity/wa-id';
+import { isIndividualWid } from '../../../engine/identity/wa-id';
/**
* Whether a string can address an individual in a `mentions` array.
*
* A mention names a person, so only the two individual dialects are accepted: `<phone>@c.us` (and
- * its raw-protocol twin `<phone>@s.whatsapp.net`) and `<lid>@lid`. A `:<device>` suffix is fine —
- * {@link parseWaId} strips it.
+ * its raw-protocol twin `<phone>@s.whatsapp.net`) and `<lid>@lid`, each with a numeric user-part.
+ * A `:<device>` suffix is fine — {@link isIndividualWid} strips it.
*
* Rejecting the rest is what keeps a malformed entry out of the engine. Nothing validated the shape
* before, so a bare number or any other junk string reached WhatsApp Web's `createWid` inside
@@ -14,14 +14,17 @@ import { parseWaId } from '../../../engine/identity/wa-id';
* bundle is minified, so the throw arrived as `Error: t: t` and the caller received
* `500 Internal server error` — undiagnosable from either end (#1068).
*
+ * The first version of this check tested only the DOMAIN, so `NOT A [email protected]` still satisfied it and
+ * still reached `createWid`. Sharing {@link isIndividualWid} with the group-participant guard closes
+ * that in both places at once.
+ *
* Group ids are rejected too: `mentions` is the participant list, and mentioning a group is a
* separate WhatsApp feature (`groupMentions`) that this API does not expose. A `@g.us` here was
* never honoured — it just failed later and less clearly.
*/
export function isMentionWid(value: unknown): boolean {
if (typeof value !== 'string') return false;
- const kind = parseWaId(value).kind;
- return kind === 'user' || kind === 'lid';
+ return isIndividualWid(value);
}
@ValidatorConstraint({ name: 'isMentionWid', async: false })
diff --git a/src/modules/message/dto/send-message.dto.spec.ts b/src/modules/message/dto/send-message.dto.spec.ts
index a060a4328..f74e0a78b 100644
--- a/src/modules/message/dto/send-message.dto.spec.ts
+++ b/src/modules/message/dto/send-message.dto.spec.ts
@@ -31,6 +31,16 @@ describe('SendTextMessageDto mentions', () => {
expect(errors).toHaveLength(0);
});
+ it.each([['NOT A [email protected]'], ['[email protected]'], ['@c.us'], ['abc@lid'], ['[email protected]']])(
+ 'rejects %s — a recognised domain is not enough on its own',
+ async mention => {
+ // These satisfied the original domain-only check and still reached the page-side createWid,
+ // which is the throw #1068 was filed for.
+ const errors = await validateDto(SendTextMessageDto, { chatId: '[email protected]', text: 'hi', mentions: [mention] });
+ expect(errors.length).toBeGreaterThan(0);
+ },
+ );
+
it('rejects a non-string element in mentions', async () => {
const errors = await validateDto(SendTextMessageDto, {
chatId: '[email protected]',
@@ -44,7 +54,9 @@ describe('SendTextMessageDto mentions', () => {
const errors = await validateDto(SendTextMessageDto, {
chatId: '[email protected]',
text: 'hi',
- mentions: Array.from({ length: 1025 }, (_, i) => `${i}@c.us`),
+ // Each entry must be individually VALID, or the array-size rule is not what fails and the
+ // test passes for the wrong reason: `[email protected]` is rejected on shape.
+ mentions: Array.from({ length: 1025 }, (_, i) => `${628000000000 + i}@c.us`),
});
expect(errors.length).toBeGreaterThan(0);
});