Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(storage): refuse a symbolic link in an export or a count
Yudhi Armyndharisb97fdfb06fab
diff --git a/src/common/storage/storage-local-files.spec.ts b/src/common/storage/storage-local-files.spec.tsindex 931d42afb..e828d9206 100644--- a/src/common/storage/storage-local-files.spec.ts+++ b/src/common/storage/storage-local-files.spec.ts@@ -62,6 +62,26 @@ describe('iterateLocalFiles', () => {await expect(walk()).rejects.toThrow(/maximum traversal depth/);});+ // A link is neither followed (it can loop, or leave the store) nor listed, so what it points at would be+ // missing from an export or a count without a word. Refusing is the only answer that cannot lose it.+ it.each([+ ['directory', 'linked'],+ ['file', 'alias.bin'],+ ])('fails on a symbolic link to a %s, naming it', async (kind, name) => {+ write('real/inside.bin');+ write('real.bin');+ fs.symlinkSync(path.join(root, kind === 'directory' ? 'real' : 'real.bin'), path.join(root, name));++ await expect(walk()).rejects.toThrow(new RegExp(`symbolic link.*${name.replace('.', '\\.')}`));+ });++ it('walks a prefix that is itself a symbolic link, since the walk starts from a path', async () => {+ write('elsewhere/s1/archived.bin');+ fs.symlinkSync(path.join(root, 'elsewhere'), path.join(root, 'chat-media'));++ expect(await walk('chat-media/')).toEqual(['chat-media/s1/archived.bin']);+ });+it('treats a directory removed during the walk as empty', async () => {write('ok.bin');write('gone/file.bin');@@ -98,6 +118,16 @@ describe('iterateLocalFiles', () => {expect(warn).toHaveBeenCalledWith(expect.stringContaining('maximum traversal depth'));});+ it('skips a symbolic link, names it, and still lists the rest', async () => {+ write('ok.bin');+ write('real/inside.bin');+ fs.symlinkSync(path.join(root, 'real'), path.join(root, 'linked'));+ const { options, warn } = lenient();++ expect(await walk('', options)).toEqual(['ok.bin', 'real/inside.bin']);+ expect(warn).toHaveBeenCalledWith(expect.stringMatching(/"linked".*symbolic link/));+ });+it('skips without a logger too', async () => {write('ok.bin');write('locked/hidden.bin');diff --git a/src/common/storage/storage-local-files.ts b/src/common/storage/storage-local-files.tsindex d180c83c8..40062b0f1 100644--- a/src/common/storage/storage-local-files.ts+++ b/src/common/storage/storage-local-files.ts@@ -65,6 +65,10 @@ export async function* iterateLocalFiles(queue.push({ dir: relativePath, depth: depth + 1 });} else if (entry.isFile()) {yield relativePath;+ } else if (entry.isSymbolicLink()) {+ // Not followed: a link can loop or leave the store, and what it points at would be missing from the+ // listing. getFile and a walk that starts below the link still reach it.+ unreadable(relativePath, new Error(`Storage path is a symbolic link and is not followed: ${relativePath}`));}}}diff --git a/src/common/storage/storage.service.spec.ts b/src/common/storage/storage.service.spec.tsindex 83a10fd95..0d0c8df4e 100644--- a/src/common/storage/storage.service.spec.ts+++ b/src/common/storage/storage.service.spec.ts@@ -435,3 +435,36 @@ describe('StorageService.createExportStream enumerates the whole store', () => {expect(ticks).toBeGreaterThan(100);}, 30000);});++describe('StorageService local store holding a symbolic link', () => {+ let baseDir: string;+ let localPath: string;+ let service: StorageService;++ beforeEach(() => {+ ({ service, baseDir, localPath } = makeLocalService());+ fs.mkdirSync(path.join(baseDir, 'bigdisk'), { recursive: true });+ fs.writeFileSync(path.join(baseDir, 'bigdisk', 'archived.bin'), 'archived');+ fs.mkdirSync(localPath, { recursive: true });+ fs.writeFileSync(path.join(localPath, 'real.bin'), 'real');+ fs.symlinkSync(path.join(baseDir, 'bigdisk'), path.join(localPath, 'chat-media'));+ });++ afterEach(() => {+ fs.rmSync(baseDir, { recursive: true, force: true });+ });++ // The file count is the pre-check before the export that moves the store to another backend. Both must+ // fail on a link rather than answer for a store that is missing the media behind it.+ it('refuses a file count and an export, which would otherwise leave that media behind', async () => {+ await expect(service.getFileCount()).rejects.toThrow(/symbolic link.*chat-media/);+ await expect(service.createExportStream()).rejects.toThrow(/symbolic link.*chat-media/);+ });++ it('still serves the linked file and lets a sweep of the linked prefix list it', async () => {+ expect((await service.getFile('chat-media/archived.bin')).toString()).toBe('archived');+ const listed: string[] = [];+ for await (const file of service.iterateFiles('chat-media/', { skipUnreadable: true })) listed.push(file);+ expect(listed).toEqual(['chat-media/archived.bin']);+ });+});