Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): refuse an ingress delivery for a rebound instance

Yudhi Armyndharisafc011e5e193
diff --git a/src/core/plugins/ingress-instance-config.spec.ts b/src/core/plugins/ingress-instance-config.spec.ts
index 1392261ae..1c28d9402 100644
--- a/src/core/plugins/ingress-instance-config.spec.ts
+++ b/src/core/plugins/ingress-instance-config.spec.ts
@@ -348,4 +348,29 @@ describe('ingress dispatch keeps what was decided when the delivery was admitted
expect(calls[0].verified).toBe(want);
},
);
+
+ it('refuses a delivery admitted for a session the instance has since left', async () => {
+ const rebound = instanceRow('acct-a', { apiToken: 'token-A' }, 'S-new');
+ const { bridge, calls } = makeBridge({ rows: [rebound], sessionConfig: { 'S-new': { endpoint: 'https://new' } } });
+
+ await expect(bridge.dispatchWebhookForInstance(job('acct-a', 'S-old'))).rejects.toThrow(/rebound/);
+ expect(calls).toHaveLength(0);
+ });
+
+ it('treats null, undefined and * as the one all-sessions scope', async () => {
+ const wildcard = instanceRow('acct-a', { apiToken: 'token-A' }, '*');
+ const { bridge, calls } = makeBridge({ rows: [wildcard] });
+
+ await bridge.dispatchWebhookForInstance(job('acct-a', null));
+ await bridge.dispatchWebhookForInstance(job('acct-a', '*'));
+
+ expect(calls).toHaveLength(2);
+ });
+
+ it('refuses a delivery admitted for all sessions once the instance is bound to one', async () => {
+ const { bridge, calls } = makeBridge({ rows: [row()] });
+
+ await expect(bridge.dispatchWebhookForInstance(job('acct-a', null))).rejects.toThrow(/rebound/);
+ expect(calls).toHaveLength(0);
+ });
});
diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.ts
index b9baf12de..1e2273b25 100644
--- a/src/core/plugins/plugin-sandbox-bridge.ts
+++ b/src/core/plugins/plugin-sandbox-bridge.ts
@@ -17,6 +17,9 @@ import { registerPluginSearchProvider, unregisterPluginSearchProvider } from './
import { INGRESS_DISPATCH_TIMEOUT_MS } from '../../modules/integration/integration.constants';
import type { IngressJobData } from '../../modules/queue/processors/ingress.processor';
+/** The all-sessions scope is spelled null, undefined or '*' depending on where it was stored. */
+const scopeKey = (scope: string | null | undefined): string => (!scope || scope === '*' ? '*' : scope);
+
/** Time budget for a sandboxed plugin's hook handler before the chain proceeds without it. */
const SANDBOX_HOOK_TIMEOUT_MS = 5000;
/** A sandboxed plugin's healthCheck must answer within this, else it's reported unhealthy (not hung). */
@@ -209,6 +212,11 @@ export class PluginSandboxBridge {
if (!instance || instance.enabled === false) {
throw new Error('instance ' + d.instanceId + ' of plugin ' + d.pluginId + ' is disabled or deleted');
}
+ // The job carries the session the instance acted on at admission. Rebound since, the delivery would
+ // reach the handler under that session with the new session's config, so it is refused instead.
+ if (scopeKey(instance.sessionScope) !== scopeKey(d.sessionId)) {
+ throw new Error('instance ' + d.instanceId + ' of plugin ' + d.pluginId + ' was rebound since this delivery');
+ }
// Three layers, most specific last: the base ('*') config, then the operator's per-session
// override from PUT /plugins/:id/sessions/:sessionId/config, then THIS instance's own config.
// The instance layer is what keeps two instances sharing one session scope apart — provisioning