Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): read native digits in the key chat access list

Yudhi Armyndharis070a9f3fe897
diff --git a/dashboard/src/pages/ApiKeys.test.ts b/dashboard/src/pages/ApiKeys.test.ts
index f0a90baa0..cd0df32de 100644
--- a/dashboard/src/pages/ApiKeys.test.ts
+++ b/dashboard/src/pages/ApiKeys.test.ts
@@ -184,6 +184,48 @@ test('a create sends the IP allow-list, chats and expiry only when filled in', a
});
});
+// The gateway takes ASCII digits only; a number typed with an Arabic or Persian keyboard is sent as the same number.
+test('a create folds native digits in the chat list and sends the ASCII chat id', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ const create = await openCreate();
+ fireEvent.change(screen.getByLabelText('Name'), { target: { value: 'crm-bot' } });
+ fireEvent.change(screen.getByLabelText('Chat access (optional)'), {
+ target: {
+ value: '\u0666\u0662\u0668\u0661\u0662\u0663\u0664\n\u06f1\u06f2\u06f0\u06f3\u06f6\u06f3\u06f0\u06f0\[email protected]',
+ },
+ });
+ assert.equal(screen.queryByText(/Not a chat ID or phone number/) === null, true);
+ assert.equal(create.disabled, false);
+ fireEvent.click(create);
+ await waitFor(() => assert.ok(createBody));
+ assert.deepEqual(createBody, {
+ name: 'crm-bot',
+ role: 'operator',
+ allowedSessions: [],
+ allowedChats: ['[email protected]', '[email protected]'],
+ });
+});
+
+test('a native digit entry that is no chat id is still refused and names the folded value', async () => {
+ const { screen, fireEvent } = rtl;
+ const create = await openCreate();
+ fireEvent.change(screen.getByLabelText('Name'), { target: { value: 'crm-bot' } });
+ fireEvent.change(screen.getByLabelText('Chat access (optional)'), { target: { value: '\u0661\u0662\u0663\u0664' } });
+ assert.equal(create.disabled, true);
+ screen.getByText('Not a chat ID or phone number: 1234');
+});
+
+test('an edit in native digits for the chat already stored sends nothing', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ const save = await openEdit({ ...billingBot, allowedChats: ['[email protected]'] });
+ fireEvent.change(screen.getByLabelText('Chat access (optional)'), {
+ target: { value: '\u0666\u0662\u0668\u0661\u0662\u0663\u0664' },
+ });
+ fireEvent.click(save);
+ await waitFor(() => assert.equal(screen.queryByRole('button', { name: 'Save' }) === null, true));
+ assert.deepEqual(updateBodies, []);
+});
+
// The key exists once the request lands; a modal closed meanwhile would leave its one-time secret
// unseen and pop it up on the next Create API Key click.
test('the create modal stays open until the request settles and then shows the secret', async () => {
diff --git a/dashboard/src/utils/sessionScope.test.ts b/dashboard/src/utils/sessionScope.test.ts
index ceda53ba5..ee09e25e3 100644
--- a/dashboard/src/utils/sessionScope.test.ts
+++ b/dashboard/src/utils/sessionScope.test.ts
@@ -90,6 +90,52 @@ test('a chat list qualifies a bare phone number the way the gateway stores it',
assert.deepEqual(normalizeChatScope('1234'), ['1234']);
});
+// The gateway takes ASCII digits only, so what an operator types in the digits of their own script is folded first.
+const inScript = (text: string, zero: number) =>
+ text.replace(/\d/g, digit => String.fromCodePoint(zero + Number(digit)));
+const ZEROS = { arabicIndic: 0x0660, persian: 0x06f0, devanagari: 0x0966, telugu: 0x0c66, fullWidth: 0xff10 };
+
+test('a chat list folds the digits of any script before it qualifies a number', () => {
+ for (const [script, zero] of Object.entries(ZEROS)) {
+ const number = inScript('6281234', zero);
+ assert.deepEqual(normalizeChatScope(number), ['[email protected]'], script);
+ assert.deepEqual(normalizeChatScope(` ${number}@c.us \n${number}\n6281234`), ['[email protected]'], script);
+ assert.equal(invalidChatEntry(normalizeChatScope(number)), null, script);
+ }
+});
+
+test('a chat list folds native digits inside a group, a contact, a lid and a device id', () => {
+ const ids = ['[email protected]', '[email protected]', '123456@lid', '6281234:[email protected]'];
+ assert.deepEqual(normalizeChatScope(ids.map(id => inScript(id, ZEROS.persian)).join('\n')), ids);
+});
+
+test('a chat list reads a number of mixed scripts and drops direction marks around it', () => {
+ assert.deepEqual(normalizeChatScope(`62${inScript('81234', ZEROS.arabicIndic)}`), ['[email protected]']);
+ assert.deepEqual(normalizeChatScope(`\u200e${inScript('6281234', ZEROS.arabicIndic)}\u200f`), ['[email protected]']);
+});
+
+test('an entry that is not a chat id after folding is still refused', () => {
+ const short = normalizeChatScope(inScript('1234', ZEROS.arabicIndic));
+ assert.deepEqual(short, ['1234']);
+ assert.equal(invalidChatEntry(short), '1234');
+ const letters = normalizeChatScope(`${inScript('1234567', ZEROS.devanagari)}[email protected]`);
+ assert.equal(invalidChatEntry(letters), '[email protected]');
+ // A circled digit is not a decimal digit: left as typed for the gateway to refuse.
+ const circled = normalizeChatScope(`\u24606281234`);
+ assert.equal(invalidChatEntry(circled), circled[0]);
+ // A plus is refused for ASCII input and stays refused.
+ assert.equal(invalidChatEntry(normalizeChatScope('+6281234')), '+6281234');
+});
+
+test('a draft in native digits patches exactly as the same draft in ASCII digits', () => {
+ const key = { role: 'operator', allowedChats: ['[email protected]'] };
+ const draft = apiKeyDraft(key);
+ const native = (chats: string) => apiKeyPatch(key, { ...draft, chats: inScript(chats, ZEROS.persian) });
+ assert.deepEqual(native('6281234'), {});
+ assert.deepEqual(native('6281234\n62812349'), apiKeyPatch(key, { ...draft, chats: '6281234\n62812349' }));
+ assert.deepEqual(native('6281234\n62812349'), { allowedChats: ['[email protected]', '[email protected]'] });
+});
+
test('an expiry round-trips through the datetime-local value in local time', () => {
const local = toDateTimeLocal('2027-03-04T05:06:59.000Z');
assert.match(local, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/);
diff --git a/dashboard/src/utils/sessionScope.ts b/dashboard/src/utils/sessionScope.ts
index d7ccef097..7b28f929e 100644
--- a/dashboard/src/utils/sessionScope.ts
+++ b/dashboard/src/utils/sessionScope.ts
@@ -1,3 +1,5 @@
+import { normalizePhoneText } from './phone.ts';
+
/**
* Operator and viewer (reader) API keys may be limited to an explicit session
* allowlist. An empty or missing list means the key can reach every session,
@@ -64,10 +66,15 @@ export function parseScopeList(text: string): string[] {
/**
* The chat allow-list in the form the gateway stores it (normalizeChatAllowList): a bare phone number
* becomes `<digits>@c.us`. The gateway refuses a list with repeats before it normalizes, and compares
- * stored entries in this form, so both the request and the no-op check use it.
+ * stored entries in this form, so both the request and the no-op check use it. The gateway takes ASCII
+ * digits only, so a number typed in the digits of another script is folded first, as elsewhere in the dashboard.
*/
export function normalizeChatScope(text: string): string[] {
- return [...new Set(parseScopeList(text).map(entry => (/^\d{5,}$/.test(entry) ? `${entry}@c.us` : entry)))];
+ return [
+ ...new Set(
+ parseScopeList(normalizePhoneText(text)).map(entry => (/^\d{5,}$/.test(entry) ? `${entry}@c.us` : entry)),
+ ),
+ ];
}
/** The most chats a key may list: the gateway answers a longer list with 400 (ALLOWED_CHATS_MAX). */