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
Add a fast lint gate for risky loader call sites (#12636)
Daniel Hane8a4bc9bab3f
diff --git a/.github/workflows/lint-ci.yml b/.github/workflows/lint-ci.ymlindex 9bb6fba248c..269886ee65f 100644--- a/.github/workflows/lint-ci.yml+++ b/.github/workflows/lint-ci.yml@@ -173,6 +173,14 @@ jobs:- name: No new code shapes that heuristic scanners quarantinerun: python scripts/lint_av_shapes.py+ - name: Risky-loader gate self-test+ run: python scripts/lint_risky_loaders.py --self-test++ # Dynamic imports, trust_remote_code turned on in code, unpinned code fetches and+ # unsafe deserialization. Existing sites live in scripts/risky_loaders_baseline.json.+ - name: No new risky loader call sites+ run: python scripts/lint_risky_loaders.py+- name: Python ruff check (whole repo)# The narrow rule set in pyproject.toml [tool.ruff.lint]# selects E9 / F63 / F7 / F82 -- syntax errors, brokendiff --git a/scripts/lint_risky_loaders.py b/scripts/lint_risky_loaders.pynew file mode 100644index 00000000000..e0c54c6526f--- /dev/null+++ b/scripts/lint_risky_loaders.py@@ -0,0 +1,681 @@+#!/usr/bin/env python3+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.+"""Fail CI on a new call site that loads code or objects in a way a model repo can steer.++`lint_exec_literals.py` covers `exec`, `eval` and `compile`. These are the other+shapes behind holes fixed here since July 2026, each as one rule:++ dynamic-import import_module / __import__ / import_from_string /+ get_class_from_dynamic_module / spec_from_file_location /+ runpy / pydoc.locate handed a name that is not written out+ (sentence-transformers modules.json, mlx `model_type`).+ trust-remote-code `trust_remote_code` switched on in code, rather than by the+ caller: a default, an assignment, a dict entry or a literal+ keyword (the Studio defaults that ran Hub code unasked).+ unpinned-code-fetch Python fetched from a branch head (`.../main/x.py`), a+ `git clone` with no checkout of a fixed revision, or a Hub+ download with no `revision` in a function that then puts+ the result on `sys.path` or loads a file from it (Spark-TTS).+ unsafe-deserialize pickle / dill / joblib / marshal / shelve, `torch.load` with+ `weights_only` not left at the safe default, `np.load` with+ `allow_pickle=True`, `yaml.load` without a safe loader.++Every rule is one AST pass and a few set lookups, so the whole tree takes seconds.+Existing call sites are recorded in a baseline beside this script with a reason+each, keyed on the call's own text rather than its line, with a count, exactly as+in `lint_exec_literals.py`: only a new site fails.++ python scripts/lint_risky_loaders.py # check, exit 1 on a new site+ python scripts/lint_risky_loaders.py --update # rewrite the baseline+ python scripts/lint_risky_loaders.py --self-test # prove the rules still fire+"""++from __future__ import annotations++import argparse+import ast+import hashlib+import json+import re+import sys+from pathlib import Path++REPO_ROOT = Path(__file__).resolve().parents[1]+BASELINE_PATH = Path(__file__).resolve().parent / "risky_loaders_baseline.json"++EXCLUDED_PARTS = frozenset(+ {+ "tests",+ "node_modules",+ "build",+ "dist",+ ".venv",+ "venv",+ "site-packages",+ ".git",+ ".tox",+ ".mypy_cache",+ ".pytest_cache",+ "__pycache__",+ ".ipynb_checkpoints",+ ".eggs",+ "unsloth_compiled_cache",+ }+)++DYNAMIC_IMPORTS = {+ "importlib.import_module": "import_module",+ "importlib.__import__": "__import__",+ "importlib.util.spec_from_file_location": "spec_from_file_location",+ "importlib.machinery.SourceFileLoader": "SourceFileLoader",+ "runpy.run_path": "runpy.run_path",+ "runpy.run_module": "runpy.run_module",+ "pydoc.locate": "pydoc.locate",+ "pkgutil.resolve_name": "pkgutil.resolve_name",+}+# Matched on the last name alone: distinctive enough, and reached through several+# import paths (`transformers.dynamic_module_utils`, `sentence_transformers.util`).+DYNAMIC_IMPORT_TAILS = {+ "import_from_string": "import_from_string",+ "get_class_from_dynamic_module": "get_class_from_dynamic_module",+ "get_class_in_module": "get_class_in_module",+}++PICKLE_LIKE = {+ f"{module}.{function}"+ for module in ("pickle", "_pickle", "cPickle", "dill", "cloudpickle")+ for function in ("load", "loads", "Unpickler")+} | {+ "joblib.load",+ "marshal.load",+ "marshal.loads",+ "shelve.open",+ "pandas.read_pickle",+ "yaml.unsafe_load",+ "yaml.unsafe_load_all",+}+SAFE_YAML_LOADERS = {"SafeLoader", "CSafeLoader", "BaseLoader", "CBaseLoader"}++HUB_DOWNLOADS = {"snapshot_download", "hf_hub_download"}+CODE_LOADERS = {+ "sys.path.insert",+ "sys.path.append",+ "importlib.util.spec_from_file_location",+ "importlib.machinery.SourceFileLoader",+ "runpy.run_path",+}+BRANCH_HEAD_PY = re.compile(+ r"(?:raw\.githubusercontent\.com/[^/\s]+/[^/\s]+/(?:refs/heads/)?(?:main|master)/"+ r"|github\.com/[^/\s]+/[^/\s]+/raw/(?:refs/heads/)?(?:main|master)/)\S*\.py\b"+)+PIN_WORDS = {"checkout", "--revision", "reset"}+++def _imports(tree: ast.AST) -> dict:+ """Local name -> dotted origin, so aliases (`import importlib as il`) resolve."""+ table = {}+ for node in ast.walk(tree):+ if isinstance(node, ast.ImportFrom) and node.module and not node.level:+ for alias in node.names:+ table[alias.asname or alias.name] = f"{node.module}.{alias.name}"+ elif isinstance(node, ast.Import):+ for alias in node.names:+ if alias.asname:+ table[alias.asname] = alias.name+ else:+ head = alias.name.split(".")[0]+ table[head] = head+ return table+++def _dotted(node: ast.AST) -> str:+ parts = []+ while isinstance(node, ast.Attribute):+ parts.append(node.attr)+ node = node.value+ if not isinstance(node, ast.Name):+ return ""+ parts.append(node.id)+ return ".".join(reversed(parts))+++def _qualified(func: ast.AST, table: dict) -> str:+ name = _dotted(func)+ if not name:+ return ""+ head, _, rest = name.partition(".")+ if head == "__import__":+ return "importlib.__import__"+ if head in table:+ return table[head] + ("." + rest if rest else "")+ return name+++def _is_written_out(node: ast.AST) -> bool:+ if isinstance(node, ast.Constant):+ return isinstance(node.value, (str, bytes))+ if isinstance(node, ast.JoinedStr):+ return not any(isinstance(part, ast.FormattedValue) for part in node.values)+ if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):+ return _is_written_out(node.left) and _is_written_out(node.right)+ return False+++def _is_true(node: ast.AST) -> bool:+ return isinstance(node, ast.Constant) and node.value is True+++def _keyword(call: ast.Call, name: str):+ for keyword in call.keywords:+ if keyword.arg == name:+ return keyword.value+ return None+++def _first_argument(call: ast.Call, keyword: str):+ if call.args:+ return call.args[0]+ return _keyword(call, keyword)+++# Sinks whose second argument is the file that gets executed, and its keyword.+_PATH_KEYWORDS = {+ "spec_from_file_location": "location",+ "SourceFileLoader": "path",+ "get_class_in_module": "module_path",+}+_TARGET_KEYWORDS = {+ "runpy.run_path": "path_name",+ "runpy.run_module": "mod_name",+ "pydoc.locate": "path",+}+++def _dynamic_import(call: ast.Call, qualified: str):+ sink = DYNAMIC_IMPORTS.get(qualified) or DYNAMIC_IMPORT_TAILS.get(qualified.split(".")[-1])+ if sink is None:+ return None+ if sink in _PATH_KEYWORDS:+ keyword = _PATH_KEYWORDS[sink]+ target = call.args[1] if len(call.args) > 1 else _keyword(call, keyword)+ else:+ target = _first_argument(call, _TARGET_KEYWORDS.get(sink, "name"))+ if target is None or _is_written_out(target):+ return None+ return sink+++def _unsafe_deserialize(call: ast.Call, qualified: str):+ if qualified in PICKLE_LIKE:+ return qualified+ if qualified == "torch.load":+ # Omitted is unsafe too: torch < 2.6, still supported, defaults to weights_only=False.+ if not _is_true(_keyword(call, "weights_only")):+ return "torch.load"+ elif qualified == "numpy.load":+ if _is_true(_keyword(call, "allow_pickle")):+ return "numpy.load"+ elif qualified in ("yaml.load", "yaml.load_all"):+ loader = _keyword(call, "Loader")+ if loader is None and len(call.args) > 1:+ loader = call.args[1]+ if loader is None or _dotted(loader).split(".")[-1] not in SAFE_YAML_LOADERS:+ return qualified+ return None+++def _true_field_default(value: ast.AST) -> bool:+ """`Field(True, ...)` / `field(default = True)`: the schema default Studio's request models use."""+ if not (+ isinstance(value, ast.Call) and _dotted(value.func).split(".")[-1] in ("Field", "field")+ ):+ return False+ return (bool(value.args) and _is_true(value.args[0])) or _is_true(_keyword(value, "default"))+++def _trust_remote_code(node: ast.AST):+ """`trust_remote_code` turned on by the code itself."""+ if isinstance(node, ast.Call):+ if _is_true(_keyword(node, "trust_remote_code")):+ return "keyword"+ func = node.func+ if (+ isinstance(func, ast.Attribute)+ and func.attr == "setdefault"+ and len(node.args) == 2+ and isinstance(node.args[0], ast.Constant)+ and node.args[0].value == "trust_remote_code"+ and _is_true(node.args[1])+ ):+ return "setdefault"+ elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)):+ arguments = node.args+ positional = arguments.posonlyargs + arguments.args+ pairs = list(+ zip(positional[len(positional) - len(arguments.defaults) :], arguments.defaults)+ )+ pairs += [+ (a, d) for a, d in zip(arguments.kwonlyargs, arguments.kw_defaults) if d is not None+ ]+ if any(a.arg == "trust_remote_code" and _is_true(d) for a, d in pairs):+ return "default"+ elif (+ isinstance(node, (ast.Assign, ast.AnnAssign))+ and node.value is not None+ and (_is_true(node.value) or _true_field_default(node.value))+ ):+ targets = node.targets if isinstance(node, ast.Assign) else [node.target]+ for target in targets:+ if isinstance(target, (ast.Name, ast.Attribute)) and (+ getattr(target, "id", None) == "trust_remote_code"+ or getattr(target, "attr", None) == "trust_remote_code"+ ):+ return "assignment"+ if (+ isinstance(target, ast.Subscript)+ and isinstance(target.slice, ast.Constant)+ and target.slice.value == "trust_remote_code"+ ):+ return "item"+ elif isinstance(node, ast.Dict):+ for key, value in zip(node.keys, node.values):+ if (+ isinstance(key, ast.Constant)+ and key.value == "trust_remote_code"+ and _is_true(value)+ ):+ return "dict"+ return None+++def _git_words(command) -> list:+ """The written-out words of a git command, from an argv list or a shell string; [] otherwise."""+ if isinstance(command, (ast.List, ast.Tuple)):+ first = command.elts[0] if command.elts else None+ if not (isinstance(first, ast.Constant) and first.value == "git"):+ return []+ words = [+ e.value+ for e in command.elts+ if isinstance(e, ast.Constant) and isinstance(e.value, str)+ ]+ else:+ if isinstance(command, ast.JoinedStr) and command.values:+ command = command.values[0]+ if (+ not (isinstance(command, ast.Constant) and isinstance(command.value, str))+ or "git " not in command.value+ ):+ return []+ # The first git command in a chain, so `cd llama.cpp && git reset --hard X` counts.+ segments = (part.split() for part in re.split(r"&&|;|\|\|", command.value))+ words = next((w for w in segments if w[:1] == ["git"]), [])+ return words if words[:1] == ["git"] else []+++def _unpinned_fetches(calls: list, git_commands: list) -> list:+ """Per function: a clone with no pin, or a revision-less Hub download feeding a code loader."""+ found = []+ loads_code = any(qualified in CODE_LOADERS for _, qualified in calls)+ for call, qualified in calls:+ if (+ qualified.split(".")[-1] in HUB_DOWNLOADS+ and loads_code+ and _keyword(call, "revision") is None+ ):+ found.append((call, "hub-download-loaded-as-code"))+ # Written-out git commands, passed directly or held in a variable first. A pin is a git+ # command that moves to a revision, not any string that mentions one.+ if not any(set(words) & PIN_WORDS for _, words in git_commands):+ found += [+ (node, "git-clone-unpinned") for node, words in git_commands if words[1:2] == ["clone"]+ ]+ return found+++def _key(relative: str, rule: str, sink: str, node: ast.AST) -> dict:+ return {+ "file": relative,+ "rule": rule,+ "sink": sink,+ "digest": hashlib.sha256(ast.unparse(node).encode()).hexdigest()[:16],+ "line": getattr(node, "lineno", 0),+ }+++_FUNCTIONS = (ast.FunctionDef, ast.AsyncFunctionDef)+_TRUST_SHAPES = (ast.Call, ast.Lambda, ast.Assign, ast.AnnAssign, ast.Dict) + _FUNCTIONS+++def _notebook_source(path: Path, relative: str) -> str:+ """A notebook's code cells as one module; `%magic` / `!command` lines are blanked to keep line numbers."""+ try:+ document = json.loads(path.read_text(encoding = "utf-8"))+ except (OSError, UnicodeDecodeError, json.JSONDecodeError) as error:+ raise SystemExit(f"{relative}: could not be read ({error.__class__.__name__})")+ lines = []+ for cell in document.get("cells") or []:+ if cell.get("cell_type") != "code":+ continue+ source = cell.get("source") or []+ for line in (source if isinstance(source, str) else "".join(source)).splitlines():+ lines.append("" if line.lstrip().startswith(("%", "!")) else line)+ lines.append("")+ return "\n".join(lines)+++def scan_file(path: Path, relative: str) -> list:+ source = _notebook_source(path, relative) if path.suffix == ".ipynb" else path.read_bytes()+ try:+ tree = ast.parse(source, filename = str(path))+ except (SyntaxError, ValueError, MemoryError, RecursionError) as error:+ if path.suffix == ".ipynb":+ # A notebook mid-edit need not parse as one module; failing the build on it would be noise.+ return []+ # Unparsed is unchecked, and reporting it clean is the bypass this gate exists to avoid.+ raise SystemExit(f"{relative}: could not be parsed ({error.__class__.__name__})")+ found = []+ imports = []+ calls_by_owner: dict = {}+ git_by_owner: dict = {}+ covered = set()+ stack = [(tree, tree)]+ while stack:+ node, owner = stack.pop()+ for child in ast.iter_child_nodes(node):+ if isinstance(child, _TRUST_SHAPES):+ shape = _trust_remote_code(child)+ if shape:+ # A function default is keyed on the signature, not the whole body.+ subject = child.args if isinstance(child, _FUNCTIONS) else child+ entry = _key(relative, "trust-remote-code", shape, subject)+ entry["line"] = child.lineno+ found.append(entry)+ if isinstance(child, ast.Call):+ calls_by_owner.setdefault(owner, []).append(child)+ elif isinstance(child, _FUNCTIONS):+ stack.append((child, child))+ continue+ elif isinstance(child, (ast.Import, ast.ImportFrom)):+ imports.append(child)+ elif isinstance(child, ast.Constant) and isinstance(child.value, str):+ if "/ma" in child.value and BRANCH_HEAD_PY.search(child.value):+ found.append(_key(relative, "unpinned-code-fetch", "branch-head-url", child))+ if (+ isinstance(child, (ast.List, ast.Tuple, ast.JoinedStr, ast.Constant))+ and id(child) not in covered+ ):+ words = _git_words(child)+ if words:+ git_by_owner.setdefault(owner, []).append((child, words))+ if isinstance(child, ast.JoinedStr):+ covered.add(id(child.values[0]))+ stack.append((child, owner))++ table = _imports(ast.Module(body = imports, type_ignores = []))+ # Insertion order, so findings print in the same order on every run.+ for owner in list(calls_by_owner) + [o for o in git_by_owner if o not in calls_by_owner]:+ calls = [(call, _qualified(call.func, table)) for call in calls_by_owner.get(owner, ())]+ for call, qualified in calls:+ sink = _dynamic_import(call, qualified)+ if sink:+ found.append(_key(relative, "dynamic-import", sink, call))+ sink = _unsafe_deserialize(call, qualified)+ if sink:+ found.append(_key(relative, "unsafe-deserialize", sink, call))+ for call, sink in _unpinned_fetches(calls, git_by_owner.get(owner, [])):+ found.append(_key(relative, "unpinned-code-fetch", sink, call))+ return found+++def collect(targets: list) -> list:+ found = []+ seen = set()+ for target in targets:+ root = REPO_ROOT / target+ if root.is_file() and root.suffix in (".py", ".ipynb"):+ paths = [root]+ elif root.is_dir():+ paths = sorted(q for suffix in ("*.py", "*.ipynb") for q in root.rglob(suffix))+ else:+ raise SystemExit(+ f"{target}: scan target does not exist, so nothing under it was checked"+ )+ for path in paths:+ # This file's self-test sources spell out every bad shape on purpose.+ if not path.is_file() or path.resolve() == Path(__file__).resolve():+ continue+ relative = _relative(path)+ if relative in seen or EXCLUDED_PARTS & set(Path(relative).parts):+ continue+ seen.add(relative)+ found.extend(scan_file(path, relative))+ return found+++def _relative(path: Path) -> str:+ try:+ return path.resolve().relative_to(REPO_ROOT).as_posix()+ except ValueError:+ return path.as_posix()+++def _identity(entry: dict) -> tuple:+ return (entry["file"], entry["rule"], entry["sink"], entry["digest"])+++def _counted(entries: list) -> dict:+ counts: dict = {}+ for entry in entries:+ counts[_identity(entry)] = counts.get(_identity(entry), 0) + 1+ return counts+++def main() -> int:+ parser = argparse.ArgumentParser(+ description = __doc__, formatter_class = argparse.RawDescriptionHelpFormatter+ )+ parser.add_argument("--update", action = "store_true", help = "rewrite the baseline")+ parser.add_argument("--self-test", action = "store_true", help = "check the rules still fire")+ parser.add_argument("--paths", nargs = "*", help = "scan these instead of the defaults")+ arguments = parser.parse_args()++ if arguments.self_test:+ return self_test()+ if arguments.update and arguments.paths:+ # --update rewrites the whole baseline, so a partial scan would drop every other entry.+ parser.error("--update rewrites the whole baseline; run it without --paths")++ document = json.loads(BASELINE_PATH.read_text(encoding = "utf-8"))+ found = collect(arguments.paths or document["targets"])+ baseline_rel = BASELINE_PATH.relative_to(REPO_ROOT).as_posix()++ if arguments.update:+ # Carry reasons over, so a reviewed entry never silently becomes an unreviewed one.+ reasons = {_identity(e): e.get("reason", "") for e in document["entries"]}+ document["entries"] = sorted(+ (+ {+ "file": f,+ "rule": r,+ "sink": s,+ "digest": d,+ "count": n,+ "reason": reasons.get((f, r, s, d), "REVIEW ME"),+ }+ for (f, r, s, d), n in _counted(found).items()+ ),+ key = _identity,+ )+ BASELINE_PATH.write_text(json.dumps(document, indent = 2) + "\n", encoding = "utf-8")+ print(f"baseline: {len(document['entries'])} entries, {len(found)} call sites")+ return 0++ entries = document["entries"]+ if arguments.paths:+ scopes = [_relative(REPO_ROOT / path) for path in arguments.paths]+ if "." not in scopes:+ entries = [+ e+ for e in entries+ if any(e["file"] == p or e["file"].startswith(p.rstrip("/") + "/") for p in scopes)+ ]+ allowed = {_identity(e): e["count"] for e in entries}+ observed = _counted(found)+ lines = {_identity(e): e["line"] for e in found}++ new = sorted(k for k, n in observed.items() if n > allowed.get(k, 0))+ if new:+ print(f"{len(new)} risky loader call site(s) not in the baseline:\n")+ for key in new:+ f, r, s, _ = key+ print(f" {f}:{lines[key]} [{r}] {s}")+ print(+ "\nEither load from a written-out name, a pinned revision or a safe format, or - if "+ "the input really is trusted - record it:\n"+ " 1. python scripts/lint_risky_loaders.py --update\n"+ f' 2. replace the new entr(y/ies)\' "REVIEW ME" reason in {baseline_rel} with why it is safe.'+ )+ return 1++ unreviewed = sorted(+ (e["file"], e["rule"], e["sink"])+ for e in entries+ if e.get("reason", "REVIEW ME") in ("", "REVIEW ME")+ )+ if unreviewed:+ print(f"{len(unreviewed)} baseline entr(y/ies) carry no justification:\n")+ for f, r, s in unreviewed:+ print(f" {f} [{r}] {s}")+ print(f"\nSay why the input is trusted in the entry's `reason` field in {baseline_rel}.")+ return 1++ # Fewer calls than allowed counts too, so a removed duplicate cannot make room for a new one.+ stale = sorted(k for k in allowed if observed.get(k, 0) < allowed[k])+ if stale:+ # An entry outliving its call site would re-permit whatever lands on that digest next.+ print(f"{len(stale)} baseline entr(y/ies) match fewer calls than recorded:\n")+ for f, r, s, d in stale:+ print(f" {f} [{r}] {s} {d}")+ print(+ "\nA risky call was removed or rewritten; run `python scripts/lint_risky_loaders.py --update`."+ )+ return 1++ print(f"ok: {len(found)} risky loader call site(s), all recorded")+ return 0+++_BAD = {+ "dynamic-import": """+import importlib+from importlib import import_module as im+from sentence_transformers.util import import_from_string+from transformers.dynamic_module_utils import get_class_from_dynamic_module+def f(config, name, path):+ importlib.import_module(f"mlx_lm.models.{config.model_type}")+ im(name)+ __import__(name)+ import_from_string(config["type"])+ get_class_from_dynamic_module(config.auto_map["AutoModel"], name)+ importlib.util.spec_from_file_location("x", path)+ importlib.machinery.SourceFileLoader("x", path)+""",+ "trust-remote-code": """+def load(name, trust_remote_code = True):+ AutoConfig.from_pretrained(name, trust_remote_code = True)+ trust_remote_code = True+ kwargs["trust_remote_code"] = True+ kwargs.setdefault("trust_remote_code", True)+ return {"trust_remote_code": True}+class Request(BaseModel):+ trust_remote_code: bool = Field(True, description = "x")+""",+ "unpinned-code-fetch": """+import sys, subprocess+from huggingface_hub import snapshot_download+URL = "https://github.com/ggml-org/llama.cpp/raw/refs/heads/master/convert_hf_to_gguf.py"+def codec():+ path = snapshot_download("org/codec")+ sys.path.insert(0, path)+def clone():+ subprocess.run(["git", "clone", "https://github.com/org/repo"])+def shell_clone(folder):+ run(f"git clone https://github.com/org/repo {folder}")+def held_clone():+ commands = ["git clone --recursive https://github.com/org/repo", "pip install x"]+ try_execute(commands)+""",+ "unsafe-deserialize": """+import pickle, torch, yaml, joblib+import numpy as np+def f(path, flag):+ pickle.loads(open(path, "rb").read())+ torch.load(path, weights_only = False)+ torch.load(path, weights_only = flag)+ torch.load(path)+ np.load(path, allow_pickle = True)+ yaml.load(open(path))+ joblib.load(path)+""",+}+_BAD_COUNTS = {+ "dynamic-import": 7,+ "trust-remote-code": 7,+ "unpinned-code-fetch": 5,+ "unsafe-deserialize": 7,+}++_GOOD = """+import importlib, subprocess, sys, torch, yaml+import numpy as np+from huggingface_hub import snapshot_download+URL = "https://github.com/ggml-org/llama.cpp/raw/b1234/convert_hf_to_gguf.py"+def f(name, path, trust_remote_code = False):+ importlib.import_module("torch.nn")+ importlib.util.find_spec(name)+ __import__("os")+ AutoConfig.from_pretrained(name, trust_remote_code = trust_remote_code)+ torch.load(path, weights_only = True)+ np.load(path)+ yaml.load(open(path), Loader = yaml.SafeLoader)+ yaml.safe_load(open(path))+ snapshot_download(name)+def chained_pin(version):+ try_execute(["git clone https://github.com/org/repo", f"cd repo && git reset --hard {version}"])+def pinned():+ subprocess.run(["git", "clone", "https://github.com/org/repo"])+ subprocess.run(["git", "checkout", "0123abc"])+def codec():+ path = snapshot_download("org/codec", revision = "0123abc")+ sys.path.insert(0, path)+"""+++def self_test() -> int:+ """Each rule fires on its bad shapes and stays quiet on the good ones, on a runner with nothing but Python."""+ import tempfile++ failures = []+ with tempfile.TemporaryDirectory() as directory:+ cases = [(rule, source, _BAD_COUNTS[rule], rule) for rule, source in _BAD.items()]+ cases.append(("good", _GOOD, 0, None))+ for label, source, expected, rule in cases:+ path = Path(directory) / f"{label.replace('-', '_')}.py"+ path.write_text(source, encoding = "utf-8")+ hits = scan_file(path, path.name)+ count = len([h for h in hits if rule is None or h["rule"] == rule])+ if count != expected or (rule is not None and len(hits) != expected):+ failures.append(+ f"{label}: expected {expected} finding(s), got {[(h['rule'], h['sink']) for h in hits]}"+ )+ if failures:+ print("self-test FAILED:\n " + "\n ".join(failures))+ return 1+ print("self-test: ok")+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/scripts/risky_loaders_baseline.json b/scripts/risky_loaders_baseline.jsonnew file mode 100644index 00000000000..3f3946b6c59--- /dev/null+++ b/scripts/risky_loaders_baseline.json@@ -0,0 +1,670 @@+{+ "_comment": "Call sites that predate the gate, each with why it is safe. New ones fail; see scripts/lint_risky_loaders.py.",+ "targets": [+ "unsloth",+ "unsloth_cli",+ "studio",+ "docker",+ "scripts",+ "cli.py",+ "unsloth-cli.py"+ ],+ "entries": [+ {+ "file": "scripts/prequant_probe.py",+ "rule": "unsafe-deserialize",+ "sink": "torch.load",+ "digest": "a9e1e9849ae6234f",+ "count": 1,+ "reason": "Maintainer probe script, not shipped; CKPT is a constant path to a locally built checkpoint."+ },+ {+ "file": "studio/backend/core/_torchao_stub.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "a332a4e1ffe5dbe0",+ "count": 1,+ "reason": "Loads the installed torchao package file it is stubbing around, located via find_spec."+ },+ {+ "file": "studio/backend/core/export/export.py",+ "rule": "unpinned-code-fetch",+ "sink": "git-clone-unpinned",+ "digest": "f7dadd837277bc8d",+ "count": 1,+ "reason": "Known residual: clones llama.cpp for convert_lora_to_gguf.py at the bundled release tag when known (--branch tag), else upstream master, the same upstream trust as installing llama.cpp; pinning is a separate change."+ },+ {+ "file": "studio/backend/core/export/worker.py",+ "rule": "trust-remote-code",+ "sink": "assignment",+ "digest": "a751fa788de6b6e6",+ "count": 1,+ "reason": "Nemotron-H only, for unsloth/ or nvidia/ names confirmed as genuine first-party Hub repos by is_trusted_org_repo."+ },+ {+ "file": "studio/backend/core/export/worker.py",+ "rule": "trust-remote-code",+ "sink": "keyword",+ "digest": "58d5d0f2c979e2ae",+ "count": 1,+ "reason": "Argument to the consent evaluator inside `if trust_remote_code:`; it scans the code and blocks unless approved."+ },+ {+ "file": "studio/backend/core/import_guards.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "8a43c55c018acccf",+ "count": 1,+ "reason": "Re-imports unsloth / unsloth_zoo by their fixed names after purging stubs."+ },+ {+ "file": "studio/backend/core/inference/__init__.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "21de5335c8af2970",+ "count": 1,+ "reason": "Lazy attribute loader over the fixed _LAZY_ATTRS table of this package's own submodules."+ },+ {+ "file": "studio/backend/core/inference/diffusion_cache.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 1,+ "reason": "Iterates the fixed _UNREGISTERED_BLOCK_METADATA table of diffusers block classes."+ },+ {+ "file": "studio/backend/core/inference/diffusion_flux2_rope.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "0d420008334333ee",+ "count": 1,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/diffusion_prequant.py",+ "rule": "dynamic-import",+ "sink": "__import__",+ "digest": "7daba5f579d7e4de",+ "count": 1,+ "reason": "Iterates the fixed (diffusion_families, video_families) pair inside this package."+ },+ {+ "file": "studio/backend/core/inference/diffusion_prequant.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "7ff41ce8b4bc8d80",+ "count": 1,+ "reason": "Iterates the fixed _PREQUANT_SAFE_GLOBALS allowlist of torchao classes."+ },+ {+ "file": "studio/backend/core/inference/diffusion_prompt_cache.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "e37f0ae9a5cd2615",+ "count": 1,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/diffusion_qwenimage21.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "0d420008334333ee",+ "count": 1,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/diffusion_qwenimage21_rope.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "0d420008334333ee",+ "count": 1,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/diffusion_qwenimage_rope.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "0d420008334333ee",+ "count": 1,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/diffusion_torchao_patches.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 2,+ "reason": "Module names come from fixed in-file torchao tables."+ },+ {+ "file": "studio/backend/core/inference/diffusion_zimage_fused.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "0d420008334333ee",+ "count": 3,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/ltx2_import_compat.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 1,+ "reason": "Called with fixed LTX-2 / diffusers module names from this file."+ },+ {+ "file": "studio/backend/core/inference/mcp_client.py",+ "rule": "dynamic-import",+ "sink": "__import__",+ "digest": "cdd1ee7836f42455",+ "count": 1,+ "reason": "Iterates a fixed pair of mcp.shared.exceptions class names."+ },+ {+ "file": "studio/backend/core/inference/mcp_client.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "84203683666dcacf",+ "count": 1,+ "reason": "Top-level package of an already imported HTTP client class (its __module__), not external input."+ },+ {+ "file": "studio/backend/core/inference/native_audio.py",+ "rule": "trust-remote-code",+ "sink": "keyword",+ "digest": "9acb0bb5af068e24",+ "count": 1,+ "reason": "MOSS types are in REMOTE_CODE_AUDIO_TYPES, so load() raises without trust_remote_code, and the codec repo is added to the consent scan targets."+ },+ {+ "file": "studio/backend/core/inference/prequant_legacy_int8.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "7ff41ce8b4bc8d80",+ "count": 1,+ "reason": "Module name comes from a fixed table or constant in this file, not from a model, request or download."+ },+ {+ "file": "studio/backend/core/inference/vllm_server.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "3bc930c482122edb",+ "count": 1,+ "reason": "Iterates two fixed vLLM middleware module paths."+ },+ {+ "file": "studio/backend/core/inference/vllm_server.py",+ "rule": "dynamic-import",+ "sink": "runpy.run_module",+ "digest": "e87de9560cced259",+ "count": 1,+ "reason": "Entry shim: the module comes from the argv Studio itself builds to launch vLLM's server."+ },+ {+ "file": "studio/backend/core/inference/worker.py",+ "rule": "trust-remote-code",+ "sink": "assignment",+ "digest": "a751fa788de6b6e6",+ "count": 1,+ "reason": "Nemotron only (_needs_nemotron_trust): genuine first-party unsloth/ or nvidia/ repos confirmed by is_trusted_org_repo."+ },+ {+ "file": "studio/backend/core/inference/worker.py",+ "rule": "trust-remote-code",+ "sink": "keyword",+ "digest": "0c3bb6fa20c6f007",+ "count": 1,+ "reason": "Argument to the consent evaluator inside `if trust_remote_code:`; it scans the code and blocks unless approved."+ },+ {+ "file": "studio/backend/core/library.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "b991b1dc9a21d26e",+ "count": 1,+ "reason": "Prefix-bounded to core.inference.<gallery>; the name comes from the fixed _GALLERIES table."+ },+ {+ "file": "studio/backend/core/systemone/laya_runtime.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "938c44d9558f813a",+ "count": 1,+ "reason": "Loads the vendored laya package shipped inside Studio (_VENDORED_LAYA)."+ },+ {+ "file": "studio/backend/core/training/account_jobs.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "9eb7902991e3aa20",+ "count": 1,+ "reason": "Job module and target come from Studio's own spawn spec (account_process_spec callers), not a request."+ },+ {+ "file": "studio/backend/core/training/worker.py",+ "rule": "dynamic-import",+ "sink": "__import__",+ "digest": "ffd393570a168e12",+ "count": 1,+ "reason": "Probes fixed kernel package names (causal_conv1d, mamba_ssm, flash_attn) after installing them."+ },+ {+ "file": "studio/backend/core/training/worker.py",+ "rule": "trust-remote-code",+ "sink": "item",+ "digest": "64be60c61f04074b",+ "count": 1,+ "reason": "Nemotron only, for unsloth/ or nvidia/ names confirmed as genuine first-party Hub repos by is_trusted_org_repo."+ },+ {+ "file": "studio/backend/core/training/worker.py",+ "rule": "trust-remote-code",+ "sink": "keyword",+ "digest": "b0e48aa315431594",+ "count": 1,+ "reason": "Argument to the consent evaluator, reached only when config trust_remote_code is set; it scans and blocks unless approved."+ },+ {+ "file": "studio/backend/hub/services/models/deletion.py",+ "rule": "dynamic-import",+ "sink": "__import__",+ "digest": "b097279f2312331d",+ "count": 1,+ "reason": "Iterates two fixed module:attr strings for the active diffusion and video engines."+ },+ {+ "file": "studio/backend/routes/training.py",+ "rule": "trust-remote-code",+ "sink": "item",+ "digest": "81b2dbc83d544567",+ "count": 1,+ "reason": "Only when the bundled model defaults YAML asks for it and is_trusted_org_repo confirms a first-party repo."+ },+ {+ "file": "studio/backend/utils/hardware/hardware.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "880a3dce444c227f",+ "count": 1,+ "reason": "Loads dataset_num_proc.py from the installed unsloth package, located via find_spec."+ },+ {+ "file": "studio/backend/utils/hf_xet_fallback.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 2,+ "reason": "Every caller passes a fixed unsloth_zoo.hf_xet_health / unsloth_zoo.hf_xet_tuning name."+ },+ {+ "file": "studio/backend/utils/mlx_repair.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "7ff41ce8b4bc8d80",+ "count": 1,+ "reason": "Iterates the fixed _MLX_RUNTIME_IMPORTS tuple."+ },+ {+ "file": "studio/backend/utils/models/unsloth_mirror.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "01b003571a7b15a2",+ "count": 1,+ "reason": "Loads mapper.py from the installed unsloth package, located via find_spec."+ },+ {+ "file": "studio/backend/utils/native_path_leases.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "52e9a0e89a8e084e",+ "count": 1,+ "reason": "Target is a module path passed by Studio's own worker spawn code."+ },+ {+ "file": "studio/backend/utils/security/__init__.py",+ "rule": "trust-remote-code",+ "sink": "default",+ "digest": "02604be5f1981efe",+ "count": 1,+ "reason": "The default only means 'the caller is asking for remote code'; this function is the consent scan itself, not a load."+ },+ {+ "file": "studio/backend/utils/security/__init__.py",+ "rule": "trust-remote-code",+ "sink": "default",+ "digest": "530cdf25b852d669",+ "count": 1,+ "reason": "The default only means 'the caller is asking for remote code'; this function is the consent scan itself, not a load."+ },+ {+ "file": "studio/backend/utils/security/remote_code_scan.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "d96c86392e96178e",+ "count": 1,+ "reason": "Loads the repo's own scripts/scan_packages.py scanner from this checkout or install."+ },+ {+ "file": "studio/backend/utils/ssm_runtime.py",+ "rule": "dynamic-import",+ "sink": "__import__",+ "digest": "ffd393570a168e12",+ "count": 1,+ "reason": "Probes fixed SSM kernel package names."+ },+ {+ "file": "studio/backend/utils/third_party_source.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 1,+ "reason": "import_pinned_module: the source is fetched at a pinned revision and sha256-verified before import."+ },+ {+ "file": "studio/backend/utils/torch_warmup.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 1,+ "reason": "Iterates the fixed _DIFFUSERS_PREWARM_MODEL_MODULES tuple."+ },+ {+ "file": "studio/backend/utils/utils.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "4885bc5b2c6e8403",+ "count": 1,+ "reason": "Iterates the fixed _OFFLINE_CONSTANTS table of huggingface_hub / transformers modules."+ },+ {+ "file": "studio/install_audio_cpp_prebuilt.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "67045d79b6761bf3",+ "count": 1,+ "reason": "Loads a helper .py shipped next to this installer."+ },+ {+ "file": "studio/install_python_stack.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 2,+ "reason": "Iterates fixed packaging / pip._vendor.packaging module names."+ },+ {+ "file": "unsloth/__init__.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "27bf0ec897678cb9",+ "count": 1,+ "reason": "Loads unsloth's own _mlx_raw_text.py from the installed package directory; the path is derived from __file__."+ },+ {+ "file": "unsloth/_gpu_init.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "8c9ff15e7eed9db1",+ "count": 1,+ "reason": "Loads unsloth's own hf cache helper from the installed package directory; the path is derived from __file__."+ },+ {+ "file": "unsloth/dataset_num_proc.py",+ "rule": "dynamic-import",+ "sink": "__import__",+ "digest": "fd6171fa78fd5a1e",+ "count": 1,+ "reason": "Called with the stdlib multiprocessing module names only, to read their start method."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "1cf4f547b72b3f63",+ "count": 1,+ "reason": "Iterates the fixed _VLLM_COMPILED_EXTENSIONS tuple to surface ABI-broken vLLM extensions."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "228679880d1b3db7",+ "count": 1,+ "reason": "Iterates a fixed dict of vLLM LoRA op module paths."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "59e72c01edb14c86",+ "count": 3,+ "reason": "Module names come from fixed in-file tables (_PEFT_TORCHAO_TENSOR_SUBCLASSES and the transformers symbol homes); remote code can only name an attribute, which resolves inside an already installed transformers module."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "8a43c55c018acccf",+ "count": 2,+ "reason": "Names come from the fixed _PEFT_CONVERSION_SYMBOLS table and the transformers conversion module list."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "c19dff67fe237be9",+ "count": 1,+ "reason": "Imports the torchao NF4 module at a fixed relocated path (_TORCHAO_NF4_NEW constant)."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "cbc2c89aff4d7aaf",+ "count": 1,+ "reason": "Iterates the fixed _IMAGE_PROCESSING_SYMBOL_HOMES tuple of transformers modules."+ },+ {+ "file": "unsloth/import_fixes.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "e79cc5aade79e54d",+ "count": 1,+ "reason": "Imports the fixed literal peft.utils.transformers_weight_conversion held in a local."+ },+ {+ "file": "unsloth/models/_utils.py",+ "rule": "dynamic-import",+ "sink": "get_class_from_dynamic_module",+ "digest": "1bf1ae2e2285284f",+ "count": 1,+ "reason": "Returns None unless hub_kwargs carries trust_remote_code, same contract as from_pretrained."+ },+ {+ "file": "unsloth/models/_utils.py",+ "rule": "dynamic-import",+ "sink": "get_class_from_dynamic_module",+ "digest": "281f4ae0ed10fad9",+ "count": 1,+ "reason": "Returns early unless trust_remote_code is set (resolve_remote_code_model_class)."+ },+ {+ "file": "unsloth/models/_utils.py",+ "rule": "dynamic-import",+ "sink": "get_class_from_dynamic_module",+ "digest": "b7a268bb9c7ebe2c",+ "count": 1,+ "reason": "Returns early unless trust_remote_code is set, and refuses cross-repo (--) references."+ },+ {+ "file": "unsloth/models/_utils.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "ed81224ea9382685",+ "count": 1,+ "reason": "Sibling of the config class's own module: only resolves inside an installed transformers package or an already trusted remote-code module."+ },+ {+ "file": "unsloth/models/_utils.py",+ "rule": "unsafe-deserialize",+ "sink": "torch.load",+ "digest": "7de1f016ac2d1555",+ "count": 1,+ "reason": "Reads back a .pt file this function just wrote with torch.save into the local offload folder; never a downloaded file."+ },+ {+ "file": "unsloth/models/loader.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "ebe737f7f7773a19",+ "count": 1,+ "reason": "Prefix-bounded to transformers.models.<type>.modeling_<type>; can only import a module transformers already ships."+ },+ {+ "file": "unsloth/models/loader_utils.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "bbec50d33c36333c",+ "count": 1,+ "reason": "Iterates the fixed (loraga, corda) pair under peft.tuners.lora."+ },+ {+ "file": "unsloth/models/loader_utils.py",+ "rule": "unpinned-code-fetch",+ "sink": "branch-head-url",+ "digest": "da770f64b1008f75",+ "count": 1,+ "reason": "mapper.py from unsloth main is parsed as data with ast (tables and literal strings only), never imported or executed."+ },+ {+ "file": "unsloth/models/rl.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "705322645c08649e",+ "count": 1,+ "reason": "Prefix-bounded to trl.experimental.<name>; names come from walking the installed TRL package."+ },+ {+ "file": "unsloth/models/rl.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "7c2f62e557f23dba",+ "count": 1,+ "reason": "Home module of an installed TRL config class (its __module__), not external input."+ },+ {+ "file": "unsloth/models/rl.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "fe73e10b8cc4e18a",+ "count": 1,+ "reason": "Prefix-bounded to trl.trainer.<config module>; name comes from the installed TRL trainer classes."+ },+ {+ "file": "unsloth/models/sentence_transformer.py",+ "rule": "dynamic-import",+ "sink": "import_from_string",+ "digest": "180573f5aeefdb98",+ "count": 2,+ "reason": "Gated: classes outside sentence_transformers.* need trust_remote_code; inside it the path must be dotted identifiers resolving to a module class (#12436)."+ },+ {+ "file": "unsloth/models/sentence_transformer.py",+ "rule": "trust-remote-code",+ "sink": "keyword",+ "digest": "9a08f32d1af83ea3",+ "count": 1,+ "reason": "Reached only after the trust_remote_code gate in _resolve_module_class raises for an untrusted non sentence_transformers class."+ },+ {+ "file": "unsloth/save.py",+ "rule": "trust-remote-code",+ "sink": "setdefault",+ "digest": "c6f8db768c498be7",+ "count": 1,+ "reason": "Only when the model itself was already loaded via approved remote code (_loaded_via_remote_code), so the export reload follows that decision."+ },+ {+ "file": "unsloth/save.py",+ "rule": "unpinned-code-fetch",+ "sink": "git-clone-unpinned",+ "digest": "6030da6ce18569c3",+ "count": 1,+ "reason": "Known residual: the legacy GGUF save path clones llama.cpp from upstream master (install_llama_cpp_clone_non_blocking / install_llama_cpp_blocking), the same upstream trust as installing llama.cpp; pinning is a separate change."+ },+ {+ "file": "unsloth/save.py",+ "rule": "unpinned-code-fetch",+ "sink": "git-clone-unpinned",+ "digest": "72fcdff75af05310",+ "count": 1,+ "reason": "Known residual: the legacy GGUF save path clones llama.cpp from upstream master (install_llama_cpp_clone_non_blocking / install_llama_cpp_blocking), the same upstream trust as installing llama.cpp; pinning is a separate change."+ },+ {+ "file": "unsloth/save.py",+ "rule": "unsafe-deserialize",+ "sink": "torch.load",+ "digest": "7de1f016ac2d1555",+ "count": 1,+ "reason": "Reads back a .pt file this function just wrote with torch.save into temporary_location; never a downloaded file."+ },+ {+ "file": "unsloth/trainer.py",+ "rule": "dynamic-import",+ "sink": "get_class_from_dynamic_module",+ "digest": "553d43d8ed624697",+ "count": 1,+ "reason": "Returns None unless trust_remote_code is granted, and remote code is tried last."+ },+ {+ "file": "unsloth/trainer.py",+ "rule": "dynamic-import",+ "sink": "import_module",+ "digest": "d78c0ba207ccee5a",+ "count": 1,+ "reason": "Iterates a fixed tuple of torch wrapper classes (DataParallel, FSDP, OptimizedModule)."+ },+ {+ "file": "unsloth_cli/_studio_deps.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "64638e81faf798ab",+ "count": 1,+ "reason": "Loads studio/install_manifest.py from this checkout or install, not a download."+ },+ {+ "file": "unsloth_cli/commands/eval.py",+ "rule": "unsafe-deserialize",+ "sink": "yaml.load",+ "digest": "99bb2a156edb2dfc",+ "count": 1,+ "reason": "_TaskYamlLoader subclasses yaml.SafeLoader; it only adds a tag for lm-eval's !function references."+ },+ {+ "file": "unsloth_cli/commands/studio.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "1eec04886487848b",+ "count": 1,+ "reason": "Loads Studio's own backend/run.py from the installed Studio tree."+ },+ {+ "file": "unsloth_cli/commands/studio.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "af52cd628452c924",+ "count": 1,+ "reason": "Loads Studio's own cloudflare_tunnel.py from the installed Studio tree."+ },+ {+ "file": "unsloth_cli/commands/studio.py",+ "rule": "dynamic-import",+ "sink": "spec_from_file_location",+ "digest": "fff48dd3d69aa0d2",+ "count": 1,+ "reason": "Loads Studio's own auth/storage.py from the installed Studio tree."+ }+ ]+}diff --git a/tests/security/test_lint_risky_loaders.py b/tests/security/test_lint_risky_loaders.pynew file mode 100644index 00000000000..d5bc86808c9--- /dev/null+++ b/tests/security/test_lint_risky_loaders.py@@ -0,0 +1,299 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++"""`scripts/lint_risky_loaders.py` fails CI on a new risky loader call site.++These pin which shapes each rule reports, which it leaves alone, and that the baseline+cannot be used to smuggle a new call site past it.+"""++from __future__ import annotations++import importlib.util+import json+import subprocess+import sys+from pathlib import Path++import pytest++SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "lint_risky_loaders.py"+BASELINE = SCRIPT.parent / "risky_loaders_baseline.json"+++def _module():+ spec = importlib.util.spec_from_file_location("lint_risky_loaders_under_test", SCRIPT)+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++def _rules(tmp_path, source):+ sample = tmp_path / "sample.py"+ sample.write_text(source, encoding = "utf-8")+ return [(f["rule"], f["sink"]) for f in _module().scan_file(sample, "sample.py")]+++REPORTED = {+ "config model_type into import_module": (+ "import importlib\ndef f(c):\n importlib.import_module(f'mlx_lm.models.{c.model_type}')\n",+ ("dynamic-import", "import_module"),+ ),+ "aliased import_module": (+ "from importlib import import_module as im\ndef f(n):\n im(n)\n",+ ("dynamic-import", "import_module"),+ ),+ "modules.json type into import_from_string": (+ "from sentence_transformers.util import import_from_string\n"+ "def f(cfg):\n import_from_string(cfg['type'])\n",+ ("dynamic-import", "import_from_string"),+ ),+ "auto_map into get_class_from_dynamic_module": (+ "from transformers import dynamic_module_utils as d\n"+ "def f(c, n):\n d.get_class_from_dynamic_module(c.auto_map['AutoModel'], n)\n",+ ("dynamic-import", "get_class_from_dynamic_module"),+ ),+ "a computed path into spec_from_file_location": (+ "import importlib.util\ndef f(p):\n importlib.util.spec_from_file_location('x', p)\n",+ ("dynamic-import", "spec_from_file_location"),+ ),+ "a computed path into SourceFileLoader": (+ "import importlib.machinery\ndef f(p):\n importlib.machinery.SourceFileLoader('x', p)\n",+ ("dynamic-import", "SourceFileLoader"),+ ),+ "a computed path into get_class_in_module": (+ "from transformers.dynamic_module_utils import get_class_in_module\n"+ "def f(p):\n get_class_in_module('Model', p)\n",+ ("dynamic-import", "get_class_in_module"),+ ),+ "trust_remote_code default": (+ "def load(name, trust_remote_code = True):\n pass\n",+ ("trust-remote-code", "default"),+ ),+ "trust_remote_code keyword": (+ "def f(n):\n AutoModel.from_pretrained(n, trust_remote_code = True)\n",+ ("trust-remote-code", "keyword"),+ ),+ "trust_remote_code schema default": (+ "class R(BaseModel):\n trust_remote_code: bool = Field(True, description = 'x')\n",+ ("trust-remote-code", "assignment"),+ ),+ "trust_remote_code dict item": (+ "def f(kw):\n kw['trust_remote_code'] = True\n",+ ("trust-remote-code", "item"),+ ),+ "Python from a branch head": (+ "URL = 'https://raw.githubusercontent.com/org/repo/main/tool.py'\n",+ ("unpinned-code-fetch", "branch-head-url"),+ ),+ "revision-less download put on sys.path": (+ "import sys\nfrom huggingface_hub import snapshot_download\n"+ "def f():\n sys.path.insert(0, snapshot_download('org/codec'))\n",+ ("unpinned-code-fetch", "hub-download-loaded-as-code"),+ ),+ "git clone with no checkout": (+ "import subprocess\ndef f():\n subprocess.run(['git', 'clone', 'https://github.com/o/r'])\n",+ ("unpinned-code-fetch", "git-clone-unpinned"),+ ),+ "a shell-string git clone": (+ "def f(folder):\n run(f'git clone https://github.com/o/r {folder}')\n",+ ("unpinned-code-fetch", "git-clone-unpinned"),+ ),+ "a clone held in a command list": (+ "def f():\n commands = ['git clone https://github.com/o/r', 'pip install x']\n"+ " try_execute(commands)\n",+ ("unpinned-code-fetch", "git-clone-unpinned"),+ ),+ "runpy.run_path by keyword": (+ "import runpy\ndef f(p):\n runpy.run_path(path_name = p)\n",+ ("dynamic-import", "runpy.run_path"),+ ),+ "a clone next to a message that only mentions checkout": (+ "import subprocess\ndef f():\n subprocess.run(['git', 'clone', 'u'])\n"+ " print('source checkout detected')\n",+ ("unpinned-code-fetch", "git-clone-unpinned"),+ ),+ "torch.load with weights_only omitted": (+ "import torch\ndef f(p):\n torch.load(p)\n",+ ("unsafe-deserialize", "torch.load"),+ ),+ "torch.load weights_only=False": (+ "import torch\ndef f(p):\n torch.load(p, weights_only = False)\n",+ ("unsafe-deserialize", "torch.load"),+ ),+ "pickle.loads": (+ "import pickle\ndef f(b):\n pickle.loads(b)\n",+ ("unsafe-deserialize", "pickle.loads"),+ ),+ "yaml.load without a loader": (+ "import yaml\ndef f(s):\n yaml.load(s)\n",+ ("unsafe-deserialize", "yaml.load"),+ ),+ "np.load allow_pickle": (+ "import numpy as np\ndef f(p):\n np.load(p, allow_pickle = True)\n",+ ("unsafe-deserialize", "numpy.load"),+ ),+}++QUIET = {+ "a written-out import": "import importlib\nimportlib.import_module('torch.nn')\n",+ "find_spec, which does not execute": "import importlib.util\ndef f(n):\n importlib.util.find_spec(n)\n",+ "a local function that shares the name": "def import_module(n):\n return n\ndef f(n):\n import_module(n)\n",+ "trust_remote_code forwarded from the caller": (+ "def f(n, trust_remote_code = False):\n"+ " AutoModel.from_pretrained(n, trust_remote_code = trust_remote_code)\n"+ ),+ "a schema default of False": "class R(BaseModel):\n trust_remote_code: bool = Field(False)\n",+ "a pinned URL": "URL = 'https://raw.githubusercontent.com/org/repo/v1.2/tool.py'\n",+ "a pinned download put on sys.path": (+ "import sys\nfrom huggingface_hub import snapshot_download\n"+ "def f():\n sys.path.insert(0, snapshot_download('org/codec', revision = 'abc123'))\n"+ ),+ "a download that is not loaded as code": (+ "from huggingface_hub import snapshot_download\ndef f(n):\n return snapshot_download(n)\n"+ ),+ "a clone pinned by a chained reset": (+ "def f(v):\n try_execute(['git clone u', f'cd r && git reset --hard {v}'])\n"+ ),+ "git clone followed by a checkout": (+ "import subprocess\ndef f():\n subprocess.run(['git', 'clone', 'u'])\n"+ " subprocess.run(['git', 'checkout', 'abc123'])\n"+ ),+ "torch.load with weights_only=True": "import torch\ndef f(p):\n torch.load(p, weights_only = True)\n",+ "yaml with a safe loader": "import yaml\ndef f(s):\n yaml.load(s, Loader = yaml.SafeLoader)\n yaml.safe_load(s)\n",+}++[email protected]("label", sorted(REPORTED))+def test_reported(tmp_path, label):+ source, expected = REPORTED[label]+ assert _rules(tmp_path, source) == [expected]++[email protected]("label", sorted(QUIET))+def test_quiet(tmp_path, label):+ assert _rules(tmp_path, QUIET[label]) == []+++def test_self_test_passes():+ result = subprocess.run(+ [sys.executable, str(SCRIPT), "--self-test"], capture_output = True, text = True+ )+ assert result.returncode == 0, result.stdout + result.stderr+++def test_a_second_identical_call_is_not_covered_by_one_baseline_entry(tmp_path, monkeypatch):+ module = _module()+ sample = tmp_path / "sample.py"+ sample.write_text(+ "import pickle\ndef f(b):\n pickle.loads(b)\n pickle.loads(b)\n", encoding = "utf-8"+ )+ found = module.scan_file(sample, "sample.py")+ entry = dict(found[0], count = 1, reason = "reviewed")+ entry.pop("line")+ baseline = tmp_path / "baseline.json"+ baseline.write_text(+ json.dumps({"targets": [str(sample)], "entries": [entry]}), encoding = "utf-8"+ )+ monkeypatch.setattr(module, "BASELINE_PATH", baseline)+ monkeypatch.setattr(module, "REPO_ROOT", tmp_path)+ monkeypatch.setattr(sys, "argv", ["lint_risky_loaders.py"])+ assert module.main() == 1+++def test_a_removed_duplicate_leaves_no_spare_allowance(tmp_path, monkeypatch):+ module = _module()+ sample = tmp_path / "sample.py"+ sample.write_text("import pickle\ndef f(b):\n pickle.loads(b)\n", encoding = "utf-8")+ entry = dict(module.scan_file(sample, "sample.py")[0], count = 2, reason = "reviewed")+ entry.pop("line")+ baseline = tmp_path / "baseline.json"+ baseline.write_text(+ json.dumps({"targets": [str(sample)], "entries": [entry]}), encoding = "utf-8"+ )+ monkeypatch.setattr(module, "BASELINE_PATH", baseline)+ monkeypatch.setattr(module, "REPO_ROOT", tmp_path)+ monkeypatch.setattr(sys, "argv", ["lint_risky_loaders.py"])+ assert module.main() == 1+++def test_update_refuses_a_partial_scan(monkeypatch):+ module = _module()+ monkeypatch.setattr(sys, "argv", ["lint_risky_loaders.py", "--update", "--paths", "scripts"])+ with pytest.raises(SystemExit) as raised:+ module.main()+ assert raised.value.code == 2+++def test_a_scoped_run_ignores_entries_outside_its_paths():+ result = subprocess.run(+ [+ sys.executable,+ str(SCRIPT),+ "--paths",+ "unsloth_zoo/compiler.py"+ if (SCRIPT.parents[1] / "unsloth_zoo").is_dir()+ else "unsloth/models/loader.py",+ ],+ capture_output = True,+ text = True,+ cwd = SCRIPT.parents[1],+ )+ assert result.returncode == 0, result.stdout + result.stderr+++def test_a_root_scope_keeps_every_entry(tmp_path, monkeypatch):+ module = _module()+ sample = tmp_path / "pkg" / "sample.py"+ sample.parent.mkdir()+ sample.write_text("import pickle\ndef f(b):\n pickle.loads(b)\n", encoding = "utf-8")+ entry = dict(module.scan_file(sample, "pkg/sample.py")[0], count = 1, reason = "reviewed")+ entry.pop("line")+ baseline = tmp_path / "baseline.json"+ baseline.write_text(json.dumps({"targets": ["pkg"], "entries": [entry]}), encoding = "utf-8")+ monkeypatch.setattr(module, "BASELINE_PATH", baseline)+ monkeypatch.setattr(module, "REPO_ROOT", tmp_path)+ monkeypatch.setattr(sys, "argv", ["lint_risky_loaders.py", "--paths", "."])+ assert module.main() == 0+++def test_overlapping_paths_scan_a_file_once():+ module = _module()+ target = "unsloth_zoo" if (module.REPO_ROOT / "unsloth_zoo").is_dir() else "unsloth/models"+ once = module.collect([target])+ assert module.collect([target, target]) == once+++def test_notebook_code_cells_are_scanned(tmp_path):+ notebook = tmp_path / "sample.ipynb"+ cells = [+ {+ "cell_type": "code",+ "source": ["!pip install x\n", "import pickle\n", "pickle.loads(b)\n"],+ },+ {"cell_type": "markdown", "source": ["pickle.loads(b)"]},+ ]+ notebook.write_text(json.dumps({"cells": cells}), encoding = "utf-8")+ hits = _module().scan_file(notebook, "sample.ipynb")+ assert [(h["rule"], h["sink"], h["line"]) for h in hits] == [+ ("unsafe-deserialize", "pickle.loads", 3)+ ]+++def test_every_baseline_entry_is_reviewed():+ entries = json.loads(BASELINE.read_text(encoding = "utf-8"))["entries"]+ assert entries+ assert all(e.get("reason") and e["reason"] != "REVIEW ME" for e in entries)+++def test_the_tree_matches_the_baseline():+ result = subprocess.run([sys.executable, str(SCRIPT)], capture_output = True, text = True)+ assert result.returncode == 0, result.stdout + result.stderr+++def test_lint_ci_runs_the_gate():+ workflow = (SCRIPT.parents[1] / ".github" / "workflows" / "lint-ci.yml").read_text(+ encoding = "utf-8"+ )+ assert "lint_risky_loaders.py --self-test" in workflow+ assert "python scripts/lint_risky_loaders.py\n" in workflow