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.

fix(studio): list approved custom-code models in /v1/models (#15945)

Long Yixingd0c962daae07
diff --git a/scripts/risky_loaders_baseline.json b/scripts/risky_loaders_baseline.json
index ca1c9845204..8c92d5ff956 100644
--- a/scripts/risky_loaders_baseline.json
+++ b/scripts/risky_loaders_baseline.json
@@ -330,6 +330,14 @@
"count": 1,
"reason": "Iterates two fixed module:attr strings for the active diffusion and video engines."
},
+ {
+ "file": "studio/backend/routes/inference.py",
+ "rule": "trust-remote-code",
+ "sink": "item",
+ "digest": "c47fdb9c09e7e0a3",
+ "count": 1,
+ "reason": "Auto-switch sets it only with a fingerprint of code this account approved, recomputed from disk; the worker consent gate rescans and enforces it."
+ },
{
"file": "studio/backend/routes/training.py",
"rule": "trust-remote-code",
diff --git a/studio/backend/core/inference/local_model_resolver.py b/studio/backend/core/inference/local_model_resolver.py
index 32e4199cf01..24916fd7afe 100644
--- a/studio/backend/core/inference/local_model_resolver.py
+++ b/studio/backend/core/inference/local_model_resolver.py
@@ -620,18 +620,25 @@ def _weights_are_servable(load_dir) -> bool:
return not (load_dir / "modules.json").is_file()
-def _config_is_servable_here(load_dir, config: dict) -> bool:
+def _config_is_servable_here(approval_ids, load_dir, config: dict) -> bool:
"""Only the approval boundary and category are decided here; remote code is read as data only."""
+ from utils.security.remote_code_approvals import approved_local_fingerprint
from utils.security.remote_code_scan import REMOTE_CODE_CONFIG_FILES
- # trust_remote_code needs an approval fingerprint a switch has not got; read as data only.
+ auto_map = False
for name in REMOTE_CODE_CONFIG_FILES:
candidate = config if name == "config.json" else _read_json(load_dir / name)
if not isinstance(candidate, dict):
continue
# model_file runs repo code and bypasses trust_remote_code (MLX loaders exec_module it).
- if candidate.get("auto_map") or candidate.get("model_file"):
+ if candidate.get("model_file"):
return False
+ auto_map = auto_map or bool(candidate.get("auto_map"))
+ # A switch can only pass on an approval the account already gave for exactly this code.
+ if auto_map and not approved_local_fingerprint(
+ current_account().username, approval_ids, str(load_dir)
+ ):
+ return False
return _is_generative_chat_config(load_dir, config)
@@ -701,7 +708,11 @@ def _local_weights_entry(loader_id: str, info) -> Optional[_LocalGgufEntry]:
if not _weights_are_servable(load_dir):
return None
config = _read_json(load_dir / "config.json")
- if not isinstance(config, dict) or not _config_is_servable_here(load_dir, config):
+ # Listing passes the raw id, the index the alias: both must see a path-keyed approval.
+ approval_ids = (loader_id, getattr(info, "id", None), path, str(load_dir))
+ if not isinstance(config, dict) or not _config_is_servable_here(
+ approval_ids, load_dir, config
+ ):
return None
# No quants: quantization is baked in, so there is no ":<quant>" to pin.
return _LocalGgufEntry(loader_id, str(load_dir), (), is_gguf = False)
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 5c740591c2e..55b7505c266 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -10866,6 +10866,7 @@ async def _maybe_auto_switch_model(
resolve_trusted_cached_local_gguf,
warm_index_soon,
)
+ from utils.security.remote_code_approvals import approved_local_fingerprint
from core.inference.llama_keepwarm import (
get_last_unloaded_model,
inference_lifecycle_gate,
@@ -11372,6 +11373,14 @@ async def _maybe_auto_switch_model(
param = "model",
),
) from None
+ # Stash restores too: the load carries this account's approval of the code on disk.
+ remote_code_fingerprint = (
+ None
+ if target_is_gguf
+ else await asyncio.to_thread(
+ approved_local_fingerprint, current_subject, (override_id, target_id), target_id
+ )
+ )
key = _switch_key(override_id, variant)
_note_switch_waiter(key, 1)
waiter_noted = True
@@ -11460,6 +11469,11 @@ async def _maybe_auto_switch_model(
)
if caller_hf_token:
load_kwargs["hf_token"] = caller_hf_token
+ if remote_code_fingerprint:
+ load_kwargs["trust_remote_code"] = True
+ load_kwargs["approved_remote_code_fingerprint"] = (
+ remote_code_fingerprint
+ )
if beside:
load_kwargs["alongside"] = True
saved_gpu_ids = load_kwargs.get("gpu_ids")
diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.py
index da5cefb72d0..f07081efdb9 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -10384,13 +10384,114 @@ def test_an_audio_request_probes_audio_capability_on_a_non_gguf_target(monkeypat
assert seen.get("vision") == "/srv/models/A.gguf"
-def test_a_custom_code_checkpoint_is_not_switchable(tmp_path):
- path = _local_checkpoint(tmp_path, "CustomCode")
+_CUSTOM_CODE = "class MyModel: pass\n"
+
+
+def _custom_code_checkpoint(root):
+ path = _local_checkpoint(root, "CustomCode")
(path / "config.json").write_text(
'{"architectures": ["Qwen3ForCausalLM"], "model_type": "qwen3", "auto_map": {"AutoModel": "modeling.MyModel"}}'
)
- info = SimpleNamespace(id = str(path), path = str(path))
+ # Bytes: write_text turns \n into \r\n on Windows.
+ (path / "modeling.py").write_bytes(_CUSTOM_CODE.encode())
+ (path / "layers").mkdir()
+ (path / "layers" / "helper.py").write_bytes(_CUSTOM_CODE.encode())
+ return path
+
+
+def _approve_custom_code(subject, code = _CUSTOM_CODE):
+ """What the UI's consent dialog stores for org/CustomCode: keyed and fingerprinted by repo id."""
+ from utils.security import remote_code_approvals as approvals
+ from utils.security.remote_code_scan import remote_code_fingerprint
+
+ approvals.record(
+ subject,
+ "org/customcode",
+ commit_sha = None,
+ # Hub keys use "/" on every OS.
+ fingerprint = remote_code_fingerprint(
+ {"org/customcode\0modeling.py": code, "org/customcode\0layers/helper.py": _CUSTOM_CODE}
+ ),
+ max_severity = None,
+ )
+
+
+def test_a_custom_code_checkpoint_is_switchable_only_with_its_approval(tmp_path):
+ info = SimpleNamespace(id = "org/CustomCode", path = str(_custom_code_checkpoint(tmp_path)))
+ assert resolver.local_servable_model(info) is None
+ _approve_custom_code("someone-else")
+ assert resolver.local_servable_model(info) is None
+ _approve_custom_code("unsloth", code = "class Older: pass\n")
assert resolver.local_servable_model(info) is None
+ _approve_custom_code("unsloth")
+ assert resolver.local_servable_model(info) == (False, ())
+
+
+def test_a_path_approved_folder_checkpoint_lists_and_switches_alike(tmp_path):
+ """A ./models or LM Studio folder is approved by its path; the index keys it by alias."""
+ from utils.security import remote_code_approvals as approvals
+ from utils.security.consent import _fingerprint_target_key
+ from utils.security.remote_code_scan import remote_code_fingerprint, repo_remote_code_files
+
+ path = str(_custom_code_checkpoint(tmp_path))
+ key = _fingerprint_target_key(path)
+ files = repo_remote_code_files(path)
+ approvals.record(
+ "unsloth",
+ approvals.approval_target_key([path]),
+ commit_sha = None,
+ fingerprint = remote_code_fingerprint({f"{key}\0{n}": b for n, b in files.items()}),
+ max_severity = None,
+ )
+ info = SimpleNamespace(id = path, path = path, model_id = None, display_name = "CustomCode")
+ assert resolver.local_servable_model(info) == (False, ())
+ assert resolver._local_servable_entry("CustomCode", info) is not None
+
+
[email protected]("via_stash", [False, True])
[email protected]("approved", [True, False])
+def test_an_approved_custom_code_target_loads_with_its_approval(
+ monkeypatch, tmp_path, approved, via_stash
+):
+ from utils.security.consent import evaluate_remote_code_consent_for_targets
+
+ path = str(_custom_code_checkpoint(tmp_path))
+ calls = []
+
+ async def _load(request, *args, **kwargs):
+ calls.append(request)
+
+ _wire_on(
+ monkeypatch,
+ resolves_to = None if via_stash else (path, None, "org/CustomCode"),
+ backend = _FakeBackend(None),
+ recorder = _load,
+ )
+ monkeypatch.setattr(
+ inference_route,
+ "get_inference_backend",
+ lambda: SimpleNamespace(active_model_name = None, models = {}),
+ )
+ if approved:
+ _approve_custom_code("tester")
+ if via_stash:
+ # An idle unload of the API-loaded model, restored by a request that names no model.
+ monkeypatch.setattr(kw, "_inflight", 0)
+ monkeypatch.setattr(kw, "_last_unloaded_model", (path, None, "org/CustomCode"))
+ monkeypatch.setattr(kw, "_last_unloaded_mlx", True)
+ _run_hook(inference_route._RELOAD_ONLY_MODEL)
+ else:
+ _run_hook("org/CustomCode")
+
+ assert [c.model_path for c in calls] == [path]
+ assert calls[0].trust_remote_code is approved
+ # The worker's own consent check must accept what the switch passes.
+ decision = evaluate_remote_code_consent_for_targets(
+ [path],
+ trust_remote_code = True,
+ approved_fingerprint = calls[0].approved_remote_code_fingerprint,
+ )
+ assert (decision.reason == "approved by fingerprint") is approved
def test_an_embedding_checkpoint_is_not_switchable(tmp_path):
diff --git a/studio/backend/utils/security/remote_code_approvals.py b/studio/backend/utils/security/remote_code_approvals.py
index e07b03c4c24..6d91ccbdd0a 100644
--- a/studio/backend/utils/security/remote_code_approvals.py
+++ b/studio/backend/utils/security/remote_code_approvals.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import contextlib
import json
import os
+import pathlib
import threading
from dataclasses import dataclass
from datetime import datetime, timezone
@@ -161,6 +162,49 @@ def lookup(subject: str, target_key: str) -> Optional[StoredApproval]:
)
+def approved_local_fingerprint(subject: str, approved_as, load_dir: str) -> Optional[str]:
+ """The consent fingerprint of *load_dir*'s remote code when *subject* approved that exact
+ code under any id in *approved_as* (one id or several) with the current ruleset, else None.
+ Only an external auto_map repo is fetched; the rest is read from disk."""
+ from utils.paths import is_local_path
+ from utils.security.consent import _fingerprint_target_key
+ from utils.security.remote_code_scan import (
+ RemoteCodeUnscannable,
+ remote_code_fingerprint,
+ repo_remote_code_files,
+ )
+
+ ids = [approved_as] if isinstance(approved_as, str) else list(dict.fromkeys(approved_as))
+ approvals = [
+ (target, stored)
+ for target in ids
+ if target
+ and (stored := lookup(subject, approval_target_key([target]))) is not None
+ and stored.scanner_version == SCANNER_VERSION
+ ]
+ if not approvals:
+ return None
+ try:
+ files = repo_remote_code_files(load_dir)
+ except RemoteCodeUnscannable:
+ return None
+ if not files:
+ return None
+
+ def keyed(target, name_of = str):
+ key = _fingerprint_target_key(target)
+ return remote_code_fingerprint(
+ {f"{key}\0{name_of(name)}": body for name, body in files.items()}
+ )
+
+ for target, stored in approvals:
+ # A Hub approval fingerprinted "a/b.py"; a local one, like the worker, "a\\b.py" on Windows.
+ name_of = str if is_local_path(target) else (lambda n: pathlib.PurePath(n).as_posix())
+ if keyed(target, name_of) == stored.fingerprint:
+ return keyed(load_dir)
+ return None
+
+
def record(
subject: str,
target_key: str,