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.

Read auto_map at every config level in the remote code gate (#12453)

Daniel Han69011c2896f7
diff --git a/studio/backend/tests/test_nested_auto_map_consent.py b/studio/backend/tests/test_nested_auto_map_consent.py
new file mode 100644
index 00000000000..8f5de9518d5
--- /dev/null
+++ b/studio/backend/tests/test_nested_auto_map_consent.py
@@ -0,0 +1,146 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""An auto_map on a sub-config is remote code, and the gate has to see it.
+
+transformers reads auto_map off whichever config builds the thing being built, so a
+composite model declares it on `text_config` rather than at the top level.
+`unsloth/models/_utils.py` resolves `text_config.auto_map["AutoModelForCausalLM"]`
+through `get_class_from_dynamic_module`, which downloads and executes that module, and
+`unsloth/models/loader.py` walks every sub-config level for the compiler. A gate that
+read only `cfg["auto_map"]` therefore reported "ships no remote code" for a repo whose
+code the load runs, and allowed it with no scan, no findings and no fingerprint.
+
+The scanner runs for real here; only the local model directory is a fixture.
+"""
+
+import json
+
+import pytest
+
+from utils.security import evaluate_remote_code_consent
+from utils.security.remote_code_scan import HIGH
+
+# The helpers this fix adds are imported inside the tests that use them, deliberately.
+# At module scope they would make this whole file fail to import on the code before the
+# fix, and "the test errored" is not evidence of anything. The tests that go through the
+# public gate have to fail on an assertion there, which is the actual defect.
+
+
+def _model(tmp_path, config):
+ """A local model directory whose auto_map target is suspicious enough to block."""
+ model = tmp_path / "model"
+ model.mkdir()
+ (model / "config.json").write_text(json.dumps(config), encoding = "utf-8")
+ (model / "modeling_evil.py").write_text(
+ "import subprocess\nsubprocess.Popen(['id'])\n",
+ encoding = "utf-8",
+ )
+ return model
+
+
+NESTED_AUTO_MAP = {
+ "model_type": "llama",
+ "text_config": {
+ "model_type": "llama",
+ "auto_map": {"AutoModelForCausalLM": "modeling_evil.Model"},
+ },
+}
+
+
+def test_a_nested_auto_map_is_scanned_rather_than_called_a_noop(tmp_path):
+ """The defect, end to end through the public gate.
+
+ Before the fix this returned has_remote_code False with "trust_remote_code is a
+ no-op" in the reason, for a repo whose modelling file the load executes.
+ """
+ model = _model(tmp_path, NESTED_AUTO_MAP)
+
+ decision = evaluate_remote_code_consent(str(model), trust_remote_code = True)
+
+ assert decision.has_remote_code is True
+ assert decision.blocked is True
+ assert decision.max_severity == HIGH
+ assert decision.fingerprint
+ assert "no-op" not in decision.reason
+
+
+def test_the_nested_module_is_named_in_the_refs_the_scan_fetches(tmp_path):
+ """The trigger firing is not enough: the ref has to reach the fetch list too."""
+ from utils.security.remote_code_scan import _auto_map_refs
+ assert _auto_map_refs(NESTED_AUTO_MAP) == {(None, "modeling_evil.py")}
+
+
+def test_a_top_level_auto_map_is_unchanged(tmp_path):
+ """Negative control. The common case must behave exactly as it did."""
+ model = _model(tmp_path, {"auto_map": {"AutoModel": "modeling_evil.Model"}})
+
+ decision = evaluate_remote_code_consent(str(model), trust_remote_code = True)
+
+ assert decision.has_remote_code is True
+ assert decision.blocked is True
+ assert decision.max_severity == HIGH
+
+
+def test_a_model_with_no_auto_map_anywhere_is_still_a_noop(tmp_path):
+ """The fast path for every ordinary model: no scan, no dialog, no slowdown.
+
+ This is the regression that would hurt, because it is every user. A walk that
+ reported auto_map where there is none would put the consent dialog in front of
+ models that ship no Python at all.
+ """
+ model = tmp_path / "plain"
+ model.mkdir()
+ (model / "config.json").write_text(
+ json.dumps(
+ {
+ "model_type": "llama",
+ "architectures": ["LlamaForCausalLM"],
+ "text_config": {"model_type": "llama", "hidden_size": 8},
+ "vision_config": {"model_type": "siglip", "hidden_size": 8},
+ }
+ ),
+ encoding = "utf-8",
+ )
+
+ decision = evaluate_remote_code_consent(str(model), trust_remote_code = True)
+
+ assert decision.has_remote_code is False
+ assert decision.blocked is False
+ assert "no-op" in decision.reason
+
+
+ "config, expected",
+ [
+ ({}, False),
+ ({"auto_map": {}}, False),
+ ({"auto_map": {"AutoModel": "modeling_evil.Model"}}, True),
+ # The real composite shapes: a sub-config, and a sub-config of a sub-config
+ # (Qwen-Omni nests a thinker_config that itself carries a text_config).
+ ({"text_config": {"auto_map": {"AutoModel": "m.C"}}}, True),
+ ({"thinker_config": {"text_config": {"auto_map": {"AutoModel": "m.C"}}}}, True),
+ # A list of sub-configs, which some processor configs use.
+ ({"sub_configs": [{"auto_map": {"AutoModel": "m.C"}}]}, True),
+ # Not a dict, so not an auto_map transformers would read.
+ ({"auto_map": "modeling_evil.Model"}, False),
+ ({"text_config": None}, False),
+ ({"text_config": "llama"}, False),
+ ],
+)
+def test_the_walk_agrees_with_what_transformers_would_read(config, expected):
+ from utils.security.remote_code_scan import config_declares_auto_map
+ assert config_declares_auto_map(config) is expected
+
+
+def test_the_walk_is_depth_bounded(tmp_path):
+ """A config is attacker-supplied JSON, so the walk must not be unbounded."""
+ from utils.security.remote_code_scan import config_declares_auto_map, iter_auto_maps
+
+ config: dict = {"auto_map": {"AutoModel": "modeling_evil.Model"}}
+ for _ in range(64):
+ config = {"text_config": config}
+
+ # Terminates, and returns an answer rather than recursing to the interpreter limit.
+ assert config_declares_auto_map(config) is False
+ assert list(iter_auto_maps(config)) == []
diff --git a/studio/backend/utils/security/consent.py b/studio/backend/utils/security/consent.py
index 3465fb756d5..37deaf8a9f9 100644
--- a/studio/backend/utils/security/consent.py
+++ b/studio/backend/utils/security/consent.py
@@ -20,6 +20,7 @@ from utils.security.remote_code_scan import (
HIGH,
MEDIUM,
RemoteCodeUnscannable,
+ config_declares_auto_map,
remote_code_config_paths,
remote_code_fingerprint,
repo_remote_code_files,
@@ -90,7 +91,10 @@ def _config_has_auto_map(
configs = _load_remote_code_configs(model_name, hf_token, load_subdirs = load_subdirs)
if configs is None:
return None
- if not any(bool((cfg or {}).get("auto_map")) for cfg in configs):
+ # Every nesting level, not just the top: a composite model declares auto_map on a
+ # sub-config, and the loader resolves it from there, so a top-level-only read
+ # returned "ships no remote code" for a repo whose code the load would run.
+ if not any(config_declares_auto_map(cfg or {}) for cfg in configs):
return False
return True
diff --git a/studio/backend/utils/security/remote_code_scan.py b/studio/backend/utils/security/remote_code_scan.py
index 48a26d5664d..93f58fec481 100644
--- a/studio/backend/utils/security/remote_code_scan.py
+++ b/studio/backend/utils/security/remote_code_scan.py
@@ -551,11 +551,51 @@ def _iter_auto_map_strings(value):
yield from _iter_auto_map_strings(item)
+_MAX_CONFIG_DEPTH = 12
+
+
+def iter_auto_maps(cfg, _depth: int = 0):
+ """Yield every ``auto_map`` dict in a config, at the top level and in any sub-config.
+
+ transformers reads ``auto_map`` off whichever config object builds the thing being
+ built, and for a composite model that is a SUB-config, not the top level. Reading
+ only ``cfg["auto_map"]`` therefore scans less than the load executes:
+ ``unsloth/models/_utils.py`` resolves ``text_config.auto_map["AutoModelForCausalLM"]``
+ through ``get_class_from_dynamic_module``, and ``unsloth/models/loader.py`` already
+ walks every level for the compiler because nesting goes beyond text/vision/audio
+ (Qwen-Omni ``thinker_config``, nested ``llm_config``). A repo whose only ``auto_map``
+ sits on a sub-config used to read as "ships no remote code", so the gate allowed it
+ with no scan, no findings and no fingerprint.
+
+ Depth-bounded rather than unbounded: a config is attacker-supplied JSON, and the
+ bound is what keeps a deeply nested one from costing anything. Twelve is far past
+ the deepest real composite (three).
+ """
+ if _depth > _MAX_CONFIG_DEPTH or not isinstance(cfg, dict):
+ return
+ auto_map = cfg.get("auto_map")
+ if isinstance(auto_map, dict) and auto_map:
+ yield auto_map
+ for key, value in cfg.items():
+ if key == "auto_map":
+ continue
+ if isinstance(value, dict):
+ yield from iter_auto_maps(value, _depth + 1)
+ elif isinstance(value, (list, tuple)):
+ for item in value:
+ if isinstance(item, dict):
+ yield from iter_auto_maps(item, _depth + 1)
+
+
+def config_declares_auto_map(cfg) -> bool:
+ """Whether a config declares executable remote code anywhere in its nesting."""
+ return any(True for _ in iter_auto_maps(cfg))
+
+
def _auto_map_refs(cfg: dict) -> set:
- """``(repo, filename)`` pairs referenced by config auto_map. ``repo`` is ``None`` for own-repo code. An external ``owner/name--module.Class`` ref (transformers' cross-repo form) yields ``("owner/name", "module.py")`` so cross-repo code is scanned and fingerprinted too."""
+ """``(repo, filename)`` pairs referenced by config auto_map. ``repo`` is ``None`` for own-repo code. An external ``owner/name--module.Class`` ref (transformers' cross-repo form) yields ``("owner/name", "module.py")`` so cross-repo code is scanned and fingerprinted too. Every nesting level is read, since that is where a composite model's auto_map lives."""
out = set()
- am = cfg.get("auto_map") or {}
- if isinstance(am, dict):
+ for am in iter_auto_maps(cfg):
for value in am.values():
# A value may be a string OR a [slow, fast] list (tokenizers); cover both.
for ref in _iter_auto_map_strings(value):