Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): reject archives with duplicate normalized paths
Yudhi Armyndharisa84358cd0f03
diff --git a/src/modules/plugins/plugin-installer.spec.ts b/src/modules/plugins/plugin-installer.spec.tsindex 17f76b594..66c20f200 100644--- a/src/modules/plugins/plugin-installer.spec.ts+++ b/src/modules/plugins/plugin-installer.spec.ts@@ -191,6 +191,20 @@ describe('parsePluginPackage', () => {expect(() => parsePluginPackage(z.toBuffer())).toThrow(/unsafe path/i);});+ it('rejects two entries that normalize to the same path, so the validated manifest is the one written', () => {+ const z = new AdmZip();+ z.addFile('manifest.json', Buffer.from(JSON.stringify(validManifest)));+ z.addFile('index.js', Buffer.from('x'));+ z.addFile('other.json', Buffer.from(JSON.stringify({ ...validManifest, id: 'other-id' })));+ z.getEntries().find(e => e.entryName === 'other.json')!.entryName = 'z/../manifest.json';+ expect(() => parsePluginPackage(z.toBuffer())).toThrow(/duplicate path/i);+ });++ it('rejects entries that differ only in case (one file on a case-insensitive filesystem)', () => {+ const buf = zipOf({ 'manifest.json': JSON.stringify(validManifest), 'index.js': 'x', 'INDEX.js': 'y' });+ 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 8517c46f9..d05ffca7f 100644--- a/src/modules/plugins/plugin-installer.ts+++ b/src/modules/plugins/plugin-installer.ts@@ -102,6 +102,10 @@ 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.+ const seen = new Set<string>();let actualBytes = 0;for (const e of packaged) {const relPath = e.entryName.slice(prefix.length);@@ -110,6 +114,9 @@ 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();+ if (seen.has(key)) throw new BadRequestException(`Duplicate path in archive: ${e.entryName}`);+ seen.add(key);let data: Buffer;try {data = readEntryData(e, limits.maxTotalBytes);