Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): refuse archive paths that collide after Unicode folding
Yudhi Armyndharisf04c03821c5f
diff --git a/src/modules/plugins/plugin-installer.spec.ts b/src/modules/plugins/plugin-installer.spec.tsindex 66c20f200..d2fd8c23c 100644--- a/src/modules/plugins/plugin-installer.spec.ts+++ b/src/modules/plugins/plugin-installer.spec.ts@@ -205,6 +205,22 @@ describe('parsePluginPackage', () => {expect(() => parsePluginPackage(buf)).toThrow(/duplicate path/i);});+ it('rejects entries that collide only under Unicode case folding (long s folds to s)', () => {+ const other = JSON.stringify({ ...validManifest, id: 'other-id' });+ const buf = zipOf({ 'manifest.json': JSON.stringify(validManifest), 'index.js': 'x', 'manife\u017Ft.json': other });+ expect(() => parsePluginPackage(buf)).toThrow(/duplicate path/i);+ });++ it('rejects entries that differ only in Unicode normalization (NFC vs NFD)', () => {+ const buf = zipOf({+ 'manifest.json': JSON.stringify(validManifest),+ 'index.js': 'x',+ 'caf\u00e9.js': 'a',+ 'cafe\u0301.js': 'b',+ });+ expect(() => parsePluginPackage(buf)).toThrow(/duplicate path/i);+ });+it('rejects a package missing its declared main file', () => {const buf = zipOf({ 'manifest.json': JSON.stringify(validManifest), 'other.js': 'x' });expect(() => parsePluginPackage(buf)).toThrow(/missing its main file/i);diff --git a/src/modules/plugins/plugin-installer.ts b/src/modules/plugins/plugin-installer.tsindex d05ffca7f..7a9a63052 100644--- a/src/modules/plugins/plugin-installer.ts+++ b/src/modules/plugins/plugin-installer.ts@@ -102,9 +102,11 @@ export function parsePluginPackage(buffer: Buffer, limits: PackageLimits = DEFAUif (declared > limits.maxTotalBytes) throw new BadRequestException('The archive contents exceed the size limit');const entries: { relPath: string; data: Buffer }[] = [];- // Normalized paths already taken, lowercased because a case-insensitive filesystem writes- // `Manifest.json` and `manifest.json` to one file. Entries are written in order, so a repeat would- // let a later `z/../manifest.json` replace the manifest validated above.+ // Normalized paths already taken, NFC-normalized and case-folded because a case- or+ // normalization-insensitive filesystem (APFS, NTFS) writes `Manifest.json` and `manifest.json`, or+ // an NFC and an NFD spelling, to one file. Upper-then-lower also folds characters such as the long+ // s that toLowerCase() leaves alone. Entries are written in order, so a repeat would let a later+ // `z/../manifest.json` replace the manifest validated above.const seen = new Set<string>();let actualBytes = 0;for (const e of packaged) {@@ -114,7 +116,7 @@ export function parsePluginPackage(buffer: Buffer, limits: PackageLimits = DEFAUif (relPath.includes('\\') || norm.startsWith('..') || norm === '..' || path.posix.isAbsolute(norm)) {throw new BadRequestException(`Unsafe path in archive: ${e.entryName}`);}- const key = norm.toLowerCase();+ const key = norm.normalize('NFC').toUpperCase().toLowerCase();if (seen.has(key)) throw new BadRequestException(`Duplicate path in archive: ${e.entryName}`);seen.add(key);let data: Buffer;