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: a managed account's own Hub token covers a resident loaded from the Hub cache (#14533)

Daniel Hanc2aa8fe4277c
diff --git a/studio/backend/hub/services/models/account_access.py b/studio/backend/hub/services/models/account_access.py
index 9124d2c1667..43ffe7c9ccf 100644
--- a/studio/backend/hub/services/models/account_access.py
+++ b/studio/backend/hub/services/models/account_access.py
@@ -899,6 +899,30 @@ def _cached_repo(path: Path) -> tuple[str, str] | None:
return None
+def cached_model_repo(reference: str) -> str | None:
+ """Recover a resident's Hub identity only from a verified model-cache path."""
+ from utils.hf_cache_settings import known_hf_hub_caches
+ from utils.paths.path_utils import is_network_or_device_path
+
+ if not reference or is_network_or_device_path(reference):
+ return None
+ try:
+ path = Path(reference).expanduser()
+ cached = _cached_repo(path)
+ if cached is None or cached[1] != "model":
+ return None
+ resolved = path.resolve()
+ if _cached_repo(resolved) != cached:
+ return None
+ if Path(normalize_path(reference)).expanduser().resolve() != resolved:
+ return None
+ if any(resolved.is_relative_to(root.resolve()) for root in known_hf_hub_caches()):
+ return cached[0]
+ except (OSError, RuntimeError, ValueError):
+ pass
+ return None
+
+
def model_visible(
reference: str,
*,
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index b546d6c4d3b..e0be9c9369a 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -11692,8 +11692,14 @@ async def _maybe_auto_switch_model(
await asyncio.to_thread(account_access.require_model_access, resident)
except HTTPException as refusal:
# A Hub resident the caller's own token can fetch is theirs to use as well.
- if refusal.status_code != 404 or not await _caller_token_authorizes_download(
- str(resident or ""), fastapi_request
+ if refusal.status_code != 404:
+ raise
+ resident_reference = str(resident or "")
+ cached_repo = await asyncio.to_thread(
+ account_access.cached_model_repo, resident_reference
+ )
+ if not await _caller_token_authorizes_download(
+ cached_repo or resident_reference, fastapi_request
):
raise
return
diff --git a/studio/backend/tests/test_resident_cache_path_token.py b/studio/backend/tests/test_resident_cache_path_token.py
new file mode 100644
index 00000000000..fbdaba66308
--- /dev/null
+++ b/studio/backend/tests/test_resident_cache_path_token.py
@@ -0,0 +1,107 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A caller's own Hub token unlocks a resident loaded from a Hub cache path, as for a repo id."""
+
+import asyncio
+from types import SimpleNamespace
+
+import pytest
+
+from .test_account_inference_isolation import isolated, ALICE # noqa: F401 - autouse fixture
+from utils.account_context import arun_as
+from hub.dependencies import HUB_HF_TOKEN_HEADER
+from hub.services.models import account_access as access
+from routes import inference
+
+
+def _setup(
+ monkeypatch,
+ tmp_path,
+ resident_kind,
+ auto_switch,
+ in_cache = True,
+):
+ from utils import hf_cache_settings, openai_auto_switch_settings as settings
+ from core.inference import local_model_resolver
+
+ cache = tmp_path / "hub"
+ cache.mkdir()
+ # Outside the cache: a folder an account could name like a cache entry.
+ root = cache if in_cache else tmp_path / "account-models"
+ snapshot = root / "models--org--private" / "snapshots" / "revision"
+ snapshot.mkdir(parents = True)
+ gguf = snapshot / "weights.gguf"
+ gguf.touch()
+ resident = {"repo_id": "org/private", "snapshot": str(snapshot), "gguf": str(gguf)}[
+ resident_kind
+ ]
+ is_gguf = resident_kind == "gguf"
+ backend = SimpleNamespace(
+ active_model_name = None if is_gguf else resident, _openai_advertised_id = "org/private"
+ )
+ llama = SimpleNamespace(
+ is_loaded = is_gguf, model_identifier = resident, _openai_advertised_id = "org/private"
+ )
+ monkeypatch.setattr(inference, "get_inference_backend", lambda: backend)
+ monkeypatch.setattr(inference, "_peek_inference_backend", lambda: backend)
+ monkeypatch.setattr(inference, "get_llama_cpp_backend", lambda: llama)
+ monkeypatch.setattr(inference, "_resident_npu_model", lambda: None)
+ monkeypatch.setattr(hf_cache_settings, "known_hf_hub_caches", lambda: [cache])
+ monkeypatch.setattr(settings, "get_openai_auto_switch_enabled", lambda: auto_switch)
+ monkeypatch.setattr(settings, "get_openai_auto_download_enabled", lambda: True)
+ monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: False)
+ monkeypatch.setattr(local_model_resolver, "warm_index_soon", lambda: None)
+ monkeypatch.setattr(inference, "_claim_slot_for_non_preview", lambda request: None)
+
+ async def catalog():
+ return []
+
+ monkeypatch.setattr(inference, "_cached_local_catalog", catalog)
+ probes = []
+
+ class Hub:
+ def repo_info(self, repo_id, *, repo_type, token, timeout):
+ assert repo_id == "org/private" and token == "caller-owned-token"
+ probes.append(repo_id)
+ return SimpleNamespace(gated = True)
+
+ def auth_check(self, repo_id, *, repo_type, token):
+ assert repo_id == "org/private" and token == "caller-owned-token"
+
+ monkeypatch.setattr(access, "HfApi", Hub)
+ request = SimpleNamespace(
+ scope = {},
+ state = SimpleNamespace(),
+ headers = {HUB_HF_TOKEN_HEADER: "caller-owned-token"},
+ url = SimpleNamespace(path = "/v1/chat/completions"),
+ )
+ return request, probes
+
+
[email protected]("resident_kind", ["repo_id", "snapshot", "gguf"])
[email protected]("auto_switch", [False, True])
+def test_authorized_hub_token_can_use_the_matching_resident(
+ monkeypatch, tmp_path, resident_kind, auto_switch
+):
+ request, probes = _setup(monkeypatch, tmp_path, resident_kind, auto_switch)
+ assert inference._loaded_identity_satisfies("org/private")
+ for _ in range(2):
+ asyncio.run(
+ arun_as(ALICE, inference._maybe_auto_switch_model("org/private", request, "alice"))
+ )
+ assert probes
+
+
[email protected]("resident_kind", ["snapshot", "gguf"])
+def test_a_cache_named_folder_outside_the_hub_cache_is_still_refused(
+ monkeypatch, tmp_path, resident_kind
+):
+ from fastapi import HTTPException
+
+ request, _ = _setup(monkeypatch, tmp_path, resident_kind, False, in_cache = False)
+ with pytest.raises(HTTPException) as refused:
+ asyncio.run(
+ arun_as(ALICE, inference._maybe_auto_switch_model("org/private", request, "alice"))
+ )
+ assert refused.value.status_code == 404