Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): read quoted CSV recipients as one field
Yudhi Armyndharis10bacb16a8c0
diff --git a/dashboard/src/utils/bulkRecipients.test.ts b/dashboard/src/utils/bulkRecipients.test.tsindex f03ee2db3..df7fe3608 100644--- a/dashboard/src/utils/bulkRecipients.test.ts+++ b/dashboard/src/utils/bulkRecipients.test.ts@@ -111,3 +111,53 @@ test('a chat id written in full-width characters passes through as the plain chaassert.deepEqual(parseBulkRecipients(fullwidth('[email protected]')), ['[email protected]']);assert.deepEqual(parseBulkRecipients(fullwidth('[email protected]')), []);});++test('CSV quoting preserves full chat IDs and existing recipient syntax', () => {+ assert.deepEqual(parseBulkRecipients('"[email protected]","[email protected]"\r\n"[email protected]"'), [+ '[email protected]',+ '[email protected]',+ '[email protected]',+ ]);+ assert.deepEqual(+ parseBulkRecipients(+ '"\u0666\u0662\u0668\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\[email protected]";"[email protected]"',+ ),+ ['[email protected]', '[email protected]'],+ );+ assert.deepEqual(parseBulkRecipients('"Alice ""A""",[email protected]'), ['[email protected]']);+ assert.deepEqual(parseBulkRecipients('"+1 (555) 010-2233"\[email protected]'), [+ '[email protected]',+ '[email protected]',+ ]);+});++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 separator inside a quoted CSV field never concatenates two phone numbers', () => {+ assert.deepEqual(parseBulkRecipients('"628123456789,15550102233",[email protected]'), ['[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', () => {+ assert.deepEqual(parseBulkRecipients('"628111111111\n628222222222\n628333333333'), [+ '[email protected]',+ '[email protected]',+ '[email protected]',+ ]);+ assert.deepEqual(parseBulkRecipients('628111111111\n "628222222222\n628333333333'), [+ '[email protected]',+ '[email protected]',+ '[email protected]',+ ]);+ assert.deepEqual(parseBulkRecipients('"[email protected]\[email protected]'), ['[email protected]']);+});diff --git a/dashboard/src/utils/bulkRecipients.ts b/dashboard/src/utils/bulkRecipients.tsindex b26f5eb98..3cd3573e2 100644--- a/dashboard/src/utils/bulkRecipients.ts+++ b/dashboard/src/utils/bulkRecipients.ts@@ -30,7 +30,49 @@ 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\u060C\u061B]/;+const FIELD_SEPARATORS = /[,;\t\r\n\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;+}/*** Fewest digits a bare number may carry to be treated as a phone number, matching the bound the@@ -50,22 +92,16 @@ const CHAT_ID = /^[0-9][0-9:-]*@(c\.us|g\.us|s\.whatsapp\.net|lid|newsletter|broexport function parseBulkRecipients(text: string): string[] {const seen = new Set<string>();- // 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('@')) {- 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`);+ // 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;}+ const digits = field.replace(/[^0-9]/g, '');+ if (digits.length >= MIN_PHONE_DIGITS) seen.add(`${digits}@c.us`);}return [...seen];}