Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): check package size before reading the manifest (#1790)
Yudhi Armyndharis752cae58c339
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bf0abb252..5f867d1e7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Enforce the plugin package size limit before reading `manifest.json`, and reject archive entries whose path starts with `/`.- Refuse Baileys animated stickers over 500 frames and run at most two sticker conversions at once.- Stop pending webhook deliveries from holding over-cap inline media already omitted from their payload.- Resolve `./data/...` paths from `.env.generated` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs.diff --git a/src/modules/plugins/plugin-installer.spec.ts b/src/modules/plugins/plugin-installer.spec.tsindex d2fd8c23c..ce494aa83 100644--- a/src/modules/plugins/plugin-installer.spec.ts+++ b/src/modules/plugins/plugin-installer.spec.ts@@ -1,4 +1,5 @@import AdmZip from 'adm-zip';+import zlib from 'zlib';import { BadRequestException } from '@nestjs/common';import { parsePluginPackage } from './plugin-installer';@@ -38,9 +39,9 @@ function zipWithCorruptEntry(target: string): Buffer {/*** Build a plugin zip whose `target` entry declares uncompressed size = 0 in BOTH the central- * directory and the local header, while the entry still carries real (deflated) content. adm-zip- * only sets zlib `maxOutputLength` when the declared size is > 0, so without a bound this entry- * inflates with no cap — the zip-bomb residual gap (a lying-header entry).+ * directory and the local header, while the entry still carries real (deflated) content. The+ * declared-size guard counts it as 0, so only readEntryData's `maxOutputLength` cap and the running+ * actual-bytes total bound what it inflates to.*/function zipWithLyingZeroSizeEntries(targets: Record<string, string>): Buffer {const z = new AdmZip();@@ -244,9 +245,61 @@ describe('parsePluginPackage', () => {expect(() => parsePluginPackage(zipOf(files), { maxEntries: 3, maxTotalBytes: 1e9 })).toThrow(/too many/i);});+ /** Assert the archive is rejected with no entry inflated and the manifest text never parsed. */+ function expectRejectedUnread(buf: Buffer, maxTotalBytes: number, manifest: string, message: RegExp): void {+ const inflate = jest.spyOn(zlib, 'inflateRawSync');+ const parse = jest.spyOn(JSON, 'parse');+ try {+ expect(() => parsePluginPackage(buf, { maxEntries: 100, maxTotalBytes })).toThrow(message);+ expect(inflate).not.toHaveBeenCalled();+ expect(parse.mock.calls.filter(([text]) => text === manifest)).toHaveLength(0);+ } finally {+ inflate.mockRestore();+ parse.mockRestore();+ }+ }+it('rejects an archive that exceeds the size limit (before decompressing)', () => {- const buf = zipOf({ 'manifest.json': JSON.stringify(validManifest), 'index.js': 'a'.repeat(100) });- expect(() => parsePluginPackage(buf, { maxEntries: 100, maxTotalBytes: 10 })).toThrow(/size limit/i);+ const manifest = JSON.stringify(validManifest);+ const buf = zipOf({ 'manifest.json': manifest, 'index.js': 'a'.repeat(100) });+ expectRejectedUnread(buf, 10, manifest, /size limit/i);+ });++ it.each([+ ['manifest.json', /size limit/i],+ ['/manifest.json', /unsafe path/i],+ ['//manifest.json', /unsafe path/i],+ ['pkg/manifest.json', /size limit/i],+ ])('rejects an oversized %s without inflating or parsing it', (name, message) => {+ // The manifest honestly declares more than the limit. adm-zip caps inflation at the declared+ // size, not at maxTotalBytes, so reading it first would decompress and JSON-parse the whole thing.+ // adm-zip strips a leading slash on addFile, so slash-led names are written into the raw bytes+ // over a placeholder of the same length (the CRC does not cover names).+ const manifest = JSON.stringify(validManifest) + ' '.repeat(64 * 1024);+ const dir = name.slice(0, -'manifest.json'.length);+ const placeholder = dir.replace(/^\/+/, m => 'X'.repeat(m.length));+ const buf = Buffer.from(zipOf({ [`${placeholder}manifest.json`]: manifest, [`${placeholder}index.js`]: 'x' }));+ for (const file of ['manifest.json', 'index.js']) {+ const from = Buffer.from(placeholder + file);+ for (let at = buf.indexOf(from); at !== -1; at = buf.indexOf(from, at + 1)) buf.write(dir + file, at);+ }+ expect(new AdmZip(buf).getEntries().map(e => e.entryName)).toContain(name);+ expectRejectedUnread(buf, 1024, manifest, message);+ });++ it('rejects a stored manifest larger than its declared size, without inflating or parsing it', () => {+ // adm-zip returns a STORED entry at its real length, so a central header that declares 10 bytes+ // would slip past the declared-size check and hand the whole manifest to JSON.parse.+ const manifest = JSON.stringify(validManifest) + ' '.repeat(64 * 1024);+ const z = new AdmZip();+ z.addFile('manifest.json', Buffer.from(manifest));+ z.addFile('index.js', Buffer.from('x'));+ z.getEntries().find(e => e.entryName === 'manifest.json')!.header.method = 0;+ const buf = Buffer.from(z.toBuffer());+ const cen = buf.lastIndexOf('manifest.json') - 46; // the central directory comes last+ expect(buf.readUInt32LE(cen)).toBe(0x02014b50);+ buf.writeUInt32LE(10, cen + 24); // declared uncompressed size+ expectRejectedUnread(buf, 1024, manifest, /corrupt or too large/i);});it('rejects a corrupt entry with a clean 400, not an uncaught decompression error / 500', () => {@@ -258,8 +311,8 @@ describe('parsePluginPackage', () => {});it('rejects a lying size=0 entry whose actual content exceeds the cap (no unbounded inflation)', () => {- // adm-zip skips zlib `maxOutputLength` when declared size is 0, so this entry would inflate with- // NO cap. The declared aggregate (0 + tiny manifest + index) passes the pre-check; only the+ // readEntryData inflates a size=0 entry itself, with zlib `maxOutputLength` set to the cap. The+ // declared aggregate (0 + tiny manifest + index) passes the pre-check; only the// actual-bytes bound stops the lying entry. Actual content (500B) exceeds the per-entry cap (200B)// → ERR_BUFFER_TOO_LARGE → BadRequestException.const buf = zipWithLyingZeroSizeEntry('big.bin', 'A'.repeat(500));diff --git a/src/modules/plugins/plugin-installer.ts b/src/modules/plugins/plugin-installer.tsindex 7a9a63052..17e3b6811 100644--- a/src/modules/plugins/plugin-installer.ts+++ b/src/modules/plugins/plugin-installer.ts@@ -18,12 +18,13 @@ export interface PackageLimits {export const DEFAULT_PACKAGE_LIMITS: PackageLimits = { maxEntries: 200, maxTotalBytes: 20 * 1024 * 1024 };/**- * Decompress one zip entry with a hard cap on actual output bytes. adm-zip only forwards zlib- * `maxOutputLength` when the entry's declared uncompressed size is positive, so an entry that lies- * about being empty (header.size = 0) would otherwise inflate with NO cap — a memory-exhaustion- * vector. For that case we inflate bounded ourselves; every other path is left to `getData()` (a- * corrupt/CRC mismatch or a lying-small header throws `BAD_CRC` / `ERR_BUFFER_TOO_LARGE`, which the- * caller catches and maps to a clean 400).+ * Decompress one zip entry with a hard cap on actual output bytes. An entry that lies about being+ * empty (header.size = 0) is inflated here with zlib `maxOutputLength` set to the caller's cap.+ * Every other entry goes through `getData()`: adm-zip caps a DEFLATED entry at its declared size,+ * but copies a STORED entry at its real length whatever the header says, so output larger than the+ * declared size is rejected here. That keeps every positive-size entry within the declared sum the+ * caller has already checked. A corrupt entry, a CRC mismatch, or a lying-small header throws, and+ * the caller maps that to a clean 400.*/function readEntryData(entry: AdmZip.IZipEntry, maxBytes: number): Buffer {if (entry.header.size === 0 && entry.header.compressedSize > 0) {@@ -33,7 +34,9 @@ function readEntryData(entry: AdmZip.IZipEntry, maxBytes: number): Buffer {// lying size=0 entry cannot grow unbounded in memory before we reject the archive.return zlib.inflateRawSync(compressed, { maxOutputLength: maxBytes });}- return entry.getData();+ const data = entry.getData();+ if (data.length > entry.header.size) throw new Error('Entry is larger than its declared size');+ return data;}export interface ParsedPackage {@@ -62,6 +65,10 @@ export function parsePluginPackage(buffer: Buffer, limits: PackageLimits = DEFAUif (files.length === 0) throw new BadRequestException('The archive is empty');if (files.length > limits.maxEntries) throw new BadRequestException('The archive has too many files');+ // A leading slash makes path.posix.dirname return '/' or '//', so the prefix below would not match+ // the manifest's own name and its declared size would escape the size guard.+ const absolute = files.find(e => e.entryName.startsWith('/'));+ if (absolute) throw new BadRequestException(`Unsafe path in archive: ${absolute.entryName}`);// Package root = directory of the shallowest manifest.json (handles flat and single-folder zips).const manifestEntry = files@@ -71,6 +78,13 @@ export function parsePluginPackage(buffer: Buffer, limits: PackageLimits = DEFAUconst dir = path.posix.dirname(manifestEntry.entryName);const prefix = dir === '.' ? '' : dir + '/';+ // Size guard FIRST, off the declared header sizes, so an archive that declares more than the limit+ // is rejected before any entry, the manifest included, is decompressed. An entry that declares+ // size 0 adds nothing here; readEntryData still inflates it, capped at maxTotalBytes.+ const packaged = files.filter(e => !prefix || e.entryName.startsWith(prefix));+ const declared = packaged.reduce((sum, e) => sum + e.header.size, 0);+ if (declared > limits.maxTotalBytes) throw new BadRequestException('The archive contents exceed the size limit');+let manifestRaw: Buffer;try {manifestRaw = readEntryData(manifestEntry, limits.maxTotalBytes);@@ -96,11 +110,6 @@ export function parsePluginPackage(buffer: Buffer, limits: PackageLimits = DEFAU}const manifest = parsed;- // Size guard FIRST, off the declared header sizes, so a zip bomb is rejected before we decompress.- const packaged = files.filter(e => !prefix || e.entryName.startsWith(prefix));- const declared = packaged.reduce((sum, e) => sum + e.header.size, 0);- if (declared > limits.maxTotalBytes) throw new BadRequestException('The archive contents exceed the size limit');-const entries: { relPath: string; data: Buffer }[] = [];// 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