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.

Re-approve three unsloth-zoo exec sites, pinned, and say which reviews are which (#11318)

Daniel Han020304267edd
diff --git a/scripts/scan_packages.py b/scripts/scan_packages.py
index 59484aeb675..6efdc2290ee 100644
--- a/scripts/scan_packages.py
+++ b/scripts/scan_packages.py
@@ -56,6 +56,7 @@ Exit codes:
import argparse
import atexit
import bisect
+import collections
import contextlib
import hashlib
import io
@@ -2625,6 +2626,145 @@ def _load_baseline(path: str) -> "dict[tuple[str, str, str, str], set[str] | Non
return keys
+def _evidence_spans(evidence: str) -> list[str]:
+ """Canonical matched lines of one finding, in discovery order."""
+ canon = _canon_evidence(evidence)
+ return canon.split("\n") if canon else []
+
+
+def _load_baseline_evidence(
+ path: "str | None",
+) -> "dict[tuple[str, str, str], list[tuple[str, list[str]]]]":
+ """Load the baseline's own evidence, grouped by site, as {(package, relpath, check): [(evidence_hash, spans)]}.
+
+ `_load_baseline` deliberately reduces every entry to a key and its pins, which is all
+ the gate needs. Saying *why* a reviewed site reopened needs the entry's matched lines
+ as well, so this reads them separately rather than widening the suppression loader.
+ """
+ if not path:
+ return {}
+ try:
+ with open(path, "r", encoding = "utf-8") as fh:
+ data = json.load(fh)
+ except (OSError, json.JSONDecodeError):
+ return {}
+ if not isinstance(data, dict) or not isinstance(data.get("entries"), list):
+ return {}
+ sites: dict[tuple[str, str, str], list[tuple[str, list[str]]]] = {}
+ for e in data["entries"]:
+ if not isinstance(e, dict):
+ continue
+ try:
+ site = (_norm_pkg(e["package"]), _relpath_in_package(e["file"]), e["check"])
+ except (KeyError, TypeError):
+ continue
+ evidence = e.get("evidence") or ""
+ digest = e.get("evidence_hash") or _evidence_hash(evidence)
+ sites.setdefault(site, []).append((digest, _evidence_spans(evidence)))
+ return sites
+
+
+def _span_delta(active_spans: list[str], baseline_spans: list[str]) -> tuple[int, int]:
+ """(added, removed) matched lines, as a multiset difference against one baseline entry."""
+ added = collections.Counter(active_spans)
+ removed = collections.Counter(baseline_spans)
+ shared = added & removed
+ added -= shared
+ removed -= shared
+ return sum(added.values()), sum(removed.values())
+
+
+def _classify_reviewed_site(f: Finding, entries: list[tuple[str, list[str]]]) -> tuple[str, str]:
+ """Say what actually differs between a reopened finding and the baseline entries at its site.
+
+ Three cases reach the report identically and want different reviews:
+
+ * the same matched code under a pin that no longer matches -- nothing flagged changed,
+ something else in that file did, and the pin is doing exactly its job;
+ * new matched lines inside a reviewed file -- a genuinely new occurrence, which wants
+ the same read a brand new site would get, not a glance at a diff;
+ * matched lines edited, removed or reordered -- the narrow "did known metaprogramming
+ move" question.
+
+ Membership of `(package, file, check)` alone cannot tell them apart, because a file's
+ matches are aggregated into one finding: an added `exec` reopens the same key an edited
+ one does.
+ """
+ spans = _evidence_spans(f.evidence)
+ digest = _evidence_hash(f.evidence)
+ if any(digest == h for h, _ in entries):
+ return ("pin", "same matched code, file digest outside the pin")
+ best = min((_span_delta(spans, b) for _, b in entries), key = lambda d: (d[0] + d[1], d[0]))
+ added, removed = best
+ if added and not removed:
+ return ("unread", f"{added} matched line(s) appended to a reviewed file, none gone")
+ if added:
+ # A multiset diff cannot tell "this line was rewritten" from "one went, an unrelated
+ # one arrived", and it does not need to: both leave matched code that was never read
+ # in its current form, which is the same position a new occurrence puts you in.
+ # Narrowing the full read to strict additions would route exec(compile(src, path,
+ # "exec")) -> exec(payload) -- an edit, by the diff -- to the "did it just move" path.
+ return (
+ "unread",
+ f"{added} matched line(s) added and {removed} gone: the flagged code was rewritten",
+ )
+ if removed:
+ return ("edited", f"{removed} matched line(s) gone, none added")
+ return ("edited", "matched lines reordered")
+
+
+def _report_reviewed_sites(
+ active: list[Finding],
+ baseline: "dict[tuple[str, str, str, str], set[str] | None]",
+ baseline_path: "str | None",
+) -> None:
+ """Separate "this reviewed site changed" from "this site is new", and say which kind of change.
+
+ Both reach the report as an identical CRITICAL/HIGH line, and they need different
+ reviews: the first asks whether known metaprogramming moved, the second asks whether
+ something dangerous just appeared. A file's matches are aggregated into a single
+ finding, though, so a reviewed site also reopens when a *new* occurrence is appended to
+ it -- that one wants the full read, not a diff -- and a pinned entry reopens on an edit
+ elsewhere in the file with its matched code untouched. Each line below says which it
+ is. The gate is unchanged: every finding above still fails the run.
+ """
+ if not baseline or not active:
+ return
+ site_evidence = _load_baseline_evidence(baseline_path)
+ reviewed_sites = {(pkg, path, check) for pkg, path, check, _ in baseline}
+ moved = [
+ f
+ for f in active
+ if (_norm_pkg(f.package), _relpath_in_package(f.filename), f.check) in reviewed_sites
+ ]
+ if not moved:
+ return
+ print(
+ f"\n {len(moved)} of the {len(active)} finding(s) above are at a site already "
+ f"reviewed in {baseline_path}. What differs from the reviewed entry:"
+ )
+ verdicts = []
+ for f in sorted(moved, key = lambda f: (f.package, _relpath_in_package(f.filename))):
+ rel = _relpath_in_package(f.filename)
+ entries = site_evidence.get((_norm_pkg(f.package), rel, f.check), [])
+ kind, why = (
+ _classify_reviewed_site(f, entries)
+ if entries
+ else ("unknown", "baseline entry carries no evidence")
+ )
+ verdicts.append(kind)
+ print(f" {f.severity} {f.package} {rel} ({f.check})\n {why}")
+ if "unread" in verdicts:
+ print(
+ " A site reporting added or rewritten matched lines carries flagged code that "
+ "was never reviewed in its current form: read it as you would a new site."
+ )
+ print(
+ " Re-review, then regenerate with --write-baseline. A finding with no line here "
+ "is at a site that was never reviewed and wants a full read of the file."
+ )
+
+
def _write_baseline(
path: str,
findings: list[Finding],
@@ -2880,6 +3020,7 @@ def main() -> int:
active, suppressed = _partition_baseline(all_findings, baseline)
print_findings(active)
+ _report_reviewed_sites(active, baseline, baseline_path)
if suppressed:
crit_s = sum(1 for f in suppressed if f.severity == CRITICAL)
high_s = sum(1 for f in suppressed if f.severity == HIGH)
diff --git a/scripts/scan_packages_baseline.json b/scripts/scan_packages_baseline.json
index 60f86826825..5747cc5c216 100644
--- a/scripts/scan_packages_baseline.json
+++ b/scripts/scan_packages_baseline.json
@@ -1989,6 +1989,33 @@
"evidence": "Obfusc: L4855: _mod = __import__(module_name, fromlist=[\"_\"])\nExec: L158: mx.eval(model.parameters()) | L192: mx.eval([v for k, v in tree_flatten(casted) if is_mlx_norm_parameter_path(k)]) | L1254: mx.eval(model.parameters()) | L1255: mx.eval(mx.distributed.all_sum(mx.array(1.0), stream=mx.cpu)) | L1322: mx.eval(model.parameters()) | L1325: mx.eval(mx.distributed.all_sum(mx.array(1.0), stream=mx.cpu)) | L2711: mx.eval(altup.correct(predictions, activated)) | L3210: mx.eval(baseline, perturbed) | L4503: model.eval() | L5242: mx.eval(model.parameters()) | L5357: mx.eval(module.weight) | L8769: lambda: mx.eval(model.parameters()), | L8829: lambda: mx.eval(model.parameters()), | L8985: mx.eval(model.parameters())",
"evidence_hash": "cf9c8ed7bf9c33f83b6280d7d2d60b072262a04ce3e886a1f38c4003fbef01f4",
"file_sha256": "a865c6b908885b4a7a7df38dc6960c645f1b60b011b720d741e02092ee239903"
+ },
+ {
+ "package": "unsloth-zoo",
+ "file": "unsloth_zoo/compiler.py",
+ "check": "Advanced obfuscation (marshal/compile/zlib) + exec/eval",
+ "severity": "HIGH",
+ "evidence": "Obfusc: L1603: exec(compile(source, file_location, \"exec\"), new_module.__dict__) | L1842: _mod = __import__(model_location, fromlist=items) | L6260: f\" {chr(92)}{chr(92)} /| Num examples = {num_examples:,} | Num Epochs = {num_train_epochs:,} | Total steps = {max_steps:,}\\\\n\"\\\\\nL6261: f\"O^O/ {chr(92)}_/ {chr(92)} Batch size per device = {self._train_batch_size:,} | Gradient accumulation steps = {args.gradient_accumulation_steps}\\\\n\"\\\\\nL6262: f\"{chr(92)} / Data Parallel GPUs = {args.world_size} | Total batch size ({self._train_batch_size} x {args.gradient_accumulation_steps} x {args.world_size}) = {total_train_batch_size: sha256:b7ea9cdbe360ad323911014caf12a9d4ec87cb36195a511080e4e484c2fb80d2\nL6263: f' \"-____-\" Trainable parameters = {get_model_param_count(model, trainable_only=True):,} of {get_model_param_count(model):,} ({get_model_param_count(model, trainable_only=True)/get_model_p sha256:9e832c1e7b2815aa44dc42638d821cb9df22550db88d85bd4bfb29c13f984b53 | L6261: f\"O^O/ {chr(92)}_/ {chr(92)} Batch size per device = {self._train_batch_size:,} | Gradient accumulation steps = {args.gradient_accumulation_steps}\\\\n\"\\\\\nL6262: f\"{chr(92)} / Data Parallel GPUs = {args.world_size} | Total batch size ({self._train_batch_size} x {args.gradient_accumulation_steps} x {args.world_size}) = {total_train_batch_size: sha256:b7ea9cdbe360ad323911014caf12a9d4ec87cb36195a511080e4e484c2fb80d2\nL6263: f' \"-____-\" Trainable parameters = {get_model_param_count(model, trainable_only=True):,} of {get_model_param_count(model):,} ({get_model_param_count(model, trainable_only=True)/get_model_p sha256:9e832c1e7b2815aa44dc42638d821cb9df22550db88d85bd4bfb29c13f984b53 | L6260: f\" {chr(92)}{chr(92)} /| Num examples = {num_examples:,} | Num Epochs = {num_train_epochs:,} | Total steps = {max_steps:,}\\\\n\"\\\\\nL6261: f\"O^O/ {chr(92)}_/ {chr(92)} Batch size per device = {self._train_batch_size:,} | Gradient accumulation steps = {args.gradient_accumulation_steps}\\\\n\"\\\\\nL6262: f\"{chr(92)} / Data Parallel GPUs = {args.world_size} | Total batch size ({self._train_batch_size} x {args.gradient_accumulation_steps} x {args.world_size}) = {total_train_batch_size: sha256:b7ea9cdbe360ad323911014caf12a9d4ec87cb36195a511080e4e484c2fb80d2\nExec: L1603: exec(compile(source, file_location, \"exec\"), new_module.__dict__) | L1771: _modeling_file = eval(model_location) | L2498: f = eval(f\"{model_location}.{module}\") | L2839: exec(f\"def raise_{j}(*args, **kwargs): print('{function}')\", globals(), locals()) | L2840: try: exec(f\"EMPTY_LOGITS.{function} = raise_{j}\", globals(), locals()) | L4322: exec(f\"import {parent}\", locals(), globals()) | L4436: dir(eval(parent)), | L4440: exec(f\"{parent}.{child}.forward = forward\", globals(), locals()) | L4514: module = eval(f\"modeling_file.{module}\") | L4541: inner_class = eval(f\"modeling_file.{inner_class}\") | L4671: exec(f\"from timm.layers.norm_act import {norm}\") | L4679: forward = eval(norm).forward | L4685: exec(f\"timm.layers.norm_act.{norm}.forward = forward\") | L4702: exec(f\"from timm.models._efficientnet_blocks import {block}\") | L4710: forward = eval(block).forward | L4716: exec(f\"timm.models._efficientnet_blocks.{block}.forward = forward\") | L4956: exec(\nL4957: f\"{model_location}.torch.nn.{module}.forward = forward\",\nL4958: globals(),\nL4959: locals(),\nL4960: ) | L4962: exec(f\"{model_location}.nn.{module}.forward = forward\", globals(), locals()) | L4966: exec(\nL4967: f\"combined_module.torch.nn.{module}.forward = forward\",\nL4968: globals(),\nL4969: locals(),\nL4970: ) | L4972: exec(f\"combined_module.nn.{module}.forward = forward\", globals(), locals()) | L5049: source = eval(f\"{model_location}.torch\") | L5056: function = eval(f\"source.nn.{module}\") | L5250: exec(\"import transformers\", globals()) | L5263: exec(\nL5264: \"model_logger.addFilter(HideLoggingMessage('`use_cache`'))\", globals(), locals()\nL5265: ) | L5267: exec(\nL5268: \"model_logger.addFilter(HideLoggingMessage('compile_config'))\",\nL5269: globals(),\nL5270: locals(),\nL5271: ) | L5505: source = eval(f\"modeling_file.{module}\") | L5519: source = eval(f\"modeling_file.{module}\") | L5620: source = eval(f\"modeling_file.{module}\") | L5658: source = eval(f\"{model_location}.{module}\") | L5738: source = eval(f\"{model_location}.{module}\") | L5786: source = eval(f\"{model_location}.{module}\") | L6023: source = eval(f\"{model_location}.{module}\") | L6034: exec(\nL6035: f\"{model_location}.{module}._update_causal_mask = no_update_causal_mask\",\nL6036: globals(),\nL6037: ) | L6100: source = eval(f\"{model_location}.{module}\") | L6141: module_cls = eval(f\"{model_location}.{module}\") | L6178: module_cls = eval(f\"{model_location}.{module}\") | L6245: exec(\nL6246: \"from transformers.trainer import (\" + \", \".join(x for x in good_items) + \")\",\nL6247: globals(),\nL6248: ) | L6310: exec(inner_training_loop, globals()) | L6319: function = eval(f\"{model_location}.{module}\") | L6406: function = eval(f\"{model_location}.{module}\") | L6560: exec(\nL6561: f\"{model_location}.{module} = combined_module.{module}\",\nL6562: globals(),\nL6563: locals(),\nL6564: ) | L6574: check_dicts = dir(eval(f\"{model_location}\")) | L6576: item = eval(f\"{model_location}.{check}\") | L6586: exec(\nL6587: f\"{model_location}.{check}['{key}'] = combined_module.{replaced_class}\",\nL6588: globals(),\nL6589: locals(),\nL6590: )",
+ "evidence_hash": "aaba0e6f7c289e2d6ee4ac8ff927d691a450fb3913a4245aa0fab585dd28d871",
+ "file_sha256": "17a5c7785c98fbc483f6dbf8c54ae6df3e15a65ee60b404d4d12fee609e4e928"
+ },
+ {
+ "package": "unsloth-zoo",
+ "file": "unsloth_zoo/vllm_utils.py",
+ "check": "Advanced obfuscation (marshal/compile/zlib) + exec/eval",
+ "severity": "HIGH",
+ "evidence": "Obfusc: L3781: lora_types.append(getattr(__import__(module_path, fromlist = [class_name]), class_name))\nExec: L1231: cross_attn_layer = eval(f'vllm_internals.{name}') | L1266: layernorm = eval(vllm_name).state_dict()[\"weight\"] | L1567: parent = eval(f\"new_model.{parent_path}\") if parent_path else new_model | L1572: exec(f\"new_model.{layer_name_br} = layer\") | L1642: exec(f\"new_model.{layer_name_br}.weight = None\") | L1643: exec(f\"new_model.{layer_name_br}.weight = weight_param\") | L1645: exec(f\"new_model.{layer_name_br}.bias = None\") | L1646: exec(f\"new_model.{layer_name_br}.bias = bias\") | L1652: exec(f\"new_model.{layer_name} = layer\") | L1668: new_model.eval() | L3600: exec(f\"module.to({dtype})\") | L3623: exec(f\"module.to({dtype})\") | L4536: model.eval() | L4576: new_model.eval()",
+ "evidence_hash": "4928e730b84a3fa71dcc50cfe185c77c2028be09ad908706710ab864786b4659",
+ "file_sha256": "105d9cbd9defda789da66044f1d71f416f441aafd17bc424efbb91f05cdac823"
+ },
+ {
+ "package": "unsloth-zoo",
+ "file": "unsloth_zoo/temporary_patches/moe_utils.py",
+ "check": "Advanced obfuscation (marshal/compile/zlib) + exec/eval",
+ "severity": "HIGH",
+ "evidence": "Obfusc: L330: exec(compile(cached_bytes, cache_file, \"exec\"), module.__dict__)\nExec: L330: exec(compile(cached_bytes, cache_file, \"exec\"), module.__dict__)",
+ "evidence_hash": "ef22a4fe0d8ec7e2b0e4afc6e6a3d997cd94c981bf59790562217d4f9dd70732",
+ "file_sha256": "5a5b88e76bd20c9fa1607d962a768b748eb8086d9c77e799b0f67bf00c99214b"
}
]
}
diff --git a/tests/security/test_scan_packages.py b/tests/security/test_scan_packages.py
index 93fa7f1201f..340dae3f386 100644
--- a/tests/security/test_scan_packages.py
+++ b/tests/security/test_scan_packages.py
@@ -3,7 +3,9 @@
from __future__ import annotations
+import contextlib
import hashlib
+import io
import json
import os
import re
@@ -512,6 +514,21 @@ def test_context_dependent_unsloth_zoo_findings_are_digest_pinned():
"unsloth_zoo/compiler.py",
"Advanced obfuscation (marshal/compile/zlib) + exec/eval",
),
+ # Both approved for the first time alongside compiler.py, and both for the same
+ # reason it is on this list: the matched lines are ordinary metaprogramming that
+ # says nothing about the rest of the file. vllm_utils.py builds attribute paths
+ # out of checkpoint-supplied state dict keys and execs them; moe_utils.py execs a
+ # cached copy of itself, having first compared it byte for byte against the
+ # in-tree source. Approving either on evidence alone would let a later payload in
+ # the same file ride an unchanged match.
+ (
+ "unsloth_zoo/vllm_utils.py",
+ "Advanced obfuscation (marshal/compile/zlib) + exec/eval",
+ ),
+ (
+ "unsloth_zoo/temporary_patches/moe_utils.py",
+ "Advanced obfuscation (marshal/compile/zlib) + exec/eval",
+ ),
}
# The evidence hashes of the superseded compiler.py variants, which are already
# in the baseline unpinned. These are frozen by construction: an evidence hash is
@@ -2713,3 +2730,119 @@ def test_building_the_fixtures_leaves_the_callers_environment_alone() -> None:
os.environ.pop("SOURCE_DATE_EPOCH", None)
else:
os.environ["SOURCE_DATE_EPOCH"] = previous
+
+
+# --- what a reopened baseline entry is told to be -----------------------------------------
+
+
+def _reviewed_site_report(tmp_path, entries, findings):
+ """Run the reopened-site reporter over a hand-written baseline and return what it printed."""
+ path = tmp_path / "baseline.json"
+ path.write_text(json.dumps({"entries": entries}), encoding = "utf-8")
+ loaded = sp._load_baseline(str(path))
+ buffer = io.StringIO()
+ with contextlib.redirect_stdout(buffer):
+ sp._report_reviewed_sites(findings, loaded, str(path))
+ return buffer.getvalue()
+
+
+_EXEC_CHECK = "Advanced obfuscation (marshal/compile/zlib) + exec/eval"
+
+
+def _exec_entry(evidence, digest = None):
+ entry = {
+ "package": "unsloth-zoo",
+ "file": "unsloth_zoo/compiler.py",
+ "check": _EXEC_CHECK,
+ "severity": sp.HIGH,
+ "evidence": evidence,
+ "evidence_hash": sp._evidence_hash(evidence),
+ }
+ if digest is not None:
+ entry["file_sha256"] = digest
+ return entry
+
+
+def _exec_finding(evidence, digest = ""):
+ return sp.Finding(
+ sp.HIGH, "unsloth-zoo", "unsloth_zoo/compiler.py", _EXEC_CHECK, evidence, digest
+ )
+
+
+def test_an_occurrence_added_to_a_reviewed_file_is_not_called_a_mere_change(tmp_path):
+ """A file's matches are aggregated into one finding, so appending a new `exec` reopens the
+ same `(package, file, check)` an edited one does. Membership of that triple therefore cannot
+ mean "the flagged code changed": reporting it that way sends a genuinely new occurrence down
+ the narrow "did known metaprogramming move" path, which is the one review it must not get.
+ """
+ report = _reviewed_site_report(
+ tmp_path,
+ [_exec_entry("L10: exec(compile(src, path, 'exec'))")],
+ [_exec_finding("L10: exec(compile(src, path, 'exec'))\nL88: eval(user_supplied)")],
+ )
+ assert "1 matched line(s) appended to a reviewed file, none gone" in report, report
+ assert "read it as you would a new site" in report, report
+
+
+def test_a_pin_miss_says_the_matched_code_is_unchanged(tmp_path):
+ """Identical evidence under a digest pin that no longer matches: the flagged lines did not
+ change, some other edit to the file did. Calling that "different evidence" describes the one
+ thing that is provably the same.
+ """
+ evidence = "L10: exec(compile(src, path, 'exec'))"
+ report = _reviewed_site_report(
+ tmp_path,
+ [_exec_entry(evidence, digest = "a" * 64)],
+ [_exec_finding(evidence, digest = "b" * 64)],
+ )
+ assert "same matched code, file digest outside the pin" in report, report
+ assert "new matched line(s)" not in report, report
+
+
+def test_a_rewritten_match_is_called_a_rewrite_and_not_a_new_occurrence(tmp_path):
+ report = _reviewed_site_report(
+ tmp_path,
+ [_exec_entry("L10: exec(compile(src, path, 'exec'))")],
+ [_exec_finding("L10: exec(compile(src, path, 'exec'), module.__dict__)")],
+ )
+ assert "1 matched line(s) added and 1 gone: the flagged code was rewritten" in report, report
+ assert "appended" not in report, report
+
+
+def test_a_rewritten_match_still_asks_for_the_full_read(tmp_path):
+ """Deliberate, and the reason it is pinned: a multiset diff cannot separate "this line was
+ rewritten" from "one left, an unrelated one arrived", and both leave flagged code nobody has
+ read in its current form. Reserving the full read for strict additions would send
+ `exec(compile(src, path, "exec"))` -> `exec(payload)` -- an edit, by the diff -- down the
+ narrow "did known metaprogramming move" path, which is the review that misses it.
+ """
+ report = _reviewed_site_report(
+ tmp_path,
+ [_exec_entry("L10: exec(compile(src, path, 'exec'))")],
+ [_exec_finding("L10: exec(payload)")],
+ )
+ assert "read it as you would a new site" in report, report
+
+
+def test_a_match_that_only_disappeared_does_not_ask_for_the_full_read(tmp_path):
+ """The other side of that choice: nothing flagged arrived, so there is nothing unread."""
+ report = _reviewed_site_report(
+ tmp_path,
+ [_exec_entry("L10: exec(one)\nL20: exec(two)")],
+ [_exec_finding("L10: exec(one)")],
+ )
+ assert "1 matched line(s) gone, none added" in report, report
+ assert "read it as you would a new site" not in report, report
+
+
+def test_a_site_that_was_never_reviewed_gets_no_line(tmp_path):
+ report = _reviewed_site_report(
+ tmp_path,
+ [_exec_entry("L10: exec(compile(src, path, 'exec'))")],
+ [
+ sp.Finding(
+ sp.HIGH, "unsloth-zoo", "unsloth_zoo/vllm_utils.py", _EXEC_CHECK, "L4: exec(new)"
+ )
+ ],
+ )
+ assert report.strip() == "", report