Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): skip only the webhook whose stored row is malformed
Yudhi Armyndharis31048767581e
diff --git a/src/modules/webhook/webhook-delivery.service.spec.ts b/src/modules/webhook/webhook-delivery.service.spec.tsindex 07e8bf093..bca63d690 100644--- a/src/modules/webhook/webhook-delivery.service.spec.ts+++ b/src/modules/webhook/webhook-delivery.service.spec.ts@@ -1357,6 +1357,22 @@ describe('WebhookDeliveryService', () => {lidStore.getCached.mockImplementation((lid: string) => (lid === '111' ? '628999' : null));expect(await deliveries(f, 'message.received', data)).toBe(1);});++ it('skips only the webhook whose stored events or filters are malformed', async () => {+ mockFetch.mockClear();+ const healthy = createMockWebhook({ id: 'wh-ok', events: ['*'] });+ const badEvents = createMockWebhook({ id: 'wh-bad-events', events: {} as unknown as string[] });+ const badFilters = createMockWebhook({+ id: 'wh-bad-filters',+ events: ['*'],+ filters: { conditions: [null] } as unknown as WebhookFilters,+ });+ (repository.find as jest.Mock).mockResolvedValue([badEvents, badFilters, healthy]);++ await expect(service.dispatch('sess-1', 'message.received', { from: '[email protected]' })).resolves.toBeUndefined();++ expect(mockFetch).toHaveBeenCalledTimes(1);+ });});// ── custom-header sanitization ───────────────────────────────diff --git a/src/modules/webhook/webhook-delivery.service.ts b/src/modules/webhook/webhook-delivery.service.tsindex bf280fdac..178e157a8 100644--- a/src/modules/webhook/webhook-delivery.service.ts+++ b/src/modules/webhook/webhook-delivery.service.ts@@ -271,8 +271,25 @@ export class WebhookDeliveryService implements OnModuleInit, OnModuleDestroy {// Resolve a lid actor to its phone through the persistent table so a phone filter matches a// lid-addressed sender (e.g. an unresolved @lid group participant). Absent store -> no resolution.const resolveLid = (jid: string): string | null => this.lidMappingStore?.resolveLid(jid) ?? null;- const subscribed = webhooks.filter(w => w.events.includes(event) || w.events.includes('*'));- const matching = subscribed.filter(w => evaluateFilters(w.filters, event, data, resolveLid));+ // 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.+ const subscribed = webhooks.filter(+ w => Array.isArray(w.events) && (w.events.includes(event) || w.events.includes('*')),+ );+ const matching = subscribed.filter(w => {+ try {+ return evaluateFilters(w.filters, event, data, resolveLid);+ } catch (error) {+ this.logger.warn('Skipping webhook with malformed filters', {+ webhookId: w.id,+ event,+ error: String(error),+ action: 'webhook_filters_invalid',+ });+ return false;+ }+ });// A subscribed webhook that a filter drops leaves no trace otherwise: dispatch() awaits an empty// array and returns, and the delivery-failure table only records deliveries that were ATTEMPTED.// That is fine when the filter is doing its job, and indistinguishable from it when it is not —