Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): count API key name length as the gateway does

Yudhi Armyndharisd57d48a41733
diff --git a/dashboard/src/pages/ApiKeys.test.ts b/dashboard/src/pages/ApiKeys.test.ts
index 9c790bfeb..71ffa6b06 100644
--- a/dashboard/src/pages/ApiKeys.test.ts
+++ b/dashboard/src/pages/ApiKeys.test.ts
@@ -202,6 +202,19 @@ test('the create modal stays open until the request settles and then shows the s
await screen.findByText('owa_k1_new');
});
+// The gateway counts an emoji, with or without its presentation selector, as one character.
+test('a name of two emoji keeps Create disabled', async () => {
+ const { screen, fireEvent } = rtl;
+ const create = await openCreate();
+ for (const name of ['\u{1F511}\u{1F511}', '\u2714\uFE0F\u2714\uFE0F']) {
+ fireEvent.change(screen.getByLabelText('Name'), { target: { value: name } });
+ assert.equal(create.disabled, true, `Create is enabled for ${name}`);
+ screen.getByText('Use at least 3 characters.');
+ }
+ fireEvent.change(screen.getByLabelText('Name'), { target: { value: '\u{1F511}\u{1F511}\u{1F511}' } });
+ assert.equal(create.disabled, false);
+});
+
// A production gateway refuses a name over 100 characters with a bare "Bad Request".
test('the key name cannot exceed the 100 characters the gateway takes', async () => {
await openCreate();
diff --git a/dashboard/src/pages/ApiKeys.tsx b/dashboard/src/pages/ApiKeys.tsx
index 57413aad7..53d6b5a85 100644
--- a/dashboard/src/pages/ApiKeys.tsx
+++ b/dashboard/src/pages/ApiKeys.tsx
@@ -36,6 +36,7 @@ import { Modal } from '../components/Modal';
import { SessionScopePicker } from '../components/SessionScopePicker';
import { useToast } from '../hooks/useToast';
import { copyToClipboard } from '../utils/clipboard';
+import { captionLength } from '../utils/bulkMedia';
import {
apiKeyDraft,
apiKeyPatch,
@@ -312,9 +313,10 @@ export function ApiKeys() {
const newErrors = limitErrors(newKey.role, newKey.ips, newKey.chats);
const editErrors =
editDraft && editingKey ? limitErrors(editDraft.role, editDraft.ips, editDraft.chats, editingKey) : null;
- // The gateway requires a name of at least 3 characters; the input stops at its 100-character limit.
- const canCreate =
- !createMutation.isPending && newKey.name.trim().length >= 3 && newErrors.ip === null && newErrors.chat === null;
+ // The gateway requires a name of at least 3 characters, counted the way its validator counts them
+ // (an emoji is one); the input stops at its 100-character limit.
+ const nameTooShort = captionLength(newKey.name.trim()) < 3;
+ const canCreate = !createMutation.isPending && !nameTooShort && newErrors.ip === null && newErrors.chat === null;
const canSave = !updateMutation.isPending && editErrors?.ip === null && editErrors.chat === null;
const handleRevoke = async (id: string) => {
@@ -572,7 +574,7 @@ export function ApiKeys() {
value={newKey.name}
onChange={e => setNewKey({ ...newKey, name: e.target.value })}
/>
- {newKey.name.length > 0 && newKey.name.trim().length < 3 && (
+ {newKey.name.length > 0 && nameTooShort && (
<span className="key-field-hint">{t('apiKeys.nameTooShort')}</span>
)}
<label htmlFor="ak-2">{t('common.role')}</label>