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 remote-code scan: read each Hub config once per scan request (#16606)

Daniel Hanaf173fcc494f
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index fced3d63a9c..0fab0a35c1b 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -2767,7 +2767,7 @@ async def scan_model_remote_code(
_subdirs = tuple(dict.fromkeys((*consent_load_subdirs[_target], *_subdirs)))
consent_load_subdirs[_target] = _subdirs
# Record repos our scan first pulls in, computed before preflight, so decline purges only those.
- from utils.security.remote_code_scan import external_auto_map_repos
+ from utils.security.remote_code_scan import external_auto_map_repos, shared_config_reads
scan_created_repos: list = []
_seen_created: set = set()
@@ -2788,33 +2788,35 @@ async def scan_model_remote_code(
pass
external_refs: list = []
- for _target in security_targets:
- # Use the pre-base-resolution snapshot for the primary (see above).
- _mark_scan_created(
- _target,
- preexisting = _primary_preexisting if _target == primary_cache_target else None,
- )
- for _ext in external_auto_map_repos(
- _target,
- hf_token,
- load_subdirs = consent_load_subdirs[_target],
- ):
- # Found after authorization: refuse like the base, else its cached Python leaks as source snippets
- if not is_local_path(_ext) and cached_read_refused(
- hf_token, repo_id = _ext, is_cached = lambda e = _ext: _repo_maybe_cached(e)
+ # One advisory scan: its passes share each config read; the load-time gates re-read fresh.
+ with shared_config_reads():
+ for _target in security_targets:
+ # Use the pre-base-resolution snapshot for the primary (see above).
+ _mark_scan_created(
+ _target,
+ preexisting = _primary_preexisting if _target == primary_cache_target else None,
+ )
+ for _ext in external_auto_map_repos(
+ _target,
+ hf_token,
+ load_subdirs = consent_load_subdirs[_target],
):
- raise HTTPException(
- status_code = 404,
- detail = _UNAUTHORIZED_CACHED_MODEL,
- )
- external_refs.append(_ext)
- _mark_scan_created(_ext)
- decision = preflight_remote_code_consent_for_targets(
- security_targets,
- hf_token = hf_token,
- subject = current_subject,
- load_subdirs_by_target = consent_load_subdirs,
- )
+ # Found after authorization: refuse like the base, else its cached Python leaks as source snippets
+ if not is_local_path(_ext) and cached_read_refused(
+ hf_token, repo_id = _ext, is_cached = lambda e = _ext: _repo_maybe_cached(e)
+ ):
+ raise HTTPException(
+ status_code = 404,
+ detail = _UNAUTHORIZED_CACHED_MODEL,
+ )
+ external_refs.append(_ext)
+ _mark_scan_created(_ext)
+ decision = preflight_remote_code_consent_for_targets(
+ security_targets,
+ hf_token = hf_token,
+ subject = current_subject,
+ load_subdirs_by_target = consent_load_subdirs,
+ )
payload = decision.response_payload()
blocks = (
[(payload.get("block_cause"), payload.get("model_name"))]
diff --git a/studio/backend/tests/test_hf_optional_file_probe.py b/studio/backend/tests/test_hf_optional_file_probe.py
index 346eecf6d35..945126fcd70 100644
--- a/studio/backend/tests/test_hf_optional_file_probe.py
+++ b/studio/backend/tests/test_hf_optional_file_probe.py
@@ -43,12 +43,8 @@ _GUARDED = {
"_read_raw_config_json": "hf_file_definitely_absent",
"get_base_model_from_lora_identifier": "hf_file_definitely_absent",
},
- "utils/security/consent.py": {"_load_remote_code_configs": "hf_file_definitely_absent"},
"utils/security/file_security.py": {"_indexed_shard_paths": "hf_file_definitely_absent"},
- "utils/security/remote_code_scan.py": {
- "external_auto_map_repos": "hf_file_definitely_absent",
- "repo_remote_code_files": "hf_file_definitely_absent",
- },
+ "utils/security/remote_code_scan.py": {"hub_config_path": "hf_file_definitely_absent"},
}
diff --git a/studio/backend/tests/test_remote_code_shared_config_reads.py b/studio/backend/tests/test_remote_code_shared_config_reads.py
new file mode 100644
index 00000000000..b3a08dde3ed
--- /dev/null
+++ b/studio/backend/tests/test_remote_code_shared_config_reads.py
@@ -0,0 +1,292 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The remote-code-scan route reads each Hub config once per request; nothing else changes. Only a
+downloaded path or the Hub's own 404 is shared, only inside one scope, one context, one token
+and one endpoint, and the load-time gates in the workers never share at all."""
+
+import asyncio
+import json
+import threading
+from pathlib import Path
+
+import pytest
+from huggingface_hub.errors import EntryNotFoundError, LocalEntryNotFoundError
+
+import huggingface_hub
+import utils.hf_probe as hf_probe
+import utils.security.remote_code_scan as rcs
+from utils.security.consent import (
+ _load_remote_code_configs,
+ evaluate_remote_code_consent_for_targets,
+)
+from utils.security.remote_code_scan import (
+ external_auto_map_repos,
+ repo_remote_code_files,
+ shared_config_reads,
+)
+
+_BACKEND = Path(__file__).resolve().parents[1]
+_CONFIG = json.dumps(
+ {"auto_map": {"AutoModel": "modeling_x.M", "AutoTokenizer": "ext/repo--tok.T"}}
+)
+
+
+class _Hub:
+ """In-process Hub: files per repo, a log of every probe and download, scripted failures."""
+
+ def __init__(self, tmp_path, monkeypatch):
+ self.root = tmp_path
+ self.repos = {
+ "org/model": {
+ "config.json": _CONFIG,
+ "tokenizer_config.json": "{}",
+ "modeling_x.py": "class M:\n pass\n",
+ },
+ "ext/repo": {"tok.py": "class T:\n pass\n"},
+ }
+ self.calls = []
+ self.fail = {} # (repo, file) -> list of exceptions raised by the next downloads
+ self.version = 0
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", self.download)
+ monkeypatch.setattr(huggingface_hub, "list_repo_files", self.listing)
+ monkeypatch.setattr(hf_probe, "hf_file_definitely_absent", self.absent)
+
+ def absent(
+ self,
+ repo,
+ filename,
+ *,
+ token = None,
+ **_kw,
+ ):
+ self.calls.append(("probe", repo, filename, token))
+ return filename not in self.repos.get(repo, {})
+
+ def download(
+ self,
+ repo_id = None,
+ filename = None,
+ *args,
+ token = None,
+ cache_dir = None,
+ **_kw,
+ ):
+ if args:
+ filename = args[0]
+ self.calls.append(("download", repo_id, filename, token))
+ pending = self.fail.get((repo_id, filename))
+ if pending:
+ raise pending.pop(0)
+ files = self.repos.get(repo_id, {})
+ if filename not in files:
+ raise EntryNotFoundError(filename)
+ self.version += 1
+ path = self.root / f"v{self.version}" / repo_id / filename
+ path.parent.mkdir(parents = True, exist_ok = True)
+ path.write_text(files[filename], encoding = "utf-8")
+ return str(path)
+
+ def listing(
+ self,
+ repo_id,
+ token = None,
+ **_kw,
+ ):
+ self.calls.append(("list", repo_id, None, token))
+ return list(self.repos.get(repo_id, {}))
+
+ def config_reads(self, kind = None):
+ return [
+ c
+ for c in self.calls
+ if c[2] in rcs.REMOTE_CODE_CONFIG_FILES and (kind is None or c[0] == kind)
+ ]
+
+
+def hub(tmp_path, monkeypatch):
+ monkeypatch.delenv("HF_HUB_OFFLINE", raising = False)
+ return _Hub(tmp_path, monkeypatch)
+
+
+def _three_passes(token = None):
+ """What the route runs per target: external refs, the auto_map check, then the scan."""
+ refs = external_auto_map_repos("org/model", token)
+ configs = _load_remote_code_configs("org/model", token)
+ files = repo_remote_code_files("org/model", hf_token = token)
+ return refs, configs, files
+
+
+def test_one_scope_reads_each_config_once(hub):
+ with shared_config_reads():
+ shared = _three_passes()
+ shared_reads = hub.config_reads()
+ hub.calls.clear()
+ fresh = _three_passes()
+ assert shared == fresh
+ per_file = len(rcs.REMOTE_CODE_CONFIG_FILES)
+ # Present: one probe + one download; absent: one probe.
+ assert len(shared_reads) == per_file + 2
+ assert len(hub.config_reads()) == 3 * len(shared_reads)
+ # The code itself is still downloaded and listed fresh by the scan.
+ assert [c for c in hub.calls if c[0] == "list"] == [
+ ("list", "org/model", None, None),
+ ("list", "ext/repo", None, None),
+ ]
+
+
+def test_outside_a_scope_nothing_is_shared(hub):
+ _three_passes()
+ first = len(hub.config_reads())
+ _three_passes()
+ assert len(hub.config_reads()) == 2 * first
+ assert rcs._CONFIG_READS.get() is None
+
+
+def test_a_failed_read_is_never_kept(hub):
+ """A transient error, an auth error and a local cache miss (offline/timeout) are all retried by
+ the next pass, exactly as without the scope."""
+ hub.fail[("org/model", "config.json")] = [
+ RuntimeError("503"),
+ LocalEntryNotFoundError("timeout, nothing cached"),
+ ]
+ with shared_config_reads():
+ assert external_auto_map_repos("org/model") == set() # best effort: skips the failure
+ # The auto_map check sees the local miss as absence, as it always has.
+ assert _load_remote_code_configs("org/model") == [{}]
+ files = repo_remote_code_files("org/model")
+ downloads = [c for c in hub.config_reads("download") if c[2] == "config.json"]
+ assert len(downloads) == 3
+ assert set(files) == {"modeling_x.py", "ext/repo--tok.py"}
+
+
+def test_a_confirmed_404_is_kept_for_the_scope_only(hub):
+ with shared_config_reads():
+ _three_passes()
+ _three_passes()
+ probes = [c for c in hub.config_reads("probe") if c[2] == "processor_config.json"]
+ assert len(probes) == 1
+ with shared_config_reads():
+ _three_passes()
+ probes = [c for c in hub.config_reads("probe") if c[2] == "processor_config.json"]
+ assert len(probes) == 2
+
+
+def test_a_file_changed_between_two_actions_is_read_again(hub):
+ with shared_config_reads():
+ first = external_auto_map_repos("org/model")
+ hub.repos["org/model"]["config.json"] = json.dumps(
+ {"auto_map": {"AutoModel": "other/repo--evil.M"}}
+ )
+ hub.repos["other/repo"] = {"evil.py": "import os\nos.system('id')\n"}
+ with shared_config_reads():
+ second = external_auto_map_repos("org/model")
+ files = repo_remote_code_files("org/model")
+ assert first == {"ext/repo"} and second == {"other/repo"}
+ assert "other/repo--evil.py" in files
+
+
+def test_a_config_purged_from_the_cache_mid_scan_is_downloaded_again(hub):
+ with shared_config_reads():
+ external_auto_map_repos("org/model")
+ for c in hub.root.glob("v*/org/model/config.json"):
+ c.unlink()
+ files = repo_remote_code_files("org/model")
+ assert "ext/repo--tok.py" in files
+ assert len([c for c in hub.config_reads("download") if c[2] == "config.json"]) == 2
+
+
+def test_tokens_never_share_reads(hub):
+ with shared_config_reads():
+ _load_remote_code_configs("org/model", None)
+ _load_remote_code_configs("org/model", "hf_a")
+ _load_remote_code_configs("org/model", "hf_b")
+ _load_remote_code_configs("org/model", "hf_a")
+ tokens = [c[3] for c in hub.config_reads("download") if c[2] == "config.json"]
+ assert tokens == [None, "hf_a", "hf_b"]
+
+
+def test_the_endpoint_and_source_are_part_of_the_key(hub, monkeypatch):
+ from huggingface_hub import constants
+ with shared_config_reads():
+ _load_remote_code_configs("org/model")
+ monkeypatch.setattr(constants, "ENDPOINT", "https://mirror.example")
+ _load_remote_code_configs("org/model")
+ monkeypatch.setattr("utils.hub_settings.active_source", lambda: "modelscope")
+ _load_remote_code_configs("org/model")
+ assert len([c for c in hub.config_reads("download") if c[2] == "config.json"]) == 3
+
+
+def test_threads_and_concurrent_requests_do_not_share(hub):
+ seen = {}
+
+ def other_thread():
+ seen["scope"] = rcs._CONFIG_READS.get()
+ _load_remote_code_configs("org/model")
+
+ with shared_config_reads():
+ _load_remote_code_configs("org/model")
+ t = threading.Thread(target = other_thread)
+ t.start()
+ t.join()
+ assert seen["scope"] is None
+ assert len([c for c in hub.config_reads("download") if c[2] == "config.json"]) == 2
+
+ hub.calls.clear()
+ scopes = []
+
+ async def request():
+ with shared_config_reads():
+ scopes.append(rcs._CONFIG_READS.get())
+ await asyncio.sleep(0)
+ _load_remote_code_configs("org/model")
+ await asyncio.sleep(0)
+ _load_remote_code_configs("org/model")
+
+ async def two_requests():
+ await asyncio.gather(request(), request())
+
+ asyncio.run(two_requests())
+ assert scopes[0] is not scopes[1]
+ assert len([c for c in hub.config_reads("download") if c[2] == "config.json"]) == 2
+
+
+def test_the_scope_is_cleared_on_exit_and_on_error(hub):
+ with pytest.raises(RuntimeError):
+ with shared_config_reads():
+ _load_remote_code_configs("org/model")
+ raise RuntimeError("route failed")
+ assert rcs._CONFIG_READS.get() is None
+ with shared_config_reads():
+ outer = rcs._CONFIG_READS.get()
+ with shared_config_reads():
+ assert rcs._CONFIG_READS.get() is outer
+ assert rcs._CONFIG_READS.get() is outer
+ assert rcs._CONFIG_READS.get() is None
+
+
+def test_decision_and_fingerprint_match_the_unshared_gate(hub):
+ hub.repos["org/model"]["modeling_x.py"] = (
+ "import subprocess\nsubprocess.Popen('id', shell=True)\n"
+ )
+ fresh = evaluate_remote_code_consent_for_targets(["org/model"], trust_remote_code = True)
+ with shared_config_reads():
+ external_auto_map_repos("org/model")
+ shared = evaluate_remote_code_consent_for_targets(["org/model"], trust_remote_code = True)
+ assert shared.response_payload() == fresh.response_payload()
+ assert shared.fingerprint and shared.fingerprint == fresh.fingerprint
+
+
+def test_only_the_advisory_route_shares_reads():
+ """The load-time gates are authoritative, so they always read the Hub fresh."""
+ route = (_BACKEND / "routes" / "models.py").read_text(encoding = "utf-8")
+ assert "with shared_config_reads():" in route
+ for rel in (
+ "core/inference/worker.py",
+ "core/training/worker.py",
+ "core/export/worker.py",
+ "utils/security/consent.py",
+ ):
+ src = (_BACKEND / rel).read_text(encoding = "utf-8")
+ assert "shared_config_reads" not in src, rel
diff --git a/studio/backend/utils/security/consent.py b/studio/backend/utils/security/consent.py
index e10772872dc..8cb84395ab7 100644
--- a/studio/backend/utils/security/consent.py
+++ b/studio/backend/utils/security/consent.py
@@ -22,6 +22,7 @@ from utils.security.remote_code_scan import (
MEDIUM,
RemoteCodeUnscannable,
config_declares_auto_map,
+ hub_config_path,
remote_code_config_paths,
remote_code_fingerprint,
repo_remote_code_files,
@@ -149,32 +150,15 @@ def _load_remote_code_configs(
configs.append(json.loads(p.read_text(encoding = "utf-8-sig")))
return configs
- from huggingface_hub import hf_hub_download
- from hub.utils.hf_tokens import anonymous_retrying
-
- hf_hub_download = anonymous_retrying(hf_hub_download)
- from huggingface_hub.utils import EntryNotFoundError
- from utils.hf_cache_settings import active_hf_hub_cache
- from utils.hf_probe import hf_file_definitely_absent
-
configs = []
for name in remote_code_config_paths(load_subdirs):
- # Probe optional configs without caching 404s; other failures still fail closed.
- if hf_file_definitely_absent(model_name, name, token = hf_token):
- continue
try:
- p = hf_hub_download(
- repo_id = model_name,
- filename = name,
- token = hf_token,
- cache_dir = active_hf_hub_cache(),
- )
- except EntryNotFoundError:
- continue
+ p = hub_config_path(model_name, name, hf_token)
except Exception:
# Transient/auth failure is not "absent" -> fail closed to "unknown" so the caller scans.
return None
- configs.append(json.loads(Path(p).read_text(encoding = "utf-8-sig")))
+ if p is not None:
+ configs.append(json.loads(Path(p).read_text(encoding = "utf-8-sig")))
# Every config was read or a genuine 404 -> an empty list is a definitive "no auto_map".
return configs
except Exception as exc:
diff --git a/studio/backend/utils/security/remote_code_scan.py b/studio/backend/utils/security/remote_code_scan.py
index 749c0a8244d..02991f53b95 100644
--- a/studio/backend/utils/security/remote_code_scan.py
+++ b/studio/backend/utils/security/remote_code_scan.py
@@ -5,6 +5,8 @@
from __future__ import annotations
+import contextlib
+import contextvars
import hashlib
import io
import tokenize
@@ -62,6 +64,88 @@ class RemoteCodeUnscannable(Exception):
"""The repo's executable code could not be fully fetched/listed to scan. Raised (not returned empty) so the gate distinguishes "code PRESENT but unreadable" (offline/gated/transient/404/listing failure), which fails CLOSED, from "repo has NO .py" (empty result), where trust_remote_code is a no-op and the load is allowed. Conflating them would block a code-free repo or fail open on code we could not see."""
+_CONFIG_READS: contextvars.ContextVar = contextvars.ContextVar(
+ "unsloth_remote_code_config_reads", default = None
+)
+
+
+def shared_config_reads():
+ """Within this scope each Hub config is probed and downloaded once per (endpoint, source,
+ cache, repo, file, token); only a downloaded path or a confirmed 404 is kept, never a
+ failure. Per context, so concurrent requests and threads never see each other's reads."""
+ if _CONFIG_READS.get() is not None:
+ yield
+ return
+ reset = _CONFIG_READS.set({})
+ try:
+ yield
+ finally:
+ _CONFIG_READS.reset(reset)
+
+
+def _config_read_key(model_name: str, cfg_name: str, hf_token) -> Optional[tuple]:
+ try:
+ from huggingface_hub import constants as hf_constants
+ from utils.hub_settings import active_source
+
+ token = (
+ hashlib.sha256(hf_token.encode("utf-8")).hexdigest()
+ if isinstance(hf_token, str)
+ else repr(hf_token)
+ )
+ return (
+ hf_constants.ENDPOINT,
+ os.environ.get("HF_ENDPOINT"),
+ bool(hf_constants.HF_HUB_OFFLINE),
+ os.environ.get("HF_HUB_OFFLINE"),
+ active_source(),
+ str(active_hf_hub_cache()),
+ model_name,
+ cfg_name,
+ token,
+ )
+ except Exception:
+ return None
+
+
+def hub_config_path(model_name: str, cfg_name: str, hf_token) -> Optional[str]:
+ """Cached path of an optional Hub config, None when absent; any other failure raises.
+ Probes first so a 404 is not cached (utils/hf_probe.py)."""
+ from huggingface_hub import hf_hub_download
+ from huggingface_hub.errors import LocalEntryNotFoundError
+ from huggingface_hub.utils import EntryNotFoundError
+ from hub.utils.hf_tokens import anonymous_retrying
+ from utils.hf_probe import hf_file_definitely_absent
+
+ hf_hub_download = anonymous_retrying(hf_hub_download)
+ reads = _CONFIG_READS.get()
+ key = _config_read_key(model_name, cfg_name, hf_token) if reads is not None else None
+ if key is not None and key in reads:
+ path = reads[key]
+ # A kept path purged from the cache meanwhile is read again.
+ if path is None or os.path.isfile(path):
+ return path
+ if hf_file_definitely_absent(model_name, cfg_name, token = hf_token):
+ path = None
+ else:
+ try:
+ path = hf_hub_download(
+ model_name,
+ cfg_name,
+ token = hf_token,
+ cache_dir = active_hf_hub_cache(),
+ )
+ except EntryNotFoundError as exc:
+ # A local cache miss (offline, timeout) reads as absent but is no answer to keep.
+ if isinstance(exc, LocalEntryNotFoundError):
+ return None
+ path = None
+ if key is not None:
+ reads[key] = path
+ return path
+
+
# Fallback used only if scripts/scan_packages.py is absent (stripped install).
_FALLBACK_PATTERNS: tuple[tuple[re.Pattern, str, str], ...] = (
(
@@ -468,28 +552,18 @@ def repo_remote_code_files(
hf_hub_download = anonymous_retrying(hf_hub_download)
list_repo_files = anonymous_retrying(list_repo_files)
- from huggingface_hub.utils import EntryNotFoundError
- from utils.hf_probe import hf_file_definitely_absent
- # Collect auto_map refs from EVERY config that can declare one. A 404 (EntryNotFoundError) means the config is absent -> skip; any other failure is transient/auth and could hide an auto_map, so fail closed (unscannable).
+ # Refs from EVERY config that can declare auto_map; an unreadable one could hide one, so fail closed.
refs = set()
for cfg_name in remote_code_config_paths(load_subdirs):
- # Avoid caching expected 404s; other failures still reach the fail-closed path.
- if hf_file_definitely_absent(model_name, cfg_name, token = hf_token):
- continue
try:
- cfg_path = hf_hub_download(
- model_name,
- cfg_name,
- token = hf_token,
- cache_dir = active_hf_hub_cache(),
- )
- except EntryNotFoundError:
- continue
+ cfg_path = hub_config_path(model_name, cfg_name, hf_token)
except Exception as exc:
raise RemoteCodeUnscannable(
f"{model_name}: config {cfg_name} could not be fetched ({exc})"
) from exc
+ if cfg_path is None:
+ continue
try:
refs |= _auto_map_refs(json.loads(Path(cfg_path).read_text(encoding = "utf-8-sig")))
except Exception:
@@ -631,28 +705,13 @@ def external_auto_map_repos(
repos.update(repo for repo, _fn in refs if repo)
return repos
- from huggingface_hub import hf_hub_download
- from hub.utils.hf_tokens import anonymous_retrying
-
- hf_hub_download = anonymous_retrying(hf_hub_download)
- from huggingface_hub.utils import EntryNotFoundError
- from utils.hf_probe import hf_file_definitely_absent
-
for cfg_name in remote_code_config_paths(load_subdirs):
- # Same guard as the scanner above; see utils/hf_probe.py.
- if hf_file_definitely_absent(model_name, cfg_name, token = hf_token):
- continue
try:
- cfg_path = hf_hub_download(
- model_name,
- cfg_name,
- token = hf_token,
- cache_dir = active_hf_hub_cache(),
- )
- except EntryNotFoundError:
- continue
+ cfg_path = hub_config_path(model_name, cfg_name, hf_token)
except Exception:
continue
+ if cfg_path is None:
+ continue
try:
refs = _auto_map_refs(json.loads(Path(cfg_path).read_text(encoding = "utf-8-sig")))
except Exception: