Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): refuse a catalog entry whose fields are not text

Yudhi Armyndharis20f74f33961e
diff --git a/src/modules/plugins/catalog.spec.ts b/src/modules/plugins/catalog.spec.ts
index 89be156c2..91071d650 100644
--- a/src/modules/plugins/catalog.spec.ts
+++ b/src/modules/plugins/catalog.spec.ts
@@ -1,4 +1,4 @@
-import { compareSemver, annotateCatalog, CatalogEntry } from './catalog';
+import { compareSemver, annotateCatalog, isCatalogEntry, CatalogEntry } from './catalog';
describe('compareSemver', () => {
it('orders by major, minor, then patch', () => {
@@ -62,3 +62,28 @@ describe('annotateCatalog', () => {
expect(rcToRc[0].updateAvailable).toBe(false);
});
});
+
+describe('isCatalogEntry', () => {
+ const ok = { id: 'a', name: 'A', version: '1.0.0' };
+
+ it('accepts the required fields alone, optional fields of the right type and unknown extras', () => {
+ expect(isCatalogEntry(ok)).toBe(true);
+ expect(isCatalogEntry({ ...ok, description: 'd', keywords: ['x'], download: 'https://x/a.zip', stars: 5 })).toBe(
+ true,
+ );
+ });
+
+ it.each([
+ null,
+ 5,
+ [],
+ { ...ok, id: '' },
+ { ...ok, name: undefined },
+ { ...ok, version: 1 },
+ { ...ok, author: {} },
+ { ...ok, keywords: 'x' },
+ { ...ok, keywords: [1] },
+ ])('rejects %j', value => {
+ expect(isCatalogEntry(value)).toBe(false);
+ });
+});
diff --git a/src/modules/plugins/catalog.ts b/src/modules/plugins/catalog.ts
index 59465265f..b7caf8c08 100644
--- a/src/modules/plugins/catalog.ts
+++ b/src/modules/plugins/catalog.ts
@@ -21,6 +21,39 @@ export interface CatalogEntry {
[key: string]: unknown;
}
+const OPTIONAL_STRING_FIELDS = [
+ 'type',
+ 'status',
+ 'description',
+ 'author',
+ 'license',
+ 'minOpenWAVersion',
+ 'testedOpenWAVersion',
+ 'releasedAt',
+ 'repoUrl',
+ 'homepage',
+ 'download',
+] as const;
+
+/**
+ * Whether `value` has the shape of {@link CatalogEntry}: non-empty string id, name and version, string
+ * optional fields, and a string-array `keywords`. Extra fields are tolerated. The dashboard lists and
+ * searches these fields as text, so a value of another type would break the page that renders them.
+ */
+export function isCatalogEntry(value: unknown): value is CatalogEntry {
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;
+ const entry = value as Record<string, unknown>;
+ const text = (field: unknown): boolean => typeof field === 'string' && field !== '';
+ return (
+ text(entry.id) &&
+ text(entry.name) &&
+ text(entry.version) &&
+ OPTIONAL_STRING_FIELDS.every(key => entry[key] === undefined || typeof entry[key] === 'string') &&
+ (entry.keywords === undefined ||
+ (Array.isArray(entry.keywords) && entry.keywords.every(word => typeof word === 'string')))
+ );
+}
+
/** A catalog entry annotated with this instance's install state. */
export interface CatalogPlugin extends CatalogEntry {
installed: boolean;
diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.ts
index 25cb114d3..28de61350 100644
--- a/src/modules/plugins/plugins.service.spec.ts
+++ b/src/modules/plugins/plugins.service.spec.ts
@@ -1205,6 +1205,13 @@ describe('PluginsService remote catalog', () => {
fetchMock.mockImplementationOnce(() => Promise.resolve(Buffer.from(json)));
};
+ it('keeps unknown extra fields and every optional field of the right type', async () => {
+ serveOnce(
+ '[{"id":"a","name":"A","version":"1.0.0","description":"d","keywords":["x"],"stars":5,"download":"https://x/a.zip"}]',
+ );
+ await expect(service().getCatalog()).resolves.toMatchObject([{ id: 'a', stars: 5, keywords: ['x'] }]);
+ });
+
it('annotates a well-formed catalog', async () => {
serveOnce('[{"id":"a","name":"A","version":"1.0.0"}]');
await expect(service().getCatalog()).resolves.toEqual([
@@ -1212,15 +1219,21 @@ describe('PluginsService remote catalog', () => {
]);
});
- 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/);
- },
- );
+ it.each([
+ '[null]',
+ '[{"id":"a","name":"A","version":"1.0.0"},5]',
+ '[[]]',
+ '[{"id":"a","name":{},"version":"1.0.0"}]',
+ '[{"id":"a","name":"A","version":"1.0.0","description":7}]',
+ '[{"id":"a","name":"A"}]',
+ '[{"id":"","name":"A","version":"1.0.0"}]',
+ '[{"id":"a","name":"A","version":"1.0.0","keywords":"x"}]',
+ ])('answers 400, not 500, for a catalog with a malformed 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 24b71cb28..c7e8d01fc 100644
--- a/src/modules/plugins/plugins.service.ts
+++ b/src/modules/plugins/plugins.service.ts
@@ -16,7 +16,7 @@ import { PluginDto } from './dto/plugin.dto';
import { redactSecretConfig, restoreSecretConfig } from './redact-config';
import { parsePluginPackage } from './plugin-installer';
import { assertDownloadSha256, assertPluginInstallUrl, fetchSafeBuffer } from './plugin-download';
-import { annotateCatalog, CatalogEntry, CatalogPlugin } from './catalog';
+import { annotateCatalog, CatalogEntry, CatalogPlugin, isCatalogEntry } from './catalog';
import { redactSsrfError } from '../../common/security/ssrf-guard';
import { createLogger } from '../../common/services/logger.service';
@@ -484,10 +484,10 @@ 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');
+ if (!parsed.every(isCatalogEntry)) {
+ throw new Error('catalog entries must be JSON objects with a string id, name and version');
}
- entries = parsed as CatalogEntry[];
+ entries = parsed;
} catch (error) {
throw new BadRequestException(
`Invalid plugin catalog JSON: ${error instanceof Error ? error.message : String(error)}`,