Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): check the instance secret like the API does

Yudhi Armyndharis09ba42088233
diff --git a/dashboard/src/components/PluginInstances.test.ts b/dashboard/src/components/PluginInstances.test.ts
index 67ea00a50..9f63675d5 100644
--- a/dashboard/src/components/PluginInstances.test.ts
+++ b/dashboard/src/components/PluginInstances.test.ts
@@ -113,4 +113,54 @@ test('the created view shows an auto-generated verify token with a copy button',
const token = within(dialog).getByText('auto-verify-token-4567');
assert.ok(token.closest('.pi-secret')?.querySelector('button'), 'no copy button next to the verify token');
within(dialog).getByText('Verify token');
+ const create = calls.find(c => c.method === 'POST' && c.path === BASE);
+ assert.ok(create, 'expected an instance create request');
+ assert.equal((create.body as { secret?: string }).secret, undefined);
+});
+
+test('invalid instance secrets show the matching localized length guidance', async () => {
+ const { default: i18n, supportedLanguages } = await import('../i18n/index.ts');
+ const { screen, fireEvent } = rtl;
+ const errors = [
+ ['ASCII over maximum', 'x'.repeat(513), 'common.fieldTooLong', { max: 512, count: 513 }],
+ ['padded astral over maximum', ` ${'\u{1F600}'.repeat(513)} `, 'common.fieldTooLong', { max: 512, count: 513 }],
+ ['presentation over maximum', '\u2764\uFE0F'.repeat(513), 'common.fieldTooLong', { max: 512, count: 513 }],
+ ['ASCII below minimum', 'x'.repeat(15), 'plugins.instances.errors.invalidSecret', undefined],
+ ['astral below minimum', '\u{1F600}'.repeat(8), 'plugins.instances.errors.invalidSecret', undefined],
+ ] as const;
+
+ try {
+ for (const language of supportedLanguages) {
+ await i18n.changeLanguage(language);
+ renderInstances();
+ fireEvent.click(await screen.findByRole('button', { name: i18n.t('plugins.instances.create') }));
+ fireEvent.change(screen.getByLabelText(i18n.t('plugins.instances.form.instanceId')), {
+ target: { value: 'acct2' },
+ });
+ const secret = screen.getByLabelText(i18n.t('plugins.instances.form.secret'));
+ for (const [label, value, key, options] of errors) {
+ fireEvent.change(secret, { target: { value } });
+ fireEvent.click(screen.getByRole('button', { name: i18n.t('common.create') }));
+ const expected = i18n.t(key, options);
+ assert.equal(screen.getByText(expected).textContent, expected, `${language}: ${label}`);
+ if (key === 'common.fieldTooLong') {
+ assert.match(expected, /512/, language);
+ assert.match(expected, /513/, language);
+ assert.doesNotMatch(expected, /\{\{|\}\}/, language);
+ }
+ assert.equal(
+ calls.some(c => c.method === 'POST' && c.path === BASE),
+ false,
+ `${language}: ${label}`,
+ );
+ }
+ rtl.cleanup();
+ queryClient?.clear();
+ calls.length = 0;
+ }
+ } finally {
+ rtl.cleanup();
+ queryClient?.clear();
+ await i18n.changeLanguage('en');
+ }
});
diff --git a/dashboard/src/components/PluginInstances.tsx b/dashboard/src/components/PluginInstances.tsx
index 2e33f0fea..1b060cdbe 100644
--- a/dashboard/src/components/PluginInstances.tsx
+++ b/dashboard/src/components/PluginInstances.tsx
@@ -10,6 +10,7 @@ import {
useDeleteInstanceMutation,
} from '../hooks/queries';
import { isValidInstanceId, isValidInstanceSecret, parseEditScope, parseInstanceConfig } from '../utils/instanceForm';
+import { captionLength } from '../utils/bulkMedia';
import { copyToClipboard } from '../utils/clipboard';
import { Modal } from './Modal';
import { useToast } from '../hooks/useToast';
@@ -61,7 +62,10 @@ export function PluginInstances({ pluginId }: { pluginId: string }) {
return;
}
if (!isValidInstanceSecret(form.secret)) {
- setFormError(t('plugins.instances.errors.invalidSecret'));
+ const count = captionLength(form.secret.trim());
+ setFormError(
+ count > 512 ? t('common.fieldTooLong', { max: 512, count }) : t('plugins.instances.errors.invalidSecret'),
+ );
return;
}
try {
diff --git a/dashboard/src/utils/instanceForm.test.ts b/dashboard/src/utils/instanceForm.test.ts
index 49eea91f9..481eb8be0 100644
--- a/dashboard/src/utils/instanceForm.test.ts
+++ b/dashboard/src/utils/instanceForm.test.ts
@@ -36,3 +36,26 @@ test('parseEditScope: a blank field resets a bound scope to all sessions', () =>
assert.equal(parseEditScope('sess-a', ''), null);
assert.equal(parseEditScope('*', ''), null);
});
+
+for (const [label, secret, valid] of [
+ ['blank', '', true],
+ ['whitespace', ' ', true],
+ ['ASCII below minimum', 'x'.repeat(15), false],
+ ['ASCII minimum', 'x'.repeat(16), true],
+ ['ASCII maximum', 'x'.repeat(512), true],
+ ['ASCII over maximum', 'x'.repeat(513), false],
+ ['eight astral characters', '\u{1F600}'.repeat(8), false],
+ ['astral below minimum', '\u{1F600}'.repeat(15), false],
+ ['astral minimum', '\u{1F600}'.repeat(16), true],
+ ['astral maximum', '\u{1F600}'.repeat(512), true],
+ ['astral over maximum', '\u{1F600}'.repeat(513), false],
+ ['presentation below minimum', '\u2764\uFE0F'.repeat(15), false],
+ ['presentation minimum', '\u2764\uFE0F'.repeat(16), true],
+ ['presentation maximum', '\u2764\uFE0F'.repeat(512), true],
+ ['presentation over maximum', '\u2764\uFE0F'.repeat(513), false],
+ ['padded valid astral secret', ` ${'\u{1F600}'.repeat(16)} `, true],
+] as const) {
+ test(`instance secret validation: ${label}`, () => {
+ assert.equal(isValidInstanceSecret(secret), valid);
+ });
+}
diff --git a/dashboard/src/utils/instanceForm.ts b/dashboard/src/utils/instanceForm.ts
index a8299e91c..35628cdfd 100644
--- a/dashboard/src/utils/instanceForm.ts
+++ b/dashboard/src/utils/instanceForm.ts
@@ -1,3 +1,5 @@
+import { captionLength } from './bulkMedia.ts';
+
export const INSTANCE_ID_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
export function isValidInstanceId(id: string): boolean {
@@ -6,9 +8,11 @@ export function isValidInstanceId(id: string): boolean {
type ParseResult = { ok: true; value: Record<string, unknown> | undefined } | { ok: false };
-/** Blank → auto-generate (server-side). Otherwise must be a real secret (>= 16 chars), mirroring the server DTO. */
+/** Blank means auto-generate; otherwise use the server DTO character count and its 16 to 512 bounds. */
export function isValidInstanceSecret(raw: string): boolean {
- return raw.trim() === '' || raw.trim().length >= 16;
+ const secret = raw.trim();
+ const length = captionLength(secret);
+ return secret === '' || (length >= 16 && length <= 512);
}
/** Blank → no config (undefined). Otherwise must parse to a plain JSON object. */