Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(storage): reject incomplete media exports
Yudhi Armyndharis8c433c3be441
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a3a152d99..65ee0c426 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Fixed++- Refuse media exports and file counts when local directories cannot be fully enumerated.+## [0.25.0] - 2026-10-15### Addeddiff --git a/src/common/storage/storage-local-files.ts b/src/common/storage/storage-local-files.tsindex c71cee96c..7d46e9f3f 100644--- a/src/common/storage/storage-local-files.ts+++ b/src/common/storage/storage-local-files.ts@@ -31,7 +31,7 @@ export async function listLocalFiles(localPath: string): Promise<string[]> {/*** Full local enumeration as a stream — no count cap. Async + iterative (a work queue, not* recursion) so a deep/wide media tree can't block the event loop or stack-overflow; still- * bounded by the max directory depth so a pathological tree can't descend unbounded.+ * bounded by the max directory depth. An incomplete traversal fails instead of hiding files.*/export async function* iterateLocalFiles(localPath: string, prefix = ''): AsyncGenerator<string> {// Iterative BFS: a queue of [relativeDir, depth] avoids unbounded call-stack growth. A prefix@@ -42,14 +42,17 @@ export async function* iterateLocalFiles(localPath: string, prefix = ''): AsyncGwhile (queue.length > 0) {const { dir, depth } = queue.shift()!;- if (depth >= LOCAL_TRAVERSAL_MAX_DEPTH) continue;+ if (depth >= LOCAL_TRAVERSAL_MAX_DEPTH) {+ throw new Error(`Storage directory exceeds the maximum traversal depth: ${dir}`);+ }const fullPath = path.join(localPath, dir);let entries: fs.Dirent[];try {entries = await fs.promises.readdir(fullPath, { withFileTypes: true });- } catch {- continue; // dir vanished or unreadable — skip rather than abort the whole traversal+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;+ continue; // A directory removed during enumeration has no files left to export.}for (const entry of entries) {diff --git a/test/storage-export-streaming.e2e-spec.ts b/test/storage-export-streaming.e2e-spec.tsindex a2f2c993d..c501430ae 100644--- a/test/storage-export-streaming.e2e-spec.ts+++ b/test/storage-export-streaming.e2e-spec.ts@@ -2,8 +2,82 @@jest.unmock('archiver');import { randomBytes } from 'crypto';+import * as fs from 'fs';+import * as os from 'os';+import * as path from 'path';import { Readable } from 'stream';+import { ConfigService } from '@nestjs/config';import { createExportStream, ExportFileSource, importFromStream } from '../src/common/storage/storage-transfer';+import { StorageService } from '../src/common/storage/storage.service';++describe('Local storage export completeness', () => {+ let localPath: string;+ let storage: StorageService;++ beforeEach(() => {+ localPath = fs.mkdtempSync(path.join(os.tmpdir(), 'openwa-export-'));+ storage = new StorageService({+ get: (key: string) => ({ 'storage.type': 'local', 'storage.localPath': localPath })[key],+ } as unknown as ConfigService);+ });++ afterEach(() => {+ jest.restoreAllMocks();+ storage.onModuleDestroy();+ fs.rmSync(localPath, { recursive: true, force: true });+ });++ it.each(['EACCES', 'EIO'])('refuses an export and count when directory enumeration fails with %s', async code => {+ await storage.putFile('nested/media.bin', Buffer.from('preserve this file'));+ const error = Object.assign(new Error(`Cannot enumerate storage: ${code}`), { code });+ jest.spyOn(fs.promises, 'readdir').mockRejectedValue(error);++ await expect(storage.createExportStream()).rejects.toBe(error);+ await expect(storage.getFileCount()).rejects.toBe(error);+ expect((await storage.getFile('nested/media.bin')).toString()).toBe('preserve this file');+ });++ it('refuses an export and count when an accepted key exceeds the traversal depth', async () => {+ const key = [...Array<string>(20).fill('nested'), 'media.bin'].join('/');+ await storage.putFile(key, Buffer.from('deep media'));++ expect((await storage.getFile(key)).toString()).toBe('deep media');+ await expect(storage.createExportStream()).rejects.toThrow(/maximum traversal depth/);+ await expect(storage.getFileCount()).rejects.toThrow(/maximum traversal depth/);+ });++ it('still treats a missing local fallback directory as empty', async () => {+ fs.rmSync(localPath, { recursive: true });+ const logger = { warn: jest.fn(), error: jest.fn(), log: jest.fn(), debug: jest.fn() };++ expect(await storage.getFileCount()).toEqual({ count: 0, sizeBytes: 0 });+ const output = await storage.createExportStream();+ await expect(importFromStream(output, jest.fn(), logger as never)).resolves.toEqual({ imported: 0, failed: 0 });+ });++ it('exports every readable file with its original bytes', async () => {+ const source = new Map([+ ['root.bin', Buffer.from('root media')],+ ['nested/media.bin', Buffer.from('nested media')],+ ]);+ for (const [key, bytes] of source) await storage.putFile(key, bytes);+ const imported = new Map<string, Buffer>();+ const logger = { warn: jest.fn(), error: jest.fn(), log: jest.fn(), debug: jest.fn() };++ const output = await storage.createExportStream();+ await expect(+ importFromStream(+ output,+ (key, bytes) => {+ imported.set(key, bytes);+ return Promise.resolve();+ },+ logger as never,+ ),+ ).resolves.toEqual({ imported: source.size, failed: 0 });+ expect(imported).toEqual(source);+ });+});/*** The storage export runs against the real archiver here (the unit config stubs it). An export of a