Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(storage): export the whole store, not the first STORAGE_LIST_MAX_FILES

Yudhi Armyndharisb98b71b54f8d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3f2c950d7..6ed83969c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `GET /infra/storage/export` now enumerates the whole store. It listed with `listFiles()`, which stops at `STORAGE_LIST_MAX_FILES` and returns without logging or throwing, so an operator following the documented local→S3 migration — export, repoint `STORAGE_TYPE`, import — silently left media behind on the backend they were about to decommission, and the `files/count` pre-check was truncated by the same code path, so their own consistency check could not reveal it. The export now walks the uncapped `iterateFiles()`, which exists for exactly this: the cap is a per-call guard, not a completeness contract.
+
- An ingress route whose manifest omits `maxBodyBytes` is no longer unbounded by its own cap. The check compared a byte length against `undefined`, which is always false, so the per-route 413 the published contract promises was silently inert — and every accepted delivery is persisted with the body stored twice and carried into the queue, turning one forgotten manifest field into a write amplifier with no load-time error and no runtime signal. A route with no usable value now falls back to the process-wide body limit (what it was already bounded by in practice, so nothing accepted today starts failing) and the gap is logged once per route instead of passing in silence.
- A `message:sending` plugin handler that returns a payload without a usable `input` no longer turns every outbound send on that session into a 500. The gate read `.input` off the hook chain's reply unchecked, so a malformed envelope handed `undefined` to each caller — or threw inside the gate for a `null` — with nothing in the error naming a plugin. The send is now refused with a message that names the hook, and refused rather than sent: this is a moderation chokepoint, and a handler whose reply cannot be read may have been redacting something. A chain that replies with nothing at all is unchanged and keeps the caller's own input.
diff --git a/src/common/storage/storage.service.spec.ts b/src/common/storage/storage.service.spec.ts
index 35c1e1b26..290c96f39 100644
--- a/src/common/storage/storage.service.spec.ts
+++ b/src/common/storage/storage.service.spec.ts
@@ -298,3 +298,28 @@ describe('StorageService local traversal (async + bounded)', () => {
expect(seen.length).toBe(20); // complete where listFiles() above truncates at 5
});
});
+
+/**
+ * The export enumerated with listFiles(), which stops at STORAGE_LIST_MAX_FILES and returns without
+ * logging or throwing — so the documented local→S3 migration (export, repoint STORAGE_TYPE, import)
+ * silently left media behind, and the operator's own files/count pre-check was truncated by the same
+ * path. iterateFiles() exists for exactly this case: its own doc calls the cap "a per-call DoS guard,
+ * not a completeness contract" and tells callers needing the whole store to iterate instead.
+ */
+describe('StorageService.createExportStream enumerates the whole store', () => {
+ it('walks the uncapped iterator rather than the capped listing', async () => {
+ const { service } = makeLocalService();
+ const listFiles = jest.spyOn(service, 'listFiles');
+ const iterateFiles = jest.spyOn(service, 'iterateFiles').mockImplementation(async function* () {
+ yield await Promise.resolve('media/a.bin');
+ });
+ jest.spyOn(service, 'getFile').mockResolvedValue(Buffer.from('x'));
+
+ // The enumerator runs before the archive is constructed, so which one was used is settled even
+ // if archiving itself cannot run in this environment. That is the whole claim here.
+ await service.createExportStream().catch(() => undefined);
+
+ expect(iterateFiles).toHaveBeenCalled();
+ expect(listFiles).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/common/storage/storage.service.ts b/src/common/storage/storage.service.ts
index 617379e59..4bce02636 100644
--- a/src/common/storage/storage.service.ts
+++ b/src/common/storage/storage.service.ts
@@ -368,13 +368,26 @@ export class StorageService implements OnModuleDestroy {
// ============================================================================
createExportStream(): Promise<PassThrough> {
+ // Enumerated with iterateFiles(), NOT listFiles(). listFiles() stops at STORAGE_LIST_MAX_FILES
+ // and returns without logging or throwing — a per-call DoS guard, as its own doc says, and not a
+ // completeness contract. Spending it here made the documented local→S3 migration (export,
+ // repoint STORAGE_TYPE, import) leave media behind on the old backend silently, and the
+ // operator's own files/count pre-check was truncated by the same path, so the consistency check
+ // could not reveal the gap. An export exists to be complete; that is what the uncapped walk is for.
return createExportStream(
- () => this.listFiles(),
+ () => this.listAllFiles(),
filePath => this.getFile(filePath),
this.logger,
);
}
+ /** Every key in the store, uncapped — the completeness counterpart to the capped listFiles(). */
+ private async listAllFiles(): Promise<string[]> {
+ const files: string[] = [];
+ for await (const file of this.iterateFiles()) files.push(file);
+ return files;
+ }
+
// Best-effort, NOT atomic: see the implementation in storage-transfer.ts for the full contract.
importFromStream(inputStream: Readable): Promise<number> {
return importFromStream(inputStream, (filePath, data) => this.putFile(filePath, data), this.logger);