Skip to content

Commit

Permalink
Address issues handling zip files in ModelScan._iterate_models() (#219)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthieu Maitre <[email protected]>
  • Loading branch information
mmaitre314 and maitre-matt authored Nov 5, 2024
1 parent 8133838 commit 024100c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 2 additions & 6 deletions modelscan/modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]:
with Model(file) as model:
yield model

if (
not _is_zipfile(file, model.get_stream())
and Path(file).suffix
not in self._settings["supported_zip_extensions"]
):
if not _is_zipfile(file, model.get_stream()):
continue

try:
Expand All @@ -114,7 +110,7 @@ def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]:
continue

yield Model(file_name, file_io)
except zipfile.BadZipFile as e:
except (zipfile.BadZipFile, RuntimeError) as e:
logger.debug(
"Skipping zip file %s, due to error",
str(model.get_source()),
Expand Down
Binary file added tests/data/password_protected.zip
Binary file not shown.
18 changes: 17 additions & 1 deletion tests/test_modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dill
import pytest
import requests
import shutil
import socket
import subprocess
import sys
Expand Down Expand Up @@ -331,6 +332,10 @@ def file_path(tmp_path_factory: Any) -> Any:

initialize_data_file(f"{tmp}/data/malicious14.pkl", malicious14_gen())

shutil.copy(
f"{os.path.dirname(__file__)}/data/password_protected.zip", f"{tmp}/data/"
)

return tmp


Expand Down Expand Up @@ -1361,7 +1366,18 @@ def test_scan_directory_path(file_path: str) -> None:
"benign0_v3.dill",
"benign0_v4.dill",
}
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["summary"]["skipped"]["skipped_files"] == [
{
"category": "SCAN_NOT_SUPPORTED",
"description": "Model Scan did not scan file",
"source": "password_protected.zip",
},
{
"category": "BAD_ZIP",
"description": "Skipping zip file due to error: File 'test.txt' is encrypted, password required for extraction",
"source": "password_protected.zip",
},
]
assert results["errors"] == []


Expand Down

0 comments on commit 024100c

Please sign in to comment.