Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): block webhook saves with incomplete filters

Yudhi Armyndharis0965580995b6
diff --git a/dashboard/src/pages/Webhooks.test.ts b/dashboard/src/pages/Webhooks.test.ts
index e31c33524..c5861db44 100644
--- a/dashboard/src/pages/Webhooks.test.ts
+++ b/dashboard/src/pages/Webhooks.test.ts
@@ -411,3 +411,48 @@ test('replacing headers sends the whole map, and no rows sends an empty map', as
await rtl.waitFor(() => assert.equal(updateCalls, 1));
assert.deepEqual(updateBody!.headers, {});
});
+
+// FilterBuilder starts a condition as "sender is" with no contact, and the gateway refuses an empty
+// value list and more than 20 conditions. Either would come back as raw English in a toast.
+test('an incomplete filter condition keeps Create disabled and says why', async () => {
+ const { screen, fireEvent } = rtl;
+ const create = await openCreateModal();
+ assert.equal(create.disabled, false);
+
+ fireEvent.click(screen.getByRole('button', { name: 'Add condition' }));
+ assert.equal(create.disabled, true);
+ screen.getByText('Give every filter condition a value, and use at most 20 conditions.');
+ fireEvent.click(create);
+ await new Promise(resolve => setTimeout(resolve, 50));
+ assert.equal(createCalls, 0);
+
+ fireEvent.click(screen.getByRole('button', { name: 'Remove condition' }));
+ assert.equal(create.disabled, false);
+});
+
+test('more than 20 filter conditions keep Save disabled', async () => {
+ const { screen, fireEvent } = rtl;
+ webhooksStatus = 200;
+ webhookList = [
+ {
+ id: 'w1',
+ sessionId: 'sess-1',
+ url: 'https://example.test/hook',
+ events: ['message.received'],
+ active: true,
+ filters: { conditions: Array.from({ length: 20 }, () => ({ field: 'fromMe', operator: 'is', value: true })) },
+ },
+ ];
+ window.sessionStorage.setItem('openwa_user_role', 'operator');
+ renderWebhooks();
+ fireEvent.click(await screen.findByTitle('Edit'));
+ const save = screen.getByRole<HTMLButtonElement>('button', { name: 'Save Changes' });
+ assert.equal(save.disabled, false);
+
+ // A 21st condition that is complete on its own: only the count is wrong.
+ fireEvent.click(screen.getByRole('button', { name: 'Add condition' }));
+ const fields = screen.getAllByLabelText('Filter field');
+ fireEvent.change(fields[fields.length - 1], { target: { value: 'fromMe' } });
+ assert.equal(save.disabled, true);
+ screen.getByText('Give every filter condition a value, and use at most 20 conditions.');
+});
diff --git a/dashboard/src/pages/Webhooks.tsx b/dashboard/src/pages/Webhooks.tsx
index befbe1b13..f4bbdb9eb 100644
--- a/dashboard/src/pages/Webhooks.tsx
+++ b/dashboard/src/pages/Webhooks.tsx
@@ -37,6 +37,15 @@ import './Webhooks.css';
// Filters only apply to message.* events (the wildcard subscribes to them too).
const supportsFilters = (events: string[]) => events.some(e => e === '*' || e.startsWith('message.'));
+// The gateway refuses more than 20 conditions and an id or enum condition with no values, and a new
+// condition starts as "sender is" with none, so an untouched one would come back as a raw error.
+const MAX_FILTER_CONDITIONS = 20;
+const filtersInvalid = (events: string[], filters: WebhookFilters | null | undefined) =>
+ supportsFilters(events) &&
+ !!filters &&
+ (filters.conditions.length > MAX_FILTER_CONDITIONS ||
+ filters.conditions.some(c => Array.isArray(c.value) && c.value.length === 0));
+
type TFn = ReturnType<typeof useTranslation>['t'];
// One-line, human-readable summary of a condition for the badge popover, reusing the FilterBuilder labels.
@@ -223,8 +232,8 @@ export function Webhooks() {
return t(`webhooks.eventDescriptions.${name}`, { defaultValue: name });
};
- // The gateway requires a URL and at least one event, so the buttons stay disabled until both are set
- // instead of surfacing the raw validation message in a toast.
+ // The gateway requires a URL, at least one event and complete filters, so the buttons stay disabled
+ // until all are set instead of surfacing the raw validation message in a toast.
const newHeaders = buildHeaderMap(newAuth.headers);
const newAuthError = secretError(newAuth.secret) ?? (newHeaders.ok ? null : newHeaders.error);
const editHeaders = buildHeaderMap(editAuth.headers);
@@ -236,12 +245,14 @@ export function Webhooks() {
!!newWebhook.url.trim() &&
!!newWebhook.sessionId &&
newWebhook.events.length > 0 &&
+ !filtersInvalid(newWebhook.events, newWebhook.filters) &&
!newAuthError;
const canSave =
!!editWebhook &&
!updateMutation.isPending &&
!!editWebhook.url.trim() &&
editWebhook.events.length > 0 &&
+ !filtersInvalid(editWebhook.events, editWebhook.filters) &&
!editAuthError;
const handleCreate = async () => {
@@ -468,6 +479,11 @@ export function Webhooks() {
chats={chats}
/>
)}
+ {filtersInvalid(newWebhook.events, newWebhook.filters) && (
+ <span className="hint error" role="status">
+ {t('webhooks.filters.incomplete', 'Give every filter condition a value, and use at most 20 conditions.')}
+ </span>
+ )}
<div className="filter-builder webhook-auth">
<div className="filter-builder-head">
<span className="filter-builder-title">{t('webhooks.auth.title')}</span>
@@ -544,6 +560,11 @@ export function Webhooks() {
chats={chats}
/>
)}
+ {filtersInvalid(editWebhook.events, editWebhook.filters) && (
+ <span className="hint error" role="status">
+ {t('webhooks.filters.incomplete', 'Give every filter condition a value, and use at most 20 conditions.')}
+ </span>
+ )}
<div className="filter-builder webhook-auth">
<div className="filter-builder-head">
<span className="filter-builder-title">{t('webhooks.auth.title')}</span>