Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): skip opt-in events in the redrive of a wildcard webhook

Yudhi Armyndharis69e2a529ce4c
diff --git a/src/modules/webhook/webhook-recovery.pg.spec.ts b/src/modules/webhook/webhook-recovery.pg.spec.ts
index e62f13fa6..7de8271de 100644
--- a/src/modules/webhook/webhook-recovery.pg.spec.ts
+++ b/src/modules/webhook/webhook-recovery.pg.spec.ts
@@ -132,6 +132,39 @@ describePostgres('webhook and media recovery on PostgreSQL', () => {
expect(await service.redrive({ sessionId: 'outside' }, [sessionId])).toMatchObject({ redriven: 0 });
});
+ // The replay skips an opt-in event for a webhook that only subscribes to '*' without spending an
+ // attempt, so a row selected for one would sit at the head of every batch for good.
+ it('leaves an opt-in event out of the batch of a webhook that only subscribes to *', async () => {
+ await webhooks.update({ id: input.webhookId }, { events: ['*'] });
+ await failures.save({
+ ...input,
+ event: 'message.receipt',
+ idempotencyKey: 'receipt',
+ payload: {},
+ createdAt: new Date(Date.now() - 2000),
+ });
+ await failures.save({
+ ...input,
+ idempotencyKey: 'ordinary',
+ payload: { body: 'ok' },
+ createdAt: new Date(Date.now() - 1000),
+ });
+ const delivery = {
+ redeliver: jest.fn(async (_webhook: Webhook, _session: string, _event: string, key: string) => {
+ await failures.delete({ idempotencyKey: key });
+ return 'delivered';
+ }),
+ };
+ const service = new WebhookRedriveService(webhooks, failures, delivery as never, config);
+
+ expect(await service.redrive({ limit: 1 }, [sessionId])).toMatchObject({ delivered: 1, remaining: 0 });
+ expect(delivery.redeliver.mock.calls.map(call => call[3])).toEqual(['ordinary']);
+
+ await webhooks.update({ id: input.webhookId }, { events: ['message.receipt'] });
+ expect(await service.redrive({ limit: 1 }, [sessionId])).toMatchObject({ delivered: 1, remaining: 0 });
+ expect(delivery.redeliver.mock.calls.map(call => call[3])).toEqual(['ordinary', 'receipt']);
+ });
+
it('converges two connections on one terminal identity', async () => {
const result = await Promise.all([
recordWebhookDeliveryFailure(failures, logger, input),
diff --git a/src/modules/webhook/webhook-redrive.service.spec.ts b/src/modules/webhook/webhook-redrive.service.spec.ts
index 429af4689..08933b8cd 100644
--- a/src/modules/webhook/webhook-redrive.service.spec.ts
+++ b/src/modules/webhook/webhook-redrive.service.spec.ts
@@ -281,6 +281,28 @@ describe('WebhookRedriveService', () => {
expect(delivery.redeliver.mock.calls[0][2]).toBe('session.disconnected');
});
+ it('keeps opt-in events out of a wildcard subscription, so they cannot pin the head of a batch', async () => {
+ // message.receipt must be named; '*' no longer covers it. Rows left over from a time the webhook did
+ // name it would otherwise be selected, skipped without a counter change, and selected again forever.
+ await webhooks.update(primary.id, { events: ['*'] });
+ for (let i = 0; i < 3; i++) {
+ await addFailure({ event: 'message.receipt', createdAt: new Date(Date.now() - 120_000) });
+ }
+ const wanted = await addFailure({ event: 'message.received' });
+
+ expect(await service.redrive({ limit: 2 })).toMatchObject({ delivered: 1, skipped: 0, remaining: 0 });
+ expect(delivery.redeliver).toHaveBeenCalledTimes(1);
+ expect(delivery.redeliver.mock.calls[0][3]).toBe(wanted.idempotencyKey);
+ });
+
+ it('replays an opt-in event once the webhook names it', async () => {
+ await webhooks.update(primary.id, { events: ['*', 'message.receipt'] });
+ await addFailure({ event: 'message.receipt' });
+
+ expect(await service.redrive({})).toMatchObject({ delivered: 1, remaining: 0 });
+ expect(delivery.redeliver.mock.calls[0][2]).toBe('message.receipt');
+ });
+
it('matches exact event members and ignores non-array subscriptions', async () => {
await webhooks.update(primary.id, { events: ['message.received.extra'] });
await addFailure();
diff --git a/src/modules/webhook/webhook-redrive.service.ts b/src/modules/webhook/webhook-redrive.service.ts
index df92d6af0..9819a6dc4 100644
--- a/src/modules/webhook/webhook-redrive.service.ts
+++ b/src/modules/webhook/webhook-redrive.service.ts
@@ -9,6 +9,7 @@ import { isDeliverableWebhook } from './utils/deliver-once';
import { MAX_TIMER_MS } from '../../config/configuration';
import { resolveSessionScope } from '../../common/security/session-scope';
import { createLogger } from '../../common/services/logger.service';
+import { WILDCARD_EXCLUDED_EVENTS } from '../../common/utils/wildcard-events';
/** Rows replayed per call when the caller names no limit, and the most one call will take. */
export const DEFAULT_WEBHOOK_REDRIVE_LIMIT = 100;
@@ -158,7 +159,14 @@ export class WebhookRedriveService implements OnModuleDestroy {
...(request.webhookId ? { webhookId: request.webhookId } : {}),
})
.andWhere('webhook.active = :active', { active: true })
- .andWhere(`EXISTS (SELECT 1 FROM ${subscriptions} WHERE ${subscribedEvent} IN ('*', failure.event))`);
+ // The same rule as subscriptionCoversEvent: '*' covers every event except the opt-in ones, which a
+ // webhook must name. Selecting them for a '*'-only webhook would put rows at the head of every batch
+ // that the replay then skips without raising their attempts, so they would never leave it.
+ .andWhere(
+ `EXISTS (SELECT 1 FROM ${subscriptions} WHERE ${subscribedEvent} = failure.event` +
+ ` OR (${subscribedEvent} = '*' AND failure.event NOT IN (:...wildcardExcluded)))`,
+ { wildcardExcluded: [...WILDCARD_EXCLUDED_EVENTS] },
+ );
const limit = Math.min(Math.max(1, request.limit ?? DEFAULT_WEBHOOK_REDRIVE_LIMIT), MAX_WEBHOOK_REDRIVE_LIMIT);
// A call still queued when shutdown starts reads no rows (or their payloads); it only counts them.
const rows = this.stop.signal.aborted