Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): refuse a pairing phone with other text
Yudhi Armyndharis0fee9139c8f8
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.tsindex 04cd74019..7243b1539 100644--- a/dashboard/src/pages/Sessions.test.ts+++ b/dashboard/src/pages/Sessions.test.ts@@ -756,21 +756,29 @@ test('the pairing phone field leaves a composition in progress alone and reads iassert.equal(phoneInput.value, '628123456789');});-// A native maxLength counts the separators of a pasted number and cuts it before the page reads it: "+62 812-3456-7890"-// is 17 characters but 12 digits. And cutting a number of more than 15 digits would leave a valid-looking one nobody-// typed, so it is kept whole, refused in the field, and no pairing code is requested for it.-test('the pairing phone field keeps a formatted paste whole and refuses one of more than 15 digits', async () => {+async function openPairingPhoneField() {const { screen, fireEvent, within } = rtl;resetFetchCalls();renderSessions();-await screen.findByText('new-device');const qrCard = screen.getByText('new-device').closest('.session-card') as HTMLElement;fireEvent.click(within(qrCard).getByRole('button', { name: 'Show QR' }));await screen.findByAltText('QR');fireEvent.click(screen.getByRole('tab', { name: 'Link with Phone Number' }));- const phoneInput = screen.getByLabelText('Phone Number') as HTMLInputElement;- const generate = screen.getByRole('button', { name: 'Generate Pairing Code' }) as HTMLButtonElement;+ return {+ phoneInput: screen.getByLabelText('Phone Number') as HTMLInputElement,+ generate: screen.getByRole('button', { name: 'Generate Pairing Code' }) as HTMLButtonElement,+ };+}++const pairingCodeRequests = () => fetchCalls.filter(c => c.path.endsWith('/pairing-code'));++// A native maxLength counts the separators of a pasted number and cuts it before the page reads it: "+62 812-3456-7890"+// is 17 characters but 12 digits. And cutting a number of more than 15 digits would leave a valid-looking one nobody+// typed, so it is kept whole, refused in the field, and no pairing code is requested for it.+test('the pairing phone field keeps a formatted paste whole and refuses one of more than 15 digits', async () => {+ const { screen, fireEvent } = rtl;+ const { phoneInput, generate } = await openPairingPhoneField();assert.equal(phoneInput.hasAttribute('maxlength'), false, 'a native maxLength cuts a pasted formatted number');fireEvent.change(phoneInput, { target: { value: '+62 812-3456-7890' } });@@ -788,11 +796,7 @@ test('the pairing phone field keeps a formatted paste whole and refuses one of massert.equal(generate.disabled, true);fireEvent.keyDown(phoneInput, { key: 'Enter' });- assert.equal(- fetchCalls.some(c => c.path.endsWith('/pairing-code')),- false,- 'a pairing code was requested for a number of more than 15 digits',- );+ assert.equal(pairingCodeRequests().length, 0, 'a pairing code was requested for a number of more than 15 digits');fireEvent.change(phoneInput, { target: { value: '6281234567890' } });assert.equal(phoneInput.getAttribute('aria-invalid'), null);@@ -800,6 +804,48 @@ test('the pairing phone field keeps a formatted paste whole and refuses one of massert.equal(generate.disabled, false);});+// Joining the digit groups of text around a number makes a valid-looking one nobody typed: "Order 12 call +62 ..." and+// "... ext. 12" both hold 15 digits. Only digits and the usual formatting are read; anything else is kept and refused.+test('the pairing phone field accepts formatted numbers whole and refuses text with other characters', async () => {+ const { screen, fireEvent } = rtl;+ const { phoneInput, generate } = await openPairingPhoneField();++ const accepted: Array<[string, string]> = [+ ['+62 812-3456-7890', '6281234567890'],+ ['(021) 555 0100', '0215550100'],+ ['+62\u00a0812.3456.7890', '6281234567890'],+ [' +1 (555) 123-4567 ', '15551234567'],+ ];+ for (const [pasted, digits] of accepted) {+ fireEvent.change(phoneInput, { target: { value: pasted } });+ assert.equal(phoneInput.value, digits, `${pasted} was not read whole`);+ assert.equal(phoneInput.getAttribute('aria-invalid'), null, `${pasted} was refused`);+ assert.equal(generate.disabled, false, `${pasted} left Generate disabled`);+ }++ const refused = [+ 'Order 12 call +62 812-3456-7890',+ '+62 812-3456-7890 ext. 12',+ '62812abc3456',+ '+62 812 +3456 7890',+ ];+ for (const pasted of refused) {+ fireEvent.change(phoneInput, { target: { value: pasted } });+ assert.equal(phoneInput.value, pasted, `${pasted} was rewritten to a number nobody typed`);+ assert.equal(phoneInput.getAttribute('aria-invalid'), 'true', `${pasted} was not marked invalid`);+ assert.equal(screen.getAllByText(/Enter a valid phone number/).length, 1, `${pasted} showed no single error`);+ assert.equal(generate.disabled, true, `${pasted} left Generate enabled`);+ fireEvent.keyDown(phoneInput, { key: 'Enter' });+ assert.equal(pairingCodeRequests().length, 0, `a pairing code was requested for ${pasted}`);+ }++ // Cleaning the text up makes it a number again.+ fireEvent.change(phoneInput, { target: { value: '+62 812-3456-7890' } });+ assert.equal(phoneInput.value, '6281234567890');+ assert.equal(phoneInput.getAttribute('aria-invalid'), null);+ assert.equal(generate.disabled, false);+});+// Nothing server-side refuses a pairing code for a number linked elsewhere, so the panel has to say// what it can cost before the operator types one.test('the phone pairing tab warns that a code can unlink an existing session', async () => {diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsxindex 25a9fa14b..1743e1a9a 100644--- a/dashboard/src/pages/Sessions.tsx+++ b/dashboard/src/pages/Sessions.tsx@@ -70,7 +70,19 @@ function restrictionTitle(restriction: AccountRestriction, t: TFunction): string// The most digits a phone number has. A longer paste is kept whole and refused, never cut to a number nobody typed.const PAIRING_PHONE_MAX_DIGITS = 15;-const pairingDigits = (text: string) => normalizePhoneText(text).replace(/\D/g, '');+// What a number may be written with besides its digits: a leading plus, spaces (a no-break space included), hyphens,+// dots and parentheses.+const PHONE_FORMATTING = /^\+?[\d\s().-]*$/;+// A number written that way is read to its digits. Any other text (a letter, an extension, a sentence around the+// number) is kept as pasted and refused, since joining its digit groups would make a number nobody typed.+const readPairingPhone = (text: string) => {+ const normalized = normalizePhoneText(text);+ return PHONE_FORMATTING.test(normalized.trim()) ? normalized.replace(/\D/g, '') : normalized;+};+const pairingPhoneRefused = (text: string) => {+ const normalized = normalizePhoneText(text);+ return !PHONE_FORMATTING.test(normalized.trim()) || normalized.replace(/\D/g, '').length > PAIRING_PHONE_MAX_DIGITS;+};export function Sessions() {const { t } = useTranslation();@@ -650,7 +662,7 @@ export function Sessions() {}};- const pairingPhoneTooLong = pairingDigits(phoneNumber).length > PAIRING_PHONE_MAX_DIGITS;+ const pairingPhoneInvalid = pairingPhoneRefused(phoneNumber);const filteredSessions = filterSessions(sessions, searchQuery, statusFilter);// A failed read says nothing about how many sessions there are: with none held, the banner is the only message.const listUnknown = listFailed && sessions.length === 0;@@ -900,18 +912,18 @@ export function Sessions() {// A composition in progress is left as typed, since rewriting it garbles what the IME commits;// it is read when the composition ends. Pasted and directly typed text is read at once.// No native maxLength: it counts the separators of a pasted "+62 812-3456-7890" and cuts it- // short. A paste of more than 15 digits is kept and refused below, with no request.- aria-invalid={pairingPhoneTooLong || undefined}- aria-describedby={pairingPhoneTooLong ? 'pairing-phone-error' : undefined}+ // short. A paste of more than 15 digits, or with other characters, is kept and refused below.+ aria-invalid={pairingPhoneInvalid || undefined}+ aria-describedby={pairingPhoneInvalid ? 'pairing-phone-error' : undefined}onChange={e =>setPhoneNumber(- (e.nativeEvent as InputEvent).isComposing ? e.target.value : pairingDigits(e.target.value),+ (e.nativeEvent as InputEvent).isComposing ? e.target.value : readPairingPhone(e.target.value),)}- onCompositionEnd={e => setPhoneNumber(pairingDigits(e.currentTarget.value))}- onKeyDown={e => e.key === 'Enter' && !pairingPhoneTooLong && handleGeneratePairingCode()}+ onCompositionEnd={e => setPhoneNumber(readPairingPhone(e.currentTarget.value))}+ onKeyDown={e => e.key === 'Enter' && !pairingPhoneInvalid && handleGeneratePairingCode()}/>- {pairingPhoneTooLong && (+ {pairingPhoneInvalid && (<p id="pairing-phone-error" className="input-error" role="alert">{t('sessions.pairing.invalidPhone')}</p>