From 9344fdf1b67e551b63412b2df9ce16f390c0c6fa Mon Sep 17 00:00:00 2001 From: Otto Sichert Date: Thu, 16 Jun 2022 00:40:26 +0200 Subject: [PATCH] Simplify guarding against corrupt ZIP files Signed-off-by: Otto Sichert --- .../src/reading/tree/ZipArchiveResponse.ts | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts b/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts index 418f32b155..501113d9f1 100644 --- a/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts +++ b/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts @@ -26,6 +26,14 @@ import { } from '../types'; import { streamToTimeoutPromise } from './util'; +const guardCorruptZipStream = (stream: Readable) => + streamToTimeoutPromise(stream, { + eventName: 'entry', + timeoutMs: 3000, + getError: (entry: Entry) => + new Error(`Timed out while unzipping ${entry.type}: ${entry.path}`), + }); + /** * Wraps a zip archive stream into a tree response reader. */ @@ -104,13 +112,7 @@ export class ZipArchiveResponse implements ReadTreeResponse { entry.autodrain(); } }); - - await streamToTimeoutPromise(parseStream, { - eventName: 'entry', - timeoutMs: 3000, - getError: (entry: Entry) => - new Error(`Timed out while unzipping ${entry.type}: ${entry.path}`), - }); + await guardCorruptZipStream(parseStream); return files; } @@ -132,13 +134,7 @@ export class ZipArchiveResponse implements ReadTreeResponse { entry.autodrain(); } }); - - await streamToTimeoutPromise(parseStream, { - eventName: 'entry', - timeoutMs: 3000, - getError: (entry: Entry) => - new Error(`Timed out while unzipping ${entry.type}: ${entry.path}`), - }); + await guardCorruptZipStream(parseStream); archive.finalize(); @@ -168,13 +164,7 @@ export class ZipArchiveResponse implements ReadTreeResponse { entry.autodrain(); } }); - - await streamToTimeoutPromise(parseStream, { - eventName: 'entry', - timeoutMs: 3000, - getError: (entry: Entry) => - new Error(`Timed out while unzipping ${entry.type}: ${entry.path}`), - }); + await guardCorruptZipStream(parseStream); return dir; }