Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(storage): skip an unreadable lost+found at the storage root
Yudhi Armyndharis2529309ec767
diff --git a/src/common/storage/storage-local-files.spec.ts b/src/common/storage/storage-local-files.spec.tsindex e828d9206..40711421b 100644--- a/src/common/storage/storage-local-files.spec.ts+++ b/src/common/storage/storage-local-files.spec.ts@@ -56,6 +56,42 @@ describe('iterateLocalFiles', () => {await expect(walk()).rejects.toBe(error);});+ // mkfs leaves a root-owned lost+found (mode 0700) at the top of a volume. A store that sits on a volume of+ // its own, read by an unprivileged user, would otherwise refuse every export and count for good.+ describe('for lost+found', () => {+ const lostAndFound = 'lost+found';++ it.each(['EACCES', 'EPERM'])('skips an unreadable %s at the storage root and says so', async code => {+ write('ok.bin');+ write(`${lostAndFound}/recovered.bin`);+ failReaddirAt(path.join(root, lostAndFound), code);+ const log = jest.fn();++ expect(await walk('', { logger: { log, warn: jest.fn() } })).toEqual(['ok.bin']);+ expect(log).toHaveBeenCalledWith(expect.stringContaining(lostAndFound));+ });++ it('walks one it can read like any other directory', async () => {+ write(`${lostAndFound}/recovered.bin`);++ expect(await walk()).toEqual([`${lostAndFound}/recovered.bin`]);+ });++ it('still fails on one that is not at the storage root', async () => {+ write(`nested/${lostAndFound}/recovered.bin`);+ const error = failReaddirAt(path.join(root, 'nested', lostAndFound));++ await expect(walk()).rejects.toBe(error);+ });++ it('still fails on an error that is not a permission error', async () => {+ write(`${lostAndFound}/recovered.bin`);+ const error = failReaddirAt(path.join(root, lostAndFound), 'EIO');++ await expect(walk()).rejects.toBe(error);+ });+ });+it('fails on a directory below the maximum depth', async () => {write(deepKey);@@ -94,7 +130,7 @@ describe('iterateLocalFiles', () => {describe('when asked to skip what it cannot read', () => {const lenient = (): { options: LocalWalkOptions; warn: jest.Mock } => {const warn = jest.fn();- return { options: { skipUnreadable: true, logger: { warn } }, warn };+ return { options: { skipUnreadable: true, logger: { log: jest.fn(), warn } }, warn };};it('skips an unreadable directory, names it, and still lists the rest', async () => {diff --git a/src/common/storage/storage-local-files.ts b/src/common/storage/storage-local-files.tsindex 40062b0f1..6ac06a12d 100644--- a/src/common/storage/storage-local-files.ts+++ b/src/common/storage/storage-local-files.ts@@ -14,9 +14,12 @@ export interface LocalWalkOptions {* could not read alone. An export or a count must not set it, because a short answer hides files.*/skipUnreadable?: boolean;- logger?: Pick<LoggerService, 'warn'>;+ logger?: Pick<LoggerService, 'log' | 'warn'>;}+/** Created by mkfs at the top of a volume; root-owned and mode 0700, so an unprivileged app cannot read it. */+const LOST_AND_FOUND = 'lost+found';+/*** Local enumeration as a stream. 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@@ -52,8 +55,14 @@ export async function* iterateLocalFiles(try {entries = await fs.promises.readdir(fullPath, { withFileTypes: true });} catch (error) {+ const code = (error as NodeJS.ErrnoException).code;// A directory removed during enumeration has no files left to export.- if ((error as NodeJS.ErrnoException).code !== 'ENOENT') unreadable(dir, error as Error);+ if (code === 'ENOENT') continue;+ if (dir === LOST_AND_FOUND && (code === 'EACCES' || code === 'EPERM')) {+ options.logger?.log(`Skipping the unreadable ${LOST_AND_FOUND} directory at the storage root`);+ continue;+ }+ unreadable(dir, error as Error);continue;}diff --git a/src/common/storage/storage.service.spec.ts b/src/common/storage/storage.service.spec.tsindex 0d0c8df4e..973728c05 100644--- a/src/common/storage/storage.service.spec.ts+++ b/src/common/storage/storage.service.spec.ts@@ -468,3 +468,36 @@ describe('StorageService local store holding a symbolic link', () => {expect(listed).toEqual(['chat-media/archived.bin']);});});++describe('StorageService local store on a volume whose lost+found it cannot read', () => {+ let baseDir: string;+ let service: StorageService;++ beforeEach(() => {+ let localPath: string;+ ({ service, baseDir, localPath } = makeLocalService());+ fs.mkdirSync(path.join(localPath, 'lost+found'), { recursive: true });+ fs.writeFileSync(path.join(localPath, 'real.bin'), 'real');+ // A mode-000 directory is still readable by root, so the failure is injected for that one path.+ const readdir = fs.promises.readdir.bind(fs.promises) as (...args: unknown[]) => Promise<unknown>;+ jest+ .spyOn(fs.promises, 'readdir')+ .mockImplementation(((target: fs.PathLike, ...rest: unknown[]) =>+ String(target).endsWith('lost+found')+ ? Promise.reject(Object.assign(new Error('EACCES: permission denied, scandir'), { code: 'EACCES' }))+ : readdir(target, ...rest)) as never);+ jest.spyOn(console, 'log').mockImplementation(() => undefined);+ });++ afterEach(() => {+ jest.restoreAllMocks();+ fs.rmSync(baseDir, { recursive: true, force: true });+ });++ it('counts the media around it, as it did before directories were required to be readable', async () => {+ expect(await service.getFileCount()).toEqual({ count: 1, sizeBytes: 4 });+ const listed: string[] = [];+ for await (const file of service.iterateFiles()) listed.push(file);+ expect(listed).toEqual(['real.bin']);+ });+});