Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): skip a webhook whose stored conditions is not an array

Yudhi Armyndharis180742d37cfa
diff --git a/src/modules/webhook/webhook-delivery.service.spec.ts b/src/modules/webhook/webhook-delivery.service.spec.ts
index 8fb2273e9..920eb94ce 100644
--- a/src/modules/webhook/webhook-delivery.service.spec.ts
+++ b/src/modules/webhook/webhook-delivery.service.spec.ts
@@ -1373,6 +1373,39 @@ describe('WebhookDeliveryService', () => {
expect(mockFetch).toHaveBeenCalledTimes(1);
});
+
+ it.each([
+ ['conditions is a string', { conditions: 'x' }],
+ ['conditions is an object', { conditions: {} }],
+ ['filters is an array', [{ field: 'from', operator: 'is', value: ['x'] }]],
+ ['filters is a string', 'x'],
+ ])('skips a webhook whose stored %s instead of delivering unfiltered', async (_label, filters) => {
+ mockFetch.mockClear();
+ const healthy = createMockWebhook({ id: 'wh-ok', events: ['*'] });
+ const badFilters = createMockWebhook({
+ id: 'wh-bad-conditions',
+ events: ['*'],
+ filters: filters as unknown as WebhookFilters,
+ });
+ (repository.find as jest.Mock).mockResolvedValue([badFilters, healthy]);
+
+ await service.dispatch('sess-1', 'message.received', { from: '[email protected]' });
+
+ expect(mockFetch).toHaveBeenCalledTimes(1);
+ });
+
+ it.each([
+ ['an empty object', {}],
+ ['an empty conditions array', { conditions: [] }],
+ ])('still delivers to a webhook whose stored filters is %s', async (_label, filters) => {
+ mockFetch.mockClear();
+ const webhook = createMockWebhook({ id: 'wh-empty', events: ['*'], filters: filters as WebhookFilters });
+ (repository.find as jest.Mock).mockResolvedValue([webhook]);
+
+ await service.dispatch('sess-1', 'message.received', { from: '[email protected]' });
+
+ expect(mockFetch).toHaveBeenCalledTimes(1);
+ });
});
// ── custom-header sanitization ───────────────────────────────
diff --git a/src/modules/webhook/webhook-delivery.service.ts b/src/modules/webhook/webhook-delivery.service.ts
index 9a55b7d25..5e66cc5ac 100644
--- a/src/modules/webhook/webhook-delivery.service.ts
+++ b/src/modules/webhook/webhook-delivery.service.ts
@@ -273,12 +273,23 @@ export class WebhookDeliveryService implements OnModuleInit, OnModuleDestroy {
const resolveLid = (jid: string): string | null => this.lidMappingStore?.resolveLid(jid) ?? null;
// A row is judged on its own: one whose stored events or filters are malformed (e.g. restored from
// a hand-edited backup) is skipped, instead of throwing here and dropping the event for every
- // other webhook of the session.
+ // other webhook of the session. A `filters` that is not a plain object, or a non-array `conditions`,
+ // is refused explicitly: evaluateFilters reads either as "no filter", which would deliver every
+ // subscribed event unfiltered.
const subscribed = webhooks.filter(
w => Array.isArray(w.events) && (w.events.includes(event) || w.events.includes('*')),
);
const matching = subscribed.filter(w => {
try {
+ const filters: unknown = w.filters;
+ if (
+ filters != null &&
+ (typeof filters !== 'object' ||
+ Array.isArray(filters) ||
+ (w.filters?.conditions != null && !Array.isArray(w.filters.conditions)))
+ ) {
+ throw new TypeError('filters must be an object with a conditions array');
+ }
return evaluateFilters(w.filters, event, data, resolveLid);
} catch (error) {
this.logger.warn('Skipping webhook with malformed filters', {