Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): clear the scope config slice when an instance is retired

Yudhi Armyndharis1f091d1de6b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 68a047bf0..98ca50bc7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -62,7 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `.env.example` shipped five keys the Infrastructure dashboard owns — `POSTGRES_BUILTIN`, `DATABASE_SSL`, `DATABASE_SSL_REJECT_UNAUTHORIZED`, `REDIS_BUILTIN` and `MINIO_BUILTIN` — uncommented, so an operator who followed the documented `cp .env.example .env` pinned them: the dashboard control still saved and reported success while the running value never changed, most consequentially leaving a Postgres connection unencrypted after the operator enabled SSL. The file's own header already promised a gate against this, but that gate only reached keys docker-compose forwards blank; it now covers every key the dashboard writes. `.env.minimal`, the other documented starting point, no longer pins the three built-in datastore toggles either; its deliberate development pins are left alone.
-- Two enabled instances of one integration plugin that share a session scope no longer collapse onto a single config, so an ingress delivery authenticated with one instance's secret is no longer handled with the credentials of whichever instance was provisioned last. Dispatch now layers the instance's own config over the base config and any per-session override.
+- Two enabled instances of one integration plugin that share a session scope no longer collapse onto a single config, so an ingress delivery authenticated with one instance's secret is no longer handled with the credentials of whichever instance was provisioned last. Dispatch layers the instance's own config over the base config, and consults the per-session override only while it can be attributed to that instance — with one enabled instance on the scope, which is every single-instance deployment, it still applies exactly as before. With siblings it does not: the override shares its storage with the per-scope projection of whichever instance was provisioned last, so on a shared scope it is not the operator's value but the other tenant's. Action required: an operator who has set a per-session config for a plugin and then provisions a second enabled instance on that same session should move the shared keys onto each instance instead, because the override stops reaching either of them. Retiring an instance clears that scope's config slice too, so the surviving tenant falls back to the plugin's own defaults rather than inheriting the endpoint and credentials of the instance just disabled or deleted — the same collapse, reachable by turning one of the two off.
## [0.17.0] - 2026-08-12
diff --git a/src/modules/integration/integration-instance.controller.spec.ts b/src/modules/integration/integration-instance.controller.spec.ts
index e6c5810b2..aadd5f5bb 100644
--- a/src/modules/integration/integration-instance.controller.spec.ts
+++ b/src/modules/integration/integration-instance.controller.spec.ts
@@ -196,7 +196,7 @@ describe('IntegrationInstanceController provisioning bridge', () => {
expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-2', { baseUrl: 'https://y' }); // new bound
});
- it('keeps the session + its config when a disabled instance shares its scope with an ENABLED sibling', async () => {
+ it('keeps the session but CLEARS its config when a disabled instance shares its scope with a sibling', async () => {
const { loader, audit, setPluginSessionConfig, setPluginSessions } = build();
(loader.getPlugin as jest.Mock).mockReturnValue({
manifest: { id: 'chatwoot-adapter', ingress: [{ route: 'chatwoot' }], permissions: ['webhook:ingress'] },
@@ -223,12 +223,14 @@ describe('IntegrationInstanceController provisioning bridge', () => {
await controller.patch('chatwoot-adapter', 'acct1', { enabled: false });
- // The sibling still fires on sess-1: neither the session nor its sessionConfig may be torn down.
- expect(setPluginSessionConfig).not.toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {});
+ // The sibling still fires on sess-1, so the SESSION stays bound. Its config slice does not: it is
+ // keyed by scope and holds the retiring instance's projection, which dispatch would hand to the
+ // survivor as soon as this leaves one enabled instance on the scope.
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {});
expect(setPluginSessions).not.toHaveBeenCalled();
});
- it('keeps the session + its config when a deleted instance shares its scope with an ENABLED sibling', async () => {
+ it('keeps the session but CLEARS its config when a deleted instance shares its scope with a sibling', async () => {
const { loader, audit, setPluginSessionConfig, setPluginSessions } = build();
(loader.getPlugin as jest.Mock).mockReturnValue({
manifest: { id: 'chatwoot-adapter', ingress: [{ route: 'chatwoot' }], permissions: ['webhook:ingress'] },
@@ -260,7 +262,9 @@ describe('IntegrationInstanceController provisioning bridge', () => {
await controller.remove('chatwoot-adapter', 'acct1');
- expect(setPluginSessionConfig).not.toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {});
+ // Same rule as the disable path — and the delete case is the sharper one: the row is gone, so the
+ // slice is the deleted tenant's only remaining trace and the survivor would inherit it.
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {});
expect(setPluginSessions).not.toHaveBeenCalled();
});
@@ -295,7 +299,7 @@ describe('IntegrationInstanceController provisioning bridge', () => {
expect(setPluginSessions).toHaveBeenCalledWith('chatwoot-adapter', []);
});
- it('keeps the OLD scope bound by an ENABLED sibling on a scope move (PATCH sessionScope) and binds the new one', async () => {
+ it('keeps the OLD scope ACTIVE for an ENABLED sibling on a scope move, and binds the new one', async () => {
const { loader, audit, setPluginSessionConfig, setPluginSessions } = build();
(loader.getPlugin as jest.Mock).mockReturnValue({
manifest: { id: 'chatwoot-adapter', ingress: [{ route: 'chatwoot' }], permissions: ['webhook:ingress'] },
@@ -333,7 +337,9 @@ describe('IntegrationInstanceController provisioning bridge', () => {
await controller.patch('chatwoot-adapter', 'acct1', { sessionScope: 'sess-2' });
- expect(setPluginSessionConfig).not.toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {}); // old scope kept for the sibling
+ // Moving off sess-1 leaves the sibling alone on it, so the slice this instance projected there
+ // must go with it — otherwise the sibling inherits the config of a tenant that has moved away.
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {});
expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-2', { baseUrl: 'https://y' }); // new scope bound
expect(setPluginSessions).toHaveBeenCalledWith('chatwoot-adapter', ['sess-1', 'sess-2']); // sess-1 never stripped
});
diff --git a/src/modules/integration/scope-binding.service.spec.ts b/src/modules/integration/scope-binding.service.spec.ts
index 55e555331..1c5e72582 100644
--- a/src/modules/integration/scope-binding.service.spec.ts
+++ b/src/modules/integration/scope-binding.service.spec.ts
@@ -264,3 +264,92 @@ describe('ScopeBindingService.onApplicationBootstrap reconciliation', () => {
}
});
});
+
+/**
+ * Retiring one instance must not leave its config behind for the tenant that survives.
+ *
+ * `sessionConfig` is keyed by SCOPE, so it holds whichever instance was projected onto that scope
+ * last. Keeping it when an enabled sibling remained was deliberate — dropping the scope from
+ * `activeSessions` would silence that sibling until the next boot — but the config slice is a
+ * different thing, and once this teardown leaves a SINGLE enabled instance on the scope,
+ * `PluginSandboxBridge.scopeHasAtMostOneInstance` re-declares the slice attributable to that
+ * survivor and merges it under the survivor's own row. A survivor relying on a plugin default for a
+ * key it does not define was therefore handed the retired tenant's value for it.
+ */
+describe('ScopeBindingService.applyScopeBinding retires an instance without leaking its config', () => {
+ function build(activeSessions: string[] = ['sess-1']) {
+ const setPluginSessionConfig = jest.fn();
+ const setPluginSessions = jest.fn();
+ const loader = {
+ getPlugin: jest.fn().mockReturnValue({ manifest: { id: 'chatwoot' }, activeSessions }),
+ setPluginSessionConfig,
+ setPluginSessions,
+ updatePluginConfig: jest.fn(),
+ } as unknown as PluginLoaderService;
+ const audit = { logInfo: jest.fn(), logWarn: jest.fn() } as unknown as AuditService;
+ const sessions = { findOne: jest.fn().mockResolvedValue({ id: 'sess-1' }) } as unknown as SessionService;
+ const svc = (rows: unknown[]): ScopeBindingService =>
+ new ScopeBindingService(
+ { list: jest.fn().mockResolvedValue(rows) } as unknown as PluginInstanceService,
+ loader,
+ audit,
+ sessions,
+ );
+ return { svc, setPluginSessionConfig, setPluginSessions };
+ }
+
+ /** The row shape `applyScopeBinding` reads: the controller persists it BEFORE calling us. */
+ const row = (instanceId: string, enabled: boolean, sessionScope: string | null = 'sess-1') => ({
+ pluginId: 'chatwoot',
+ instanceId,
+ sessionScope,
+ config: {},
+ enabled,
+ });
+
+ it('clears the scope config slice when a DISABLED sibling leaves one enabled instance behind', async () => {
+ const { svc, setPluginSessionConfig, setPluginSessions } = build();
+ // 'b' was provisioned last, so the slice holds ITS config. The controller has already persisted
+ // b as disabled, so its row is still listed — just no longer enabled.
+ await svc([row('a', true), row('b', false)]).applyScopeBinding('chatwoot', 'sess-1', {}, false);
+
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot', 'sess-1', {});
+ // …but the session stays active: dropping it would silence 'a' until the next boot.
+ expect(setPluginSessions).not.toHaveBeenCalled();
+ });
+
+ it('clears it for a DELETED sibling too, whose row is gone entirely', async () => {
+ const { svc, setPluginSessionConfig, setPluginSessions } = build();
+ // DELETE removes the row before this runs, so 'b' is not in the list at all — the surviving
+ // slice is the only trace of it, which is exactly what must not be inherited.
+ await svc([row('a', true)]).applyScopeBinding('chatwoot', 'sess-1', {}, false);
+
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot', 'sess-1', {});
+ expect(setPluginSessions).not.toHaveBeenCalled();
+ });
+
+ // Negative twin: a DISABLED sibling is not a tenant receiving deliveries, so it must not hold the
+ // scope open. With nothing enabled left, the full teardown still runs — slice cleared AND the
+ // scope dropped from activeSessions. Without this, the fix above could be "always return early".
+ it('still deactivates the session when no ENABLED sibling remains', async () => {
+ const { svc, setPluginSessionConfig, setPluginSessions } = build();
+ await svc([row('a', false)]).applyScopeBinding('chatwoot', 'sess-1', {}, false);
+
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot', 'sess-1', {});
+ expect(setPluginSessions).toHaveBeenCalledWith('chatwoot', []);
+ });
+
+ // Negative twin: a sibling on a DIFFERENT scope has no claim on this one.
+ it('deactivates when the only enabled sibling binds another scope', async () => {
+ const { svc, setPluginSessions } = build();
+ await svc([row('a', true, 'sess-2')]).applyScopeBinding('chatwoot', 'sess-1', {}, false);
+ expect(setPluginSessions).toHaveBeenCalledWith('chatwoot', []);
+ });
+
+ // Activation is untouched: it must still write the instance's config, not an empty slice.
+ it('leaves the activation path writing the instance config', async () => {
+ const { svc, setPluginSessionConfig } = build([]);
+ await svc([]).applyScopeBinding('chatwoot', 'sess-1', { baseUrl: 'https://a.example' }, true);
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot', 'sess-1', { baseUrl: 'https://a.example' });
+ });
+});
diff --git a/src/modules/integration/scope-binding.service.ts b/src/modules/integration/scope-binding.service.ts
index 1d46e42ce..fb9edc60d 100644
--- a/src/modules/integration/scope-binding.service.ts
+++ b/src/modules/integration/scope-binding.service.ts
@@ -167,8 +167,23 @@ export class ScopeBindingService implements OnApplicationBootstrap {
// instance being torn down.
const siblings = await this.instances.list(pluginId);
if (!activate && siblings.some(i => i.enabled && i.sessionScope === scope)) {
- // A sibling still binds this scope: leave the session active and its sessionConfig intact —
- // stripping either would silence the sibling until the next boot-time reconciliation.
+ // A sibling still binds this scope, so the SESSION stays active: dropping it would silence
+ // that sibling until the next boot-time reconciliation.
+ //
+ // The config slice does NOT survive with it. It is keyed by scope, so it holds whichever
+ // instance was projected last — usually the one being retired here — and once this teardown
+ // leaves a single enabled instance on the scope, dispatch re-declares the slice attributable
+ // to that survivor (PluginSandboxBridge.scopeHasAtMostOneInstance) and merges it beneath the
+ // survivor's own row. A survivor relying on a plugin default for a key it does not define
+ // was handed the retired tenant's endpoint and credentials for it — the cross-tenant collapse
+ // per-instance resolution exists to prevent, reachable by disabling or deleting one of two
+ // instances. Clearing costs the survivor nothing it owns: resolveInstanceConfig layers its
+ // own row on top either way, and boot reconciliation re-projects it.
+ //
+ // An operator's per-session override (PUT /plugins/:id/config/:sessionId) shares this slice
+ // and is cleared with it. That is the safe direction — provisioning already overwrites such
+ // an override, and a missing default beats another tenant's credential.
+ this.loader.setPluginSessionConfig(pluginId, scope, {});
return;
}
this.loader.setPluginSessionConfig(pluginId, scope, activate ? config : {});