Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): read native-script digits in typed phone numbers

Yudhi Armyndharis9b87fe21f649
diff --git a/dashboard/src/components/FilterBuilder.test.ts b/dashboard/src/components/FilterBuilder.test.ts
index 9fbbb5b0e..0bf61af1e 100644
--- a/dashboard/src/components/FilterBuilder.test.ts
+++ b/dashboard/src/components/FilterBuilder.test.ts
@@ -100,3 +100,26 @@ test('removing a row does not hand its unsent chip text to the row below', () =>
assert.deepEqual(latest, { conditions: [{ field: 'chatId', operator: 'is', value: ['[email protected]', '[email protected]'] }] });
assert.equal(container.querySelector('.chips-text'), left);
});
+
+// A contact typed as a number is turned into its chat id from the digits; those may be native-script or full-width.
+for (const [script, zero] of [
+ ['Arabic-Indic', 0x0660],
+ ['full-width', 0xff10],
+] as const) {
+ test(`a contact typed in ${script} digits is added as an ASCII chat id`, () => {
+ const changes: (WebhookFilters | null)[] = [];
+ const { container } = rtl.render(
+ createElement(FilterBuilder, {
+ filters: { conditions: [{ field: 'sender', operator: 'is', value: [] }] },
+ onChange: next => changes.push(next),
+ chats: [],
+ }),
+ );
+ const input = container.querySelector<HTMLInputElement>('.chips-text');
+ assert.ok(input, 'expected the contact input');
+ const typed = [...'628123456789'].map(d => String.fromCharCode(zero + Number(d))).join('');
+ rtl.fireEvent.change(input, { target: { value: typed } });
+ rtl.fireEvent.keyDown(input, { key: 'Enter' });
+ assert.deepEqual(changes.at(-1)?.conditions?.[0].value, ['[email protected]']);
+ });
+}
diff --git a/dashboard/src/components/FilterBuilder.tsx b/dashboard/src/components/FilterBuilder.tsx
index 88bcb3735..4a3771545 100644
--- a/dashboard/src/components/FilterBuilder.tsx
+++ b/dashboard/src/components/FilterBuilder.tsx
@@ -10,6 +10,7 @@ import {
type WebhookFilterOperator,
} from '../services/api';
import { filterValueLabel } from '../utils/enumLabels';
+import { normalizePhoneText } from '../utils/phone';
import { MAX_FILTER_CONDITIONS, MAX_FILTER_TEXT_LENGTH } from '../utils/webhookFilters';
import './FilterBuilder.css';
@@ -60,7 +61,7 @@ function normalizeToJid(raw: string): string | null {
const value = raw.trim();
if (!value) return null;
if (value.includes('@')) return value.toLowerCase();
- const digits = value.replace(/[^0-9]/g, '');
+ const digits = normalizePhoneText(value).replace(/[^0-9]/g, '');
return digits ? `${digits}@c.us` : null;
}
diff --git a/dashboard/src/pages/MessageTester.test.ts b/dashboard/src/pages/MessageTester.test.ts
index ea21d80ca..da6e37ace 100644
--- a/dashboard/src/pages/MessageTester.test.ts
+++ b/dashboard/src/pages/MessageTester.test.ts
@@ -1031,6 +1031,48 @@ for (const mode of ['text', 'forward'] as const) {
}
}
+// The recipient and a forward target are resolved through the number check, which takes ASCII digits only.
+for (const [script, zero] of [
+ ['Arabic-Indic', 0x0660],
+ ['full-width', 0xff10],
+] as const) {
+ test(`a recipient and a forward target typed in ${script} digits are looked up as ASCII digits`, async () => {
+ const checked: string[] = [];
+ globalThis.fetch = (async (input: RequestInfo | URL) => {
+ const path = String(input);
+ if (path.endsWith('/sessions')) return jsonResponse([{ id: 's1', name: 'Main', status: 'ready' }]);
+ if (path.includes('/contacts/check/')) {
+ const number = path.split('/contacts/check/')[1];
+ checked.push(number);
+ return jsonResponse({ number, exists: true, whatsappId: `${number}@c.us` });
+ }
+ if (path.includes('/messages/')) return jsonResponse({ messageId: 'sent', timestamp: 1 });
+ return jsonResponse([]);
+ }) as typeof fetch;
+ sessionStorage.setItem('openwa_api_key', 'original-key');
+ sessionStorage.setItem('openwa_user_role', 'operator');
+ const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 1 } } });
+ const view = rtl.render(
+ createElement(QueryClientProvider, { client }, createElement(RoleProvider, null, createElement(MessageTester))),
+ );
+ try {
+ await rtl.screen.findByRole('option', { name: /Main/ });
+ const native = (ascii: string) => [...ascii].map(d => String.fromCharCode(zero + Number(d))).join('');
+ type(view.container, '#mt-13', native('15550000001'));
+ rtl.fireEvent.click(rtl.screen.getByRole('button', { name: 'Forward' }));
+ type(view.container, '#mt-9', native('15550000002'));
+ type(view.container, '#mt-10', 'original-message');
+ await rtl.waitFor(() => assert.equal(sendButton().disabled, false));
+ rtl.fireEvent.click(sendButton());
+ await rtl.waitFor(() => assert.equal(checked.length, 2));
+ assert.deepEqual(checked, ['15550000001', '15550000002']);
+ } finally {
+ client.clear();
+ sessionStorage.removeItem('openwa_api_key');
+ }
+ });
+}
+
test('an unchanged actor sends to canonical personal and forward destinations', async () => {
const writes: { path: string; key: string | null; body: unknown }[] = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
diff --git a/dashboard/src/pages/MessageTester.tsx b/dashboard/src/pages/MessageTester.tsx
index d421946bd..58c3d8c7e 100644
--- a/dashboard/src/pages/MessageTester.tsx
+++ b/dashboard/src/pages/MessageTester.tsx
@@ -30,6 +30,7 @@ import {
import { PageHeader } from '../components/PageHeader';
import { GroupPicker } from '../components/GroupPicker';
import { groupLabel, isGatewayRefusal, planGroupSend } from '../utils/groupSelection';
+import { normalizePhoneText } from '../utils/phone';
import { sendSequentially } from '../utils/sendSequentially';
import { keepsSameKey } from '../utils/authLifecycle';
import './MessageTester.css';
@@ -507,7 +508,7 @@ export function MessageTester() {
// Bulk carries its own recipient list, so the shared selector's target is not resolved there.
let chatId = targetId;
if (messageType !== 'bulk' && recipientType !== 'group') {
- const resolved = await contactApi.checkNumber(session, targetId.replace(/[^0-9]/g, ''));
+ const resolved = await contactApi.checkNumber(session, normalizePhoneText(targetId).replace(/[^0-9]/g, ''));
assertCurrent();
if (!resolved.exists || !resolved.whatsappId) {
setResponse({
@@ -589,7 +590,10 @@ export function MessageTester() {
// through the same check-number flow as the main recipient.
let toChatId = forwardTo.trim();
if (!toChatId.includes('@')) {
- const resolvedTo = await contactApi.checkNumber(session, toChatId.replace(/[^0-9]/g, ''));
+ const resolvedTo = await contactApi.checkNumber(
+ session,
+ normalizePhoneText(toChatId).replace(/[^0-9]/g, ''),
+ );
assertCurrent();
if (!resolvedTo.exists || !resolvedTo.whatsappId) throw new Error(t('messageTester.notOnWhatsApp'));
toChatId = resolvedTo.whatsappId;
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.ts
index 128968795..8f75e4286 100644
--- a/dashboard/src/pages/Sessions.test.ts
+++ b/dashboard/src/pages/Sessions.test.ts
@@ -635,6 +635,26 @@ test('a typed pairing phone number survives toggling to the QR tab and back', as
);
});
+// The pairing field keeps digits only, so a number typed in another script has to be read, not dropped.
+test('a pairing phone number typed in native-script or full-width digits is kept as ASCII digits', async () => {
+ 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;
+
+ for (const zero of [0x0660, 0xff10]) {
+ const typed = [...'919876543210'].map(d => String.fromCharCode(zero + Number(d))).join('');
+ fireEvent.change(phoneInput, { target: { value: typed } });
+ assert.equal(phoneInput.value, '919876543210');
+ }
+});
+
// 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.tsx
index 2187b4318..d39c2a557 100644
--- a/dashboard/src/pages/Sessions.tsx
+++ b/dashboard/src/pages/Sessions.tsx
@@ -53,6 +53,7 @@ import { CustomSelect } from '../components/CustomSelect';
import { Modal } from '../components/Modal';
import './Sessions.css';
import { formatLastActive, formatSessionStatus } from '../utils/sessionDisplay';
+import { normalizePhoneText } from '../utils/phone';
/**
* The hover title for a restriction: the engine's own cause token, plus when enforcement ends if
@@ -884,7 +885,7 @@ export function Sessions() {
maxLength={15}
placeholder={t('sessions.pairing.phonePlaceholder')}
value={phoneNumber}
- onChange={e => setPhoneNumber(e.target.value.replace(/\D/g, ''))}
+ onChange={e => setPhoneNumber(normalizePhoneText(e.target.value).replace(/\D/g, ''))}
onKeyDown={e => e.key === 'Enter' && handleGeneratePairingCode()}
/>
<p className="input-hint" style={{ marginBottom: '1.5rem' }}>
diff --git a/dashboard/src/utils/bulkRecipients.test.ts b/dashboard/src/utils/bulkRecipients.test.ts
index 457141c41..2f01870ae 100644
--- a/dashboard/src/utils/bulkRecipients.test.ts
+++ b/dashboard/src/utils/bulkRecipients.test.ts
@@ -75,3 +75,14 @@ test('an @ field without a WhatsApp chat id shape is dropped, so an email column
['[email protected]', '120363123456789@newsletter', '628123456789:12@lid'],
);
});
+
+// A list pasted from an Arabic or a CJK source carries the digits of that script.
+test('a number written in native-script or full-width digits is read as ASCII digits', () => {
+ const toDigits = (ascii: string, zero: number) => [...ascii].map(d => String.fromCharCode(zero + Number(d))).join('');
+ assert.deepEqual(parseBulkRecipients(toDigits('628123456789', 0x0660)), ['[email protected]']);
+ assert.deepEqual(parseBulkRecipients(toDigits('628123456789', 0xff10)), ['[email protected]']);
+ assert.deepEqual(parseBulkRecipients(`${toDigits('628111222333', 0x0966)}\n628222333444`), [
+ ]);
+});
diff --git a/dashboard/src/utils/bulkRecipients.ts b/dashboard/src/utils/bulkRecipients.ts
index 562a809f6..963f947a8 100644
--- a/dashboard/src/utils/bulkRecipients.ts
+++ b/dashboard/src/utils/bulkRecipients.ts
@@ -1,3 +1,5 @@
+import { normalizePhoneText } from './phone.ts';
+
// The backend caps a bulk batch at 100 messages (ArrayMaxSize on SendBulkMessageDto).
export const BULK_MAX_RECIPIENTS = 100;
@@ -59,7 +61,7 @@ export function parseBulkRecipients(text: string): string[] {
if (CHAT_ID.test(field)) seen.add(field);
continue;
}
- const digits = field.replace(/[^0-9]/g, '');
+ const digits = normalizePhoneText(field).replace(/[^0-9]/g, '');
if (digits.length >= MIN_PHONE_DIGITS) seen.add(`${digits}@c.us`);
}
}