Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): namespace every ingress job id by plugin and instance

Yudhi Armyndharis86c81be1f46c
diff --git a/src/modules/integration/ingress-enqueue.service.spec.ts b/src/modules/integration/ingress-enqueue.service.spec.ts
index 3760d7d49..83642ca88 100644
--- a/src/modules/integration/ingress-enqueue.service.spec.ts
+++ b/src/modules/integration/ingress-enqueue.service.spec.ts
@@ -27,14 +27,14 @@ describe('IngressEnqueueService', () => {
queue = { add: jest.fn().mockResolvedValue(undefined) };
});
- it('adds a job to the ingress queue with the given jobId when queueing is enabled and a queue is present', async () => {
+ it('adds a job to the ingress queue keyed by the namespaced jobId when queueing is enabled and a queue is present', async () => {
(config.get as jest.Mock).mockReturnValue(true);
const svc = new IngressEnqueueService(loader as PluginLoaderService, config as ConfigService, queue as never);
expect(await svc.enqueue(data, 'd1')).toEqual({ outcome: 'queued' });
expect(queue.add).toHaveBeenCalledWith('ingress', data, {
- jobId: 'd1',
+ jobId: sanitizeIngressJobId('d1', 'chatwoot\u0000acct1'),
attempts: 3,
backoff: { type: 'exponential', delay: 5000 },
});
@@ -45,11 +45,17 @@ describe('IngressEnqueueService', () => {
// sanitizer those throws read as "Redis unreachable" in the catch-all and silently degraded the
// delivery to inline dispatch (no retry, no backoff, blocked redrive loop).
describe('sanitizeIngressJobId', () => {
- it('passes safe ids through unchanged (BullMQ accepts them as-is)', () => {
- expect(sanitizeIngressJobId('d1')).toBe('d1');
- expect(sanitizeIngressJobId('evt_abc-123')).toBe('evt_abc-123');
- expect(sanitizeIngressJobId('a:b:c')).toBe('a:b:c'); // exactly 3 parts is BullMQ-legal
- expect(sanitizeIngressJobId('0abc')).toBe('0abc'); // zero-PREFIXED is fine; only '0:'-leading is not
+ it('hashes ids BullMQ would accept too, so the namespace always applies', () => {
+ for (const id of ['d1', 'evt_abc-123', 'a:b:c', '0abc']) {
+ expect(sanitizeIngressJobId(id)).toMatch(/^ing-[0-9a-f]{40}$/);
+ }
+ });
+
+ it('namespaces a non-numeric id two instances share (one Svix message fanned out to both)', () => {
+ const a = sanitizeIngressJobId('msg_2LJx9', 'chatwoot\u0000acct-1');
+ const b = sanitizeIngressJobId('msg_2LJx9', 'chatwoot\u0000acct-2');
+ expect(a).not.toBe(b);
+ expect(sanitizeIngressJobId('msg_2LJx9', 'chatwoot\u0000acct-1')).toBe(a);
});
it('hashes the shapes BullMQ refuses, deterministically', () => {
@@ -173,7 +179,7 @@ describe('IngressEnqueueService', () => {
expect(await svc.enqueue(data, 'd1')).toEqual({ outcome: 'dispatched' });
expect(queue.add).toHaveBeenCalledWith('ingress', data, {
- jobId: 'd1',
+ jobId: sanitizeIngressJobId('d1', 'chatwoot\u0000acct1'),
attempts: 3,
backoff: { type: 'exponential', delay: 5000 },
});
diff --git a/src/modules/integration/ingress-enqueue.service.ts b/src/modules/integration/ingress-enqueue.service.ts
index cea1a0da0..4b1a2f13c 100644
--- a/src/modules/integration/ingress-enqueue.service.ts
+++ b/src/modules/integration/ingress-enqueue.service.ts
@@ -43,25 +43,22 @@ export function resolveIngressJobOptions(): { attempts: number; backoff: { type:
* '0:'-prefixed id ("JobId cannot be '0' or start with '0:'"). Providers send numeric dedup headers
* (`svix-Id: 12345`), and the redrive path mints `redrive:<uuid>`, so these refusals happen in
* practice, and because enqueue()'s catch-all treats ANY add() throw as "Redis unreachable", the job
- * silently degraded to inline dispatch with no retry, no backoff, and a blocked redrive loop. Map the
- * refused shapes to a deterministic sha256 prefix: BullMQ's exactly-once dedup only needs the id
- * STABLE per delivery, not recognizable, and the reconciler replays through this same function so
- * its dedup against an earlier enqueue is preserved.
+ * silently degraded to inline dispatch with no retry, no backoff, and a blocked redrive loop. Every id
+ * therefore maps to a deterministic sha256 prefix, which BullMQ always accepts: its exactly-once dedup
+ * only needs the id STABLE per delivery, not recognizable, and the reconciler replays through this
+ * same function so its dedup against an earlier enqueue is preserved.
*
- * The hash input is namespaced with the plugin/instance pair. BullMQ dedups jobIds across the WHOLE
- * shared ingress queue, while the database dedup is (pluginId, instanceId, providerDeliveryId), and
- * numeric provider ids are exactly the short, per-account sequence style that two instances of one
- * provider can share. Without the namespace, the second instance's delivery would collide with the
- * first's job id and BullMQ would silently discard it (resolved as the existing job, no DLQ row);
- * with it, the queue-level dedup matches the database-level scope. A non-string id (a duplicated
- * header can surface as string[]) is coerced rather than trusted to reach BullMQ's own checks.
+ * The hash input is namespaced with the plugin/instance pair, for EVERY id rather than only the
+ * refused shapes. BullMQ dedups jobIds across the WHOLE shared ingress queue, while the database dedup
+ * is (pluginId, instanceId, providerDeliveryId), and two instances can share a provider id: numeric
+ * per-account sequences, or one Standard Webhooks message fanned out to two endpoints with the same
+ * `webhook-id`. Without the namespace, the second instance's delivery would collide with the first's
+ * job id and BullMQ would silently discard it (resolved as the existing job, no DLQ row); with it, the
+ * queue-level dedup matches the database-level scope. A non-string id (a duplicated header can surface
+ * as string[]) is coerced rather than trusted to reach BullMQ's own checks.
*/
export function sanitizeIngressJobId(jobId: string, namespace = ''): string {
const raw = typeof jobId === 'string' ? jobId : String(jobId);
- const looksInteger = `${parseInt(raw, 10)}` === raw;
- const badColon = raw.includes(':') && raw.split(':').length !== 3;
- const zeroPrefixed = raw === '0' || raw.startsWith('0:');
- if (!looksInteger && !badColon && !zeroPrefixed) return raw;
return `ing-${createHash('sha256').update(`${namespace}\u0000${raw}`).digest('hex').slice(0, 40)}`;
}
diff --git a/src/modules/integration/integration-queue-wiring.spec.ts b/src/modules/integration/integration-queue-wiring.spec.ts
index 84df55fa4..469307e02 100644
--- a/src/modules/integration/integration-queue-wiring.spec.ts
+++ b/src/modules/integration/integration-queue-wiring.spec.ts
@@ -149,8 +149,8 @@ describe('IntegrationModule queue wiring (QUEUE_ENABLED conditional)', () => {
const enqueue = moduleRef.get(IngressEnqueueService, { strict: false });
expect(() => enqueue.onApplicationBootstrap()).not.toThrow();
- // ...and the full queued-dispatch contract holds: enqueue() hands the job to BullMQ (with the
- // deliveryId as jobId) instead of falling back to inline dispatch.
+ // ...and the full queued-dispatch contract holds: enqueue() hands the job to BullMQ (keyed by the
+ // namespaced deliveryId hash) instead of falling back to inline dispatch.
const outcome = await enqueue.enqueue(
{
pluginId: 'chatwoot',
@@ -165,8 +165,10 @@ describe('IntegrationModule queue wiring (QUEUE_ENABLED conditional)', () => {
expect(queue.add).toHaveBeenCalledWith(
'ingress',
expect.objectContaining({ deliveryId: 'd1' }),
- expect.objectContaining({ jobId: 'd1' }),
+ expect.anything(),
);
+ const [, , opts] = queue.add.mock.calls[0] as [string, unknown, { jobId: string }];
+ expect(opts.jobId).toMatch(/^ing-[0-9a-f]{40}$/);
} finally {
await moduleRef.close();
}