Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): gate webhook saves on every gateway filter limit

Yudhi Armyndharis14c4a8b269ba
diff --git a/dashboard/src/pages/Webhooks.test.ts b/dashboard/src/pages/Webhooks.test.ts
index 1751b5279..ab2836291 100644
--- a/dashboard/src/pages/Webhooks.test.ts
+++ b/dashboard/src/pages/Webhooks.test.ts
@@ -436,6 +436,9 @@ test('replacing headers sends the whole map, and no rows sends an empty map', as
assert.deepEqual(updateBody!.headers, {});
});
+const FILTERS_HINT =
+ 'Give every filter condition a value, and use at most 20 conditions, 100 values per condition and 1000 characters of text.';
+
// 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 () => {
@@ -445,7 +448,7 @@ test('an incomplete filter condition keeps Create disabled and says why', async
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.');
+ screen.getByText(FILTERS_HINT);
fireEvent.click(create);
await new Promise(resolve => setTimeout(resolve, 50));
assert.equal(createCalls, 0);
@@ -454,8 +457,7 @@ test('an incomplete filter condition keeps Create disabled and says why', async
assert.equal(create.disabled, false);
});
-test('more than 20 filter conditions keep Save disabled', async () => {
- const { screen, fireEvent } = rtl;
+function editWebhookWith(conditions: unknown[]): void {
webhooksStatus = 200;
webhookList = [
{
@@ -464,21 +466,41 @@ test('more than 20 filter conditions keep Save disabled', async () => {
url: 'https://example.test/hook',
events: ['message.received'],
active: true,
- filters: { conditions: Array.from({ length: 20 }, () => ({ field: 'fromMe', operator: 'is', value: true })) },
+ filters: { conditions },
},
];
window.sessionStorage.setItem('openwa_user_role', 'operator');
renderWebhooks();
+}
+
+async function openEdit(): Promise<HTMLButtonElement> {
+ const { screen, fireEvent } = rtl;
fireEvent.click(await screen.findByTitle('Edit'));
- const save = screen.getByRole<HTMLButtonElement>('button', { name: 'Save Changes' });
+ return screen.getByRole<HTMLButtonElement>('button', { name: 'Save Changes' });
+}
+
+test('more than 20 filter conditions keep Save disabled', async () => {
+ const { screen, fireEvent } = rtl;
+ // Stored before the limit: the builder no longer adds a 21st row, but it can still load one.
+ editWebhookWith(Array.from({ length: 21 }, () => ({ field: 'fromMe', operator: 'is', value: true })));
+ const save = await openEdit();
+ assert.equal(save.disabled, true);
+ screen.getByText(FILTERS_HINT);
+
+ fireEvent.click(screen.getAllByRole('button', { name: 'Remove condition' })[0]);
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' } });
+test('a condition with more than 100 values keeps Save disabled', async () => {
+ const { screen, fireEvent } = rtl;
+ const values = Array.from({ length: 101 }, (_, i) => `${i}@c.us`);
+ editWebhookWith([{ field: 'sender', operator: 'is', value: values }]);
+ const save = await openEdit();
assert.equal(save.disabled, true);
- screen.getByText('Give every filter condition a value, and use at most 20 conditions.');
+ screen.getByText(FILTERS_HINT);
+ fireEvent.click(save);
+ await new Promise(resolve => setTimeout(resolve, 50));
+ assert.equal(updateCalls, 0);
});
test('a test in flight on one webhook is not ended by a test on another', async () => {
diff --git a/dashboard/src/pages/Webhooks.tsx b/dashboard/src/pages/Webhooks.tsx
index bac1e7970..00a00e075 100644
--- a/dashboard/src/pages/Webhooks.tsx
+++ b/dashboard/src/pages/Webhooks.tsx
@@ -19,6 +19,7 @@ import { availableEventNames } from '../utils/webhookEvents';
import { filterValueLabel } from '../utils/enumLabels';
import { buildHeaderMap, generateSecret, secretError, type HeaderRow } from '../utils/webhookAuth';
import { copyToClipboard } from '../utils/clipboard';
+import { filtersIncomplete } from '../utils/webhookFilters';
import { useRole } from '../hooks/useRole';
import { useToast } from '../hooks/useToast';
import {
@@ -37,14 +38,10 @@ 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
+// The gateway refuses filters past its limits 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));
+ supportsFilters(events) && filtersIncomplete(filters);
type TFn = ReturnType<typeof useTranslation>['t'];
@@ -488,7 +485,10 @@ export function Webhooks() {
)}
{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.')}
+ {t(
+ 'webhooks.filters.incomplete',
+ 'Give every filter condition a value, and use at most 20 conditions, 100 values per condition and 1000 characters of text.',
+ )}
</span>
)}
<div className="filter-builder webhook-auth">
@@ -569,7 +569,10 @@ export function Webhooks() {
)}
{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.')}
+ {t(
+ 'webhooks.filters.incomplete',
+ 'Give every filter condition a value, and use at most 20 conditions, 100 values per condition and 1000 characters of text.',
+ )}
</span>
)}
<div className="filter-builder webhook-auth">