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.

Studio and Core: close remote-code gate gaps in Sentence Transformers, GGUF export, scans and tokenizer paths (#14437)

Daniel Han0bc82e1b61fc
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.py
index 493519b11f7..e507e277bb0 100644
--- a/studio/backend/core/export/export.py
+++ b/studio/backend/core/export/export.py
@@ -210,6 +210,30 @@ def _llama_cpp_scripts_pin():
os.environ.pop("UNSLOTH_LLAMA_CPP_SCRIPTS_DIR", None)
+def _converter_remote_code(approved: bool):
+ """Keep convert_hf_to_gguf.py to the load's consent-gated trust decision for one conversion.
+
+ The converter loads several architectures' tokenizers with trust_remote_code=True, so a
+ checkpoint loaded with built-in classes (never seen by the consent gate) could run its
+ auto_map code there. Transformers imports that code only from its HF_MODULES_CACHE copy,
+ which the converter child inherits: a path no directory can be created at refuses it.
+ Covers the MLX save path too, which has no guard of its own.
+ """
+ if approved:
+ yield
+ return
+ existing = os.environ.get("HF_MODULES_CACHE")
+ os.environ["HF_MODULES_CACHE"] = os.path.join(os.path.abspath(__file__), "no_remote_code")
+ try:
+ yield
+ finally:
+ if existing is None:
+ os.environ.pop("HF_MODULES_CACHE", None)
+ else:
+ os.environ["HF_MODULES_CACHE"] = existing
+
+
def _multi_gpu_device_map_kwargs() -> dict:
"""``device_map`` kwargs for sharding a checkpoint across every visible GPU.
@@ -1769,6 +1793,8 @@ class ExportBackend:
)
# Truthiness, as above: a disabled imatrix must not reach an exporter without the kwarg.
imatrix_kw = {"imatrix_file": imatrix_file} if imatrix_file else {}
+ # True only after the load's consent gate passed; only then may the converter run auto_map code.
+ remote_code_approved = bool(getattr(self, "trust_remote_code", False))
# Resolution reads a Hub repo, so the local save needs the token; kept out of imatrix_kw, which the
# push shares and names token= itself.
local_token_kw = (
@@ -1824,7 +1850,7 @@ class ExportBackend:
try:
# Pinned to setup.sh's llama.cpp ref so the converter cannot drift past the pinned
# llama-quantize gguf API; scoped to the conversion.
- with _llama_cpp_scripts_pin():
+ with _llama_cpp_scripts_pin(), _converter_remote_code(remote_code_approved):
result = self.current_model.save_pretrained_gguf(
_model_tmp,
self.current_tokenizer,
@@ -2032,7 +2058,7 @@ class ExportBackend:
logger.warning(f"Could not publish the model card: {exception}")
else:
# Converts as well as pushes, so it needs the same scoped pin.
- with _llama_cpp_scripts_pin():
+ with _llama_cpp_scripts_pin(), _converter_remote_code(remote_code_approved):
self.current_model.push_to_hub_gguf(
repo_id,
self.current_tokenizer,
diff --git a/studio/backend/core/inference/native_audio.py b/studio/backend/core/inference/native_audio.py
index 92540eecbb2..31168488279 100644
--- a/studio/backend/core/inference/native_audio.py
+++ b/studio/backend/core/inference/native_audio.py
@@ -353,6 +353,39 @@ def _higgs_tts3_codec_target(model_name: str, hf_token: Optional[str] = None) ->
return HIGGS_TTS3_CODEC_REPO
+def _moss_local_audio_tokenizer_target(
+ model_name: str, hf_token: Optional[str] = None
+) -> Optional[str]:
+ """The codec ``audio_tokenizer_config.json`` names; the processor loads it by itself."""
+ audio_tokenizer_config = _read_audio_metadata(
+ model_name, "audio_tokenizer_config.json", hf_token, reject_oversized = True
+ )
+ candidate = audio_tokenizer_config.get("audio_tokenizer_name_or_path")
+ if isinstance(candidate, str) and candidate.strip():
+ return candidate.strip()
+ return None
+
+
+_CURATED_AUDIO_COMPANIONS = frozenset(
+ repo for repos in NATIVE_AUDIO_COMPANION_REPOS.values() for repo in repos
+)
+
+
+def _companion_adapter_base(companion: str, hf_token: Optional[str] = None) -> Optional[str]:
+ """The base ``from_pretrained`` loads in place of a companion that is a PEFT adapter."""
+ if companion in _CURATED_AUDIO_COMPANIONS:
+ return None
+ local = Path(companion).expanduser()
+ if local.is_dir() and (local / "config.json").is_file():
+ # Transformers only swaps a local folder for its base when it has no config.json.
+ return None
+ adapter_config = _read_audio_metadata(
+ companion, "adapter_config.json", hf_token, reject_oversized = True
+ )
+ base = adapter_config.get("base_model_name_or_path")
+ return base.strip() if isinstance(base, str) and base.strip() else None
+
+
def native_audio_type_from_local_path(model_name: str) -> Optional[str]:
"""Recognize a local native-audio checkpoint from bounded metadata files."""
normalized = str(model_name or "").strip()
@@ -434,12 +467,20 @@ def native_audio_security_targets(
resolved_type = audio_type or _native_audio_type(model_name)
if resolved_type == "moss_tts_local":
targets.append(_moss_local_codec_target(model_name, hf_token))
+ audio_tokenizer = _moss_local_audio_tokenizer_target(model_name, hf_token)
+ if audio_tokenizer and audio_tokenizer not in targets:
+ targets.append(audio_tokenizer)
elif resolved_type == "higgs_tts2":
targets.append(_higgs_tts2_codec_target(model_name, hf_token))
elif resolved_type == "higgs_tts3":
targets.append(_higgs_tts3_codec_target(model_name, hf_token))
else:
targets.extend(NATIVE_AUDIO_COMPANION_REPOS.get(resolved_type, ()))
+ # A companion that is only an adapter loads its base instead, so the gates must see that base.
+ for companion in targets[1:]:
+ base = _companion_adapter_base(companion, hf_token)
+ if base and base not in targets:
+ targets.append(base)
return targets
diff --git a/studio/backend/core/rag/embeddings.py b/studio/backend/core/rag/embeddings.py
index 1f36a441df3..28e34656716 100644
--- a/studio/backend/core/rag/embeddings.py
+++ b/studio/backend/core/rag/embeddings.py
@@ -437,10 +437,15 @@ _ST_PACKAGE_PREFIX = "sentence_transformers."
_ST_GATE_MARKER = "_unsloth_custom_module_gate"
-def _refuse_custom_module(class_ref, model_name_or_path, trust_remote_code) -> None:
+def _refuse_custom_module(
+ class_ref,
+ model_name_or_path,
+ trust_remote_code,
+ prefix = _ST_PACKAGE_PREFIX,
+) -> None:
if (
isinstance(class_ref, str)
- and not class_ref.startswith(_ST_PACKAGE_PREFIX)
+ and not class_ref.startswith(prefix)
and model_name_or_path is not None
and not trust_remote_code
):
@@ -486,25 +491,44 @@ def _gate_st_custom_modules() -> None:
setattr(_load_module_class_from_ref, _ST_GATE_MARKER, True)
_load_module_class_from_ref.__wrapped__ = original
setattr(owner, "_load_module_class_from_ref", _load_module_class_from_ref)
- # 5.0-5.4 Router imports via import_from_string, past both resolvers.
- try:
- import importlib
+ # Below 6.0 these module loaders import a dotted path out of their own config via
+ # import_from_string, past both resolvers: Router its types, Dense its
+ # activation_function (and calls it), WordEmbeddings its tokenizer_class.
+ for module_path, prefix, where in (
+ ("sentence_transformers.models.Router", _ST_PACKAGE_PREFIX, "behind this Router"),
+ ("sentence_transformers.models.Dense", "torch.", "in a Dense module"),
+ (
+ "sentence_transformers.models.WordEmbeddings",
+ _ST_PACKAGE_PREFIX,
+ "in a WordEmbeddings module",
+ ),
+ ):
+ try:
+ import importlib
- router = importlib.import_module("sentence_transformers.models.Router")
- original_from_string = getattr(router, "import_from_string", None)
- if original_from_string is not None and not getattr(
- original_from_string, _ST_GATE_MARKER, False
- ):
+ module = importlib.import_module(module_path)
+ original_from_string = getattr(module, "import_from_string", None)
+ if original_from_string is None or getattr(
+ original_from_string, _ST_GATE_MARKER, False
+ ):
+ continue
- def import_from_string(dotted_path, *args, **kwargs):
- _refuse_custom_module(dotted_path, "behind this Router", False)
- return original_from_string(dotted_path, *args, **kwargs)
+ def import_from_string(
+ dotted_path,
+ *args,
+ _original = original_from_string,
+ _prefix = prefix,
+ _where = where,
+ **kwargs,
+ ):
+ _refuse_custom_module(dotted_path, _where, False, prefix = _prefix)
+ return _original(dotted_path, *args, **kwargs)
setattr(import_from_string, _ST_GATE_MARKER, True)
import_from_string.__wrapped__ = original_from_string
- router.import_from_string = import_from_string
- except Exception:
- pass
+ module.import_from_string = import_from_string
+ except Exception:
+ pass
try:
import sys
from sentence_transformers.util import misc
diff --git a/studio/backend/routes/export.py b/studio/backend/routes/export.py
index 03a49895149..a5fd8057c5c 100644
--- a/studio/backend/routes/export.py
+++ b/studio/backend/routes/export.py
@@ -32,6 +32,7 @@ from auth.authentication import allow_ambient_hf_token, get_current_subject
from hub.services.models import account_access
from hub.utils.hf_tokens import HfTokenArg, hf_token_arg
+from utils.security.consent import managed_remote_code_refused
from utils.utils import safe_error_detail
try:
@@ -154,7 +155,8 @@ async def load_checkpoint(
max_seq_length = request.max_seq_length,
load_in_4bit = load_in_4bit,
offload_layers = request.offload_layers,
- trust_remote_code = request.trust_remote_code,
+ # Downgraded, not refused: the Export tab always sends true for local models.
+ trust_remote_code = request.trust_remote_code and not managed_remote_code_refused(),
approved_remote_code_fingerprint = request.approved_remote_code_fingerprint,
hf_token = export_hf_token,
# A supplied token cannot say whether it came from a session or an API key.
diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.py
index 894fd393a95..97d6fbe9f6a 100644
--- a/studio/backend/routes/training.py
+++ b/studio/backend/routes/training.py
@@ -100,6 +100,7 @@ from hub.dependencies import get_hf_token, get_request_hf_token
from hub.utils.host_paths import redact_host_paths
from hub.utils.hf_tokens import HfTokenArg, cached_read_refused, hf_token_arg
+from utils.security.consent import managed_remote_code_refused
from utils.utils import (
canonical_model_repo_id,
hf_dns_dead,
@@ -2375,7 +2376,8 @@ async def start_training(
"require_exact_dataset_resource": resume_requires_exact_dataset,
"require_validated_model_snapshot": cached_model_pin is not None,
"adapter_base_pin": getattr(model_preflight, "adapter_base_pin", None),
- "trust_remote_code": request.trust_remote_code,
+ # A managed account never forwards remote code to the worker.
+ "trust_remote_code": request.trust_remote_code and not managed_remote_code_refused(),
"approved_remote_code_fingerprint": request.approved_remote_code_fingerprint,
"subject": current_subject,
"gpu_ids": request.gpu_ids,
diff --git a/studio/backend/tests/test_embedding_model_security_gate.py b/studio/backend/tests/test_embedding_model_security_gate.py
index 87babb27726..0a1aa66289b 100644
--- a/studio/backend/tests/test_embedding_model_security_gate.py
+++ b/studio/backend/tests/test_embedding_model_security_gate.py
@@ -839,3 +839,101 @@ def test_st_gate_covers_router_sub_module_types():
with pytest.raises(ValueError, match = "not part of Sentence Transformers"):
router.import_from_string("custom_mod.Pooling")
assert router.import_from_string("sentence_transformers.models.Pooling").__name__ == "Pooling"
+
+
+def _pre_six_module(name):
+ st = pytest.importorskip("sentence_transformers")
+ if int(st.__version__.split(".")[0]) >= 6:
+ pytest.skip("sentence-transformers 6 gates this itself")
+ import importlib
+
+ module = importlib.import_module(f"sentence_transformers.models.{name}")
+ if not hasattr(module, "import_from_string"):
+ pytest.skip(f"this {name} does not resolve its config through import_from_string")
+ return module
+
+
+def _marker_package(tmp_path, monkeypatch, name):
+ marker = tmp_path / f"{name}.ran"
+ pkg = tmp_path / "site" / name
+ pkg.mkdir(parents = True)
+ (pkg / "__init__.py").write_text(
+ f"open({str(marker)!r}, 'w').write('ran')\n"
+ "def act():\n"
+ " return None\n"
+ "class Tok:\n"
+ " @staticmethod\n"
+ " def load(path):\n"
+ " return Tok()\n"
+ )
+ monkeypatch.syspath_prepend(str(tmp_path / "site"))
+ return marker
+
+
+def test_st_gate_covers_dense_activation_function(tmp_path, monkeypatch):
+ # Dense.load imports and calls config["activation_function"] below 6.0.
+ import json
+
+ dense = _pre_six_module("Dense")
+ import core.rag.embeddings as embeddings
+
+ marker = _marker_package(tmp_path, monkeypatch, "dense_payload_mod")
+ folder = tmp_path / "2_Dense"
+ folder.mkdir()
+ (folder / "config.json").write_text(
+ json.dumps(
+ {"in_features": 4, "out_features": 4, "activation_function": "dense_payload_mod.act"}
+ )
+ )
+ embeddings._gate_st_custom_modules()
+ with pytest.raises(ValueError, match = "not part of Sentence Transformers"):
+ dense.Dense.load(str(folder))
+ assert not marker.exists()
+ with pytest.raises(ValueError, match = "not part of Sentence Transformers"):
+ dense.import_from_string("os.getcwd")
+
+
+def test_st_gate_keeps_a_real_dense_module_loading(tmp_path):
+ torch = pytest.importorskip("torch")
+ dense = _pre_six_module("Dense")
+ import core.rag.embeddings as embeddings
+
+ embeddings._gate_st_custom_modules()
+ saved = dense.Dense(4, 4, activation_function = torch.nn.Tanh())
+ (tmp_path / "2_Dense").mkdir()
+ saved.save(str(tmp_path / "2_Dense"))
+ loaded = dense.Dense.load(str(tmp_path / "2_Dense"))
+ assert isinstance(loaded.activation_function, torch.nn.Tanh)
+
+
+def test_st_gate_covers_word_embeddings_tokenizer_class(tmp_path, monkeypatch):
+ import json
+
+ word = _pre_six_module("WordEmbeddings")
+ import core.rag.embeddings as embeddings
+
+ marker = _marker_package(tmp_path, monkeypatch, "word_payload_mod")
+ folder = tmp_path / "0_WordEmbeddings"
+ folder.mkdir()
+ (folder / "wordembedding_config.json").write_text(
+ json.dumps({"tokenizer_class": "word_payload_mod.Tok", "update_embeddings": False})
+ )
+ embeddings._gate_st_custom_modules()
+ with pytest.raises(ValueError, match = "not part of Sentence Transformers"):
+ word.WordEmbeddings.load(str(folder))
+ assert not marker.exists()
+ tok = "sentence_transformers.models.tokenizer.WhitespaceTokenizer.WhitespaceTokenizer"
+ assert word.import_from_string(tok).__name__ == "WhitespaceTokenizer"
+
+
+def test_st_gate_module_loader_wrappers_are_idempotent():
+ import core.rag.embeddings as embeddings
+
+ modules = [_pre_six_module(name) for name in ("Router", "Dense", "WordEmbeddings")]
+ embeddings._gate_st_custom_modules()
+ first = [module.import_from_string for module in modules]
+ embeddings._gate_st_custom_modules()
+ for module, wrapper in zip(modules, first):
+ assert module.import_from_string is wrapper
+ assert getattr(wrapper, embeddings._ST_GATE_MARKER, False)
+ assert not getattr(wrapper.__wrapped__, embeddings._ST_GATE_MARKER, False)
diff --git a/studio/backend/tests/test_native_audio_companion_targets.py b/studio/backend/tests/test_native_audio_companion_targets.py
new file mode 100644
index 00000000000..3e521402532
--- /dev/null
+++ b/studio/backend/tests/test_native_audio_companion_targets.py
@@ -0,0 +1,112 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Native audio security targets list every companion repo that actually loads."""
+
+import json
+
+import pytest
+
+from core.inference.native_audio import (
+ HIGGS_TTS3_CODEC_REPO,
+ MOSS_LOCAL_CODEC_REPO,
+ native_audio_security_targets,
+)
+
+
+def _write(path, name, value):
+ path.mkdir(parents = True, exist_ok = True)
+ (path / name).write_text(json.dumps(value), encoding = "utf-8")
+
+
+def test_moss_local_audio_tokenizer_config_codec_is_a_target(tmp_path):
+ checkpoint = tmp_path / "checkpoint"
+ _write(checkpoint, "config.json", {"model_type": "moss_tts_local"})
+ _write(
+ checkpoint,
+ "audio_tokenizer_config.json",
+ {"audio_tokenizer_class": "AutoModel", "audio_tokenizer_name_or_path": "acme/other-codec"},
+ )
+ targets = native_audio_security_targets(str(checkpoint), "moss_tts_local")
+ assert "acme/other-codec" in targets
+ assert targets[:2] == [str(checkpoint), MOSS_LOCAL_CODEC_REPO]
+
+
+def test_moss_local_audio_tokenizer_config_same_codec_is_not_duplicated(tmp_path):
+ checkpoint = tmp_path / "checkpoint"
+ _write(checkpoint, "processor_config.json", {"audio_tokenizer_name_or_path": "acme/codec"})
+ _write(
+ checkpoint, "audio_tokenizer_config.json", {"audio_tokenizer_name_or_path": "acme/codec"}
+ )
+ assert native_audio_security_targets(str(checkpoint), "moss_tts_local") == [
+ str(checkpoint),
+ "acme/codec",
+ ]
+
+
+ ("audio_type", "metadata_file", "codec_key"),
+ (
+ ("moss_tts_local", "processor_config.json", "audio_tokenizer_name_or_path"),
+ ("moss_tts_local", "audio_tokenizer_config.json", "audio_tokenizer_name_or_path"),
+ ("higgs_tts2", "audio_tokenizer_config.json", "audio_tokenizer_name_or_path"),
+ ("higgs_tts3", "config.json", "audio_tokenizer_id"),
+ ),
+)
+def test_adapter_only_codec_adds_its_base(tmp_path, audio_type, metadata_file, codec_key):
+ checkpoint = tmp_path / "checkpoint"
+ codec = tmp_path / "codec"
+ _write(checkpoint, metadata_file, {codec_key: str(codec)})
+ _write(
+ codec,
+ "adapter_config.json",
+ {"peft_type": "LORA", "base_model_name_or_path": "acme/hidden-base"},
+ )
+ targets = native_audio_security_targets(str(checkpoint), audio_type)
+ assert str(codec) in targets
+ assert "acme/hidden-base" in targets
+
+
+def test_local_codec_with_config_is_not_swapped_for_its_base(tmp_path):
+ checkpoint = tmp_path / "checkpoint"
+ codec = tmp_path / "codec"
+ _write(checkpoint, "config.json", {"audio_tokenizer_id": str(codec)})
+ _write(codec, "config.json", {"model_type": "higgs_audio_v2_tokenizer"})
+ _write(codec, "adapter_config.json", {"base_model_name_or_path": "acme/unused-base"})
+ assert native_audio_security_targets(str(checkpoint), "higgs_tts3") == [
+ str(checkpoint),
+ str(codec),
+ ]
+
+
+def test_stock_layout_targets_unchanged_and_curated_codec_not_probed(tmp_path, monkeypatch):
+ checkpoint = tmp_path / "checkpoint"
+ _write(checkpoint, "config.json", {"model_type": "higgs_multimodal_qwen3"})
+ import core.inference.native_audio as native_audio
+
+ real_read = native_audio._read_audio_metadata
+ read = []
+
+ def tracking_read(model_name, filename, *args, **kwargs):
+ read.append((model_name, filename))
+ return real_read(model_name, filename, *args, **kwargs)
+
+ monkeypatch.setattr(native_audio, "_read_audio_metadata", tracking_read)
+ assert native_audio_security_targets(str(checkpoint), "higgs_tts3") == [
+ str(checkpoint),
+ HIGGS_TTS3_CODEC_REPO,
+ ]
+ assert all(name != HIGGS_TTS3_CODEC_REPO for name, _ in read)
+
+
+def test_oversized_codec_adapter_config_fails_closed(tmp_path):
+ checkpoint = tmp_path / "checkpoint"
+ codec = tmp_path / "codec"
+ _write(checkpoint, "config.json", {"audio_tokenizer_id": str(codec)})
+ _write(
+ codec,
+ "adapter_config.json",
+ {"base_model_name_or_path": "acme/hidden-base", "padding": "x" * 1_000_000},
+ )
+ with pytest.raises(ValueError, match = "security inspection limit"):
+ native_audio_security_targets(str(checkpoint), "higgs_tts3")
diff --git a/studio/backend/tests/test_remote_code_gate_gaps.py b/studio/backend/tests/test_remote_code_gate_gaps.py
new file mode 100644
index 00000000000..db6df9f033c
--- /dev/null
+++ b/studio/backend/tests/test_remote_code_gate_gaps.py
@@ -0,0 +1,237 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Remote code reaches a load only through the consent gate.
+
+- GGUF export: llama.cpp's converter loads some tokenizers with trust_remote_code=True, so
+ unless the checkpoint load was approved for remote code the converter gets no usable
+ modules cache to import a tokenizer auto_map from.
+- Managed accounts: the export and training routes downgrade a requested
+ trust_remote_code to False (the Export tab always sends true, so no refusal).
+- A legacy list-form tokenizer auto_map counts as remote code for the scan.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import os
+import sys
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+_BACKEND_DIR = Path(__file__).resolve().parent.parent
+if str(_BACKEND_DIR) not in sys.path:
+ sys.path.insert(0, str(_BACKEND_DIR))
+_TESTS_DIR = Path(__file__).resolve().parent
+if str(_TESTS_DIR) not in sys.path:
+ sys.path.insert(0, str(_TESTS_DIR))
+
+_KEY = "HF_MODULES_CACHE"
+
+
+# ---- GGUF export: the converter follows the load's consent-gated decision ----------------
+
+
[email protected]("mlx", [False, True])
[email protected]("approved", [None, False, True])
+def test_gguf_export_withholds_the_modules_cache_unless_approved(
+ tmp_path, monkeypatch, mlx, approved
+):
+ from test_export_converter_pin import _export_mod, _zoo
+
+ mod = _export_mod(monkeypatch, mlx = mlx)
+ _zoo(monkeypatch)
+ monkeypatch.setenv(_KEY, "/mine")
+ save_dir = tmp_path / "export"
+ monkeypatch.setattr(mod, "resolve_export_write_dir", lambda _value: save_dir)
+ seen = []
+
+ class _Model:
+ def save_pretrained_gguf(self, model_save_path, tokenizer, **kwargs):
+ seen.append(os.environ.get(_KEY))
+ output_dir = Path(f"{model_save_path}_gguf")
+ output_dir.mkdir(parents = True, exist_ok = True)
+ (output_dir / "converted.gguf").write_bytes(b"gguf")
+
+ def push_to_hub_gguf(self, repo_id, tokenizer, **kwargs):
+ seen.append(os.environ.get(_KEY))
+
+ backend = mod.ExportBackend.__new__(mod.ExportBackend)
+ backend.current_model = _Model()
+ backend.current_tokenizer = object()
+ backend.current_checkpoint = None
+ if approved is not None:
+ backend.trust_remote_code = approved
+
+ success, message, _ = backend.export_gguf(str(save_dir), "Q4_K_M")
+ assert success is True, message
+ success, message, _ = backend.export_gguf(
+ "", "Q4_K_M", push_to_hub = True, repo_id = "me/model", hf_token = "hf_x"
+ )
+ assert success is True, message
+
+ assert len(seen) == 2
+ if approved:
+ assert seen == ["/mine", "/mine"]
+ else:
+ for value in seen:
+ assert value != "/mine"
+ with pytest.raises(OSError):
+ os.makedirs(value, exist_ok = True)
+ assert os.environ[_KEY] == "/mine"
+
+
+# ---- managed accounts never forward trust_remote_code ------------------------------------
+
+
+def _as_managed(
+ monkeypatch,
+ managed,
+ override = False,
+):
+ from utils.security import consent
+ monkeypatch.setattr(consent, "is_owner_context", lambda: not managed)
+ if override:
+ monkeypatch.setenv(consent.MANAGED_REMOTE_CODE_OVERRIDE, "1")
+ else:
+ monkeypatch.delenv(consent.MANAGED_REMOTE_CODE_OVERRIDE, raising = False)
+
+
+ "managed, override, expected",
+ [(True, False, False), (False, False, True), (True, True, True)],
+ ids = ["managed", "owner", "managed_with_owner_override"],
+)
+def test_export_load_downgrades_trust_for_managed_accounts(
+ monkeypatch, tmp_path, managed, override, expected
+):
+ from fastapi import FastAPI
+ from fastapi.testclient import TestClient
+
+ from auth.authentication import allow_ambient_hf_token, get_current_subject
+ from routes import export as export_routes
+
+ async def _supported():
+ return None
+
+ _as_managed(monkeypatch, managed, override)
+ monkeypatch.setattr(export_routes, "_ensure_export_supported", _supported)
+ monkeypatch.setattr(export_routes, "_authorized_adapter_base", lambda _path: None)
+ backend = MagicMock()
+ backend.load_checkpoint.return_value = (True, "loaded")
+ monkeypatch.setattr(export_routes, "get_export_backend", lambda: backend)
+
+ app = FastAPI()
+ app.include_router(export_routes.router, prefix = "/api/export")
+ app.dependency_overrides[get_current_subject] = lambda: "alice"
+ app.dependency_overrides[allow_ambient_hf_token] = lambda: True
+
+ response = TestClient(app).post(
+ "/api/export/load-checkpoint",
+ json = {"checkpoint_path": str(tmp_path), "hf_token": None, "trust_remote_code": True},
+ )
+
+ assert response.status_code == 200, response.text
+ assert backend.load_checkpoint.call_args.kwargs["trust_remote_code"] is expected
+
+
+def training_route():
+ import importlib.util
+
+ spec = importlib.util.spec_from_file_location(
+ "remote_code_gate_training_route", _BACKEND_DIR / "routes" / "training.py"
+ )
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ module._hub_unreachable = lambda: False
+ return module
+
+
+async def _inline_to_thread(function, *args, **kwargs):
+ return function(*args, **kwargs)
+
+
+ "managed, expected", [(True, False), (False, True)], ids = ["managed", "owner"]
+)
+def test_training_start_downgrades_trust_for_managed_accounts(
+ monkeypatch, tmp_path, training_route, managed, expected
+):
+ from models.training import TrainingStartRequest
+
+ model = tmp_path / "model"
+ model.mkdir()
+ (model / "config.json").write_text(json.dumps({"model_type": "llama"}), encoding = "utf-8")
+ (model / "model.safetensors").write_bytes(b"\x08\x00\x00\x00\x00\x00\x00\x00{} ")
+ request = TrainingStartRequest(
+ model_name = str(model),
+ training_type = "LoRA/QLoRA",
+ format_type = "auto",
+ hf_dataset = "org/data",
+ load_in_4bit = False,
+ trust_remote_code = True,
+ )
+ _as_managed(monkeypatch, managed)
+ captured = {}
+ backend = SimpleNamespace(
+ current_job_id = None,
+ is_training_active = lambda: False,
+ start_training = lambda **kwargs: captured.update(kwargs) or True,
+ )
+ with (
+ patch.object(training_route, "get_training_backend", return_value = backend),
+ patch.object(training_route, "_preflight_hf_dataset_request", return_value = None),
+ patch.object(training_route.asyncio, "to_thread", _inline_to_thread),
+ ):
+ response = asyncio.run(training_route.start_training(request, current_subject = "alice"))
+ assert response.status == "queued", response
+ assert "trust_remote_code" in captured, "training backend was never reached"
+ assert captured["trust_remote_code"] is expected
+
+
+# ---- a list-form tokenizer auto_map is remote code ---------------------------------------
+
+
+def test_a_list_form_tokenizer_auto_map_is_declared():
+ from utils.security.remote_code_scan import _auto_map_refs, config_declares_auto_map
+
+ config = {"auto_map": ["tokenization_evil.EvilTok", None]}
+ assert config_declares_auto_map(config) is True
+ assert _auto_map_refs(config) == {(None, "tokenization_evil.py")}
+ hub = {"auto_map": ["acme/evil--tokenization_evil.EvilTok", None]}
+ assert _auto_map_refs(hub) == {("acme/evil", "tokenization_evil.py")}
+ # Dict form and no auto_map are unchanged.
+ assert config_declares_auto_map({"auto_map": {"AutoTokenizer": [None, "t.T"]}}) is True
+ assert config_declares_auto_map({"model_type": "llama"}) is False
+ assert config_declares_auto_map({"auto_map": []}) is False
+
+
+def test_the_gate_scans_a_list_form_tokenizer_auto_map(tmp_path):
+ from utils.security import evaluate_remote_code_consent
+
+ model = tmp_path / "model"
+ model.mkdir()
+ (model / "config.json").write_text(json.dumps({"model_type": "llama"}), encoding = "utf-8")
+ (model / "tokenizer_config.json").write_text(
+ json.dumps(
+ {
+ "tokenizer_class": "PreTrainedTokenizerFast",
+ "auto_map": [None, "tokenization_evil.EvilTok"],
+ }
+ ),
+ encoding = "utf-8",
+ )
+ (model / "tokenization_evil.py").write_text(
+ "import subprocess\nsubprocess.Popen(['id'])\n", encoding = "utf-8"
+ )
+
+ decision = evaluate_remote_code_consent(str(model), trust_remote_code = True)
+
+ assert decision.has_remote_code is True
+ assert decision.fingerprint
+ assert "no-op" not in decision.reason
diff --git a/studio/backend/utils/security/remote_code_scan.py b/studio/backend/utils/security/remote_code_scan.py
index 93f58fec481..d14f42a3bd3 100644
--- a/studio/backend/utils/security/remote_code_scan.py
+++ b/studio/backend/utils/security/remote_code_scan.py
@@ -576,6 +576,9 @@ def iter_auto_maps(cfg, _depth: int = 0):
auto_map = cfg.get("auto_map")
if isinstance(auto_map, dict) and auto_map:
yield auto_map
+ elif isinstance(auto_map, (list, tuple)) and auto_map:
+ # Legacy tokenizer form: transformers reads a bare [slow, fast] list as AutoTokenizer.
+ yield {"AutoTokenizer": list(auto_map)}
for key, value in cfg.items():
if key == "auto_map":
continue
diff --git a/tests/saving/test_gguf_converter_remote_code.py b/tests/saving/test_gguf_converter_remote_code.py
new file mode 100644
index 00000000000..27e7d6ff59c
--- /dev/null
+++ b/tests/saving/test_gguf_converter_remote_code.py
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""The decision-model GGUF converter never imports a checkpoint's auto_map code.
+
+The converter loads several architectures' tokenizers with trust_remote_code=True; decision
+backbones are built-in classes, so the converter child gets no usable modules cache.
+"""
+
+from __future__ import annotations
+
+import os
+
+import pytest
+
+from unsloth.models import decision_gguf
+
+_KEY = "HF_MODULES_CACHE"
+
+
+def test_the_decision_converter_gets_no_usable_modules_cache(monkeypatch, tmp_path):
+ monkeypatch.setenv(_KEY, str(tmp_path / "modules"))
+ outfile = tmp_path / "model.gguf"
+ seen = {}
+
+ def _run(command, what, print_output, env):
+ seen.update(env)
+ outfile.write_bytes(b"GGUF")
+
+ monkeypatch.setattr(decision_gguf, "_run", _run)
+ decision_gguf._convert(tmp_path, tmp_path / "checkpoint", "f16", outfile, False, False)
+ assert seen[_KEY] != str(tmp_path / "modules")
+ with pytest.raises(OSError):
+ os.makedirs(seen[_KEY], exist_ok = True)
+ assert os.environ[_KEY] == str(tmp_path / "modules")
diff --git a/tests/test_st_modules_json_trust_gate.py b/tests/test_st_modules_json_trust_gate.py
index 7d6e874848b..3a136480ee7 100644
--- a/tests/test_st_modules_json_trust_gate.py
+++ b/tests/test_st_modules_json_trust_gate.py
@@ -1505,3 +1505,229 @@ def test_an_unreadable_module_config_is_tolerated_where_upstream_gates_it(tmp_pa
False,
st_models.Dense,
)
+
+
+# Router routes, whole-model module types and rooted module paths: each opens configs of its
+# own one level down, so the gate has to follow them where the loader does.
+
+_ROUTER = "sentence_transformers.models.Router"
+
+
+def _nested_router_model(tmp_path, inner_types):
+ """A model whose Router's only route is another Router, whose types are inner_types."""
+ model = tmp_path / "nested"
+ _write_module_config(
+ model / "0_Router",
+ "router_config.json",
+ {
+ "types": {"inner": _ROUTER},
+ "structure": {"query": ["inner"]},
+ "parameters": {},
+ },
+ )
+ _write_module_config(
+ model / "0_Router" / "inner",
+ "router_config.json",
+ {
+ "types": inner_types,
+ "structure": {"query": list(inner_types)},
+ "parameters": {},
+ },
+ )
+ (model / "modules.json").write_text(
+ json.dumps([{"idx": 0, "name": "0", "path": "0_Router", "type": _ROUTER}]),
+ encoding = "utf8",
+ )
+ return model
+
+
+def test_a_router_inside_a_router_is_checked_on_the_load_route(model_dir, tmp_path):
+ pytest.importorskip("sentence_transformers.models").Router
+ _, witness = model_dir
+ model = _nested_router_model(tmp_path, {"payload": f"{MARKER}.Thing"})
+
+ with pytest.raises(ValueError, match = "outside sentence_transformers"):
+ _load_modules(model, False)
+ assert not witness.exists()
+
+
+def test_a_router_inside_a_router_is_checked_on_the_delegated_route(
+ model_dir, tmp_path, monkeypatch
+):
+ pytest.importorskip("sentence_transformers.models").Router
+ _simulate_pre_six(monkeypatch)
+ _, witness = model_dir
+ model = _nested_router_model(tmp_path, {"payload": f"{MARKER}.Thing"})
+
+ with pytest.raises(ValueError, match = "outside sentence_transformers"):
+ FastSentenceTransformer._check_modules_json_types(str(model), None, False)
+ assert not witness.exists()
+
+
+def test_a_router_inside_a_router_is_fetched_from_the_hub_on_the_delegated_route(
+ tmp_path, monkeypatch
+):
+ pytest.importorskip("sentence_transformers.models").Router
+ _simulate_pre_six(monkeypatch)
+ model = _nested_router_model(tmp_path, {"payload": "evil_pkg.Thing"})
+ requested = []
+
+ def fake_download(repo, filename, **kwargs):
+ requested.append(filename)
+ path = model / filename
+ if not path.is_file():
+ from huggingface_hub.errors import EntryNotFoundError
+ raise EntryNotFoundError(filename)
+ return str(path)
+
+ _patch_download(monkeypatch, fake_download)
+ with pytest.raises(ValueError, match = "evil_pkg.Thing"):
+ FastSentenceTransformer._check_delegated_module_config(
+ "acme/embedder",
+ {"idx": 0, "name": "0", "path": "0_Router", "type": _ROUTER},
+ _ROUTER,
+ pytest.importorskip("sentence_transformers.models").Router,
+ )
+ assert "0_Router/inner/router_config.json" in requested
+
+
+def test_a_real_nested_router_still_passes(tmp_path, monkeypatch):
+ pytest.importorskip("sentence_transformers.models").Router
+ model = _nested_router_model(
+ tmp_path,
+ {
+ "a": "sentence_transformers.models.Pooling",
+ "b": "sentence_transformers.models.Normalize",
+ "c": "sentence_transformers.models.Dense",
+ },
+ )
+ _write_module_config(
+ model / "0_Router" / "inner" / "c",
+ "config.json",
+ {
+ "in_features": 4,
+ "out_features": 4,
+ "activation_function": "torch.nn.modules.activation.Tanh",
+ },
+ )
+ FastSentenceTransformer._check_module_config_class_refs(
+ str(model / "0_Router"),
+ _ROUTER,
+ str(model),
+ False,
+ pytest.importorskip("sentence_transformers.models").Router,
+ )
+ _simulate_pre_six(monkeypatch)
+ FastSentenceTransformer._check_modules_json_types(str(model), None, False)
+
+
+def test_a_bad_route_under_a_nested_router_is_fine_with_consent(tmp_path):
+ model = _nested_router_model(tmp_path, {"payload": "evil_pkg.Thing"})
+ FastSentenceTransformer._check_module_config_class_refs(
+ str(model / "0_Router"),
+ _ROUTER,
+ str(model),
+ True,
+ pytest.importorskip("sentence_transformers.models").Router,
+ )
+
+
+def test_a_whole_model_is_not_accepted_as_a_module(tmp_path):
+ model = tmp_path / "model"
+ (model / "inner").mkdir(parents = True)
+ (model / "modules.json").write_text(
+ json.dumps(
+ [
+ {
+ "idx": 0,
+ "name": "0",
+ "path": "inner",
+ "type": "sentence_transformers.SentenceTransformer",
+ }
+ ]
+ ),
+ encoding = "utf8",
+ )
+ with pytest.raises(ValueError, match = "whole model"):
+ FastSentenceTransformer._check_modules_json_types(str(model), None, False)
+ with pytest.raises(ValueError, match = "whole model"):
+ _load_modules(model, False)
+
+
+def test_a_whole_model_is_not_accepted_as_a_router_route(tmp_path):
+ model = tmp_path / "model"
+ _write_module_config(
+ model / "0_Router",
+ "router_config.json",
+ {
+ "types": {"inner": "sentence_transformers.SentenceTransformer"},
+ "structure": {"query": ["inner"]},
+ "parameters": {},
+ },
+ )
+ with pytest.raises(ValueError, match = "whole model"):
+ FastSentenceTransformer._check_module_config_class_refs(
+ str(model / "0_Router"),
+ _ROUTER,
+ str(model),
+ False,
+ pytest.importorskip("sentence_transformers.models").Router,
+ )
+
+
+def test_word_embeddings_tokenizer_class_must_be_a_word_tokenizer(tmp_path):
+ st_models = pytest.importorskip("sentence_transformers.models")
+ if getattr(st_models, "WordEmbeddings", None) is None:
+ pytest.skip("this sentence-transformers has no WordEmbeddings")
+ folder = _write_module_config(
+ tmp_path / "0_WordEmbeddings",
+ "wordembedding_config.json",
+ {"tokenizer_class": _ROUTER, "max_seq_length": 8},
+ )
+ with pytest.raises(ValueError, match = "word tokenizer"):
+ FastSentenceTransformer._check_module_config_class_refs(
+ str(folder),
+ "sentence_transformers.models.WordEmbeddings",
+ "acme/embedder",
+ False,
+ st_models.WordEmbeddings,
+ )
+ (folder / "wordembedding_config.json").write_text(
+ json.dumps(
+ {
+ "tokenizer_class": "sentence_transformers.models.tokenizer.WhitespaceTokenizer.WhitespaceTokenizer",
+ "max_seq_length": 8,
+ }
+ )
+ )
+ FastSentenceTransformer._check_module_config_class_refs(
+ str(folder),
+ "sentence_transformers.models.WordEmbeddings",
+ "acme/embedder",
+ False,
+ st_models.WordEmbeddings,
+ )
+
+
+def test_a_rooted_router_path_checks_the_folder_the_loader_reads(tmp_path, monkeypatch):
+ """Path(model, "/x") is "/x" for the loader, so stripping the slash checked the wrong folder."""
+ pytest.importorskip("sentence_transformers.models").Router
+ _simulate_pre_six(monkeypatch)
+ elsewhere = _write_module_config(
+ tmp_path / "elsewhere",
+ "router_config.json",
+ {
+ "types": {"payload": "evil_pkg.Thing"},
+ "structure": {},
+ "parameters": {},
+ },
+ )
+ model = tmp_path / "model"
+ model.mkdir()
+ (model / "modules.json").write_text(
+ json.dumps([{"idx": 0, "name": "0", "path": str(elsewhere), "type": _ROUTER}]),
+ encoding = "utf8",
+ )
+ # The folder-containment check, where present, refuses it before the type gate does.
+ with pytest.raises(ValueError, match = r"evil_pkg\.Thing|outside the model"):
+ FastSentenceTransformer._check_modules_json_types(str(model), None, False)
diff --git a/tests/test_transformers_security_fixes.py b/tests/test_transformers_security_fixes.py
index 8502a59f393..6094cc080b9 100644
--- a/tests/test_transformers_security_fixes.py
+++ b/tests/test_transformers_security_fixes.py
@@ -33,6 +33,7 @@ KERNEL_AFFECTED = TF_VERSION < Version("5.3.0")
KERNEL_EXPLOITABLE = Version("4.56.0") <= TF_VERSION < Version("5.3.0")
LIGHTGLUE_AFFECTED = Version("4.54.0") <= TF_VERSION < Version("5.5.0")
TEMPLATE_AFFECTED = TF_VERSION < Version("5.10.0")
+VOCAB_PATH_AFFECTED = TF_VERSION < Version("5.17.0")
def _load_import_fixes():
@@ -56,6 +57,7 @@ _TARGETS = [
(PretrainedConfig, "from_dict", "_unsloth_patched_untrusted_config_fields"),
(PretrainedConfig, "_dict_from_json_file", "_unsloth_patched_untrusted_config_fields"),
(PreTrainedTokenizerBase, "save_pretrained", "_unsloth_patched_chat_template_names"),
+ (PreTrainedTokenizerBase, "_from_pretrained", "_unsloth_patched_tokenizer_config_vocab_paths"),
]
if "save_chat_templates" in PreTrainedTokenizerBase.__dict__:
_TARGETS.append(
@@ -81,6 +83,7 @@ def patched(unpatched):
fixes = _load_import_fixes()
fixes.fix_transformers_untrusted_config_fields()
fixes.fix_transformers_chat_template_path_traversal()
+ fixes.fix_transformers_tokenizer_config_vocab_file_override()
return fixes
@@ -188,11 +191,33 @@ def _escaped_files(root):
return [p for p in Path(root).rglob("ESCAPED_TEMPLATE*")]
+def _bert_repo(path, words):
+ # vocab.txt only: with a tokenizer.json alongside, the vocab_file would not be read at all.
+ path.mkdir(parents = True)
+ (path / "vocab.txt").write_text("\n".join(["[PAD]", "[UNK]", *words]) + "\n")
+ (path / "tokenizer_config.json").write_text(json.dumps({"tokenizer_class": "BertTokenizer"}))
+ return path
+
+
+def _crafted_vocab_path_repo(tmp_path):
+ # A file outside the repo that the repo's tokenizer_config.json names as its vocabulary.
+ outside = tmp_path / "outside" / "OUTSIDE.txt"
+ outside.parent.mkdir()
+ outside.write_text("first outside line\nsecond outside line\n")
+ repo = _bert_repo(tmp_path / "vocab_repo", ["repo", "words"])
+ config_path = repo / "tokenizer_config.json"
+ config = json.loads(config_path.read_text())
+ config["vocab_file"] = str(outside)
+ config_path.write_text(json.dumps(config))
+ return repo, outside
+
+
def test_fixes_are_called_and_deleted_in_gpu_init():
source = (REPO / "unsloth" / "_gpu_init.py").read_text(encoding = "utf-8")
for name in (
"fix_transformers_untrusted_config_fields",
"fix_transformers_chat_template_path_traversal",
+ "fix_transformers_tokenizer_config_vocab_file_override",
):
assert f"\n{name}()\n" in source, f"{name} is never called in _gpu_init.py"
assert f"del {name}\n" in source, f"{name} is left bound on the unsloth namespace"
@@ -202,7 +227,9 @@ def test_fixes_are_called_on_the_mlx_path():
source = (REPO / "unsloth" / "__init__.py").read_text(encoding = "utf-8")
assert "fix_transformers_untrusted_config_fields as _fix_untrusted_config" in source
assert "fix_transformers_chat_template_path_traversal as _fix_template_names" in source
+ assert "fix_transformers_tokenizer_config_vocab_file_override as _fix_vocab_paths" in source
assert "_fix_untrusted_config()" in source and "_fix_template_names()" in source
+ assert "_fix_vocab_paths()" in source
def test_unpatched_kernel_field_reaches_the_kernel_fetch(unpatched, kernel_fetches, tmp_path):
@@ -234,6 +261,12 @@ def test_unpatched_template_name_escapes_save_dir(unpatched, tmp_path):
assert bool(_escaped_files(tmp_path / "out")) == TEMPLATE_AFFECTED
+def test_unpatched_vocab_file_in_config_opens_an_outside_path(unpatched, tmp_path):
+ repo, outside = _crafted_vocab_path_repo(tmp_path)
+ tokens = AutoTokenizer.from_pretrained(repo).convert_ids_to_tokens([0, 1])
+ assert (tokens == outside.read_text().splitlines()) == VOCAB_PATH_AFFECTED
+
+
def test_kernel_field_from_config_json_is_dropped(patched, kernel_fetches, tmp_path):
repo = _crafted_kernel_repo(tmp_path)
config = AutoConfig.from_pretrained(repo)
@@ -333,6 +366,37 @@ def test_single_string_template_still_saves(patched, tmp_path):
assert AutoTokenizer.from_pretrained(tmp_path / "saved").chat_template == "{{ messages }}"
+def test_vocab_file_in_config_is_ignored_for_the_repo_file(patched, tmp_path):
+ repo, _ = _crafted_vocab_path_repo(tmp_path)
+ tokenizer = AutoTokenizer.from_pretrained(repo)
+ assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3]) == ["[PAD]", "[UNK]", "repo", "words"]
+
+
+def test_explicit_vocab_file_still_wins(patched, tmp_path):
+ from transformers import BertTokenizer
+
+ repo, _ = _crafted_vocab_path_repo(tmp_path)
+ other = _bert_repo(tmp_path / "other", ["other", "vocab"]) / "vocab.txt"
+ tokenizer = BertTokenizer.from_pretrained(repo, vocab_file = str(other))
+ assert tokenizer.convert_ids_to_tokens([2, 3]) == ["other", "vocab"]
+
+
+def test_stock_tokenizers_still_load_save_and_reload(patched, tmp_path):
+ from transformers import BertTokenizer
+
+ slow = AutoTokenizer.from_pretrained(_bert_repo(tmp_path / "bert", ["hello", "world"]))
+ slow.save_pretrained(tmp_path / "bert_saved")
+ reloaded = AutoTokenizer.from_pretrained(tmp_path / "bert_saved")
+ assert reloaded("hello world").input_ids == slow("hello world").input_ids
+ # Legacy single-file loading resolves the file itself as the vocabulary.
+ single = BertTokenizer.from_pretrained(tmp_path / "bert" / "vocab.txt")
+ assert single.convert_ids_to_tokens([2]) == ["hello"]
+ fast = AutoTokenizer.from_pretrained(_tiny_tokenizer(tmp_path / "fast"))
+ fast.save_pretrained(tmp_path / "fast_saved")
+ reloaded = AutoTokenizer.from_pretrained(tmp_path / "fast_saved")
+ assert reloaded("hello world").input_ids == fast("hello world").input_ids
+
+
def test_benign_model_roundtrip_is_unchanged(patched, tmp_path):
repo = _tiny_llama(tmp_path / "plain")
ids = torch.tensor([[1, 2, 3, 4]])
@@ -343,6 +407,14 @@ def test_benign_model_roundtrip_is_unchanged(patched, tmp_path):
assert torch.equal(model(ids).logits, again(ids).logits)
+def _affected(owner, name):
+ if owner is PretrainedConfig:
+ return KERNEL_AFFECTED or LIGHTGLUE_AFFECTED
+ if name == "_from_pretrained":
+ return VOCAB_PATH_AFFECTED
+ return TEMPLATE_AFFECTED
+
+
def _wrap_depth(owner, name, flag):
current = owner.__dict__.get(name)
function = current.__func__ if isinstance(current, classmethod) else current
@@ -356,11 +428,9 @@ def _wrap_depth(owner, name, flag):
def test_double_apply_is_a_noop(patched):
patched.fix_transformers_untrusted_config_fields()
patched.fix_transformers_chat_template_path_traversal()
- expected_config = 1 if (KERNEL_AFFECTED or LIGHTGLUE_AFFECTED) else 0
- expected_save = 1 if TEMPLATE_AFFECTED else 0
+ patched.fix_transformers_tokenizer_config_vocab_file_override()
for owner, name, flag in _TARGETS:
- expected = expected_config if owner is PretrainedConfig else expected_save
- assert _wrap_depth(owner, name, flag) == expected
+ assert _wrap_depth(owner, name, flag) == (1 if _affected(owner, name) else 0)
def test_fixed_transformers_is_left_untouched(unpatched):
@@ -368,10 +438,10 @@ def test_fixed_transformers_is_left_untouched(unpatched):
fixes = _load_import_fixes()
fixes.fix_transformers_untrusted_config_fields()
fixes.fix_transformers_chat_template_path_traversal()
+ fixes.fix_transformers_tokenizer_config_vocab_file_override()
after = [owner.__dict__.get(name) for owner, name, _ in _TARGETS]
- for (owner, _, _), a, b in zip(_TARGETS, after, before):
- config = owner is PretrainedConfig
- if not (KERNEL_AFFECTED or LIGHTGLUE_AFFECTED if config else TEMPLATE_AFFECTED):
+ for (owner, name, _), a, b in zip(_TARGETS, after, before):
+ if not _affected(owner, name):
assert a is b
@@ -415,7 +485,9 @@ def test_windows_drive_relative_template_name_is_refused(patched, monkeypatch):
assert not patched._chat_template_name_escapes(name)
[email protected]("prerelease", ["5.3.0rc1", "5.5.0rc1", "5.10.0rc1", "5.10.0.dev0"])
+ "prerelease", ["5.3.0rc1", "5.5.0rc1", "5.10.0rc1", "5.10.0.dev0", "5.17.0rc1"]
+)
def test_prerelease_at_a_fix_boundary_still_gets_the_fix(monkeypatch, prerelease):
# A prerelease sorts before its final release (PEP 440), so it may lack the upstream fix.
fixes = _load_import_fixes()
@@ -424,10 +496,12 @@ def test_prerelease_at_a_fix_boundary_still_gets_the_fix(monkeypatch, prerelease
before = (
PretrainedConfig.__dict__["from_dict"],
PreTrainedTokenizerBase.__dict__["save_pretrained"],
+ PreTrainedTokenizerBase.__dict__["_from_pretrained"],
)
try:
fixes.fix_transformers_untrusted_config_fields()
fixes.fix_transformers_chat_template_path_traversal()
+ fixes.fix_transformers_tokenizer_config_vocab_file_override()
flag_cfg = getattr(
PretrainedConfig.__dict__["from_dict"].__func__,
fixes._UNTRUSTED_CONFIG_PATCH_FLAG,
@@ -438,9 +512,20 @@ def test_prerelease_at_a_fix_boundary_still_gets_the_fix(monkeypatch, prerelease
fixes._CHAT_TEMPLATE_NAME_PATCH_FLAG,
False,
)
- if prerelease.startswith("5.10.0"):
+ flag_vocab = getattr(
+ PreTrainedTokenizerBase.__dict__["_from_pretrained"].__func__,
+ fixes._TOKENIZER_VOCAB_PATH_PATCH_FLAG,
+ False,
+ )
+ if prerelease.startswith("5.17.0"):
+ assert flag_vocab
+ elif prerelease.startswith("5.10.0"):
assert flag_tpl
else:
assert flag_cfg
finally:
- PretrainedConfig.from_dict, PreTrainedTokenizerBase.save_pretrained = before
+ (
+ PretrainedConfig.from_dict,
+ PreTrainedTokenizerBase.save_pretrained,
+ PreTrainedTokenizerBase._from_pretrained,
+ ) = before
diff --git a/unsloth/__init__.py b/unsloth/__init__.py
index 959dd90274b..352aa3fd6bf 100644
--- a/unsloth/__init__.py
+++ b/unsloth/__init__.py
@@ -252,11 +252,13 @@ if _IS_MLX:
from .import_fixes import (
fix_transformers_untrusted_config_fields as _fix_untrusted_config,
fix_transformers_chat_template_path_traversal as _fix_template_names,
+ fix_transformers_tokenizer_config_vocab_file_override as _fix_vocab_paths,
)
_fix_untrusted_config()
_fix_template_names()
- del _fix_untrusted_config, _fix_template_names
+ _fix_vocab_paths()
+ del _fix_untrusted_config, _fix_template_names, _fix_vocab_paths
except Exception:
pass
try:
diff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.py
index 5b3ad131f6d..8bdccf49aaf 100644
--- a/unsloth/_gpu_init.py
+++ b/unsloth/_gpu_init.py
@@ -350,6 +350,7 @@ from .import_fixes import (
fix_transformers_flash_attention_mrope_packed_sequence,
fix_transformers_untrusted_config_fields,
fix_transformers_chat_template_path_traversal,
+ fix_transformers_tokenizer_config_vocab_file_override,
fix_transformers_chunked_mask_block_sequence_ids,
fix_transformers_flex_mask_graph_breaks,
fix_transformers_longcat_lsa_config,
@@ -413,9 +414,11 @@ fix_transformers_fully_masked_rows()
fix_transformers_flash_attention_mrope_packed_sequence()
fix_transformers_chunked_mask_block_sequence_ids()
fix_transformers_flex_mask_graph_breaks()
-# CVE-2026-4372 / 5241 / 9856, no-ops once transformers carries the fix; before any config loads.
+# CVE-2026-4372 / 5241 / 9856 and the tokenizer_config.json vocab path traversal, no-ops once
+# transformers carries the fix; before any config or tokenizer loads.
fix_transformers_untrusted_config_fields()
fix_transformers_chat_template_path_traversal()
+fix_transformers_tokenizer_config_vocab_file_override()
# Probe-gated: no-ops unless this transformers merges a submodule's own prefix renaming into a
# composite model's conversion mapping. Ordered here, before anything loads a checkpoint, so a
# plain transformers.from_pretrained in the same process keeps its bitsandbytes quant_state too.
@@ -509,6 +512,7 @@ del fix_transformers5_bare_annotation_configs
del fix_transformers5_legacy_config_types
del fix_transformers_untrusted_config_fields
del fix_transformers_chat_template_path_traversal
+del fix_transformers_tokenizer_config_vocab_file_override
del fix_transformers_rope_scaling_drops_theta
del fix_transformers_bnb_prequantized_save
del fix_transformers_fp8_modulelist_experts
diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py
index 633234460a7..a8fe7751b16 100644
--- a/unsloth/import_fixes.py
+++ b/unsloth/import_fixes.py
@@ -1909,6 +1909,61 @@ def fix_transformers_chat_template_path_traversal():
logger.info(f"Unsloth: Failed patching {target.__name__}.{name} ({e})")
+_TOKENIZER_VOCAB_PATH_PATCH_FLAG = "_unsloth_patched_tokenizer_config_vocab_paths"
+# Resolved files _from_pretrained reads itself; every other one it hands to the tokenizer class.
+_TOKENIZER_FILES_READ_BY_LOADER = (
+ "tokenizer_config_file",
+ "added_tokens_file",
+ "special_tokens_map_file",
+)
+
+
+def fix_transformers_tokenizer_config_vocab_file_override():
+ """Back-port of the 5.17.0 vocab file precedence: below it `_from_pretrained` keeps a
+ `vocab_file` / `merges_file` / `tokenizer_file` from tokenizer_config.json over the file it
+ resolved inside the repo, and the tokenizer opens that path as given, even outside the repo.
+ save_pretrained never writes these keys. Hand the resolved paths over as keyword arguments,
+ which apply after the config, so only an explicit caller path overrides them."""
+ try:
+ import transformers
+
+ if TrueVersion(transformers.__version__) >= TrueVersion("5.17.0"):
+ return
+ from transformers.tokenization_utils_base import PreTrainedTokenizerBase
+
+ current = PreTrainedTokenizerBase.__dict__["_from_pretrained"]
+ if not isinstance(current, classmethod):
+ return
+ original = current.__func__
+ if getattr(original, _TOKENIZER_VOCAB_PATH_PATCH_FLAG, False):
+ return
+ # The wrapper reads the resolved files from the first argument after cls.
+ params = list(inspect.signature(original).parameters)
+ if params[1:2] != ["resolved_vocab_files"]:
+ logger.info("Unsloth: Skipping the tokenizer vocab path fix (unknown signature)")
+ return
+ except Exception as e:
+ logger.info(f"Unsloth: Skipping the tokenizer vocab path fix ({e})")
+ return
+
+ @functools.wraps(original)
+ def _from_pretrained(cls, resolved_vocab_files, *args, **kwargs):
+ for args_name, file_path in dict(resolved_vocab_files or {}).items():
+ # chat_template_file and chat_template_<name> are read by the loader as well.
+ read_by_loader = args_name in _TOKENIZER_FILES_READ_BY_LOADER
+ if read_by_loader or args_name.startswith("chat_template"):
+ continue
+ if kwargs.get(args_name) is None:
+ kwargs[args_name] = file_path
+ return original(cls, resolved_vocab_files, *args, **kwargs)
+
+ setattr(_from_pretrained, _TOKENIZER_VOCAB_PATH_PATCH_FLAG, True)
+ try:
+ PreTrainedTokenizerBase._from_pretrained = classmethod(_from_pretrained)
+ except Exception as e:
+ logger.info(f"Unsloth: Failed patching PreTrainedTokenizerBase._from_pretrained ({e})")
+
+
_SDPA_MASK_PATCH_FLAG = "_unsloth_patched_sdpa_mask"
diff --git a/unsloth/models/decision_gguf.py b/unsloth/models/decision_gguf.py
index 07e023e4586..24468c2a44f 100644
--- a/unsloth/models/decision_gguf.py
+++ b/unsloth/models/decision_gguf.py
@@ -444,6 +444,9 @@ def _convert(
env["PYTHONPATH"] = os.pathsep.join(
[str(converter / "gguf-py")] + ([env["PYTHONPATH"]] if env.get("PYTHONPATH") else [])
)
+ # Decision backbones are built-in classes; the converter loads some tokenizers with
+ # trust_remote_code=True, so give it no modules cache to import a folder's auto_map code from.
+ env["HF_MODULES_CACHE"] = os.path.join(os.path.abspath(__file__), "no_remote_code")
command = [
sys.executable,
converter / "convert_hf_to_gguf.py",
diff --git a/unsloth/models/sentence_transformer.py b/unsloth/models/sentence_transformer.py
index 9a6b6cde6dd..2ea71a4d448 100644
--- a/unsloth/models/sentence_transformer.py
+++ b/unsloth/models/sentence_transformer.py
@@ -1341,9 +1341,12 @@ class FastSentenceTransformer(FastModel):
)
if class_ref.startswith("sentence_transformers."):
- return FastSentenceTransformer._import_sentence_transformers_class(
+ resolved = FastSentenceTransformer._import_sentence_transformers_class(
class_ref, model_name
)
+ if not trust_remote_code:
+ FastSentenceTransformer._refuse_whole_model_class(resolved, class_ref, model_name)
+ return resolved
if not trust_remote_code:
# Not transformers' resolve_trust_remote_code: it appends a hf.co URL that is bogus for local dirs.
@@ -1403,6 +1406,31 @@ class FastSentenceTransformer(FastModel):
)
return resolved
+ # A whole model is not a module: its load() opens a modules.json of its own, which
+ # nothing here has read, and below 6.0 imports what that names from the folder.
+ _WHOLE_MODEL_CLASS_NAMES = frozenset(
+ {"SentenceTransformer", "CrossEncoder", "SparseEncoder", "BaseModel"}
+ )
+
+ @staticmethod
+ def _refuse_whole_model_class(resolved, class_ref, model_name):
+ for ancestor in getattr(resolved, "__mro__", ()):
+ module_name = getattr(ancestor, "__module__", "") or ""
+ if (
+ module_name.split(".")[0] == "sentence_transformers"
+ and getattr(ancestor, "__name__", "")
+ in FastSentenceTransformer._WHOLE_MODEL_CLASS_NAMES
+ ):
+ raise ValueError(
+ f"Unsloth: The model {model_name} declares a whole model ({class_ref!r}) "
+ f"as one of its modules. Its loader reads a second, unchecked set of "
+ f"modules. Please pass the argument `trust_remote_code=True` to allow "
+ f"custom code to be run."
+ )
+
+ # Router routes can hold further Routers; a "" route is the same folder again.
+ _MAX_SUB_MODULE_DEPTH = 8
+
@staticmethod
def _is_transformer_module_ref(class_ref):
"""Name ST's Transformer without importing anything, since importing to compare is the same
@@ -1467,7 +1495,13 @@ class FastSentenceTransformer(FastModel):
@staticmethod
def _check_module_config_class_refs(
- load_path, class_ref, model_name, trust_remote_code, module_class
+ load_path,
+ class_ref,
+ model_name,
+ trust_remote_code,
+ module_class,
+ descend = None,
+ depth = 0,
):
if trust_remote_code:
return
@@ -1495,6 +1529,7 @@ class FastSentenceTransformer(FastModel):
config_names.append("config.json")
refs = []
+ routes = []
for config_name in dict.fromkeys(config_names):
config_path = os.path.join(load_path, config_name)
if not os.path.isfile(config_path):
@@ -1539,6 +1574,11 @@ class FastSentenceTransformer(FastModel):
for value in types.values()
if isinstance(value, str)
]
+ routes += [
+ (route, value)
+ for route, value in types.items()
+ if isinstance(route, str) and isinstance(value, str)
+ ]
elif isinstance(config.get(key), str):
refs.append((key, config[key], prefix))
@@ -1558,6 +1598,59 @@ class FastSentenceTransformer(FastModel):
f"code. Please pass the argument `trust_remote_code=True` to allow custom code to "
f"be run."
)
+ if key == "tokenizer_class":
+ # WordEmbeddings.load calls tokenizer_class.load(folder): only a word
+ # tokenizer is meant here, not a Router or a whole model with configs of
+ # their own.
+ FastSentenceTransformer._require_word_tokenizer(value, class_ref, model_name)
+
+ # Router.load runs the same unchecked loaders one folder down for each route, so
+ # each route is checked the way this module was, where the loader will read it.
+ for route, value in routes:
+ if depth >= FastSentenceTransformer._MAX_SUB_MODULE_DEPTH:
+ raise ValueError(
+ f"Unsloth: The model {model_name} nests its {class_ref} modules more than "
+ f"{FastSentenceTransformer._MAX_SUB_MODULE_DEPTH} levels deep. Please pass "
+ f"the argument `trust_remote_code=True` to allow custom code to be run."
+ )
+ sub_class = FastSentenceTransformer._import_sentence_transformers_class(
+ value, model_name
+ )
+ FastSentenceTransformer._refuse_whole_model_class(sub_class, value, model_name)
+ if descend is not None:
+ descend(route, value, sub_class, depth + 1)
+ else:
+ FastSentenceTransformer._check_module_config_class_refs(
+ os.path.join(load_path, route),
+ value,
+ model_name,
+ False,
+ sub_class,
+ depth = depth + 1,
+ )
+
+ @staticmethod
+ def _require_word_tokenizer(class_ref, module_ref, model_name):
+ from sentence_transformers.util import import_from_string
+
+ resolved = None
+ if all(part.isidentifier() for part in class_ref.split(".")):
+ try:
+ resolved = import_from_string(class_ref)
+ except Exception:
+ resolved = None
+ names = {
+ getattr(ancestor, "__name__", "")
+ for ancestor in getattr(resolved, "__mro__", ())
+ if (getattr(ancestor, "__module__", "") or "").split(".")[0] == "sentence_transformers"
+ }
+ if "WordTokenizer" not in names:
+ raise ValueError(
+ f"Unsloth: The model {model_name} declares the module {module_ref} whose "
+ f"tokenizer_class is {class_ref!r}, which is not a sentence-transformers word "
+ f"tokenizer. Please pass the argument `trust_remote_code=True` to allow custom "
+ f"code to be run."
+ )
@staticmethod
def _is_commit(revision):
@@ -1954,6 +2047,7 @@ class FastSentenceTransformer(FastModel):
token = None,
cache_dir = None,
revision = None,
+ depth = 0,
):
"""Check a module's own config on a delegated route, where nothing is local yet.
@@ -1983,10 +2077,13 @@ class FastSentenceTransformer(FastModel):
folder = module_config.get("path")
if not isinstance(folder, str):
return
+ # A rooted path is not a repository folder: Path(model, "/x") is "/x" to the
+ # loader, so that local folder is the one to check.
+ rooted = folder.startswith("/") or os.path.isabs(folder)
# An empty path is the repository root, which sentence-transformers accepts and
# loads the root config.json from. Skipping it left the whole check bypassable by
# declaring Dense with "path": "".
- folder = folder.strip("/")
+ folder = folder.rstrip("/") if rooted else folder.strip("/")
config_names = []
config_file_name = getattr(module_class, "config_file_name", None)
@@ -2020,7 +2117,9 @@ class FastSentenceTransformer(FastModel):
is_local = False
folders = []
- if is_local:
+ if rooted:
+ folders.append(folder or "/")
+ elif is_local:
folders.append(os.path.join(model_name, *folder.split("/")) if folder else model_name)
else:
from huggingface_hub.errors import EntryNotFoundError, LocalEntryNotFoundError
@@ -2074,9 +2173,34 @@ class FastSentenceTransformer(FastModel):
if content:
break
+ def descend(route, sub_ref, sub_class, sub_depth):
+ # Router.load joins the route onto its own subfolder (Path(subfolder, route)),
+ # and fetches it from the same repository.
+ sub_folder = (
+ route
+ if route.startswith("/") or os.path.isabs(route)
+ else (f"{folder}/{route}" if folder else route)
+ )
+ FastSentenceTransformer._check_delegated_module_config(
+ model_name,
+ {"path": sub_folder},
+ sub_ref,
+ sub_class,
+ token = token,
+ cache_dir = cache_dir,
+ revision = revision,
+ depth = sub_depth,
+ )
+
for load_path in dict.fromkeys(folders):
FastSentenceTransformer._check_module_config_class_refs(
- load_path, class_ref, model_name, False, module_class
+ load_path,
+ class_ref,
+ model_name,
+ False,
+ module_class,
+ descend = descend,
+ depth = depth,
)
@staticmethod