Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): refuse a pairing phone over 15 digits

Yudhi Armyndharis8ca16db881f1
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.ts
index a919b7af1..04cd74019 100644
--- a/dashboard/src/pages/Sessions.test.ts
+++ b/dashboard/src/pages/Sessions.test.ts
@@ -757,8 +757,9 @@ test('the pairing phone field leaves a composition in progress alone and reads i
});
// 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. The digits are capped instead, at the 15 a number can have.
-test('the pairing phone field caps the digits it keeps, not the characters a paste may carry', async () => {
+// 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, within } = rtl;
resetFetchCalls();
renderSessions();
@@ -769,12 +770,34 @@ test('the pairing phone field caps the digits it keeps, not the characters a pas
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;
assert.equal(phoneInput.hasAttribute('maxlength'), false, 'a native maxLength cuts a pasted formatted number');
fireEvent.change(phoneInput, { target: { value: '+62 812-3456-7890' } });
assert.equal(phoneInput.value, '6281234567890');
- fireEvent.change(phoneInput, { target: { value: '1234567890123456789' } });
- assert.equal(phoneInput.value, '123456789012345');
+ assert.equal(generate.disabled, false);
+ assert.equal(phoneInput.getAttribute('aria-invalid'), null);
+ assert.equal(screen.queryByText(/Enter a valid phone number/) === null, true);
+
+ const tooLong = '1234567890123456789';
+ fireEvent.change(phoneInput, { target: { value: tooLong } });
+ assert.equal(phoneInput.value, tooLong, 'the paste was cut to a number nobody typed');
+ assert.equal(phoneInput.getAttribute('aria-invalid'), 'true');
+ const error = screen.getByText(/Enter a valid phone number/);
+ assert.equal(phoneInput.getAttribute('aria-describedby'), error.id);
+ assert.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',
+ );
+
+ fireEvent.change(phoneInput, { target: { value: '6281234567890' } });
+ assert.equal(phoneInput.getAttribute('aria-invalid'), null);
+ assert.equal(screen.queryByText(/Enter a valid phone number/) === null, true);
+ assert.equal(generate.disabled, false);
});
// Nothing server-side refuses a pairing code for a number linked elsewhere, so the panel has to say
diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsx
index 2a908e50e..25a9fa14b 100644
--- a/dashboard/src/pages/Sessions.tsx
+++ b/dashboard/src/pages/Sessions.tsx
@@ -68,8 +68,9 @@ function restrictionTitle(restriction: AccountRestriction, t: TFunction): string
return parts.join(' · ');
}
+// 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, '').slice(0, PAIRING_PHONE_MAX_DIGITS);
+const pairingDigits = (text: string) => normalizePhoneText(text).replace(/\D/g, '');
export function Sessions() {
const { t } = useTranslation();
@@ -649,6 +650,7 @@ export function Sessions() {
}
};
+ const pairingPhoneTooLong = pairingDigits(phoneNumber).length > PAIRING_PHONE_MAX_DIGITS;
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;
@@ -897,16 +899,23 @@ export function Sessions() {
value={phoneNumber}
// 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.
- // The cap is on the digits, not a native maxLength: that counts the separators of a pasted
- // "+62 812-3456-7890" and cuts the number short.
+ // 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}
onChange={e =>
setPhoneNumber(
(e.nativeEvent as InputEvent).isComposing ? e.target.value : pairingDigits(e.target.value),
)
}
onCompositionEnd={e => setPhoneNumber(pairingDigits(e.currentTarget.value))}
- onKeyDown={e => e.key === 'Enter' && handleGeneratePairingCode()}
+ onKeyDown={e => e.key === 'Enter' && !pairingPhoneTooLong && handleGeneratePairingCode()}
/>
+ {pairingPhoneTooLong && (
+ <p id="pairing-phone-error" className="input-error" role="alert">
+ {t('sessions.pairing.invalidPhone')}
+ </p>
+ )}
<p className="input-hint" style={{ marginBottom: '1.5rem' }}>
{t('sessions.pairing.phoneHint')}
</p>