Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep every recipient the plain list parser found

Yudhi Armyndharis5553beabdade
diff --git a/dashboard/src/utils/bulkRecipients.compat.test.ts b/dashboard/src/utils/bulkRecipients.compat.test.ts
new file mode 100644
index 000000000..d7db9e639
--- /dev/null
+++ b/dashboard/src/utils/bulkRecipients.compat.test.ts
@@ -0,0 +1,110 @@
+// The recipients parser reads a chat id wrapped in CSV quotes and nothing else differently from the one that
+// shipped before quotes were considered: every recipient that parser found, this one finds too, and a quote that
+// never closes cannot take a later line with it. The reference below is that parser, kept verbatim.
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { parseBulkRecipients } from './bulkRecipients.ts';
+import { normalizePhoneText } from './phone.ts';
+
+const FIELD_SEPARATORS = /[,;\t،؛]/;
+const MIN_PHONE_DIGITS = 6;
+const CHAT_ID = /^[0-9][0-9:-]*@(c\.us|g\.us|s\.whatsapp\.net|lid|newsletter|broadcast)$/i;
+
+function shippedParse(text: string): string[] {
+ const seen = new Set<string>();
+ for (const rawLine of normalizePhoneText(text).split(/\r\n?|\n/)) {
+ for (const rawField of rawLine.split(FIELD_SEPARATORS)) {
+ const field = rawField.trim();
+ if (!field) continue;
+ if (field.includes('@')) {
+ if (CHAT_ID.test(field)) seen.add(field);
+ continue;
+ }
+ const digits = field.replace(/[^0-9]/g, '');
+ if (digits.length >= MIN_PHONE_DIGITS) seen.add(`${digits}@c.us`);
+ }
+ }
+ return [...seen];
+}
+
+// A fixed generator, so a failure names an input that can be run again.
+function random(seed: number): () => number {
+ return () => {
+ seed = (seed + 0x6d2b79f5) | 0;
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+ };
+}
+
+const NUMBERS = [
+ '628111222333',
+ '+62 812-3456-7890',
+ '+1 (555) 010-2233',
+ '0049 170 1234567',
+ '12345',
+ '1',
+ '٦٢٨١٢٣٤٥٦٧٨٩',
+ '628111222333',
+];
+const IDS = [
+ '628111222333:[email protected]',
+ '262813461250071@lid',
+ '120363000000000000@newsletter',
+ 'status@broadcast',
+];
+const SEPARATORS = [',', ';', '\t', '،', '؛', ', '];
+const LINE_ENDINGS = ['\n', '\r\n', '\r'];
+
+function build(rand: () => number): string {
+ const pick = <T>(list: readonly T[]): T => list[Math.floor(rand() * list.length)];
+ const lines: string[] = [];
+ for (let l = 1 + Math.floor(rand() * 6); l > 0; l--) {
+ const fields: string[] = [];
+ for (let f = 1 + Math.floor(rand() * 4); f > 0; f--) {
+ let field = rand() < 0.5 ? pick(NUMBERS) : rand() < 0.8 ? pick(IDS) : pick(['', ' ', 'Alice', '"', '""']);
+ const quoting = rand();
+ if (quoting < 0.35) field = `"${field}"`;
+ else if (quoting < 0.45) field = `"${field}`;
+ else if (quoting < 0.55) field = `${field}"`;
+ fields.push(field);
+ }
+ let line = fields.map((field, i) => (i === 0 ? field : pick(SEPARATORS) + field)).join('');
+ if (rand() < 0.2) line = `"${line}"`;
+ lines.push(line);
+ }
+ let text = lines.map((line, i) => (i === 0 ? line : pick(LINE_ENDINGS) + (rand() < 0.1 ? '\n' : '') + line)).join('');
+ for (let stray = Math.floor(rand() * 3); stray > 0; stray--) {
+ const at = Math.floor(rand() * (text.length + 1));
+ text = `${text.slice(0, at)}"${text.slice(at)}`;
+ }
+ return text;
+}
+
+test('every recipient the shipped parser found is still found, whatever the quoting', () => {
+ const rand = random(20261027);
+ for (let n = 0; n < 5000; n++) {
+ const input = build(rand);
+ const found = new Set(parseBulkRecipients(input));
+ for (const id of shippedParse(input)) assert.ok(found.has(id), `${id} lost from ${JSON.stringify(input)}`);
+ }
+});
+
+test('the only recipients added are chat ids a CSV quote had been hiding, and a stray quote costs no later line', () => {
+ const rand = random(7);
+ for (let n = 0; n < 5000; n++) {
+ const input = build(rand);
+ const before = new Set(shippedParse(input));
+ for (const id of parseBulkRecipients(input)) {
+ if (before.has(id)) continue;
+ assert.match(id, CHAT_ID, JSON.stringify(input));
+ assert.ok(input.includes(id) && input.includes('"'), `${id} added to ${JSON.stringify(input)}`);
+ }
+ const tail = parseBulkRecipients(`${input}\n628999888777`);
+ assert.ok(tail.includes('[email protected]'), `a later line was swallowed after ${JSON.stringify(input)}`);
+ }
+});
diff --git a/dashboard/src/utils/bulkRecipients.test.ts b/dashboard/src/utils/bulkRecipients.test.ts
index df7fe3608..691ae64e1 100644
--- a/dashboard/src/utils/bulkRecipients.test.ts
+++ b/dashboard/src/utils/bulkRecipients.test.ts
@@ -131,24 +131,29 @@ test('CSV quoting preserves full chat IDs and existing recipient syntax', () =>
]);
});
-test('a quoted CSV name cannot introduce a recipient between commas', () => {
- assert.deepEqual(parseBulkRecipients('"Alice,[email protected],office",+15550102233'), ['[email protected]']);
-});
-
-test('a quoted multiline CSV name cannot introduce a recipient on an interior line', () => {
- assert.deepEqual(parseBulkRecipients('"Alice\r\[email protected]\r\noffice",+15550102233'), ['[email protected]']);
+test('a list wrapped in one pair of quotes, or a quoted cell of several numbers, keeps every number', () => {
+ assert.deepEqual(parseBulkRecipients('"628111222333\n628222333444\n628333444555"'), [
+ ]);
+ assert.deepEqual(parseBulkRecipients('"628111222333,628222333444"'), ['[email protected]', '[email protected]']);
+ assert.deepEqual(parseBulkRecipients('"[email protected]\r\[email protected]"'), [
+ ]);
});
test('a separator inside a quoted CSV field never concatenates two phone numbers', () => {
- assert.deepEqual(parseBulkRecipients('"628123456789,15550102233",[email protected]'), ['[email protected]']);
+ assert.deepEqual(parseBulkRecipients('"628123456789,15550102233",[email protected]'), [
+ ]);
assert.deepEqual(parseBulkRecipients(' "[email protected]" '), ['[email protected]']);
});
-test('an unclosed quoted CSV field is not treated as a full chat ID', () => {
- assert.deepEqual(parseBulkRecipients('"[email protected]'), []);
-});
-
-test('a quote that never closes does not drop the recipients after it', () => {
+test('a quote that never closes does not drop the recipients around it', () => {
assert.deepEqual(parseBulkRecipients('"628111111111\n628222222222\n628333333333'), [
@@ -159,5 +164,20 @@ test('a quote that never closes does not drop the recipients after it', () => {
]);
- assert.deepEqual(parseBulkRecipients('"[email protected]\[email protected]'), ['[email protected]']);
+ assert.deepEqual(parseBulkRecipients('"[email protected]\[email protected]'), [
+ ]);
+ // One stray quote and a later one used to pair up and swallow the lines between them.
+ assert.deepEqual(
+ parseBulkRecipients('628111222333\n"628222333444\n628333444555\n628444555666\n628555666777\n"628666777888'),
+ [
+ ],
+ );
});
diff --git a/dashboard/src/utils/bulkRecipients.ts b/dashboard/src/utils/bulkRecipients.ts
index 3cd3573e2..9821ff618 100644
--- a/dashboard/src/utils/bulkRecipients.ts
+++ b/dashboard/src/utils/bulkRecipients.ts
@@ -30,49 +30,11 @@ export const BULK_RECIPIENTS_FILE_MAX_BYTES = 2 * 1024 * 1024;
* are phone-number formatting (`+1 (555) 010-2233` is one entry, not five), and no WhatsApp chat id
* shape contains a comma, a semicolon or a tab.
*/
-const FIELD_SEPARATORS = /[,;\t\r\n\u060C\u061B]/;
+const FIELD_SEPARATORS = /[,;\t\u060C\u061B]/;
-// A quoted CSV field can contain separators; none of its pieces is a recipient on its own.
-function* recipientFields(text: string): Generator<string> {
- let field = '';
- let quoted = false;
- let atStart = true;
- // A quote that never closes would swallow every recipient after it, so the text from it on is read again with
- // quotes as plain characters.
- let plain = false;
- let opened = 0;
- let fieldBeforeQuote = '';
- for (let i = 0; i < text.length; i++) {
- const ch = text[i];
- if (ch === '"' && !plain && (quoted || atStart)) {
- atStart = false;
- if (quoted && text[i + 1] === '"') {
- field += '"';
- i++;
- } else {
- quoted = !quoted;
- if (quoted) {
- opened = i;
- fieldBeforeQuote = field;
- }
- }
- } else if (!quoted && FIELD_SEPARATORS.test(ch)) {
- yield field;
- field = '';
- atStart = true;
- } else {
- field += ch;
- if (atStart && !/\s/.test(ch)) atStart = false;
- }
- if (quoted && i === text.length - 1) {
- plain = true;
- quoted = false;
- field = fieldBeforeQuote;
- i = opened - 1;
- }
- }
- yield field;
-}
+// A quote at the edge of a field is the CSV quoting of a cell, never part of a chat id: `"[email protected]"` is the chat id
+// [email protected]. Quotes are not tracked across fields or lines, so one that never closes cannot swallow what follows it.
+const stripEdgeQuotes = (field: string): string => field.replace(/^"/, '').replace(/"$/, '').trim();
/**
* Fewest digits a bare number may carry to be treated as a phone number, matching the bound the
@@ -92,16 +54,23 @@ const CHAT_ID = /^[0-9][0-9:-]*@(c\.us|g\.us|s\.whatsapp\.net|lid|newsletter|bro
export function parseBulkRecipients(text: string): string[] {
const seen = new Set<string>();
- // Fold before parsing, so native digits and full-width quotes/separators keep the same meaning.
- for (const rawField of recipientFields(normalizePhoneText(text))) {
- const field = rawField.trim();
- if (!field || FIELD_SEPARATORS.test(field)) continue;
- if (field.includes('@')) {
- if (CHAT_ID.test(field)) seen.add(field);
- continue;
+ // Folded before the split, so a list in the digits and the commas of another script is split as it reads.
+ for (const rawLine of normalizePhoneText(text).split(/\r\n?|\n/)) {
+ // Split into fields before anything else. The button offers `.csv`, and a row with more than one
+ // column used to have its columns' digits CONCATENATED by the strip below: `1,628123456789`
+ // became `[email protected]`, a different number that looks entirely plausible in the
+ // recipients box. Splitting first keeps each column a candidate on its own.
+ for (const rawField of rawLine.split(FIELD_SEPARATORS)) {
+ const field = rawField.trim();
+ if (!field) continue;
+ if (field.includes('@')) {
+ const id = stripEdgeQuotes(field);
+ if (CHAT_ID.test(id)) seen.add(id);
+ continue;
+ }
+ const digits = field.replace(/[^0-9]/g, '');
+ if (digits.length >= MIN_PHONE_DIGITS) seen.add(`${digits}@c.us`);
}
- const digits = field.replace(/[^0-9]/g, '');
- if (digits.length >= MIN_PHONE_DIGITS) seen.add(`${digits}@c.us`);
}
return [...seen];
}