Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): drop the registry entry after uninstall removed files

Yudhi Armyndharis0c9926e358c9
diff --git a/src/core/plugins/plugin-storage.service.spec.ts b/src/core/plugins/plugin-storage.service.spec.ts
index fa56582c7..f6811a26e 100644
--- a/src/core/plugins/plugin-storage.service.spec.ts
+++ b/src/core/plugins/plugin-storage.service.spec.ts
@@ -227,6 +227,18 @@ describe('PluginStorageService.deletePluginData (uninstall storage cleanup)', ()
expect(() => service.deletePluginData('never-stored')).not.toThrow();
});
+ it('throws when the storage dir cannot be removed, so the caller does not report it gone', async () => {
+ await service.createPluginStorage('stuck-plg').set('token', { secret: 'abc' });
+ const rm = jest.spyOn(fs, 'rmSync').mockImplementationOnce(() => {
+ throw Object.assign(new Error('EBUSY: resource busy'), { code: 'EBUSY' });
+ });
+
+ expect(() => service.deletePluginData('stuck-plg')).toThrow('EBUSY');
+
+ rm.mockRestore();
+ expect(fs.existsSync(path.join(dataDir, 'plugins', 'stuck-plg'))).toBe(true);
+ });
+
it('refuses a traversal id that resolves outside the storage root', () => {
// A real directory the escaping id would resolve to — it must survive the call.
const outside = path.join(dataDir, 'not-plugin-storage');
diff --git a/src/core/plugins/plugin-storage.service.ts b/src/core/plugins/plugin-storage.service.ts
index 4a12fd5fa..da8a97c78 100644
--- a/src/core/plugins/plugin-storage.service.ts
+++ b/src/core/plugins/plugin-storage.service.ts
@@ -244,7 +244,7 @@ export class PluginStorageService {
* Remove a plugin's entire data-storage directory (<dataDir>/plugins/<id>). Called on uninstall.
* Containment-guarded exactly like the package-dir delete in the loader: an id that resolves
* outside the storage root is refused, so only the named plugin's directory can ever be touched.
- * Best-effort — a removal failure is logged, never thrown, so the uninstall still completes.
+ * A removal failure is logged and thrown, so the uninstall fails with its registry entry intact.
*/
deletePluginData(pluginId: string): void {
const root = path.resolve(this.dataDir, 'plugins');
@@ -266,6 +266,7 @@ export class PluginStorageService {
action: 'plugin_storage_delete_failed',
error: error instanceof Error ? error.message : String(error),
});
+ throw error;
}
}
diff --git a/src/core/plugins/plugin-uninstaller.spec.ts b/src/core/plugins/plugin-uninstaller.spec.ts
index 2f4ca7c28..016b3660a 100644
--- a/src/core/plugins/plugin-uninstaller.spec.ts
+++ b/src/core/plugins/plugin-uninstaller.spec.ts
@@ -1,3 +1,6 @@
+// Spread the real fs so a test can spy on rmSync (the bare namespace is not configurable).
+jest.mock('fs', () => ({ __esModule: true, ...jest.requireActual<typeof import('fs')>('fs') }));
+
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
@@ -213,6 +216,27 @@ describe('PluginUninstaller — uninstall', () => {
expect(storage.getPluginEntry('relay')).toBeUndefined();
});
+ it('keeps the registry entry when the package folder cannot be removed, so the uninstall can be repeated', async () => {
+ const dir = writeUserPlugin('relay');
+ plugins.set('relay', loadedUserPlugin('relay', dir));
+ seedEntry(storage, 'relay');
+ const realRm = fs.rmSync.bind(fs);
+ const rm = jest.spyOn(fs, 'rmSync').mockImplementationOnce(() => {
+ throw Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' });
+ });
+
+ await expect(uninstaller.uninstallPlugin('relay')).rejects.toThrow('EACCES');
+
+ expect(fs.existsSync(dir)).toBe(true);
+ expect(storage.getPluginEntry('relay')).toBeDefined();
+
+ rm.mockImplementation(realRm);
+ await uninstaller.uninstallPlugin('relay');
+ expect(fs.existsSync(dir)).toBe(false);
+ expect(storage.getPluginEntry('relay')).toBeUndefined();
+ rm.mockRestore();
+ });
+
// The boot scan loads a folder by its manifest id, whatever the folder is called. The repeat of a
// failed uninstall has no runtime record left to read the folder from.
it('removes the package folder on the repeat of a failed uninstall when the folder is not the plugin id', async () => {
diff --git a/src/core/plugins/plugin-uninstaller.ts b/src/core/plugins/plugin-uninstaller.ts
index ef5d84030..604525169 100644
--- a/src/core/plugins/plugin-uninstaller.ts
+++ b/src/core/plugins/plugin-uninstaller.ts
@@ -75,7 +75,6 @@ export class PluginUninstaller {
// plugin can still take a chat over again, and before the registry entry goes: a store that cannot
// take the release fails the uninstall, and a repeat finds no runtime and goes straight to it.
await this.releaseTakeovers?.(pluginId);
- this.pluginStorage.deletePluginEntry(pluginId);
// A recorded directory came from the boot scan and is already contained. Without one the id is
// the only input, so it stays behind the traversal guard against the configured root.
@@ -98,9 +97,14 @@ export class PluginUninstaller {
// Drop the plugin's ctx.storage data dir. Under shipped defaults it lives INSIDE the package
// dir (already gone above), but a split-dir deployment (PLUGINS_DIR outside the data dir) would
// otherwise leak <dataDir>/plugins/<id> — persisted secrets included — on every uninstall.
- // Best-effort, and strictly that one plugin's directory.
+ // Strictly that one plugin's directory.
this.pluginStorage.deletePluginData(pluginId);
+ // Last, so a removal that failed above leaves the entry in place: the service answers a repeat only
+ // for an id it still knows, and without the entry a half-removed package would load again on the
+ // next boot as a new plugin with no way left to uninstall it.
+ this.pluginStorage.deletePluginEntry(pluginId);
+
this.unloadedDirs.delete(pluginId);
this.logger.log(`Plugin uninstalled: ${pluginId}`, { pluginId, action: 'plugin_uninstalled' });
}