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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep an API key from borrowing the operator's Hugging Face token (#10126) (#10142)
Harsha052b2fa51059
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.pyindex 7ed67132f6b..9827251e40d 100644--- a/studio/backend/core/export/export.py+++ b/studio/backend/core/export/export.py@@ -25,6 +25,7 @@ except Exception as _unsloth_exc:_UNSLOTH_IMPORT_ERROR = _unsloth_excfrom huggingface_hub import HfApi, ModelCard+from hub.utils.hf_tokens import HfTokenArg, is_anonymous, normalize_tokenfrom utils.hardware import clear_gpu_cachefrom utils.models import is_vision_model, get_base_model_from_lora@@ -465,7 +466,7 @@ class ExportBackend:max_seq_length: int = 2048,load_in_4bit: bool = True,trust_remote_code: bool = False,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,_device_map_override: Optional[dict] = None,) -> Tuple[bool, str]:"""@@ -475,10 +476,17 @@ class ExportBackend:checkpoints, matching the token the worker used for the security preflight(otherwise a gated repo passes scanning then 401s at from_pretrained).+ ``False`` (denied the ambient token) must travel all the way down: ``None`` reads as+ "go and find a credential" (``if token is None: get_token()`` in ``save.py`` and+ ``hf_login``), and ``get_token()`` reads the operator's stored login off disk.+Returns:Tuple of (success: bool, message: str)"""- token = hf_token if hf_token and hf_token.strip() else None+ token = normalize_token(hf_token)+ # Loaders only: the probes' cache guards refuse an anonymous read, which offline+ # misreads a cached VLM as a text model.+ probe_token = token or Nonetry:logger.info(f"Loading checkpoint: {checkpoint_path}")@@ -510,10 +518,10 @@ class ExportBackend:# skip.with _offline_window_if(local_files_only):self._audio_type = detect_audio_type(- model_id, hf_token = token, local_files_only = local_files_only+ model_id, hf_token = probe_token, local_files_only = local_files_only)self.is_vision = not self._audio_type and is_vision_model(- model_id, hf_token = token, local_files_only = local_files_only+ model_id, hf_token = probe_token, local_files_only = local_files_only)if self._audio_type == "csm":@@ -710,7 +718,7 @@ class ExportBackend:format_type: str = "16-bit (FP16)",push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,private: bool = False,compressed_method: Optional[str] = None,) -> Tuple[bool, str, Optional[str]]:@@ -841,15 +849,27 @@ class ExportBackend:logger.info(f"Saving merged model locally to: {save_directory}")ensure_dir(Path(save_directory))+ # No push, but the merge resolves the base repo and save.py turns None into+ # get_token(), so the credential still has to be spelled out.+ merged_token_kw = (+ {"token": hf_token}+ if (hf_token or is_anonymous(hf_token))+ and _supports_kwarg(self.current_model.save_pretrained_merged, "token")+ else {}+ )if _IS_MLX:self.current_model.save_pretrained_merged(save_directory,self.current_tokenizer,save_method = mlx_save_method,+ **merged_token_kw,)else:self.current_model.save_pretrained_merged(- save_directory, self.current_tokenizer, save_method = save_method+ save_directory,+ self.current_tokenizer,+ save_method = save_method,+ **merged_token_kw,)# Compressed / torchao writes to the "<dir>-<suffix>" sibling; report that as output.@@ -945,7 +965,7 @@ class ExportBackend:save_directory: str,push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,private: bool = False,base_model_id: Optional[str] = None,) -> Tuple[bool, str, Optional[str]]:@@ -1075,7 +1095,7 @@ class ExportBackend:quantization_method = "Q4_K_M",push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,imatrix_file = None,private: bool = False,gguf_shard_size: Optional[str] = None,@@ -1133,7 +1153,7 @@ class ExportBackend:local_token_kw = ({"token": hf_token}if imatrix_file- and hf_token+ and (hf_token or is_anonymous(hf_token))and _supports_kwarg(self.current_model.save_pretrained_gguf, "token")else {})@@ -1323,7 +1343,7 @@ class ExportBackend:save_directory: str,push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,private: bool = False,gguf: bool = False,gguf_outtype: str = "q8_0",@@ -1401,8 +1421,9 @@ class ExportBackend:self.current_tokenizer,save_method = "lora",quantization_method = outtype,- # Forward the token so convert_lora_to_gguf.py can fetch a gated base's config.- token = hf_token or None,+ # A token fetches a gated base's config; False keeps a denied caller+ # off get_token().+ token = normalize_token(hf_token),)# iterdir, not glob.glob: glob hides dot-leading names.final_ggufs = sorted(diff --git a/studio/backend/core/export/orchestrator.py b/studio/backend/core/export/orchestrator.pyindex 14ecdceee33..da8b6314c2d 100644--- a/studio/backend/core/export/orchestrator.py+++ b/studio/backend/core/export/orchestrator.py@@ -21,6 +21,7 @@ import queueimport threadingimport timefrom pathlib import Path+from hub.utils.hf_tokens import HfTokenArgfrom typing import Any, Deque, Dict, List, Optional, Tuplefrom utils.paths import outputs_root@@ -439,7 +440,8 @@ class ExportOrchestrator:load_in_4bit: bool = True,trust_remote_code: bool = False,approved_remote_code_fingerprint: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,+ allow_ambient: bool = True,subject: Optional[str] = None,) -> Tuple[bool, str]:"""Load a checkpoint for export.@@ -454,6 +456,7 @@ class ExportOrchestrator:"approved_remote_code_fingerprint": approved_remote_code_fingerprint,"subject": subject,"hf_token": hf_token,+ "allow_ambient": allow_ambient,}with self._lock:@@ -536,7 +539,7 @@ class ExportOrchestrator:format_type: str = "16-bit (FP16)",push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,private: bool = False,compressed_method: Optional[str] = None,) -> Tuple[bool, str, Optional[str]]:@@ -559,7 +562,7 @@ class ExportOrchestrator:save_directory: str,push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,private: bool = False,base_model_id: Optional[str] = None,) -> Tuple[bool, str, Optional[str]]:@@ -582,7 +585,7 @@ class ExportOrchestrator:quantization_method = "Q4_K_M",push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,imatrix_file = None,private: bool = False,gguf_shard_size: Optional[str] = None,@@ -607,7 +610,7 @@ class ExportOrchestrator:save_directory: str,push_to_hub: bool = False,repo_id: Optional[str] = None,- hf_token: Optional[str] = None,+ hf_token: HfTokenArg = None,private: bool = False,gguf: bool = False,gguf_outtype: str = "q8_0",diff --git a/studio/backend/core/export/worker.py b/studio/backend/core/export/worker.pyindex 1fe8d99faa1..b46bd2c8a55 100644--- a/studio/backend/core/export/worker.py+++ b/studio/backend/core/export/worker.py@@ -238,14 +238,21 @@ def _send_response(resp_queue: Any, response: dict) -> None:def _handle_load(backend, cmd: dict, resp_queue: Any) -> None:"""Handle a load_checkpoint command."""+ from hub.utils.hf_tokens import hf_token_arg+checkpoint_path = cmd["checkpoint_path"]+ # The preflight helpers read the policy off the token, so rebuild it once here.+ hf_token = hf_token_arg(cmd.get("hf_token"), allow_ambient_token = cmd.get("allow_ambient", True))max_seq_length = cmd.get("max_seq_length", 2048)load_in_4bit = cmd.get("load_in_4bit", True)# Latest-sidecar checkpoints load 16-bit here too: bnb 4-bit feeds quantized# expert weights into unvalidated paths (same flip as the chat worker).if load_in_4bit:from utils.transformers_version import latest_tier_active_for- if latest_tier_active_for(checkpoint_path, cmd.get("hf_token")):++ # Plain token, like the activation below: the sentinel reads no cache offline,+ # misses the sidecar and leaves 4-bit on.+ if latest_tier_active_for(checkpoint_path, hf_token or None):load_in_4bit = Falselogger.info("Latest-transformers sidecar active for %s - forcing a 16-bit "@@ -265,7 +272,7 @@ def _handle_load(backend, cmd: dict, resp_queue: Any) -> None:and (_cp_lower.startswith("unsloth/") or _cp_lower.startswith("nvidia/"))# Genuine first-party Hub repo only (not a local/spoof name starting# with "unsloth/"); authenticated so private repos resolve.- and is_trusted_org_repo(checkpoint_path, hf_token = cmd.get("hf_token"))+ and is_trusted_org_repo(checkpoint_path, hf_token = hf_token)):trust_remote_code = Truelogger.info(@@ -284,16 +291,15 @@ def _handle_load(backend, cmd: dict, resp_queue: Any) -> None:from utils.models.model_config import get_base_model_from_lora_identifier# Resolve a LOCAL or REMOTE adapter's base so a remote LoRA base is gated too.- _base = get_base_model_from_lora_identifier(checkpoint_path, cmd.get("hf_token"))+ _base = get_base_model_from_lora_identifier(checkpoint_path, hf_token)if _base:requested_security_targets.append(_base)except Exception as exc:logger.debug("Could not resolve LoRA base for malware scan: %s", exc)- _hf_token = cmd.get("hf_token")security_targets: list[str] = []consent_load_subdirs: dict[str, tuple] = {}for requested_target in dict.fromkeys(requested_security_targets):- load_subdirs = security_load_subdirs(requested_target, _hf_token)+ load_subdirs = security_load_subdirs(requested_target, hf_token)target, load_subdirs = load_scan_target(requested_target, load_subdirs)if target not in consent_load_subdirs:security_targets.append(target)@@ -305,7 +311,7 @@ def _handle_load(backend, cmd: dict, resp_queue: Any) -> None:for target in security_targets:_fs = evaluate_file_security(target,- hf_token = _hf_token,+ hf_token = hf_token,load_subdirs = consent_load_subdirs[target],)if _fs.blocked:@@ -330,7 +336,7 @@ def _handle_load(backend, cmd: dict, resp_queue: Any) -> None:# Scan adapter + base as one combined unit, pinned by a single fingerprint._rc = evaluate_remote_code_consent_for_targets(security_targets,- hf_token = _hf_token,+ hf_token = hf_token,trust_remote_code = True,approved_fingerprint = cmd.get("approved_remote_code_fingerprint"),subject = cmd.get("subject"),@@ -369,7 +375,7 @@ def _handle_load(backend, cmd: dict, resp_queue: Any) -> None:max_seq_length = max_seq_length,load_in_4bit = load_in_4bit,trust_remote_code = trust_remote_code,- hf_token = cmd.get("hf_token"),+ hf_token = hf_token,)_send_response(@@ -557,9 +563,21 @@ def run_export_process(*, cmd_queue: Any, resp_queue: Any, config: dict) -> Nonecheckpoint_path = config["checkpoint_path"]+ # Before the huggingface_hub import: it latches HF_HUB_DISABLE_IMPLICIT_TOKEN into a+ # module constant. So an export runs under the identity that loaded the checkpoint.+ from hub.utils.hf_tokens import apply_token_to_child_env++ if not config.get("allow_ambient", True):+ # The sentinel, not the caller's token: this worker outlives the load and serves+ # whoever exports next. The caller's own travels as an argument instead.+ apply_token_to_child_env(os.environ, False)+# ── 1. Activate correct transformers version BEFORE any ML imports ──with _offline_window_if_unreachable(step = "activating transformers"):try:+ # Plain token: _load_config_json refuses the hub cache for the sentinel, so+ # offline a cached model falls to the default sidecar. Anonymity here is the+ # scrubbed environment, not the argument._activate_transformers_version(checkpoint_path, config.get("hf_token") or None)except Exception as exc:_send_response(diff --git a/studio/backend/hub/utils/hf_tokens.py b/studio/backend/hub/utils/hf_tokens.pyindex d5388b034fa..97299adc393 100644--- a/studio/backend/hub/utils/hf_tokens.py+++ b/studio/backend/hub/utils/hf_tokens.py@@ -23,12 +23,15 @@ def hf_token_arg(hf_token: Optional[str], *, allow_ambient_token: bool) -> HfTok# Mirrors the list hub/services/download_lifecycle.py scrubs for download workers.+# HF_OIDC_RESOURCE names a token rather than holding one: hub >= 1.23 exchanges it inside+# get_token(), ahead of HF_TOKEN, so a scrubbed child would still resolve the operator's._HF_TOKEN_ENV_KEYS = ("HF_TOKEN","HF_HUB_TOKEN","HUGGING_FACE_HUB_TOKEN","HUGGINGFACE_HUB_TOKEN","HUGGINGFACEHUB_API_TOKEN",+ "HF_OIDC_RESOURCE",)diff --git a/studio/backend/mcp_server.py b/studio/backend/mcp_server.pyindex d2820180ed7..2a2c346ee95 100644--- a/studio/backend/mcp_server.py+++ b/studio/backend/mcp_server.py@@ -220,7 +220,7 @@ def create_studio_mcp() -> FastMCP:approved_remote_code_fingerprint = approved_remote_code_fingerprint,hf_token = hf_token,)- return _dump(await load(request, current_subject = "mcp"))+ return _dump(await load(request, current_subject = "mcp", allow_ambient = False))@mcp.toolasync def export_gguf(@@ -255,7 +255,7 @@ def create_studio_mcp() -> FastMCP:private = private,gguf_shard_size = gguf_shard_size,)- return _dump(await export(request, current_subject = "mcp"))+ return _dump(await export(request, current_subject = "mcp", allow_ambient = False))return mcpdiff --git a/studio/backend/routes/data_recipe/jobs.py b/studio/backend/routes/data_recipe/jobs.pyindex 4049218d9bc..07d63ca8ab1 100644--- a/studio/backend/routes/data_recipe/jobs.py+++ b/studio/backend/routes/data_recipe/jobs.py@@ -14,6 +14,7 @@ from urllib.parse import urlparsefrom fastapi import APIRouter, Depends, HTTPException, Query, Requestfrom auth.authentication import (+ allow_ambient_hf_token,authenticated_via_api_key,get_current_credential,require_ui_session_for_local_commands,@@ -546,10 +547,21 @@ def job_dataset(response_class = JSONResponse,response_model = PublishDatasetResponse,)-def publish_job_dataset(job_id: str, payload: PublishDatasetRequest):+def publish_job_dataset(+ job_id: str,+ payload: PublishDatasetRequest,+ allow_ambient: bool = Depends(allow_ambient_hf_token),+):repo_id = payload.repo_id.strip()description = payload.description.strip()hf_token = payload.hf_token.strip() if isinstance(payload.hf_token, str) else None+ hf_token = hf_token or None+ # publish_recipe_dataset hands this to the Hub client, so None publishes as the host.+ if hf_token is None and not allow_ambient:+ raise HTTPException(+ status_code = 400,+ detail = "Hugging Face token is required to publish datasets when authenticated via API key.",+ )artifact_path = (payload.artifact_path.strip() if isinstance(payload.artifact_path, str) else None)diff --git a/studio/backend/routes/export.py b/studio/backend/routes/export.pyindex 2bcc6ce9aeb..2e991263a52 100644--- a/studio/backend/routes/export.py+++ b/studio/backend/routes/export.py@@ -20,7 +20,8 @@ backend_path = Path(__file__).parent.parent.parentif str(backend_path) not in sys.path:sys.path.insert(0, str(backend_path))-from auth.authentication import get_current_subject+from auth.authentication import allow_ambient_hf_token, get_current_subject+from hub.utils.hf_tokens import HfTokenArg, hf_token_argfrom utils.utils import safe_error_detail@@ -74,9 +75,31 @@ async def _ensure_export_supported() -> None:)+def _resolve_export_hf_token(+ raw_token: Optional[str],+ *,+ push_to_hub: bool = False,+ allow_ambient: bool = True,+) -> HfTokenArg:+ """The credential this export runs under, as the anonymous-aware sentinel.++ ``None`` reads downstream as "go and find a credential" (``if token is None:+ get_token()``), so a caller denied the ambient token is spelled ``False``.+ """+ token = raw_token.strip() if isinstance(raw_token, str) and raw_token.strip() else None+ if push_to_hub and token is None and not allow_ambient:+ raise HTTPException(+ status_code = 400,+ detail = "Hugging Face token is required to push to Hub when authenticated via API key.",+ )+ return hf_token_arg(token, allow_ambient_token = allow_ambient)++@router.post("/load-checkpoint", response_model = ExportOperationResponse)async def load_checkpoint(- request: LoadCheckpointRequest, current_subject: str = Depends(get_current_subject)+ request: LoadCheckpointRequest,+ current_subject: str = Depends(get_current_subject),+ allow_ambient: bool = Depends(allow_ambient_hf_token),):"""Load a checkpoint into the export backend (ExportBackend.load_checkpoint).@@ -97,7 +120,9 @@ async def load_checkpoint(load_in_4bit = request.load_in_4bit,trust_remote_code = request.trust_remote_code,approved_remote_code_fingerprint = request.approved_remote_code_fingerprint,- hf_token = request.hf_token,+ hf_token = _resolve_export_hf_token(request.hf_token, allow_ambient = allow_ambient),+ # A supplied token cannot say whether it came from a session or an API key.+ allow_ambient = allow_ambient,subject = current_subject,)@@ -303,7 +328,9 @@ def _export_details(@router.post("/export/merged", response_model = ExportOperationResponse)async def export_merged_model(- request: ExportMergedModelRequest, current_subject: str = Depends(get_current_subject)+ request: ExportMergedModelRequest,+ current_subject: str = Depends(get_current_subject),+ allow_ambient: bool = Depends(allow_ambient_hf_token),):"""Export a merged PEFT model (16-bit or 4-bit), optionally pushing to Hub.@@ -318,7 +345,11 @@ async def export_merged_model(format_type = request.format_type,push_to_hub = request.push_to_hub,repo_id = request.repo_id,- hf_token = request.hf_token,+ hf_token = _resolve_export_hf_token(+ request.hf_token,+ push_to_hub = request.push_to_hub,+ allow_ambient = allow_ambient,+ ),private = request.private,compressed_method = request.compressed_method,)@@ -348,7 +379,9 @@ async def export_merged_model(@router.post("/export/base", response_model = ExportOperationResponse)async def export_base_model(- request: ExportBaseModelRequest, current_subject: str = Depends(get_current_subject)+ request: ExportBaseModelRequest,+ current_subject: str = Depends(get_current_subject),+ allow_ambient: bool = Depends(allow_ambient_hf_token),):"""Export a non-PEFT base model, optionally pushing to Hub.@@ -362,7 +395,11 @@ async def export_base_model(save_directory = request.save_directory,push_to_hub = request.push_to_hub,repo_id = request.repo_id,- hf_token = request.hf_token,+ hf_token = _resolve_export_hf_token(+ request.hf_token,+ push_to_hub = request.push_to_hub,+ allow_ambient = allow_ambient,+ ),private = request.private,base_model_id = request.base_model_id,)@@ -392,7 +429,9 @@ async def export_base_model(@router.post("/export/gguf", response_model = ExportOperationResponse)async def export_gguf(- request: ExportGGUFRequest, current_subject: str = Depends(get_current_subject)+ request: ExportGGUFRequest,+ current_subject: str = Depends(get_current_subject),+ allow_ambient: bool = Depends(allow_ambient_hf_token),):"""Export the current model to GGUF format, optionally pushing to Hub.@@ -409,7 +448,11 @@ async def export_gguf(quantization_method = request.quantization_method,push_to_hub = request.push_to_hub,repo_id = request.repo_id,- hf_token = request.hf_token,+ hf_token = _resolve_export_hf_token(+ request.hf_token,+ push_to_hub = request.push_to_hub,+ allow_ambient = allow_ambient,+ ),imatrix_file = imatrix_file,private = request.private,gguf_shard_size = request.gguf_shard_size,@@ -440,7 +483,9 @@ async def export_gguf(@router.post("/export/lora", response_model = ExportOperationResponse)async def export_lora_adapter(- request: ExportLoRAAdapterRequest, current_subject: str = Depends(get_current_subject)+ request: ExportLoRAAdapterRequest,+ current_subject: str = Depends(get_current_subject),+ allow_ambient: bool = Depends(allow_ambient_hf_token),):"""Export only the LoRA adapter (if the loaded model is PEFT).@@ -454,7 +499,11 @@ async def export_lora_adapter(save_directory = request.save_directory,push_to_hub = request.push_to_hub,repo_id = request.repo_id,- hf_token = request.hf_token,+ hf_token = _resolve_export_hf_token(+ request.hf_token,+ push_to_hub = request.push_to_hub,+ allow_ambient = allow_ambient,+ ),private = request.private,gguf = request.gguf,gguf_outtype = request.gguf_outtype,diff --git a/studio/backend/tests/test_hub_write_ambient_token.py b/studio/backend/tests/test_hub_write_ambient_token.pynew file mode 100644index 00000000000..d9b8af27fb8--- /dev/null+++ b/studio/backend/tests/test_hub_write_ambient_token.py@@ -0,0 +1,694 @@+# 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 backend's own HF_TOKEN must not be lent to an sk-unsloth API key (issue #10126).++Two things have to hold. A write path refuses rather than publishing or pushing as the+operator, and a load started by such a caller does not *run* as the operator: that needs the+anonymous sentinel rather than None, because None is what unsloth reads as "go and find a+credential" (``if token is None: get_token()``).+"""++from unittest.mock import MagicMock++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from auth.authentication import (+ authenticated_via_api_key,+ get_current_credential,+ get_current_subject,+)+from routes import export as export_routes+from routes.data_recipe import jobs as data_recipe_jobs_routes+++async def _fake_ensure_export_supported():+ pass+++def _app(via_api_key: bool) -> FastAPI:+ app = FastAPI()+ app.include_router(data_recipe_jobs_routes.router, prefix = "/api/data-recipe")+ app.include_router(export_routes.router, prefix = "/api")+ app.dependency_overrides[get_current_subject] = lambda: "alice"+ app.dependency_overrides[get_current_credential] = lambda: ("alice", "cred-1")+ app.dependency_overrides[authenticated_via_api_key] = lambda: via_api_key+ return app+++def _publish_manager(monkeypatch, seen):+ mgr = MagicMock()+ mgr.get_status.return_value = {+ "status": "completed",+ "execution_type": "full",+ "artifact_path": "/tmp/artifacts",+ }+ monkeypatch.setattr(data_recipe_jobs_routes, "get_job_manager", lambda: mgr)+ monkeypatch.setattr(+ data_recipe_jobs_routes,+ "publish_recipe_dataset",+ lambda artifact_path, repo_id, description, hf_token, private: (+ seen.update(token = hf_token) or f"https://huggingface.co/datasets/{repo_id}"+ ),+ )+++# ---------------------------------------------------------------- recipe dataset publish+++def test_publish_refuses_an_api_key_without_a_token(monkeypatch):+ """The hole this PR exists for: publish_recipe_dataset passes the token straight to+ HuggingFaceHubClient and card.push_to_hub, so None publishes as the host's login."""+ _publish_manager(monkeypatch, {})+ response = TestClient(_app(via_api_key = True)).post(+ "/api/data-recipe/jobs/job-1/publish",+ json = {"repo_id": "org/dataset", "description": "d", "hf_token": None},+ )+ assert response.status_code == 400+ assert "required to publish datasets" in response.json()["detail"]+++ "via_api_key,token,expected",+ [(True, "hf_caller", "hf_caller"), (False, None, None)],+)+def test_publish_allows_a_token_bearing_key_and_a_ui_session(+ monkeypatch, via_api_key, token, expected+):+ seen: dict = {}+ _publish_manager(monkeypatch, seen)+ response = TestClient(_app(via_api_key)).post(+ "/api/data-recipe/jobs/job-1/publish",+ json = {"repo_id": "org/dataset", "description": "d", "hf_token": token},+ )+ assert response.status_code == 200+ assert seen["token"] == expected+++# ------------------------------------------------------------------------ export routes++_EXPORTS = [+ ("/api/export/merged", "export_merged_model", {}),+ ("/api/export/base", "export_base_model", {}),+ ("/api/export/gguf", "export_gguf", {"quantization_method": "q4_k_m"}),+ ("/api/export/lora", "export_lora_adapter", {}),+]+++def _backend(monkeypatch, method):+ monkeypatch.setattr(export_routes, "_ensure_export_supported", _fake_ensure_export_supported)+ backend = MagicMock()+ getattr(backend, method).return_value = (True, "ok", "/tmp/export")+ monkeypatch.setattr(export_routes, "get_export_backend", lambda: backend)+ monkeypatch.setattr(+ export_routes, "_export_details", lambda *a, **kw: {"output_path": "/tmp/export"}+ )+ return backend++[email protected]("endpoint,method,extra", _EXPORTS)+def test_push_refuses_an_api_key_without_a_token(monkeypatch, endpoint, method, extra):+ monkeypatch.setattr(export_routes, "_ensure_export_supported", _fake_ensure_export_supported)+ response = TestClient(_app(via_api_key = True)).post(+ endpoint,+ json = {"save_directory": "/tmp/x", "push_to_hub": True, "repo_id": "o/m", **extra},+ )+ assert response.status_code == 400+ assert "required to push to Hub" in response.json()["detail"]++[email protected]("endpoint,method,extra", _EXPORTS)+def test_push_with_a_token_reaches_the_backend_unchanged(monkeypatch, endpoint, method, extra):+ backend = _backend(monkeypatch, method)+ response = TestClient(_app(via_api_key = True)).post(+ endpoint,+ json = {+ "save_directory": "/tmp/x",+ "push_to_hub": True,+ "repo_id": "o/m",+ "hf_token": "hf_caller",+ **extra,+ },+ )+ assert response.status_code == 200+ assert getattr(backend, method).call_args.kwargs["hf_token"] == "hf_caller"++[email protected]("endpoint,method,extra", _EXPORTS)[email protected]("via_api_key,expected", [(True, False), (False, None)])+def test_a_local_export_carries_the_callers_identity(+ monkeypatch, endpoint, method, extra, via_api_key, expected+):+ """No push, so nothing is refused, but the credential still has to be spelled: a local+ GGUF export resolves its base and imatrix from the Hub."""+ backend = _backend(monkeypatch, method)+ response = TestClient(_app(via_api_key)).post(+ endpoint, json = {"save_directory": "/tmp/x", "push_to_hub": False, **extra}+ )+ assert response.status_code == 200+ assert getattr(backend, method).call_args.kwargs["hf_token"] is expected+++ "via_api_key,token,expected_token,expected_flag",+ [+ (True, None, False, False),+ (True, "hf_caller", "hf_caller", False),+ (False, None, None, True),+ ],+)+def test_load_checkpoint_forwards_the_policy(+ monkeypatch, via_api_key, token, expected_token, expected_flag+):+ monkeypatch.setattr(export_routes, "_ensure_export_supported", _fake_ensure_export_supported)+ backend = MagicMock()+ backend.load_checkpoint.return_value = (True, "loaded")+ monkeypatch.setattr(export_routes, "get_export_backend", lambda: backend)++ response = TestClient(_app(via_api_key)).post(+ "/api/load-checkpoint", json = {"checkpoint_path": "o/m", "hf_token": token}+ )+ assert response.status_code == 200+ kwargs = backend.load_checkpoint.call_args.kwargs+ assert kwargs["hf_token"] is expected_token or kwargs["hf_token"] == expected_token+ assert kwargs["allow_ambient"] is expected_flag+++# ------------------------------------------------------------------------ export worker+++def worker_in_process(monkeypatch):+ """Let run_export_process run here without it taking over the pytest process:+ _setup_log_capture dup2s pipes over fds 1 and 2 with no teardown, and+ UNSLOTH_OFFLINE_PROBE keeps a live Hub request out of a unit test."""+ from core.export import worker++ monkeypatch.setattr(worker, "_setup_log_capture", lambda resp_queue: None)+ monkeypatch.setenv("UNSLOTH_OFFLINE_PROBE", "0")+ return worker+++ "allow_ambient,caller_token,env_token,disable_implicit,passed",+ [+ (False, None, None, "1", None),+ # The caller's token is passed, not planted: this worker serves later callers.+ (False, "hf_caller", None, "1", "hf_caller"),+ (True, None, "hf_operator_secret", None, None),+ ],+)+def test_the_worker_environment_matches_the_callers_policy(+ monkeypatch, worker_in_process, allow_ambient, caller_token, env_token, disable_implicit, passed+):+ import os++ worker = worker_in_process+ for key in ("HF_TOKEN", "HF_HUB_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HUGGINGFACEHUB_API_TOKEN"):+ monkeypatch.setenv(key, "hf_operator_secret")+ monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)++ seen: dict = {}++ def _fake_activate(path, token):+ seen["HF_TOKEN"] = os.environ.get("HF_TOKEN")+ seen["HF_HUB_TOKEN"] = os.environ.get("HF_HUB_TOKEN")+ seen["DISABLE_IMPLICIT"] = os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN")+ seen["passed"] = token+ raise SystemExit(0)++ monkeypatch.setattr(worker, "_activate_transformers_version", _fake_activate)+ with pytest.raises(SystemExit):+ worker.run_export_process(+ cmd_queue = MagicMock(),+ resp_queue = MagicMock(),+ config = {+ "checkpoint_path": "/tmp/model",+ "allow_ambient": allow_ambient,+ "hf_token": caller_token,+ },+ )++ assert seen["HF_TOKEN"] == env_token+ assert seen["DISABLE_IMPLICIT"] == disable_implicit+ if not allow_ambient:+ # The operator's aliases go even when the caller supplied its own credential.+ assert seen["HF_HUB_TOKEN"] is None+ assert seen["passed"] == passed+++def test_a_non_ambient_worker_holds_no_credential_for_the_next_caller(+ monkeypatch, worker_in_process+):+ """One worker serves many callers, so a credential in its environment is a credential+ in the ambient position for whoever exports next, the caller's own included."""+ import os++ worker = worker_in_process+ for key in (+ "HF_TOKEN",+ "HF_HUB_TOKEN",+ "HUGGING_FACE_HUB_TOKEN",+ "HUGGINGFACE_HUB_TOKEN",+ "HUGGINGFACEHUB_API_TOKEN",+ "HF_OIDC_RESOURCE",+ ):+ monkeypatch.setenv(key, "hf_operator_secret")+ monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)++ seen: dict = {}++ def _fake_activate(path, token):+ seen["env"] = {+ k: os.environ.get(k)+ for k in (+ "HF_TOKEN",+ "HF_HUB_TOKEN",+ "HUGGING_FACE_HUB_TOKEN",+ "HUGGINGFACE_HUB_TOKEN",+ "HUGGINGFACEHUB_API_TOKEN",+ "HF_OIDC_RESOURCE",+ )+ }+ seen["passed"] = token+ raise SystemExit(0)++ monkeypatch.setattr(worker, "_activate_transformers_version", _fake_activate)+ with pytest.raises(SystemExit):+ worker.run_export_process(+ cmd_queue = MagicMock(),+ resp_queue = MagicMock(),+ config = {+ "checkpoint_path": "/tmp/model",+ "allow_ambient": False,+ "hf_token": "hf_caller",+ },+ )++ assert seen["env"] == dict.fromkeys(seen["env"], None), seen["env"]+ # The caller keeps its own credential; it is passed, not planted.+ assert seen["passed"] == "hf_caller"+++def test_an_old_orchestrator_config_keeps_the_previous_behaviour(monkeypatch, worker_in_process):+ """A config from before this change has neither key; it must read as ambient."""+ import os++ worker = worker_in_process+ monkeypatch.setenv("HF_TOKEN", "hf_operator_secret")+ monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)++ seen: dict = {}++ def _fake_activate(path, token):+ seen["HF_TOKEN"] = os.environ.get("HF_TOKEN")+ seen["DISABLE_IMPLICIT"] = os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN")+ raise SystemExit(0)++ monkeypatch.setattr(worker, "_activate_transformers_version", _fake_activate)+ with pytest.raises(SystemExit):+ worker.run_export_process(+ cmd_queue = MagicMock(),+ resp_queue = MagicMock(),+ config = {"checkpoint_path": "/tmp/m", "hf_token": None},+ )++ assert seen["HF_TOKEN"] == "hf_operator_secret"+ assert seen["DISABLE_IMPLICIT"] is None+++ "allow_ambient,caller_token,expected",+ [(False, None, False), (False, "hf_caller", "hf_caller"), (True, None, None)],+)+def test_the_load_preflight_runs_under_the_callers_credential(+ monkeypatch, allow_ambient, caller_token, expected+):+ """model_config's shared-cache guards read is_anonymous(), so a plain None walks past+ them; every preflight helper but tier detection gets the same canonical value."""+ from core.export import worker+ from utils import security as security_pkg+ from utils import transformers_version+ from utils.models import model_config++ seen: dict = {}++ def _record(key, value, result):+ seen[key] = value+ return result++ class _Decision:+ blocked = False++ monkeypatch.setattr(+ transformers_version,+ "latest_tier_active_for",+ lambda name, token: _record("tier", token, False),+ )+ monkeypatch.setattr(+ model_config,+ "get_base_model_from_lora_identifier",+ lambda path, token: _record("lora_base", token, None),+ )+ monkeypatch.setattr(+ security_pkg, "security_load_subdirs", lambda t, token: _record("subdirs", token, ())+ )+ monkeypatch.setattr(security_pkg, "load_scan_target", lambda t, subdirs: (t, subdirs))+ monkeypatch.setattr(+ security_pkg,+ "evaluate_file_security",+ lambda target, hf_token, load_subdirs: _record("file_security", hf_token, _Decision()),+ )++ backend = MagicMock()+ backend.load_checkpoint.return_value = (True, "loaded")+ backend.is_vision = backend.is_peft = False++ worker._handle_load(+ backend,+ {+ "checkpoint_path": "owner/model",+ "load_in_4bit": True,+ "hf_token": caller_token,+ "allow_ambient": allow_ambient,+ },+ MagicMock(),+ )++ assert seen["lora_base"] == expected+ assert seen["subdirs"] == expected+ assert seen["file_security"] == expected+ assert backend.load_checkpoint.call_args.kwargs["hf_token"] == expected+ # Tier detection reads config.json off the hub cache, which the sentinel is refused.+ assert seen["tier"] == (expected or None)+++# ------------------------------------------------------------------------ export backend+++ "hf_token,expected", [(False, False), ("hf_caller", "hf_caller"), (None, None)]+)+def test_the_weight_loader_never_receives_none_for_an_anonymous_caller(+ monkeypatch, hf_token, expected+):+ """The whole point of the sentinel: hf_login(None) calls get_token(), which reads the+ operator's stored login from disk, and no environment scrub touches that."""+ from core.export import export as export_backend_module++ seen: dict = {}++ class _Stop(Exception):+ pass++ class _Loader:+ @staticmethod+ def from_pretrained(**kwargs):+ seen["loader"] = kwargs["token"]+ raise _Stop()++ monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)+ monkeypatch.setattr(export_backend_module, "_hf_offline", lambda: False)+ monkeypatch.setattr(+ export_backend_module,+ "detect_audio_type",+ lambda model_id, hf_token, local_files_only: seen.setdefault("audio", hf_token) and None,+ )+ monkeypatch.setattr(+ export_backend_module,+ "is_vision_model",+ lambda model_id, hf_token, local_files_only: bool(seen.setdefault("vision", hf_token))+ and False,+ )+ monkeypatch.setattr(export_backend_module, "FastLanguageModel", _Loader)++ export_backend_module.ExportBackend().load_checkpoint(+ checkpoint_path = "owner/model", hf_token = hf_token+ )++ # The probes take the plain token; only the loaders get the sentinel.+ assert seen["audio"] == (expected or None)+ assert seen["vision"] == (expected or None)+ assert seen["loader"] == expected+++def test_hf_login_reads_none_as_fetch_the_operators_stored_token():+ """The upstream contract the sentinel exists for. If this flips, the threading is moot."""+ import inspect++ # Not importorskip: that skips only on ModuleNotFoundError, and unsloth raises a plain+ # ImportError ("Unsloth: torch not found") on a torch-less install, which is supported.+ try:+ from unsloth.models._utils import hf_login+ except ImportError as exc:+ pytest.skip(f"unsloth needs torch, which this install does not ship: {exc}")++ src = inspect.getsource(hf_login)+ assert "if token is None:" in src and "get_token()" in src+ assert hf_login(False) is False++[email protected]("hf_token,expected", [(False, False), ("hf_caller", "hf_caller")])+def test_a_local_gguf_lora_conversion_carries_the_sentinel(+ monkeypatch, tmp_path, hf_token, expected+):+ """save.py substitutes get_token() only when the token is None, so False has to reach it."""+ from core.export import export as export_backend_module++ seen: dict = {}++ class _FakeModel:+ peft_config: dict = {}++ @staticmethod+ def save_pretrained_gguf(+ save_directory,+ tokenizer,+ save_method = None,+ quantization_method = None,+ token = None,+ ):+ seen["token"] = token++ monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)+ monkeypatch.setattr(export_backend_module, "_IS_MLX", False)+ monkeypatch.setattr(export_backend_module, "_apply_wsl_sudo_patch", lambda: None)++ backend = export_backend_module.ExportBackend()+ backend.current_model = _FakeModel()+ backend.current_tokenizer = object()+ backend.is_peft = True++ success, message, _path = backend.export_lora_adapter(+ save_directory = str(tmp_path), gguf = True, hf_token = hf_token+ )+ assert success, message+ assert seen["token"] == expected++[email protected]("hf_token,expected", [(False, False), ("hf_caller", "hf_caller")])+def test_a_local_merged_save_carries_the_sentinel(monkeypatch, tmp_path, hf_token, expected):+ """Not a push, but the merge resolves its base repo, and save.py turns a None into+ get_token(), which is the operator's stored login."""+ from core.export import export as export_backend_module++ seen: dict = {}++ class _FakeModel:+ @staticmethod+ def save_pretrained_merged(+ save_directory,+ tokenizer,+ save_method = None,+ token = None,+ ):+ seen["token"] = token++ monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)+ monkeypatch.setattr(export_backend_module, "_IS_MLX", False)++ backend = export_backend_module.ExportBackend()+ backend.current_model = _FakeModel()+ backend.current_tokenizer = object()++ backend.export_merged_model(save_directory = str(tmp_path), hf_token = hf_token)+ assert seen["token"] == expected+++def test_offline_type_detection_is_not_degraded_by_the_sentinel(monkeypatch):+ """model_config's cache guards refuse an anonymous cached read, which offline turns a+ cached vision model into a text one. Detection keeps the plain token for that reason."""+ from core.export import export as export_backend_module++ seen: dict = {}++ class _Stop(Exception):+ pass++ class _Loader:+ @staticmethod+ def from_pretrained(**kwargs):+ seen["loader"] = kwargs["token"]+ raise _Stop()++ monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)+ monkeypatch.setattr(export_backend_module, "_hf_offline", lambda: True)+ monkeypatch.setattr(+ export_backend_module,+ "detect_audio_type",+ lambda model_id, hf_token, local_files_only: seen.setdefault("audio", hf_token) and None,+ )+ monkeypatch.setattr(+ export_backend_module,+ "is_vision_model",+ lambda model_id, hf_token, local_files_only: bool(seen.setdefault("vision", hf_token))+ and False,+ )+ monkeypatch.setattr(export_backend_module, "FastLanguageModel", _Loader)++ export_backend_module.ExportBackend().load_checkpoint(+ checkpoint_path = "owner/vlm", hf_token = False+ )++ assert seen["audio"] is None, "an anonymous probe must not be forced down the guard"+ assert seen["vision"] is None+ assert seen["loader"] is False, "the loader still gets the sentinel"+++def test_offline_tier_detection_is_not_degraded_by_the_sentinel(monkeypatch, tmp_path):+ """_load_config_json refuses a hub-cache read for the sentinel, so offline a cached model+ whose tier is only in its config.json drops to the default sidecar."""+ import json+ from types import SimpleNamespace++ from utils import transformers_version as tv++ repo = "acme/private-finetune"+ repo_dir = tmp_path / ("models--" + repo.replace("/", "--"))+ (repo_dir / "snapshots" / "abc123").mkdir(parents = True)+ (repo_dir / "snapshots" / "abc123" / "config.json").write_text(+ json.dumps({"model_type": "qwen3_moe", "architectures": ["Qwen3MoeForCausalLM"]})+ )+ (repo_dir / "refs").mkdir(parents = True)+ (repo_dir / "refs" / "main").write_text("abc123")++ monkeypatch.setattr(tv, "get_hf_cache_paths", lambda: SimpleNamespace(hub_cache = tmp_path))+ monkeypatch.setenv("HF_HUB_OFFLINE", "1")+ monkeypatch.setenv("UNSLOTH_DISABLE_TIER_PROBE", "1")++ def _tier(hf_token):+ for cache in (+ tv._config_json_cache,+ tv._config_needs_510_cache,+ tv._config_needs_550_cache,+ tv._config_needs_530_cache,+ tv._tokenizer_class_cache,+ ):+ cache.clear()+ return tv.get_transformers_tier(repo, hf_token)++ assert _tier(False) == "default", "the sentinel is what breaks the cached tier read"+ assert _tier(None) == "530", "the plain token reads it, as it did before this change"+++def test_every_export_entry_point_declares_the_anonymous_sentinel_type():+ """``str | None`` on a parameter that receives ``False`` invites normalising it back+ to ``None``, which is the ambient state."""+ import inspect++ from core.export.export import ExportBackend+ from core.export.orchestrator import ExportOrchestrator+ from hub.utils.hf_tokens import HfTokenArg++ offenders = []+ for owner in (ExportBackend, ExportOrchestrator):+ for name in (+ "load_checkpoint",+ "export_merged_model",+ "export_base_model",+ "export_gguf",+ "export_lora_adapter",+ ):+ fn = getattr(owner, name, None)+ if fn is None:+ continue+ param = inspect.signature(fn).parameters.get("hf_token")+ if param is not None and param.annotation != HfTokenArg:+ offenders.append(f"{owner.__name__}.{name}: {param.annotation}")++ assert not offenders, "these can receive the sentinel but do not say so: " + "; ".join(+ offenders+ )+++def test_every_hub_write_route_names_the_ambient_policy():+ """The check that would have caught the two routes #10126 reported."""+ import inspect++ from auth.authentication import allow_ambient_hf_token+ from routes import export as export_routes+ from routes.data_recipe import jobs as jobs_routes++ gated = {+ (export_routes, "load_checkpoint"),+ (export_routes, "export_merged_model"),+ (export_routes, "export_base_model"),+ (export_routes, "export_gguf"),+ (export_routes, "export_lora_adapter"),+ (jobs_routes, "publish_job_dataset"),+ }+ missing = []+ for module, name in sorted(gated, key = lambda pair: pair[1]):+ fn = getattr(module, name)+ param = inspect.signature(fn).parameters.get("allow_ambient")+ if (+ param is None+ or getattr(param.default, "dependency", None) is not allow_ambient_hf_token+ ):+ missing.append(f"{module.__name__}.{name}")++ assert not missing, "these reach a Hub write without the ambient policy: " + ", ".join(missing)+++def test_every_mcp_tool_that_calls_a_gated_route_names_the_policy():+ """A direct call never resolves a ``Depends`` default, so each tool passes it or goes+ ambient in silence."""+ import inspect+ import re++ import mcp_server++ src = inspect.getsource(mcp_server.create_studio_mcp)+ calls = re.findall(r"await (?:load|export)\(request[^)]*\)", src)+ assert calls, "the MCP tools no longer call the export routes the way this test reads"+ for call in calls:+ assert "allow_ambient = False" in call, f"MCP call goes ambient: {call}"+++def test_the_worker_can_still_disable_implicit_tokens_when_it_starts():+ """huggingface_hub latches HF_HUB_DISABLE_IMPLICIT_TOKEN into a module constant at+ import, so the scrub only works while the worker module has not pulled it in yet. An+ import added anywhere in that chain would disable the switch with every other test in+ this file still green, because they only read os.environ."""+ import subprocess+ import sys+ import textwrap+ from pathlib import Path++ backend = Path(__file__).resolve().parent.parent+ probe = textwrap.dedent(+ """+ import sys+ sys.path.insert(0, %r)+ import core.export.worker # noqa: F401+ print("imported" if "huggingface_hub" in sys.modules else "clear")+ """+ ) % str(backend)+ out = subprocess.run([sys.executable, "-c", probe], capture_output = True, text = True, timeout = 300)+ assert out.returncode == 0, out.stderr[-2000:]+ assert out.stdout.strip().endswith("clear"), (+ "core.export.worker imports huggingface_hub at module scope, so "+ "HF_HUB_DISABLE_IMPLICIT_TOKEN is already latched by the time the scrub runs"+ )diff --git a/studio/backend/tests/test_mcp_server.py b/studio/backend/tests/test_mcp_server.pyindex 067c0caa129..30e90b2fb32 100644--- a/studio/backend/tests/test_mcp_server.py+++ b/studio/backend/tests/test_mcp_server.py@@ -202,8 +202,8 @@ def test_export_gguf_forwards_hf_token_and_imatrix(monkeypatch):def __init__(self, **kwargs):captured.update(kwargs)- async def fake_export(request, current_subject):- return {"current_subject": current_subject}+ async def fake_export(request, current_subject, allow_ambient):+ return {"current_subject": current_subject, "allow_ambient": allow_ambient}_stub_module(monkeypatch, "models", ExportGGUFRequest = FakeExportGGUFRequest)_stub_module(monkeypatch, "routes")@@ -231,6 +231,9 @@ def test_export_gguf_forwards_hf_token_and_imatrix(monkeypatch):assert captured["private"] is Trueassert captured["gguf_shard_size"] == "2GB"assert result["current_subject"] == "mcp"+ # A direct call skips FastAPI, so the route's Depends default never resolves; MCP has to+ # name the policy itself or allow_ambient arrives as a truthy Depends object.+ assert result["allow_ambient"] is Falsedef test_load_checkpoint_forwards_token_and_fingerprint(monkeypatch):@@ -240,15 +243,15 @@ def test_load_checkpoint_forwards_token_and_fingerprint(monkeypatch):def __init__(self, **kwargs):captured.update(kwargs)- async def fake_load(request, current_subject):- return {"current_subject": current_subject}+ async def fake_load(request, current_subject, allow_ambient):+ return {"current_subject": current_subject, "allow_ambient": allow_ambient}_stub_module(monkeypatch, "models", LoadCheckpointRequest = FakeLoadCheckpointRequest)_stub_module(monkeypatch, "routes")_stub_module(monkeypatch, "routes.export", load_checkpoint = fake_load)tool = _get_tool("load_checkpoint")- asyncio.run(+ result = asyncio.run(tool.fn(checkpoint_path = "/tmp/ckpt",approved_remote_code_fingerprint = "sha256:abc",@@ -258,6 +261,7 @@ def test_load_checkpoint_forwards_token_and_fingerprint(monkeypatch):assert captured["hf_token"] == "hf_secret"assert captured["approved_remote_code_fingerprint"] == "sha256:abc"+ assert result["allow_ambient"] is Falsedef test_stop_training_forwards_job_scope(monkeypatch):