Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(integration): validate an instance PATCH before writing enabled
Yudhi Armyndhariscc213677eb03
diff --git a/src/modules/integration/integration-instance.controller.spec.ts b/src/modules/integration/integration-instance.controller.spec.tsindex 619aa6a92..ff667b61c 100644--- a/src/modules/integration/integration-instance.controller.spec.ts+++ b/src/modules/integration/integration-instance.controller.spec.ts@@ -1,4 +1,4 @@-import { ForbiddenException, NotFoundException } from '@nestjs/common';+import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common';import { IntegrationInstanceController } from './integration-instance.controller';import { PluginInstanceService } from './plugin-instance.service';import { PluginLoaderService } from '../../core/plugins/plugin-loader.service';@@ -728,13 +728,14 @@ describe('IntegrationInstanceController reveal masking', () => {// values, since a config patch can carry credentials and audit metadata is not a credential store.describe('IntegrationInstanceController update audit', () => {function build() {+ const setPluginSessions = jest.fn();const loader = {getPlugin: jest.fn().mockReturnValue({manifest: { id: 'chatwoot-adapter', ingress: [{ route: 'chatwoot' }], permissions: ['webhook:ingress'] },activeSessions: [],}),setPluginSessionConfig: jest.fn(),- setPluginSessions: jest.fn(),+ setPluginSessions,updatePluginConfig: jest.fn(),} as unknown as PluginLoaderService;const audit = { logInfo: jest.fn(), logWarn: jest.fn() };@@ -750,10 +751,12 @@ describe('IntegrationInstanceController update audit', () => {createdAt: new Date(0),updatedAt: new Date(0),};+ const setEnabled = jest.fn().mockResolvedValue({ ...instance, enabled: false });+ const update = jest.fn().mockResolvedValue({ ...instance, config: { apiToken: 'rotated-token' } });const instances = {resolve: jest.fn().mockResolvedValue(instance),- setEnabled: jest.fn().mockResolvedValue({ ...instance, enabled: false }),- update: jest.fn().mockResolvedValue({ ...instance, config: { apiToken: 'rotated-token' } }),+ setEnabled,+ update,list: jest.fn().mockResolvedValue([]),maskedView: (i: unknown) => i,} as unknown as PluginInstanceService;@@ -763,7 +766,7 @@ describe('IntegrationInstanceController update audit', () => {audit as unknown as AuditService,new ScopeBindingService(instances, loader, audit as unknown as AuditService, sessions),);- return { controller, audit, instances };+ return { controller, audit, instances, setEnabled, update, setPluginSessions };}it('emits INTEGRATION_INSTANCE_UPDATED (INFO) on a successful patch with clean metadata', async () => {@@ -800,4 +803,20 @@ describe('IntegrationInstanceController update audit', () => {await expect(controller.patch('chatwoot-adapter', 'acct1', { enabled: false })).rejects.toThrow(NotFoundException);expect(audit.logInfo).not.toHaveBeenCalledWith(AuditAction.INTEGRATION_INSTANCE_UPDATED, expect.anything());});++ // update() can refuse the config with a 400 (a masked secret it cannot restore). Nothing may be+ // written before that: an `enabled` already saved would stay flipped in the row while the scope+ // binding and the audit entry are skipped, so the DB and the runtime disagree after a failed PATCH.+ it('persists nothing when the config is rejected, even with enabled in the same body', async () => {+ const { controller, audit, setEnabled, update, setPluginSessions } = build();+ update.mockRejectedValue(new BadRequestException('re-enter the remaining secret values'));++ await expect(+ controller.patch('chatwoot-adapter', 'acct1', { enabled: false, config: { apiToken: '***' } }),+ ).rejects.toThrow(BadRequestException);++ expect(setEnabled).not.toHaveBeenCalled();+ expect(setPluginSessions).not.toHaveBeenCalled();+ expect(audit.logInfo).not.toHaveBeenCalledWith(AuditAction.INTEGRATION_INSTANCE_UPDATED, expect.anything());+ });});diff --git a/src/modules/integration/integration-instance.controller.ts b/src/modules/integration/integration-instance.controller.tsindex 4829fcdd6..998d72faf 100644--- a/src/modules/integration/integration-instance.controller.ts+++ b/src/modules/integration/integration-instance.controller.ts@@ -126,7 +126,8 @@ export class IntegrationInstanceController {let inst: PluginInstance | null = await this.resolveVisible(pluginId, instanceId, apiKey);if (dto.sessionScope !== undefined) this.assertScopeWritable(apiKey, dto.sessionScope);const previousScope = inst.sessionScope;- if (dto.enabled !== undefined) inst = await this.instances.setEnabled(pluginId, instanceId, dto.enabled);+ // update() first: restoring the masked config can refuse the PATCH with a 400, and it does so+ // before its own save, so a rejected body must reach it before `enabled` is written.if (dto.sessionScope !== undefined || dto.config !== undefined) {inst = await this.instances.update(pluginId,@@ -135,6 +136,7 @@ export class IntegrationInstanceController {this.schemaFor(pluginId),);}+ if (dto.enabled !== undefined) inst = await this.instances.setEnabled(pluginId, instanceId, dto.enabled);const updated = inst as PluginInstance;// If the bound session changed, tear down the OLD scope (incl. a wildcard/null scope) so it stops// firing with stale config. The new scope is (re)bound right after; teardown runs first with the new