Skip to content

Commit 834a8b4

Browse files
committed
Validate downloaded file integrity and raise ValueError on hash mismatch
Signed-off-by: Enoch Mok <enochmokny@gmail.com>
1 parent 43f508b commit 834a8b4

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

monai/apps/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,11 @@ def download_url(
268268
pass
269269
logger.info(f"Downloaded: {filepath}")
270270
if not check_hash(filepath, hash_val, hash_type):
271-
raise RuntimeError(
272-
f"{hash_type} check of downloaded file failed: URL={url}, "
273-
f"filepath={filepath}, expected {hash_type}={hash_val}."
271+
raise ValueError(
272+
f"{hash_type} hash check of downloaded file failed: URL={url}, "
273+
f"filepath={filepath}, expected {hash_type}={hash_val}, "
274+
f"The file may be corrupted or tampered with. "
275+
"Please retry the download or verify the source."
274276
)
275277

276278

tests/test_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"unexpected EOF", # incomplete download
8080
"network issue",
8181
"gdown dependency", # gdown not installed
82-
"md5 check",
82+
"hash check", # check hash value of downloaded file
8383
"limit", # HTTP Error 503: Egress is over the account limit
8484
"authenticate",
8585
"timed out", # urlopen error [Errno 110] Connection timed out
@@ -180,8 +180,11 @@ def skip_if_downloading_fails():
180180
err_str = str(rt_e)
181181
if any(k in err_str for k in DOWNLOAD_FAIL_MSGS):
182182
raise unittest.SkipTest(f"Error while downloading: {rt_e}") from rt_e # incomplete download
183-
183+
184184
raise rt_e
185+
except ValueError as v_e:
186+
if "hash check" in str(v_e):
187+
raise unittest.SkipTest(f"Hash value error while downloading: {v_e}") from v_e
185188

186189

187190
SAMPLE_TIFF = "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CMU-1.tiff"

0 commit comments

Comments
 (0)