Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(integration): keep all-sessions activation for scoped keys
Yudhi Armyndharis4df5839ba47f
diff --git a/src/modules/integration/integration-instance.controller.spec.ts b/src/modules/integration/integration-instance.controller.spec.tsindex 7731195a5..737a2a1f0 100644--- a/src/modules/integration/integration-instance.controller.spec.ts+++ b/src/modules/integration/integration-instance.controller.spec.ts@@ -1024,3 +1024,55 @@ describe('IntegrationInstanceController update audit', () => {expect(audit.logInfo).not.toHaveBeenCalledWith(AuditAction.INTEGRATION_INSTANCE_UPDATED, expect.anything());});});++describe('a session-restricted key provisioning an instance', () => {+ it.each(['create', 'config', 'move', 'disable', 'remove'] as const)(+ 'leaves the operator all-sessions activation in place on %s',+ async operation => {+ const plugin = {+ manifest: { id: 'adapter', ingress: [{ route: 'event' }], permissions: ['webhook:ingress'] },+ activeSessions: ['*'],+ };+ let row = { id: 'adapter:a', pluginId: 'adapter', instanceId: 'a', enabled: true, sessionScope: 'A', config: {} };+ let present = true;+ const instances = {+ resolve: jest.fn(() => Promise.resolve(present ? row : null)),+ create: jest.fn(() => Promise.resolve(row)),+ update: jest.fn((_plugin: string, _instance: string, patch: Partial<typeof row>) => {+ row = { ...row, ...Object.fromEntries(Object.entries(patch).filter(([, value]) => value !== undefined)) };+ return Promise.resolve(row);+ }),+ remove: jest.fn(() => {+ present = false;+ return Promise.resolve(true);+ }),+ list: jest.fn(() => Promise.resolve(present ? [row] : [])),+ maskedView: (instance: unknown) => instance,+ } as unknown as PluginInstanceService;+ const loader = {+ getPlugin: () => plugin,+ setPluginSessionConfig: jest.fn(),+ setPluginSessions: (_id: string, active: string[]) => {+ plugin.activeSessions = active;+ },+ } as unknown as PluginLoaderService;+ const audit = { logInfo: jest.fn(), logWarn: jest.fn() } as unknown as AuditService;+ const controller = new IntegrationInstanceController(+ instances,+ loader,+ audit,+ new ScopeBindingService(instances, loader, audit, sessions),+ );+ const key = { allowedSessions: ['A', 'C'], allowedChats: [] } as unknown as ApiKey;++ if (operation === 'create') await controller.create('adapter', { instanceId: 'a', sessionScope: 'A' }, key);+ if (operation === 'config')+ await controller.patch('adapter', 'a', { config: { endpoint: 'https://a.invalid' } }, key);+ if (operation === 'move') await controller.patch('adapter', 'a', { sessionScope: 'C' }, key);+ if (operation === 'disable') await controller.patch('adapter', 'a', { enabled: false }, key);+ if (operation === 'remove') await controller.remove('adapter', 'a', key);++ expect(plugin.activeSessions).toContain('*');+ },+ );+});diff --git a/src/modules/integration/integration-instance.controller.ts b/src/modules/integration/integration-instance.controller.tsindex 37838f979..ea442f018 100644--- a/src/modules/integration/integration-instance.controller.ts+++ b/src/modules/integration/integration-instance.controller.ts@@ -27,6 +27,10 @@ import { ApiTags, ApiResponse } from '@nestjs/swagger';const isWildcardScope = (scope: string | null): boolean => !scope || scope === '*';+// A session-restricted key narrows nothing outside its own sessions: binding or releasing its instance+// must not retire the plugin's all-sessions activation, which an unrestricted operator set.+const isScopedKey = (apiKey: ApiKey | undefined): boolean => (apiKey?.allowedSessions?.length ?? 0) > 0;+/*** What the instance has put into the plugin's base config: its config while it is enabled on the wildcard* scope, nothing otherwise. That merged config is where a secret the instance stops contributing would stay@@ -79,7 +83,9 @@ export class IntegrationInstanceController {void this.audit.logInfo(AuditAction.INTEGRATION_INSTANCE_CREATED, {metadata: { pluginId, instanceId: dto.instanceId },});- await this.scopeBinding.applyScopeBinding(pluginId, inst.sessionScope, inst.config ?? {}, inst.enabled);+ await this.scopeBinding.applyScopeBinding(pluginId, inst.sessionScope, inst.config ?? {}, inst.enabled, {+ additive: isScopedKey(apiKey),+ });return this.view(inst, routes, /* reveal */ true);} catch (err) {if (err instanceof InstanceExistsError) throw new ConflictException(err.message);@@ -169,10 +175,12 @@ export class IntegrationInstanceController {// only hand the running plugin a config without its secrets for the length of one call.await this.scopeBinding.applyScopeBinding(pluginId, previousScope, {}, false, {previousConfig: isWildcardScope(updated.sessionScope) ? undefined : previousConfig,+ additive: isScopedKey(apiKey),});}await this.scopeBinding.applyScopeBinding(pluginId, updated.sessionScope, updated.config ?? {}, updated.enabled, {previousConfig,+ additive: isScopedKey(apiKey),});// Audit the successful update with the CHANGED FIELD NAMES only — never the values: a config patch// can carry credentials, and audit metadata is not a credential store.@@ -201,7 +209,10 @@ export class IntegrationInstanceController {// Delete the row FIRST, then tear down its scope: for a wildcard/null scope the teardown lists the// remaining instances to decide whether to retire '*', and that check must not count this instance.await this.instances.remove(pluginId, instanceId);- await this.scopeBinding.applyScopeBinding(pluginId, scope, {}, false, { previousConfig: contributedConfig(inst) });+ await this.scopeBinding.applyScopeBinding(pluginId, scope, {}, false, {+ previousConfig: contributedConfig(inst),+ additive: isScopedKey(apiKey),+ });void this.audit.logInfo(AuditAction.INTEGRATION_INSTANCE_DELETED, { metadata: { pluginId, instanceId } });}