Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): hold webhook filters to the gateway limits

Yudhi Armyndharis352154aac905
diff --git a/dashboard/src/components/FilterBuilder.test.ts b/dashboard/src/components/FilterBuilder.test.ts
index b73015f5c..d74e91af0 100644
--- a/dashboard/src/components/FilterBuilder.test.ts
+++ b/dashboard/src/components/FilterBuilder.test.ts
@@ -43,3 +43,29 @@ test('enum tags name message types and chat kinds in words', () => {
assert.ok(!types.includes('Message'), `message type tags: ${types.join(', ')}`);
assert.deepEqual(kinds, ['Individual', 'Group', 'Channel', 'Status', 'Broadcast', 'Unknown']);
});
+
+test('adding a condition stops at the gateway limit of 20', () => {
+ const sender = { field: 'sender', operator: 'is' as const, value: ['[email protected]'] };
+ const view = (count: number) =>
+ rtl.render(
+ createElement(FilterBuilder, {
+ filters: { conditions: Array.from({ length: count }, () => ({ ...sender })) },
+ onChange: () => {},
+ chats: [],
+ }),
+ );
+ const add = (container: HTMLElement) => container.querySelector<HTMLButtonElement>('.filter-add')!;
+ assert.equal(add(view(19).container).disabled, false);
+ assert.equal(add(view(20).container).disabled, true);
+});
+
+test('the body text stops at the gateway limit of 1000 characters', () => {
+ const { container } = rtl.render(
+ createElement(FilterBuilder, {
+ filters: { conditions: [{ field: 'body', operator: 'contains', value: '' }] },
+ onChange: () => {},
+ chats: [],
+ }),
+ );
+ assert.equal(container.querySelector<HTMLInputElement>('.filter-text input[type="text"]')?.maxLength, 1000);
+});
diff --git a/dashboard/src/components/FilterBuilder.tsx b/dashboard/src/components/FilterBuilder.tsx
index d570d98b4..68b080319 100644
--- a/dashboard/src/components/FilterBuilder.tsx
+++ b/dashboard/src/components/FilterBuilder.tsx
@@ -10,6 +10,7 @@ import {
type WebhookFilterOperator,
} from '../services/api';
import { filterValueLabel } from '../utils/enumLabels';
+import { MAX_FILTER_CONDITIONS, MAX_FILTER_TEXT_LENGTH } from '../utils/webhookFilters';
import './FilterBuilder.css';
type FieldKind = 'id' | 'idArray' | 'text' | 'enum' | 'boolean';
@@ -244,6 +245,7 @@ export function FilterBuilder({ filters, onChange, chats }: FilterBuilderProps)
<input
type="text"
value={typeof condition.value === 'string' ? condition.value : ''}
+ maxLength={MAX_FILTER_TEXT_LENGTH}
placeholder={t('webhooks.filters.textPlaceholder')}
onChange={e => updateAt(index, { value: e.target.value })}
/>
@@ -283,7 +285,12 @@ export function FilterBuilder({ filters, onChange, chats }: FilterBuilderProps)
);
})}
- <button type="button" className="filter-add" onClick={addCondition}>
+ <button
+ type="button"
+ className="filter-add"
+ onClick={addCondition}
+ disabled={conditions.length >= MAX_FILTER_CONDITIONS}
+ >
<Plus size={14} />
{t('webhooks.filters.addCondition')}
</button>
diff --git a/dashboard/src/utils/webhookFilters.test.ts b/dashboard/src/utils/webhookFilters.test.ts
new file mode 100644
index 000000000..cb1e12de3
--- /dev/null
+++ b/dashboard/src/utils/webhookFilters.test.ts
@@ -0,0 +1,40 @@
+// The Webhooks modal keeps Create/Save disabled while filtersIncomplete is true, so a filter the gateway
+// would reject never reaches it (a production build answers with a bare "Bad Request").
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import type { WebhookFilterCondition } from '../services/api.ts';
+import { filtersIncomplete } from './webhookFilters.ts';
+
+const sender = (value: string[]): WebhookFilterCondition => ({ field: 'sender', operator: 'is', value });
+
+test('no filters, or complete ones, pass', () => {
+ assert.equal(filtersIncomplete(null), false);
+ assert.equal(filtersIncomplete(undefined), false);
+ assert.equal(
+ filtersIncomplete({
+ conditions: [
+ sender(['[email protected]']),
+ { field: 'body', operator: 'contains', value: '' },
+ { field: 'isGroup', operator: 'is', value: false },
+ ],
+ }),
+ false,
+ );
+});
+
+test('a list condition with no values is incomplete', () => {
+ assert.equal(filtersIncomplete({ conditions: [sender(['[email protected]']), sender([])] }), true);
+ assert.equal(filtersIncomplete({ conditions: [{ field: 'type', operator: 'is', value: [] }] }), true);
+});
+
+test('the gateway limits are enforced', () => {
+ assert.equal(filtersIncomplete({ conditions: Array.from({ length: 20 }, () => sender(['[email protected]'])) }), false);
+ assert.equal(filtersIncomplete({ conditions: Array.from({ length: 21 }, () => sender(['[email protected]'])) }), true);
+ const values = (n: number) => Array.from({ length: n }, (_, i) => `${i}@c.us`);
+ assert.equal(filtersIncomplete({ conditions: [sender(values(100))] }), false);
+ assert.equal(filtersIncomplete({ conditions: [sender(values(101))] }), true);
+ assert.equal(
+ filtersIncomplete({ conditions: [{ field: 'body', operator: 'contains', value: 'x'.repeat(1001) }] }),
+ true,
+ );
+});
diff --git a/dashboard/src/utils/webhookFilters.ts b/dashboard/src/utils/webhookFilters.ts
new file mode 100644
index 000000000..f3237e4c0
--- /dev/null
+++ b/dashboard/src/utils/webhookFilters.ts
@@ -0,0 +1,20 @@
+import type { WebhookFilters } from '../services/api';
+
+// Mirror the backend limits (src/modules/webhook/filters/filter-types.ts). The gateway rejects filters
+// past them, and a production build answers with a bare "Bad Request" that names no condition.
+export const MAX_FILTER_CONDITIONS = 20;
+export const MAX_FILTER_VALUES = 100;
+export const MAX_FILTER_TEXT_LENGTH = 1000;
+
+/** True when the gateway would reject these filters: a list condition with no values, or a limit exceeded. */
+export function filtersIncomplete(filters: WebhookFilters | null | undefined): boolean {
+ const conditions = filters?.conditions ?? [];
+ return (
+ conditions.length > MAX_FILTER_CONDITIONS ||
+ conditions.some(({ value }) =>
+ Array.isArray(value)
+ ? value.length === 0 || value.length > MAX_FILTER_VALUES
+ : typeof value === 'string' && value.length > MAX_FILTER_TEXT_LENGTH,
+ )
+ );
+}