Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): keep an unreadable plugin registry instead of replacing it

Yudhi Armyndharis7382e525c43a
diff --git a/src/core/plugins/plugin-storage.service.spec.ts b/src/core/plugins/plugin-storage.service.spec.ts
index 5cc58397d..3df99f401 100644
--- a/src/core/plugins/plugin-storage.service.spec.ts
+++ b/src/core/plugins/plugin-storage.service.spec.ts
@@ -7,6 +7,7 @@ import * as os from 'os';
import * as path from 'path';
import { ConfigService } from '@nestjs/config';
import { PluginStorageService } from './plugin-storage.service';
+import { PluginStatus, PluginType } from './plugin.interfaces';
describe('PluginStorageService sandboxed per-plugin storage containment', () => {
let dataDir: string;
@@ -283,3 +284,44 @@ describe('PluginStorageService per-plugin storage quota', () => {
);
});
});
+
+describe('PluginStorageService unreadable registry', () => {
+ let dataDir: string;
+ let configService: ConfigService;
+
+ beforeEach(() => {
+ dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'owa-pluginreg-'));
+ fs.mkdirSync(path.join(dataDir, 'plugins'));
+ configService = { get: (k: string) => (k === 'dataDir' ? dataDir : undefined) } as unknown as ConfigService;
+ });
+
+ afterEach(() => {
+ fs.rmSync(dataDir, { recursive: true, force: true });
+ });
+
+ const writeEntry = (service: PluginStorageService): void =>
+ service.setPluginEntry({
+ id: 'other',
+ type: PluginType.EXTENSION,
+ name: 'Other',
+ version: '1.0.0',
+ status: PluginStatus.INSTALLED,
+ config: {},
+ builtIn: false,
+ installedAt: new Date(),
+ updatedAt: new Date(),
+ });
+
+ it.each([
+ ['fails to parse', '[{"id":"p","config":{"apiKey":"SECRET"},"enabledByOperator":true},]'],
+ ['is not an array', '{"id":"p","config":{"apiKey":"SECRET"}}'],
+ ])('moves a registry that %s aside instead of overwriting it on the next save', (_label, content) => {
+ fs.writeFileSync(path.join(dataDir, 'plugins', 'registry.json'), content);
+
+ writeEntry(new PluginStorageService(configService));
+
+ const aside = fs.readdirSync(path.join(dataDir, 'plugins')).filter(f => f.startsWith('registry.json.corrupt-'));
+ expect(aside).toHaveLength(1);
+ expect(fs.readFileSync(path.join(dataDir, 'plugins', aside[0]), 'utf-8')).toBe(content);
+ });
+});
diff --git a/src/core/plugins/plugin-storage.service.ts b/src/core/plugins/plugin-storage.service.ts
index edb5a3c6a..8ab9388e6 100644
--- a/src/core/plugins/plugin-storage.service.ts
+++ b/src/core/plugins/plugin-storage.service.ts
@@ -82,16 +82,29 @@ export class PluginStorageService {
try {
if (fs.existsSync(this.registryPath)) {
const content = fs.readFileSync(this.registryPath, 'utf-8');
- const entries = JSON.parse(content) as PluginRegistryEntry[];
- this.registry = new Map(entries.map(e => [e.id, e]));
+ const entries: unknown = JSON.parse(content);
+ if (!Array.isArray(entries)) throw new Error('registry is not a JSON array');
+ this.registry = new Map((entries as PluginRegistryEntry[]).map(e => [e.id, e]));
this.logger.debug(`Loaded ${this.registry.size} plugins from registry`, {
action: 'registry_loaded',
});
}
} catch (error) {
- this.logger.error('Failed to load plugin registry', String(error), {
- action: 'registry_load_failed',
- });
+ // The next save (every boot registers plugins) would replace the unreadable file with an empty
+ // registry, losing every plugin's config, secrets and enable decision for good. Move it aside
+ // first so the operator can repair and restore it.
+ const movedTo = `${this.registryPath}.corrupt-${Date.now()}`;
+ try {
+ fs.renameSync(this.registryPath, movedTo);
+ this.logger.error(`Failed to load plugin registry; moved it to ${movedTo}`, String(error), {
+ action: 'registry_load_failed',
+ movedTo,
+ });
+ } catch {
+ this.logger.error('Failed to load plugin registry', String(error), {
+ action: 'registry_load_failed',
+ });
+ }
}
}