Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): refuse to disable the engine in use at runtime
Yudhi Armyndharis2fd2152cd76e
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 91565e858..f6617b9c1 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -6013,6 +6013,11 @@ Disable a plugin.{ "success": true, "message": "Plugin disabled successfully" }```+The engine plugin `ENGINE_TYPE` selects (`baileys` or `whatsapp-web.js`) cannot be disabled at runtime:+the gateway runs that engine whatever its plugin status says. The request is refused in-band as+`{ "success": false, "message": "…" }` (still HTTP 200) and changes nothing; set `ENGINE_TYPE` and+restart to switch engines.+**Errors:** `401` · `403` · `404` unknown id---diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.tsindex 20e927403..ca51128db 100644--- a/src/modules/plugins/plugins.service.spec.ts+++ b/src/modules/plugins/plugins.service.spec.ts@@ -688,6 +688,63 @@ describe('PluginsService — disable when the plugin is not loaded', () => {});});+describe('PluginsService: disable an engine plugin', () => {+ let tmpDir: string;++ afterEach(() => fs.rmSync(tmpDir, { recursive: true, force: true }));++ const build = (status: PluginStatus, engineType: string) => {+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'owa-engine-'));+ const settings: Record<string, string> = {+ 'plugins.dir': path.join(tmpDir, 'plugins'),+ dataDir: tmpDir,+ 'engine.type': engineType,+ };+ const config = { get: (k: string) => settings[k] } as unknown as ConfigService;+ const loader = new PluginLoaderService(+ config,+ new HookManager(),+ new PluginStorageService(config),+ {} as unknown as ModuleRef,+ );+ loader.registerBuiltInPlugin(+ { id: 'baileys', name: 'Baileys', version: '1.0.0', type: PluginType.ENGINE, main: 'index.js' },+ {},+ );+ loader.getPlugin('baileys')!.status = status;+ return { loader, service: new PluginsService(loader, config) };+ };++ // The engine factory is pinned to engine.type and ignores plugin status, so a "disabled" engine kept+ // serving and was re-enabled on the next boot while the API reported it disabled.+ it.each([PluginStatus.ENABLED, PluginStatus.DISABLED])(+ 'refuses the active engine plugin in status %s and changes nothing',+ async status => {+ const { loader, service } = build(status, 'baileys');+ const disablePlugin = jest.spyOn(loader, 'disablePlugin');+ const setOperatorEnabled = jest.spyOn(loader, 'setOperatorEnabled');++ const res = await service.disable('baileys');++ expect(res.success).toBe(false);+ expect(res.message).toMatch(/engine\.type/);+ expect(disablePlugin).not.toHaveBeenCalled();+ expect(setOperatorEnabled).not.toHaveBeenCalled();+ expect(loader.getPlugin('baileys')!.status).toBe(status);+ },+ );++ // An engine other than engine.type runs no sessions, so disabling it is an ordinary request.+ it('answers a loaded engine that is not the active one the way it answers any idle plugin', async () => {+ const { service } = build(PluginStatus.DISABLED, 'whatsapp-web.js');++ await expect(service.disable('baileys')).resolves.toEqual({+ success: true,+ message: 'Plugin baileys is not enabled',+ });+ });+});+/*** Recovery for a plugin the gateway still has a registry entry for but whose code is gone — the* state the loader announces on every boot ("Reinstall it — its config and stored data are kept").diff --git a/src/modules/plugins/plugins.service.ts b/src/modules/plugins/plugins.service.tsindex 759af489f..1acafa983 100644--- a/src/modules/plugins/plugins.service.ts+++ b/src/modules/plugins/plugins.service.ts@@ -2,7 +2,7 @@ import { Injectable, NotFoundException, BadRequestException, ConflictException,import { ConfigService } from '@nestjs/config';import * as fs from 'fs';import * as path from 'path';-import { PluginLoaderService, PluginStatus, resolvePluginEntryPath } from '../../core/plugins';+import { PluginLoaderService, PluginStatus, PluginType, resolvePluginEntryPath } from '../../core/plugins';import { pluginUpdateBackupDirName, pluginUpdateStagingDirName } from '../../core/plugins';import type { PluginConfigSchema } from '../../core/plugins';import { PluginDto } from './dto/plugin.dto';@@ -165,6 +165,21 @@ export class PluginsService {return { success: true, message: `Plugin ${id} is not loaded; it will not be enabled on boot` };}+ if (+ plugin.manifest.type === PluginType.ENGINE &&+ id === (this.configService.get<string>('engine.type') ?? 'whatsapp-web.js')+ ) {+ // The engine factory is pinned to engine.type and never reads plugin status, and boot re-enables+ // that engine regardless: "disabling" it here reported success while sessions kept starting on+ // it. Refuse it in the same shape enable uses for a non-active engine. Kept at this layer, not in+ // the lifecycle, because shutdown and unload must still tear engines down. An engine that is not+ // engine.type runs nothing, so it takes the ordinary path below.+ return {+ success: false,+ message: `Engine "${id}" cannot be disabled at runtime. Set engine.type and restart to switch engines.`,+ };+ }+if (plugin.status !== PluginStatus.ENABLED) {// Clear the decision here too: a plugin sitting in ERROR after a failed restore is not ENABLED,// and disabling it must stop the gateway retrying it on every boot.