From dd1ef483ef90f35218f5a4a3ea37a624b38ca8da Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Wed, 26 Mar 2025 17:29:22 +0100 Subject: backend: Handle corrupt zip files Corrupt zip files would already make hircine throw an error, but depending on the exact problem it would not report which file (or even which entry in a zip file) is affected. Use ZipFile.testzip() to catch common problems and make sure to re-raise any exception within as a BadZipFile exception. This makes sure to also report decompression problems that are raised as a zlib.error exception, for example. --- src/hircine/scanner.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/hircine/scanner.py b/src/hircine/scanner.py index 29ae04f..6e3fafb 100644 --- a/src/hircine/scanner.py +++ b/src/hircine/scanner.py @@ -8,7 +8,7 @@ from datetime import datetime, timezone from enum import Enum from hashlib import file_digest from typing import NamedTuple -from zipfile import ZipFile, is_zipfile +from zipfile import BadZipFile, ZipFile, is_zipfile from blake3 import blake3 from natsort import natsorted, ns @@ -286,6 +286,11 @@ class Scanner: hash = blake3() with ZipFile(path, mode="r") as z: + try: + z.testzip() + except Exception as e: + raise BadZipFile(f"Corrupt zip file {path}") from e + input = [(path, info.filename) for info in z.infolist()] loop = asyncio.get_event_loop() -- cgit v1.2.3-2-gb3c3