Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address issues handling zip files in ModelScan._iterate_models() #219

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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