Skip to content

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.

Deduplicate the scan-packages baseline, and gate it with tests (#8110)

Daniel Handa72577146a3
diff --git a/scripts/scan_packages_baseline.json b/scripts/scan_packages_baseline.json
index 87f4d81133d..aa1fdcca72f 100644
--- a/scripts/scan_packages_baseline.json
+++ b/scripts/scan_packages_baseline.json
@@ -1586,46 +1586,6 @@
"evidence": "L111: out = extract_audio_info(msgs({\"type\": \"audio\", key: \"/tmp/a.wav\"})) sha256:2efe23ffbe2b91b8403aec9b700736919b59e5ca770f8e1f5501651b44b7d398",
"evidence_hash": "d416b79dd17b24214f3f7653ac01354507d7bf0fc464dee30a4a4b8998f063ba"
},
- {
- "package": "openai",
- "file": "openai/_base_client.py",
- "check": "C2 polling/beaconing loop detected",
- "severity": "CRITICAL",
- "evidence": "L274: while True: sha256:90a38e5c1e26893c7c273354143612640e9a9c0f079d3e2b60612d79f24e80a6",
- "evidence_hash": "1022e8e8649436ec64a98a9d9141d085452c49549fd2157b0278fc369a83ac66"
- },
- {
- "package": "openai",
- "file": "openai/auth/_workload.py",
- "check": "Accesses cloud metadata/IMDS AND makes network calls",
- "severity": "CRITICAL",
- "evidence": "IMDS: L97: url = \"http://169.254.169.254/metadata/identity/oauth2/token\" | L150: url = \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity\"\nNetwork: L78: http_client: httpx.Client | None = None, | L109: with httpx.Client() as client: | L134: http_client: httpx.Client | None = None, | L156: with httpx.Client() as client: | L251: exchange_client = DefaultHttpx2Client(follow_redirects=False) if self._use_httpx2 else httpx.Client()",
- "evidence_hash": "9717e51cb961dc14c458955d91a1e48e3753997346ecea0106bded3a8d64bfe0"
- },
- {
- "package": "openai",
- "file": "openai/resources/beta/responses/responses.py",
- "check": "C2 polling/beaconing loop detected",
- "severity": "CRITICAL",
- "evidence": "L4000: while True: sha256:f8ab538118daba9ec06e27399dbdc90a4521c3390e6a47a6348a1f180a83effd",
- "evidence_hash": "31481ea83c687acc27144d72d3832d4fb98dd1c79fb5e0ddd85080de95997b9f"
- },
- {
- "package": "openai",
- "file": "openai/resources/realtime/realtime.py",
- "check": "C2 polling/beaconing loop detected",
- "severity": "CRITICAL",
- "evidence": "L311: while True: sha256:5b63313072aae9ca28677e03426513ccf12221e4f4e0ea6c31efbe09790633b5",
- "evidence_hash": "05e1af469d651b51673763a7c4cdf759af9472fb627b7b470adc28cc237bd650"
- },
- {
- "package": "openai",
- "file": "openai/resources/responses/responses.py",
- "check": "C2 polling/beaconing loop detected",
- "severity": "CRITICAL",
- "evidence": "L3951: while True: sha256:d68ef896bf0743ca430cfacb9a3353da1f3b9c51c3a21b6450a07a32b55aa2ac",
- "evidence_hash": "160eecdd79b521bffbe8476f782b69a0724c35d1b19376a7600807165fd54f9f"
- },
{
"package": "unsloth-zoo",
"file": "tests/test_gemma4_forced_float32_ple_dtype.py",
diff --git a/tests/security/test_scan_packages.py b/tests/security/test_scan_packages.py
index f0d7e6432fd..b8e0728a8ae 100644
--- a/tests/security/test_scan_packages.py
+++ b/tests/security/test_scan_packages.py
@@ -1667,3 +1667,137 @@ def test_check_py_file_stamps_the_file_digest():
assert findings, "expected at least one finding"
expected = hashlib.sha256(src.encode("utf-8", "replace")).hexdigest()
assert all(f.file_sha256 == expected for f in findings)
+
+
+def test_the_shipped_baseline_hashes_match_their_evidence():
+ """A hand-added entry with a stale `evidence_hash` suppresses nothing: the key
+ is the hash, not the evidence text beside it, so the finding stays red and the
+ entry reads as a review that happened. Recompute every one of them."""
+ import json
+ import pathlib
+
+ path = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "scan_packages_baseline.json"
+ entries = json.loads(path.read_text(encoding = "utf-8"))["entries"]
+ assert entries, "the shipped baseline is empty"
+ wrong = [
+ (e.get("package"), e.get("file"), e.get("check"))
+ for e in entries
+ if e.get("evidence_hash") != sp._evidence_hash(e.get("evidence") or "")
+ ]
+ assert not wrong, f"evidence_hash does not match evidence: {wrong}"
+
+
+def _baseline_key(entry):
+ """The scanner's OWN key, not the raw fields: `_load_baseline` normalizes the
+ package name and strips an sdist's version-carrying archive root, so
+ `huggingface_hub` and `huggingface-hub`, or `foo-1.0/foo/a.py` and `foo/a.py`,
+ collapse to one runtime entry while a raw comparison sees two and reports
+ nothing."""
+ return (
+ sp._norm_pkg(entry.get("package") or ""),
+ sp._relpath_in_package(entry.get("file") or ""),
+ entry.get("check"),
+ entry.get("evidence_hash"),
+ )
+
+
+def _baseline_duplicates(entries):
+ """Keys whose entries say the same thing twice, or contradict each other.
+
+ Sharing a key is not itself an error: `_load_baseline` unions the pins under
+ one key into a set, so two reviewed versions with identical evidence but
+ different surrounding bytes are the supported way to approve both exact files.
+ What has no reading is a pin repeated verbatim, or an unpinned entry sitting
+ beside a pinned one -- unpinned wins there, so every pin next to it is inert
+ and reads as a narrower approval than the baseline actually grants.
+ """
+ groups = {}
+ for entry in entries:
+ groups.setdefault(_baseline_key(entry), []).append(entry.get("file_sha256") or None)
+ dupes = set()
+ for key, pins in groups.items():
+ if len(pins) != len(set(pins)) or (None in pins and len(pins) > 1):
+ dupes.add(key)
+ return dupes
+
+
+def test_the_shipped_baseline_has_no_duplicate_keys():
+ """Two entries saying the same thing means one of them was reviewed against
+ code that is no longer there, and nothing says which."""
+ import json
+ import pathlib
+
+ path = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "scan_packages_baseline.json"
+ entries = json.loads(path.read_text(encoding = "utf-8"))["entries"]
+ dupes = _baseline_duplicates(entries)
+ assert not dupes, f"duplicate baseline keys: {sorted(dupes)}"
+
+
+def test_two_reviewed_versions_may_share_a_key_with_distinct_pins(tmp_path):
+ """The representation `_load_baseline` exists to support: one evidence string
+ approved for exactly two file contents. Rejecting it would force the entry to
+ be widened to an unpinned suppression, which approves strictly more."""
+ import json
+
+ same = dict(
+ package = "requests",
+ file = "requests/api.py",
+ check = "C2 polling/beaconing loop detected",
+ severity = "CRITICAL",
+ evidence = "L1: while True:",
+ evidence_hash = sp._evidence_hash("L1: while True:"),
+ )
+ entries = [dict(file_sha256 = "a" * 64, **same), dict(file_sha256 = "b" * 64, **same)]
+ assert not _baseline_duplicates(entries)
+
+ path = tmp_path / "baseline.json"
+ path.write_text(json.dumps({"version": 1, "entries": entries}))
+ loaded = sp._load_baseline(str(path))
+ assert list(loaded.values()) == [{"a" * 64, "b" * 64}], "the loader unions the pins"
+
+
+ "pins",
+ [
+ (None, None), # the same unpinned approval, written twice
+ ("c" * 64, "c" * 64), # the same pin, written twice
+ (None, "c" * 64), # unpinned wins, so the pinned entry is inert
+ ("c" * 64, None), # and in either order
+ ],
+)
+def test_a_key_that_says_the_same_thing_twice_is_still_a_duplicate(pins):
+ same = dict(
+ package = "requests",
+ file = "requests/api.py",
+ check = "C2 polling/beaconing loop detected",
+ evidence_hash = sp._evidence_hash("L1: while True:"),
+ )
+ entries = [dict(same, **({"file_sha256": p} if p else {})) for p in pins]
+ assert _baseline_duplicates(entries) == {_baseline_key(entries[0])}
+
+
+def test_the_duplicate_check_sees_through_normalization(tmp_path):
+ """Two entries that differ only in the ways `_load_baseline` normalizes away
+ are one runtime key, so a raw field comparison reports nothing and leaves
+ exactly the review ambiguity the check exists to catch."""
+ import json
+
+ same = dict(
+ check = "C2 polling/beaconing loop detected",
+ severity = "CRITICAL",
+ evidence = "L1: while True:",
+ evidence_hash = sp._evidence_hash("L1: while True:"),
+ )
+ entries = [
+ dict(package = "huggingface_hub", file = "foo-1.0/huggingface_hub/a.py", **same),
+ dict(package = "huggingface-hub", file = "huggingface_hub/a.py", **same),
+ ]
+ assert _baseline_duplicates(entries) == {_baseline_key(entries[0])}
+
+ raw = [(e["package"], e["file"], e["check"], e["evidence_hash"]) for e in entries]
+ assert raw[0] != raw[1], "a raw comparison would have called these distinct"
+
+ # And the loader agrees: two entries in, one key out.
+ path = tmp_path / "baseline.json"
+ path.write_text(json.dumps({"version": 1, "entries": entries}))
+ assert len(sp._load_baseline(str(path))) == 1