Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(storage): import only regular files from a storage archive

Yudhi Armyndharis3fa09f098aa1
diff --git a/src/common/storage/storage-transfer.spec.ts b/src/common/storage/storage-transfer.spec.ts
index b32730314..7480025ac 100644
--- a/src/common/storage/storage-transfer.spec.ts
+++ b/src/common/storage/storage-transfer.spec.ts
@@ -1,5 +1,7 @@
import { randomBytes } from 'crypto';
import { PassThrough, Readable } from 'stream';
+import * as tar from 'tar-stream';
+import { createGzip } from 'zlib';
// archiver v8 is ESM-only and ts-jest cannot load it here. This stand-in keeps the parts of its
// TarArchive contract the export relies on: a stream entry with a known size is piped straight into
@@ -194,3 +196,29 @@ describe('importFromStream reports refused writes', () => {
expect(result).toEqual({ imported: 0, failed: names.length });
});
});
+
+describe('importFromStream writes regular files only', () => {
+ it('skips directory and link entries and drops a leading ./ from the key', async () => {
+ const pack = tar.pack();
+ pack.entry({ name: './', type: 'directory' });
+ pack.entry({ name: './status/', type: 'directory' });
+ pack.entry({ name: './status/link.jpg', type: 'symlink', linkname: 'a.jpg' });
+ pack.entry({ name: './status/hard.jpg', type: 'link', linkname: './status/a.jpg' });
+ pack.entry({ name: './status/a.jpg' }, Buffer.from('jpeg!!'));
+ pack.finalize();
+ const written = new Map<string, Buffer>();
+
+ const result = await importFromStream(
+ pack.pipe(createGzip()),
+ (name, data) => {
+ written.set(name, data);
+ return Promise.resolve();
+ },
+ makeLogger() as never,
+ );
+
+ expect([...written.keys()]).toEqual(['status/a.jpg']);
+ expect(written.get('status/a.jpg')?.toString()).toBe('jpeg!!');
+ expect(result).toEqual({ imported: 1, failed: 0 });
+ });
+});
diff --git a/src/common/storage/storage-transfer.ts b/src/common/storage/storage-transfer.ts
index b55d9b0ac..2e79c3210 100644
--- a/src/common/storage/storage-transfer.ts
+++ b/src/common/storage/storage-transfer.ts
@@ -226,6 +226,17 @@ export async function importFromStream(
fail(new Error(`Import aborted: archive exceeds the ${maxEntries}-entry limit`));
return;
}
+ // Only regular files are media. A directory or link entry carries no content, and writing it
+ // through putFile would store an empty file over any existing object with that key.
+ if (header.type !== 'file' && header.type !== 'contiguous-file') {
+ logger.debug(`Skipped ${header.type ?? 'unknown'} entry: ${header.name}`);
+ stream.on('end', () => next());
+ stream.resume();
+ return;
+ }
+ // An archive packed with `tar -C dir .` names every entry `./…`; the export's keys have no such
+ // segment, and on S3 it would become part of the object key.
+ const key = header.name.replace(/^(?:\.\/)+/, '');
const chunks: Buffer[] = [];
let entryBytes = 0;
@@ -237,7 +248,7 @@ export async function importFromStream(
if (entryBytes > maxEntryBytes) {
entryAborted = true;
stream.resume(); // drain the remainder so the source can end
- fail(new Error(`Import aborted: entry "${header.name}" exceeds the ${maxEntryBytes}-byte per-entry cap`));
+ fail(new Error(`Import aborted: entry "${key}" exceeds the ${maxEntryBytes}-byte per-entry cap`));
} else {
chunks.push(chunk);
}
@@ -246,15 +257,15 @@ export async function importFromStream(
stream.on('end', () => {
if (entryAborted || settled) return;
const data = Buffer.concat(chunks);
- putFile(header.name, data)
+ putFile(key, data)
.then(() => {
importedCount++;
- logger.debug(`Imported file: ${header.name}`);
+ logger.debug(`Imported file: ${key}`);
next();
})
.catch((error: unknown) => {
failedCount++;
- logger.error(`Failed to import file: ${header.name}`, String(error));
+ logger.error(`Failed to import file: ${key}`, String(error));
next();
});
});