Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): retract only values the instance set

Yudhi Armyndharis1745baa4c04d
diff --git a/src/modules/integration/integration-instance.controller.spec.ts b/src/modules/integration/integration-instance.controller.spec.ts
index 52b94e056..7731195a5 100644
--- a/src/modules/integration/integration-instance.controller.spec.ts
+++ b/src/modules/integration/integration-instance.controller.spec.ts
@@ -211,10 +211,18 @@ describe('IntegrationInstanceController provisioning bridge', () => {
enabled,
});
- /** `before` is the row PATCH reads; `after` is what update() stores; `others` are the sibling rows. */
- function patchWith(before: ReturnType<typeof row>, after: ReturnType<typeof row>, others: unknown[] = []) {
+ /**
+ * `before` is the row PATCH reads; `after` is what update() stores; `others` are the sibling rows. The
+ * plugin's base config holds what the instance projected into it, unless `base` says otherwise.
+ */
+ function patchWith(
+ before: ReturnType<typeof row>,
+ after: ReturnType<typeof row>,
+ others: unknown[] = [],
+ base: Record<string, unknown> = before.config,
+ ) {
const { loader, audit, updatePluginConfig, setPluginSessionConfig } = build();
- (loader.getPlugin as jest.Mock).mockReturnValue({ manifest, activeSessions: ['*'] });
+ (loader.getPlugin as jest.Mock).mockReturnValue({ manifest, activeSessions: ['*'], config: base });
const instances = {
resolve: jest.fn().mockResolvedValue(before),
update: jest.fn().mockResolvedValue(after),
@@ -232,8 +240,12 @@ describe('IntegrationInstanceController provisioning bridge', () => {
}
/** DELETE reads the row, removes it, then lists what is left: only `others`. */
- function deleteWith(before: ReturnType<typeof row>, others: unknown[] = []) {
- const built = patchWith(before, before, others);
+ function deleteWith(
+ before: ReturnType<typeof row>,
+ others: unknown[] = [],
+ base: Record<string, unknown> = before.config,
+ ) {
+ const built = patchWith(before, before, others, base);
const instances = (built.controller as unknown as { instances: { list: jest.Mock } }).instances;
instances.list.mockResolvedValue(others);
return built;
@@ -307,6 +319,14 @@ describe('IntegrationInstanceController provisioning bridge', () => {
expect(updatePluginConfig).toHaveBeenCalledWith('chatwoot-adapter', { apiToken: null });
});
+ it('is left alone when the plugin config no longer holds the value the instance put there', async () => {
+ const { controller, updatePluginConfig } = deleteWith(row(null, held), [], { ...held, apiToken: 'OPERATOR' });
+
+ await controller.remove('chatwoot-adapter', 'acct1');
+
+ expect(updatePluginConfig).not.toHaveBeenCalled();
+ });
+
it('is handed back as the value another enabled wildcard instance still defines', async () => {
const sibling = { ...row(null, { apiToken: 'B-TOKEN' }), instanceId: 'acct2' };
const { controller, updatePluginConfig } = deleteWith(row(null, held), [sibling]);
diff --git a/src/modules/integration/scope-binding.service.spec.ts b/src/modules/integration/scope-binding.service.spec.ts
index 44da87f19..ca87b32d6 100644
--- a/src/modules/integration/scope-binding.service.spec.ts
+++ b/src/modules/integration/scope-binding.service.spec.ts
@@ -578,14 +578,24 @@ describe('ScopeBindingService.applyScopeBinding retracts a secret an instance no
},
};
- function build() {
+ // What the earlier projection of the instance left in the plugin's base config.
+ const projected = {
+ apiToken: 'LEAKED',
+ baseUrl: 'https://crm',
+ replyDelayMs: 5,
+ crm: { key: 'K', url: 'https://x' },
+ };
+
+ function build(baseConfig: Record<string, unknown> = projected) {
const updatePluginConfig = jest.fn();
const setPluginSessionConfig = jest.fn();
const setPluginSessions = jest.fn();
const loader = {
- getPlugin: jest
- .fn()
- .mockReturnValue({ manifest: { id: 'chatwoot', configSchema: schema }, activeSessions: ['*'] }),
+ getPlugin: jest.fn().mockReturnValue({
+ manifest: { id: 'chatwoot', configSchema: schema },
+ activeSessions: ['*'],
+ config: baseConfig,
+ }),
setPluginSessionConfig,
setPluginSessions,
updatePluginConfig,
@@ -681,6 +691,22 @@ describe('ScopeBindingService.applyScopeBinding retracts a secret an instance no
expect(list).toHaveBeenCalledTimes(3); // only the '*' retirement check reads the siblings
});
+ // The base config is shared. A value the operator has set on the plugin since, or one a sibling projected
+ // after this instance, is not this instance's to take back.
+ it('leaves a base value that is no longer the one the instance put there', async () => {
+ const operator = build({ ...projected, apiToken: 'OPERATOR' });
+
+ await operator.svc.applyScopeBinding('chatwoot', null, {}, false, { previousConfig: { apiToken: 'LEAKED' } });
+ expect(operator.updatePluginConfig).not.toHaveBeenCalled();
+ expect(operator.list).toHaveBeenCalledTimes(1); // only the '*' retirement check reads the siblings
+
+ const partial = build({ ...projected, crm: { key: 'OTHER', url: 'https://x' } });
+ await partial.svc.applyScopeBinding('chatwoot', null, { baseUrl: 'https://crm' }, true, {
+ previousConfig: projected,
+ });
+ expect(partial.updatePluginConfig).toHaveBeenCalledWith('chatwoot', { apiToken: null, baseUrl: 'https://crm' });
+ });
+
it('only looks at siblings when a secret was removed', async () => {
const { svc, list, updatePluginConfig } = build();
diff --git a/src/modules/integration/scope-binding.service.ts b/src/modules/integration/scope-binding.service.ts
index b025fbc77..83b11e21f 100644
--- a/src/modules/integration/scope-binding.service.ts
+++ b/src/modules/integration/scope-binding.service.ts
@@ -1,3 +1,4 @@
+import { isDeepStrictEqual } from 'node:util';
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
@@ -285,9 +286,10 @@ export class ScopeBindingService implements OnApplicationBootstrap {
* defines (none, for a teardown). The wildcard projection only merges, so on its own it cannot delete a
* key: a secret the operator revoked, or one of an instance that was disabled, deleted or moved off the
* wildcard scope, would stay in the base config, in registry.json and in the config of every hook and
- * ingress delivery. Each is written as the value another enabled wildcard instance still defines (the
- * last by id, as boot reconciliation replays them), else as null, which is how a removed top-level
- * secret is stored there. Siblings are read only when a secret was actually removed.
+ * ingress delivery. A key is taken back only while the base config still holds the value the instance
+ * put there. It is written as the value another enabled wildcard instance still defines (the last by id,
+ * as boot reconciliation replays them), else as null, which is how a removed top-level secret is stored
+ * there. Siblings are read only when a secret was actually removed.
*/
private async retractedSecrets(
pluginId: string,
@@ -295,9 +297,16 @@ export class ScopeBindingService implements OnApplicationBootstrap {
previous: Record<string, unknown> | null | undefined,
): Promise<Record<string, unknown>> {
const before = previous ?? {};
- const properties = this.loader.getPlugin(pluginId)?.manifest.configSchema?.properties ?? {};
+ const plugin = this.loader.getPlugin(pluginId);
+ const properties = plugin?.manifest.configSchema?.properties ?? {};
const removed = Object.keys(before).filter(
- key => holdsSecret(properties[key]) && isDefined(before[key]) && !isDefined(config[key]),
+ key =>
+ holdsSecret(properties[key]) &&
+ isDefined(before[key]) &&
+ !isDefined(config[key]) &&
+ // Only what this instance put there: a value set on the plugin since, or one a sibling projected
+ // after it, is not the instance's to take back.
+ isDeepStrictEqual(plugin?.config[key], before[key]),
);
if (removed.length === 0) return {};
const siblings = (await this.instances.list(pluginId))