Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): serialize an upload with an uninstall or update
Yudhi Armyndharisaf5b5053e446
diff --git a/src/modules/plugins/plugins.controller.ts b/src/modules/plugins/plugins.controller.tsindex ef548ff9d..cbe3544d2 100644--- a/src/modules/plugins/plugins.controller.ts+++ b/src/modules/plugins/plugins.controller.ts@@ -74,8 +74,8 @@ export class PluginsController {@ApiResponse({ status: 400, description: 'Invalid package' })@ApiResponse({ status: 409, description: 'Plugin already installed' })@ApiResponse({ status: 413, description: 'Package exceeds the 5 MB upload limit' })- install(@UploadedFile() file: { buffer?: Buffer }): PluginDto {- return this.pluginsService.install(file);+ async install(@UploadedFile() file: { buffer?: Buffer }): Promise<PluginDto> {+ return this.pluginsService.installPackage(file);}@Post('install-url')diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.tsindex 2bccfdcad..f7286e0f4 100644--- a/src/modules/plugins/plugins.service.spec.ts+++ b/src/modules/plugins/plugins.service.spec.ts@@ -68,6 +68,45 @@ describe('PluginsService — install / uninstall (real loader + disk)', () => {expect(() => service.install({ buffer: Buffer.alloc(0) })).toThrow(/no plugin file/i);});+ it('installPackage installs like install and refuses an empty upload', async () => {+ await expect(service.installPackage({ buffer: Buffer.alloc(0) })).rejects.toThrow(/no plugin file/i);++ const dto = await service.installPackage({ buffer: pkg() });++ expect(dto.id).toBe('svc-plg');+ expect(loader.getPlugin('svc-plg')).toBeDefined();+ });++ // An uninstall awaits the plugin's takeovers before it removes the files, and an upload that landed in+ // that window wrote a directory the teardown then deleted from under the freshly loaded plugin.+ it('installPackage waits for an uninstall of the same id that is still running', async () => {+ service.install({ buffer: pkg() });+ let finish!: () => void;+ jest.spyOn(service as unknown as { uninstallInner: () => Promise<unknown> }, 'uninstallInner').mockImplementation(+ () =>+ new Promise(resolve => {+ finish = () => resolve({ success: true, message: 'ok' });+ }),+ );+ const install = jest.spyOn(service, 'install');++ const uninstalling = service.uninstall('svc-plg');+ const reinstalling = service.installPackage({ buffer: pkg() });+ const outcome = reinstalling.then(+ () => 'installed',+ (error: Error) => error.message,+ );+ await new Promise(resolve => setImmediate(resolve));+ expect(install).not.toHaveBeenCalled();++ finish();+ await uninstalling;++ // The mocked uninstall removed nothing, so the install that ran after it finds the plugin and refuses.+ await expect(outcome).resolves.toMatch(/already installed/i);+ expect(install).toHaveBeenCalledTimes(1);+ });+it('rejects a duplicate install (already installed)', () => {service.install({ buffer: pkg() });expect(() => service.install({ buffer: pkg() })).toThrow(/already installed/i);diff --git a/src/modules/plugins/plugins.service.ts b/src/modules/plugins/plugins.service.tsindex c28f367db..7fcebc708 100644--- a/src/modules/plugins/plugins.service.ts+++ b/src/modules/plugins/plugins.service.ts@@ -412,11 +412,21 @@ export class PluginsService {* means the package was substituted in transit and the install fails closed.*/async installFromUrl(url: string): Promise<PluginDto> {- const buffer = await this.downloadPackage(url);- // Peek the id (the SSRF download stays outside the lock) so the install — which writes the plugin- // directory — is serialized against any concurrent uninstall/update of the same id.- const { manifest } = parsePluginPackage(buffer);- return this.serialize(manifest.id, () => Promise.resolve(this.install({ buffer })));+ // The SSRF download stays outside the lock; the install itself takes it.+ return this.installPackage({ buffer: await this.downloadPackage(url) });+ }++ /**+ * {@link install} under the same per-id lock as uninstall and update. The id is peeked from the package+ * first, so an install (which writes the plugin directory) cannot interleave with a teardown of that+ * plugin that is still awaiting its takeovers, and then remove the files the install just wrote.+ */+ async installPackage(file?: { buffer?: Buffer }): Promise<PluginDto> {+ if (!file?.buffer?.length) {+ throw new BadRequestException('No plugin file uploaded');+ }+ const { manifest } = parsePluginPackage(file.buffer);+ return this.serialize(manifest.id, () => Promise.resolve(this.install(file)));}/**