Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): answer 400 for a catalog with a non-object entry

Yudhi Armyndharis1226d46e952f
diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.ts
index fdc9c072c..7e921dc67 100644
--- a/src/modules/plugins/plugins.service.spec.ts
+++ b/src/modules/plugins/plugins.service.spec.ts
@@ -950,3 +950,35 @@ describe('PluginsService — recovering a plugin whose code went missing', () =>
expect(fs.existsSync(path.join(pluginsDir, 'svc-plg', 'legacy.js'))).toBe(true);
});
});
+
+describe('PluginsService remote catalog', () => {
+ const fetchMock = fetchSafeBuffer as unknown as jest.Mock;
+ const service = (): PluginsService => {
+ const config = {
+ get: (k: string) => (k === 'plugins.catalogUrl' ? 'https://catalog.example/plugins.json' : undefined),
+ } as unknown as ConfigService;
+ return new PluginsService({ getAllPlugins: () => [] } as unknown as PluginLoaderService, config);
+ };
+ const serveOnce = (json: string): void => {
+ fetchMock.mockImplementationOnce(() => Promise.resolve(Buffer.from(json)));
+ };
+
+ it('annotates a well-formed catalog', async () => {
+ serveOnce('[{"id":"a","name":"A","version":"1.0.0"}]');
+ await expect(service().getCatalog()).resolves.toEqual([
+ { id: 'a', name: 'A', version: '1.0.0', installed: false, installedVersion: null, updateAvailable: false },
+ ]);
+ });
+
+ it.each(['[null]', '[{"id":"a","name":"A","version":"1.0.0"},5]', '[[]]'])(
+ 'answers 400, not 500, for a catalog with a non-object entry (%s)',
+ async json => {
+ serveOnce(json);
+ const err: unknown = await service()
+ .getCatalog()
+ .catch((e: unknown) => e);
+ expect(err).toBeInstanceOf(BadRequestException);
+ expect((err as BadRequestException).message).toMatch(/Invalid plugin catalog JSON/);
+ },
+ );
+});
diff --git a/src/modules/plugins/plugins.service.ts b/src/modules/plugins/plugins.service.ts
index db505b6ba..09ca50383 100644
--- a/src/modules/plugins/plugins.service.ts
+++ b/src/modules/plugins/plugins.service.ts
@@ -458,6 +458,9 @@ export class PluginsService {
try {
const parsed: unknown = JSON.parse(raw.toString('utf8'));
if (!Array.isArray(parsed)) throw new Error('catalog is not a JSON array');
+ if (!parsed.every(e => typeof e === 'object' && e !== null && !Array.isArray(e))) {
+ throw new Error('catalog entries must be JSON objects');
+ }
entries = parsed as CatalogEntry[];
} catch (error) {
throw new BadRequestException(