Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Malware gate: HF scan metadata only; unknown levels fail closed; no first-party exemption."""
import json
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
import pytest
from utils.security import evaluate_file_security
def _patch_status(status):
"""Patch huggingface_hub.model_info to return one fixed security_repo_status."""
def _mi(*_args, **_kwargs):
return SimpleNamespace(security_repo_status = status)
return patch("huggingface_hub.model_info", side_effect = _mi)
def _patch_raises(exc = RuntimeError("offline")):
return patch("huggingface_hub.model_info", side_effect = exc)
def _patch_no_index():
"""Make the weight-index lookup find no index files (definitive: nothing sharded)."""
from huggingface_hub.utils import EntryNotFoundError
def _dl(
repo_id = None,
filename = None,
token = None,
**kw,
):
raise EntryNotFoundError(filename or "")
return patch("huggingface_hub.hf_hub_download", side_effect = _dl)
def _patch_index(weight_map, index_filename = "pytorch_model.bin.index.json"):
"""Serve a root weight index mapping tensor names -> shard paths; others 404."""
import json
import tempfile
from pathlib import Path
from huggingface_hub.utils import EntryNotFoundError
def _dl(
repo_id = None,
filename = None,
token = None,
**kw,
):
if filename == index_filename:
p = Path(tempfile.mkdtemp()) / filename
p.write_text(json.dumps({"weight_map": weight_map}))
return str(p)
raise EntryNotFoundError(filename or "")
return patch("huggingface_hub.hf_hub_download", side_effect = _dl)
def _patch_index_unreadable():
"""Make every index fetch fail transiently (inconclusive lookup -> fail closed)."""
def _dl(
repo_id = None,
filename = None,
token = None,
**kw,
):
raise RuntimeError("transient network error")
return patch("huggingface_hub.hf_hub_download", side_effect = _dl)
def _patch_index_mixed(weight_map, readable_index, failing_index):
"""Serve one index cleanly while another fails transiently: the flagged shard is
listed only by the index we could not read, so a naive "read any index?" check breaks."""
import json
import tempfile
from pathlib import Path
from huggingface_hub.utils import EntryNotFoundError
def _dl(
repo_id = None,
filename = None,
token = None,
**kw,
):
if filename == readable_index:
p = Path(tempfile.mkdtemp()) / filename
p.write_text(json.dumps({"weight_map": weight_map}))
return str(p)
if filename == failing_index:
raise RuntimeError("transient network error")
raise EntryNotFoundError(filename or "")
return patch("huggingface_hub.hf_hub_download", side_effect = _dl)
@pytest.mark.parametrize("level", ["unsafe", "suspicious", "malicious"])
def test_blocks_each_blocking_level(level):
status = {"scansDone": True, "filesWithIssues": [{"path": "pytorch_model.bin", "level": level}]}
with _patch_status(status):
d = evaluate_file_security("evil/repo")
assert d.blocked is True
assert d.unsafe_files == [{"path": "pytorch_model.bin", "level": level}]
assert d.response_payload()["security_blocked"] is True
def test_ignores_safe_only():
status = {
"scansDone": True,
"filesWithIssues": [{"path": "model.safetensors", "level": "safe"}],
}
with _patch_status(status):
d = evaluate_file_security("good/repo")
assert d.blocked is False
assert d.unsafe_files == []
def test_blocks_unsafe_even_when_scans_not_done():
# scansDone is often False for clean repos; a flagged file must still block
status = {"scansDone": False, "filesWithIssues": [{"path": "x.pkl", "level": "unsafe"}]}
with _patch_status(status):
d = evaluate_file_security("evil/repo")
assert d.blocked is True
def test_fail_open_when_scan_unavailable():
with _patch_status(None):
d = evaluate_file_security("unknown/repo")
assert d.blocked is False
def test_fail_open_on_exception_offline():
with _patch_raises():
d = evaluate_file_security("offline/repo")
assert d.blocked is False
def test_fail_open_scans_done_no_issues():
with _patch_status({"scansDone": True, "filesWithIssues": []}):
d = evaluate_file_security("clean/repo")
assert d.blocked is False
def test_skips_local_path():
with patch("huggingface_hub.model_info", side_effect = AssertionError("should not be called")):
d = evaluate_file_security("/tmp/some/local/model")
assert d.blocked is False
assert "local" in d.reason
def test_scans_inactive_hf_cache_snapshot_path(tmp_path):
# recover repo id + commit from models--org--repo/snapshots/<rev> and scan that commit
snapshot = tmp_path / "models--evil--repo" / "snapshots" / "deadbeef"
snapshot.mkdir(parents = True)
status = {
"scansDone": True,
"filesWithIssues": [{"path": "pytorch_model.bin", "level": "unsafe"}],
}
with _patch_status(status) as model_info:
d = evaluate_file_security(str(snapshot))
assert d.blocked is True
assert model_info.call_args.args[0] == "evil/repo"
assert model_info.call_args.kwargs["revision"] == "deadbeef"
def test_local_only_scan_inspects_exact_inactive_snapshot(tmp_path):
repo = tmp_path / "models--evil--repo"
selected = repo / "snapshots" / "commit-old"
active = repo / "snapshots" / "commit-new"
selected.mkdir(parents = True)
active.mkdir(parents = True)
(selected / "pytorch_model.bin").write_bytes(b"pickle")
(active / "model.safetensors").write_bytes(b"safe")
(repo / "refs").mkdir()
(repo / "refs" / "main").write_text("commit-new")
with patch("utils.utils.hf_cache_snapshot_dir", return_value = active):
decision = evaluate_file_security(str(selected), local_only_load = True)
assert decision.blocked is True
assert decision.unsafe_files == [{"path": "pytorch_model.bin", "level": "unscanned"}]
def test_local_only_scan_allows_exact_safetensors_snapshot(tmp_path):
snapshot = tmp_path / "models--good--repo" / "snapshots" / "deadbeef"
snapshot.mkdir(parents = True)
(snapshot / "model.safetensors").write_bytes(b"safe")
with patch(
"utils.security.file_security._fetch_security_status",
side_effect = AssertionError("local-only scan must not contact the Hub"),
):
decision = evaluate_file_security(str(snapshot), local_only_load = True)
assert decision.blocked is False
assert "inert" in decision.reason
def test_unavailable_hub_scan_falls_back_to_exact_snapshot(tmp_path):
snapshot = tmp_path / "models--evil--repo" / "snapshots" / "deadbeef"
snapshot.mkdir(parents = True)
(snapshot / "pytorch_model.bin").write_bytes(b"pickle")
with patch(
"utils.security.file_security._fetch_security_status",
return_value = None,
):
decision = evaluate_file_security(str(snapshot))
assert decision.blocked is True
assert "Hub scan unavailable" in decision.reason
assert decision.unsafe_files == [{"path": "pytorch_model.bin", "level": "unscanned"}]
def test_unavailable_hub_scan_inspects_explicit_load_subdirs(tmp_path):
snapshot = tmp_path / "models--evil--repo" / "snapshots" / "deadbeef"
llm = snapshot / "LLM"
llm.mkdir(parents = True)
(llm / "pytorch_model.bin").write_bytes(b"pickle")
with patch(
"utils.security.file_security._fetch_security_status",
return_value = None,
):
decision = evaluate_file_security(
str(snapshot),
load_subdirs = ("LLM",),
)
assert decision.blocked is True
assert decision.unsafe_files == [{"path": "LLM/pytorch_model.bin", "level": "unscanned"}]
def test_local_fallback_preserves_native_sentence_transformer_module_path(tmp_path):
snapshot = tmp_path / "models--evil--repo" / "snapshots" / "deadbeef"
module_path = r"encoder\pooling"
module = snapshot / Path(module_path)
module.mkdir(parents = True)
pickle = module / "pytorch_model.bin"
pickle.write_bytes(b"pickle")
(snapshot / "modules.json").write_text(
json.dumps([{"path": module_path}]),
encoding = "utf-8",
)
decision = evaluate_file_security(str(snapshot), local_only_load = True)
assert decision.blocked is True
assert decision.unsafe_files == [
{"path": pickle.relative_to(snapshot).as_posix(), "level": "unscanned"}
]
def test_local_fallback_blocks_traversing_sentence_transformer_module_path(tmp_path):
snapshot = tmp_path / "models--evil--repo" / "snapshots" / "deadbeef"
snapshot.mkdir(parents = True)
(snapshot / "modules.json").write_text(
json.dumps([{"path": "../payload"}]),
encoding = "utf-8",
)
decision = evaluate_file_security(str(snapshot), local_only_load = True)
assert decision.blocked is True
assert "could not read" in decision.reason
def test_online_exact_scan_blocks_sentence_transformer_module_weight(tmp_path):
snapshot = tmp_path / "models--evil--repo" / "snapshots" / "deadbeef"
snapshot.mkdir(parents = True)
(snapshot / "modules.json").write_text(
json.dumps([{"path": "0_Transformer"}]),
encoding = "utf-8",
)
status = {
"scansDone": True,
"filesWithIssues": [
{
"path": "0_Transformer/pytorch_model.bin",
"level": "unsafe",
}
],
}
with _patch_status(status):
decision = evaluate_file_security(str(snapshot))
assert decision.blocked is True
assert decision.unsafe_files == [{"path": "0_Transformer/pytorch_model.bin", "level": "unsafe"}]
def test_remote_gguf_named_repo_is_still_scanned():
status = {
"scansDone": True,
"filesWithIssues": [{"path": "pytorch_model.bin", "level": "unsafe"}],
}
with _patch_status(status):
d = evaluate_file_security("evil/model.gguf")
assert d.blocked is True
assert d.unsafe_files
def test_skips_local_gguf_file():
with patch("huggingface_hub.model_info", side_effect = AssertionError("should not be called")):
d = evaluate_file_security("/tmp/models/model.gguf")
assert d.blocked is False
assert "local" in d.reason
def test_no_first_party_exemption():
status = {
"scansDone": True,
"filesWithIssues": [{"path": "pytorch_model.bin", "level": "unsafe"}],
}
with _patch_status(status):
d = evaluate_file_security("unsloth/some-model")
assert d.blocked is True
def test_malformed_entries_are_ignored():
status = {
"scansDone": True,
"filesWithIssues": ["not-a-dict", {"path": "ok.pkl", "level": "unsafe"}],
}
with _patch_status(status):
d = evaluate_file_security("evil/repo")
assert d.blocked is True
assert d.unsafe_files == [{"path": "ok.pkl", "level": "unsafe"}]
def test_response_payload_shape():
status = {"scansDone": True, "filesWithIssues": [{"path": "a.pkl", "level": "malicious"}]}
with _patch_status(status):
payload = evaluate_file_security("evil/repo").response_payload()
assert set(payload) == {"unsafe_files", "security_blocked", "reason", "block_cause"}
assert payload["security_blocked"] is True
assert payload["unsafe_files"] == [{"path": "a.pkl", "level": "malicious"}]
def test_flagged_safetensors_does_not_block():
# safetensors cannot execute code, so a flag on one is not an RCE vector
status = {
"scansDone": False,
"filesWithIssues": [{"path": "model-00001-of-00004.safetensors", "level": "unsafe"}],
}
with _patch_status(status):
d = evaluate_file_security("nvidia/some-model")
assert d.blocked is False
assert d.unsafe_files == []
def test_flagged_subdirectory_pickle_does_not_block():
# from_pretrained reads only root weights plus what the root index references
status = {
"scansDone": False,
"filesWithIssues": [
{"path": "nemo/weights/common.pt", "level": "unsafe"},
{"path": "nemo/weights/__0_0.distcp", "level": "unsafe"},
],
}
with _patch_status(status), _patch_no_index():
d = evaluate_file_security("nvidia/some-model")
assert d.blocked is False
assert d.unsafe_files == []
def test_nemotron_h_shaped_status_loads():
# real Nemotron-H-8B-Base-8K shape: flagged root safetensors + unreferenced nemo/ pickles
status = {
"scansDone": False,
"filesWithIssues": [
{"path": "nemo/weights/.metadata", "level": "unsafe"},
{"path": "nemo/weights/__0_0.distcp", "level": "unsafe"},
{"path": "nemo/weights/common.pt", "level": "unsafe"},
{"path": "model-00001-of-00004.safetensors", "level": "unsafe"},
{"path": "model-00002-of-00004.safetensors", "level": "unsafe"},
],
}
with _patch_status(status), _patch_no_index():
d = evaluate_file_security("nvidia/Nemotron-H-8B-Base-8K")
assert d.blocked is False
assert d.unsafe_files == []
def test_indexed_subdir_shard_blocks():
status = {
"scansDone": False,
"filesWithIssues": [
{"path": "shards/pytorch_model-00001-of-00002.bin", "level": "unsafe"},
],
}
weight_map = {
"layer.0.weight": "shards/pytorch_model-00001-of-00002.bin",
"layer.1.weight": "shards/pytorch_model-00002-of-00002.bin",
}
with _patch_status(status), _patch_index(weight_map):
d = evaluate_file_security("evil/sharded")
assert d.blocked is True
assert d.unsafe_files == [
{"path": "shards/pytorch_model-00001-of-00002.bin", "level": "unsafe"}
]
def test_unindexed_subdir_pickle_does_not_block_when_index_present():
status = {
"scansDone": False,
"filesWithIssues": [{"path": "extras/notes.bin", "level": "unsafe"}],
}
weight_map = {"layer.0.weight": "pytorch_model-00001-of-00001.bin"}
with _patch_status(status), _patch_index(weight_map):
d = evaluate_file_security("org/has-index")
assert d.blocked is False
assert d.unsafe_files == []
def test_inconclusive_index_lookup_blocks_subdir_pickle():
status = {
"scansDone": False,
"filesWithIssues": [{"path": "weights/model_part.bin", "level": "unsafe"}],
}
with _patch_status(status), _patch_index_unreadable():
d = evaluate_file_security("org/transient")
assert d.blocked is True
assert d.unsafe_files == [{"path": "weights/model_part.bin", "level": "unsafe"}]
def test_partial_index_read_with_transient_failure_blocks_subdir_pickle():
# a partial path set from a transiently failed index is not definitive
status = {
"scansDone": False,
"filesWithIssues": [
{"path": "shards/pytorch_model-00001-of-00002.bin", "level": "unsafe"},
],
}
safetensors_map = {"layer.0.weight": "model-00001-of-00001.safetensors"}
with (
_patch_status(status),
_patch_index_mixed(
safetensors_map,
readable_index = "model.safetensors.index.json",
failing_index = "pytorch_model.bin.index.json",
),
):
d = evaluate_file_security("evil/mixed-index")
assert d.blocked is True
assert d.unsafe_files == [
{"path": "shards/pytorch_model-00001-of-00002.bin", "level": "unsafe"}
]
def test_root_pickle_alongside_safetensors_still_blocks():
status = {
"scansDone": False,
"filesWithIssues": [
{"path": "model.safetensors", "level": "unsafe"},
{"path": "pytorch_model.bin", "level": "unsafe"},
],
}
with _patch_status(status):
d = evaluate_file_security("evil/repo")
assert d.blocked is True
assert d.unsafe_files == [{"path": "pytorch_model.bin", "level": "unsafe"}]
def test_eicar_shaped_root_files_block():
status = {
"scansDone": True,
"filesWithIssues": [
{"path": "model_broken_X.pkl", "level": "unsafe"},
{"path": "danger.dat", "level": "unsafe"},
{"path": "eicar_test_file", "level": "unsafe"},
],
}
with _patch_status(status):
d = evaluate_file_security("mcpotato/42-eicar-street")
assert d.blocked is True
assert len(d.unsafe_files) == 3
def test_unknown_future_level_fails_closed():
# Hub schema drift: unknown non-"safe" levels must block
status = {"scansDone": True, "filesWithIssues": [{"path": "weights.bin", "level": "infected"}]}
with _patch_status(status):
d = evaluate_file_security("evil/repo")
assert d.blocked is True
assert d.unsafe_files == [{"path": "weights.bin", "level": "infected"}]
def test_pending_or_scanning_level_does_not_block():
for lvl in ("pending", "scanning", "queued", "unscanned", "error"):
status = {
"scansDone": False,
"filesWithIssues": [{"path": "pytorch_model.bin", "level": lvl}],
}
with _patch_status(status):
d = evaluate_file_security("some/repo")
assert d.blocked is False, lvl
# Spark-TTS / BiCodec load from_pretrained(<snapshot>/LLM)
def test_flagged_pickle_under_load_subdir_blocks():
status = {
"scansDone": False,
"filesWithIssues": [{"path": "LLM/pytorch_model.bin", "level": "unsafe"}],
}
with _patch_status(status), _patch_no_index():
d = evaluate_file_security("org/spark-tts", load_subdirs = ("LLM",))
assert d.blocked is True
assert d.unsafe_files == [{"path": "LLM/pytorch_model.bin", "level": "unsafe"}]
def test_flagged_pickle_under_subdir_without_load_root_does_not_block():
status = {
"scansDone": False,
"filesWithIssues": [{"path": "LLM/pytorch_model.bin", "level": "unsafe"}],
}
with _patch_status(status), _patch_no_index():
d = evaluate_file_security("org/not-a-load-root")
assert d.blocked is False
assert d.unsafe_files == []
def test_indexed_shard_under_load_subdir_blocks():
status = {
"scansDone": False,
"filesWithIssues": [
{"path": "LLM/shards/pytorch_model-00001-of-00002.bin", "level": "unsafe"}
],
}
weight_map = {
"layer.0.weight": "shards/pytorch_model-00001-of-00002.bin",
"layer.1.weight": "shards/pytorch_model-00002-of-00002.bin",
}
with (
_patch_status(status),
_patch_index(weight_map, index_filename = "LLM/pytorch_model.bin.index.json"),
):
d = evaluate_file_security("org/spark-tts", load_subdirs = ("LLM",))
assert d.blocked is True
def test_flagged_root_python_helper_does_not_block():
# repo code runs only via auto_map under the consent gate, never deserialized
status = {
"scansDone": True,
"filesWithIssues": [
{"path": "build_pickles.py", "level": "unsafe"},
{"path": "train.py", "level": "suspicious"},
],
}
with _patch_status(status):
d = evaluate_file_security("org/has-helper-scripts")
assert d.blocked is False
assert d.unsafe_files == []
def test_root_pickle_still_blocks_with_flagged_python_sibling():
status = {
"scansDone": True,
"filesWithIssues": [
{"path": "convert.py", "level": "unsafe"},
{"path": "pytorch_model.bin", "level": "unsafe"},
],
}
with _patch_status(status):
d = evaluate_file_security("evil/mixed")
assert d.blocked is True
def _patch_status_capture(status):
"""Like _patch_status, but records the repo id model_info was queried with."""
seen = {}
def _mi(repo, *_a, **_k):
seen["repo"] = repo
return SimpleNamespace(security_repo_status = status)
return patch("huggingface_hub.model_info", side_effect = _mi), seen
def test_spark_tts_llm_alias_scans_real_repo():
# "Spark-TTS-0.5B/LLM" loads unsloth/Spark-TTS-0.5B with LLM as load root
status = {"filesWithIssues": [{"path": "LLM/pytorch_model.bin", "level": "unsafe"}]}
cap, seen = _patch_status_capture(status)
with cap, patch("utils.paths.is_local_path", return_value = False), _patch_no_index():
d = evaluate_file_security("Spark-TTS-0.5B/LLM", load_subdirs = ())
assert seen["repo"] == "unsloth/Spark-TTS-0.5B"
assert d.model_name == "unsloth/Spark-TTS-0.5B"
assert d.blocked is True
assert d.unsafe_files == [{"path": "LLM/pytorch_model.bin", "level": "unsafe"}]
def test_non_llm_alias_is_not_rewritten():
status = {"filesWithIssues": [{"path": "pytorch_model.bin", "level": "unsafe"}]}
cap, seen = _patch_status_capture(status)
with cap, patch("utils.paths.is_local_path", return_value = False):
d = evaluate_file_security("org/model")
assert seen["repo"] == "org/model"
assert d.model_name == "org/model"
def test_generic_slash_llm_repo_is_scanned_as_itself():
status = {"filesWithIssues": [{"path": "pytorch_model.bin", "level": "unsafe"}]}
cap, seen = _patch_status_capture(status)
with cap, patch("utils.paths.is_local_path", return_value = False):
d = evaluate_file_security("evil/LLM")
assert seen["repo"] == "evil/LLM"
assert d.model_name == "evil/LLM"
assert d.blocked is True
def test_security_load_subdirs_yaml_fallback(monkeypatch):
import utils.models.model_config as mc
from utils.security import security_load_subdirs
monkeypatch.setattr(mc, "detect_audio_type", lambda *_a, **_k: None)
monkeypatch.setattr(mc, "load_model_defaults", lambda *_a, **_k: {"audio_type": "bicodec"})
assert security_load_subdirs("unsloth/Spark-TTS-0.5B") == ("LLM",)
monkeypatch.setattr(mc, "load_model_defaults", lambda *_a, **_k: {"audio_type": None})
assert security_load_subdirs("unsloth/Llama-3.2-1B") == ()
@pytest.mark.parametrize(
"files, blocked",
[
(["config.json", "pytorch_model.bin"], ["pytorch_model.bin"]),
(["config.json", "pytorch_model.bin", "model.safetensors"], []),
(
[
"model-00001-of-00002.safetensors",
"model.safetensors.index.json",
"pytorch_model.bin",
],
[],
),
(["pytorch_model.bin.index.json", "shards/payload"], ["pytorch_model.bin.index.json"]),
(["model.safetensors", "adapter_model.bin"], ["adapter_model.bin"]),
(["LLM/pytorch_model.bin", "model.gguf"], ["LLM/pytorch_model.bin"]),
(["model.safetensors", "nemo/pytorch_model.bin"], []),
(["model.gguf", "README.md"], []),
],
)
def test_modelscope_has_no_scan_so_pickle_weights_fail_closed(monkeypatch, files, blocked):
monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
with _patch_status(None), patch("huggingface_hub.HfApi.list_repo_files", return_value = files):
d = evaluate_file_security("org/repo", load_subdirs = ("LLM",))
assert d.blocked is bool(blocked)
assert [f["path"] for f in d.unsafe_files] == blocked
if blocked:
assert "Switch the model source to Hugging Face" in d.reason
def _hub_error(cls):
response = SimpleNamespace(status_code = 404, headers = {}, url = "http://adapter", request = None)
return cls("404", response = response)
def test_modelscope_missing_repo_blocks_and_names_the_source(monkeypatch):
from huggingface_hub.errors import GatedRepoError, RepositoryNotFoundError
monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
with (
_patch_status(None),
patch(
"huggingface_hub.HfApi.list_repo_files",
side_effect = _hub_error(RepositoryNotFoundError),
),
):
decision = evaluate_file_security("x/y")
assert decision.blocked is True
assert decision.block_cause == "not_on_source"
assert "not on ModelScope" in decision.reason and "Hugging Face" in decision.reason
# A gated repo exists, so it stays "could not check".
with (
_patch_status(None),
patch("huggingface_hub.HfApi.list_repo_files", side_effect = _hub_error(GatedRepoError)),
):
decision = evaluate_file_security("x/y")
assert decision.blocked is True
assert decision.block_cause == "unscannable"
@pytest.mark.parametrize(
("weight", "blocked", "cause"),
[("model.safetensors", False, None), ("pytorch_model.bin", True, "unscanned_weights")],
)
def test_modelscope_missing_repo_judges_the_cached_snapshot(
monkeypatch, tmp_path, weight, blocked, cause
):
from huggingface_hub.errors import RepositoryNotFoundError
(tmp_path / weight).write_bytes(b"x")
monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
with (
_patch_status(None),
patch(
"huggingface_hub.HfApi.list_repo_files",
side_effect = _hub_error(RepositoryNotFoundError),
),
patch("utils.utils.hf_cache_snapshot_dir_for_repo", return_value = tmp_path),
):
decision = evaluate_file_security("x/y")
assert decision.blocked is blocked
assert decision.block_cause == cause
def test_each_block_names_its_cause(monkeypatch, tmp_path):
with _patch_status({"filesWithIssues": [{"path": "model.pkl", "level": "unsafe"}]}):
assert evaluate_file_security("x/y").block_cause == "unsafe_files"
monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
with (
_patch_status(None),
patch("huggingface_hub.HfApi.list_repo_files", return_value = ["pytorch_model.bin"]),
):
assert evaluate_file_security("x/y").block_cause == "unscanned_weights"
snapshot = tmp_path / "models--x--y" / "snapshots" / "abc"
snapshot.mkdir(parents = True)
(snapshot / "pytorch_model.bin").write_bytes(b"x")
with _patch_status(None):
assert evaluate_file_security(str(snapshot)).block_cause == "unscanned_weights"
def test_strongest_block_prefers_a_finding_over_a_failed_check():
from utils.security.file_security import strongest_block
assert strongest_block([]) == (None, None)
assert strongest_block([("not_on_source", "a"), ("critical_findings", "b")])[1] == "b"
assert strongest_block([("critical_findings", "a"), ("unsafe_files", "b")])[1] == "b"
assert strongest_block([(None, "a")]) == ("unscannable", "a")
def test_modelscope_listing_failure_blocks(monkeypatch):
monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
with (
_patch_status(None),
patch("huggingface_hub.HfApi.list_repo_files", side_effect = OSError("down")),
):
assert evaluate_file_security("org/repo").blocked is True
def test_hugging_face_scan_unavailable_stays_fail_open(monkeypatch):
monkeypatch.delenv("UNSLOTH_STUDIO_HUB_SOURCE", raising = False)
with _patch_status(None), patch("huggingface_hub.HfApi.list_repo_files") as listing:
assert evaluate_file_security("org/repo").blocked is False
listing.assert_not_called()