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 data recipe seed inspect inside a managed account's workspace (#13611)
Daniel Han1c3e50eca697
diff --git a/studio/backend/core/training/worker.py b/studio/backend/core/training/worker.pyindex 63e3312853f..db99f8571cf 100644--- a/studio/backend/core/training/worker.py+++ b/studio/backend/core/training/worker.py@@ -3772,6 +3772,10 @@ def run_training_process(*, event_queue: Any, stop_queue: Any, config: dict) ->)os.environ["PYTHONWARNINGS"] = "ignore" # before imports+ from utils.security.hub_data_files import install as guard_hub_data_files++ guard_hub_data_files()+if not config.get("allow_ambient", True):from hub.utils.hf_tokens import apply_token_to_child_env, hf_token_arg, is_anonymousdiff --git a/studio/backend/main.py b/studio/backend/main.pyindex 7d7fe7b531e..f7c3dda77b6 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -268,6 +268,11 @@ from utils.hub_settings import apply_hub_settings as _apply_hub_settings_apply_hub_settings()del _apply_hub_settings+from utils.security.hub_data_files import install as _guard_hub_data_files++_guard_hub_data_files()+del _guard_hub_data_files+# The studio bundles unsloth_zoo; declare unsloth present (as `import unsloth` does) so its# lazy submodule imports and the DiffusionGemma runner don't trip the install guard.os.environ.setdefault("UNSLOTH_IS_PRESENT", "1")diff --git a/studio/backend/routes/data_recipe/seed.py b/studio/backend/routes/data_recipe/seed.pyindex 8d44c2a0f94..65e4e0a85b2 100644--- a/studio/backend/routes/data_recipe/seed.py+++ b/studio/backend/routes/data_recipe/seed.py@@ -1098,6 +1098,30 @@ def _read_preview_rows_from_multi_files()+def _require_own_seed_source(dataset_name: str) -> None:+ """load_dataset also reads a local folder, its dataset card's data_files and any links in it, and a+ check before the read cannot pin what the read opens. A managed account previews Hub ids only, as+ the UI does; its own files go through /seed/inspect-upload."""+ if not managed_account():+ return+ parts = dataset_name.split("/")+ hub_id = len(parts) == 2 and all(+ part not in {"", ".", ".."} and all(c.isalnum() or c in "-_." for c in part)+ for part in parts+ )+ if hub_id:+ # Only a well-formed id reaches the filesystem: a UNC name would start SMB auth on Windows.+ try:+ hub_id = not Path(dataset_name).expanduser().exists()+ except (ValueError, RuntimeError, OSError):+ hub_id = False+ if not hub_id:+ raise HTTPException(+ status_code = 403,+ detail = "dataset_name must be a Hugging Face repo id like org/repo",+ )++@router.post("/seed/inspect", response_model = SeedInspectResponse)def inspect_seed_dataset(payload: SeedInspectRequest, allow_ambient_token: bool = Depends(allow_ambient_hf_token)@@ -1108,6 +1132,7 @@ def inspect_seed_dataset(status_code = 400,detail = "dataset_name must be a Hugging Face repo id like org/repo",)+ _require_own_seed_source(dataset_name)split = _normalize_optional_text(payload.split) or DEFAULT_SPLITsubset = _normalize_optional_text(payload.subset)diff --git a/studio/backend/tests/test_data_recipe_seed.py b/studio/backend/tests/test_data_recipe_seed.pyindex b2a72754f90..c23763f1252 100644--- a/studio/backend/tests/test_data_recipe_seed.py+++ b/studio/backend/tests/test_data_recipe_seed.py@@ -1443,6 +1443,91 @@ def test_seed_preview_file_follows_the_config_data_dir(monkeypatch, tmp_path):assert response.resolved_path == "datasets/org/repo/fr/train*.parquet"+def _seed_inspect_as(monkeypatch, tmp_path, *, managed):+ seed_route = _load_seed_route(monkeypatch, tmp_path)+ monkeypatch.setattr(seed_route, "managed_account", lambda: managed)+ monkeypatch.setattr(seed_route, "refuse_unauthorized_dataset_preview", lambda *a, **k: None)+ monkeypatch.setattr(seed_route, "_list_hf_repo_files", lambda **kwargs: ["train.csv"])+ monkeypatch.setattr(seed_route, "_list_hf_dataset_configs", lambda **kwargs: [])+ read = []++ def fake_preview(*, load_dataset_fn, load_kwargs, preview_size):+ read.append(load_kwargs)+ return [{"secret": "owner-only"}]++ monkeypatch.setattr(seed_route, "_load_preview_rows", fake_preview)++ def inspect(name):+ return seed_route.inspect_seed_dataset(+ SimpleNamespace(+ dataset_name = name, split = "train", subset = None, hf_token = None, preview_size = 1+ ),+ allow_ambient_token = False,+ )++ return inspect, read+++def _seed_folders(tmp_path):+ owner_rows = tmp_path / "owner" / "uploads"+ owner_rows.mkdir(parents = True)+ (owner_rows / "train.csv").write_text("secret\nowner-only\n")+ own = tmp_path / "accounts" / "alice" / "seeds"+ own.mkdir(parents = True)+ (own / "train.csv").write_text("a\n1\n")+ # load_dataset honours a card's data_files, so a folder of its own can still point elsewhere.+ (own / "README.md").write_text(+ f"---\nconfigs:\n- config_name: default\n data_files: {owner_rows / 'train.csv'}\n---\n"+ )+ linked = tmp_path / "accounts" / "alice" / "linked"+ linked.mkdir()+ (linked / "train.csv").symlink_to(owner_rows / "train.csv")+ return owner_rows, own, linked+++# A managed account previews Hub ids only (what the UI's Hugging Face mode sends); every local+# folder is refused before load_dataset, whatever its card, links or later swaps say.+ "name", ["owner", "own", "linked", "org/\x00repo", "../repo", "~nobody/repo", "a/b/c"]+)+def test_a_managed_account_inspects_hub_ids_only(monkeypatch, tmp_path, name):+ owner_rows, own, linked = _seed_folders(tmp_path)+ # Forward slashes, which Windows accepts too: a backslash path is refused earlier, as not org/repo.+ folders = {"owner": owner_rows, "own": own, "linked": linked}+ name = folders[name].as_posix() if name in folders else name+ inspect, read = _seed_inspect_as(monkeypatch, tmp_path, managed = True)++ with pytest.raises(HTTPException) as refused:+ inspect(name)++ assert refused.value.status_code == 403+ assert read == []+ inspect("org/repo")+ assert read++[email protected]("name", ["//attacker/share", "\\\\attacker\\share", "org/repo/extra"])+def test_a_malformed_id_never_reaches_the_filesystem(monkeypatch, tmp_path, name):+ inspect, read = _seed_inspect_as(monkeypatch, tmp_path, managed = True)+ probed = []+ monkeypatch.setattr(Path, "exists", lambda self: probed.append(str(self)) or False)++ with pytest.raises(HTTPException) as refused:+ inspect(name)++ assert refused.value.status_code in (400, 403)+ assert probed == [] and read == []+++def test_the_owner_still_inspects_a_local_folder(monkeypatch, tmp_path):+ _owner_rows, own, _linked = _seed_folders(tmp_path)+ inspect, read = _seed_inspect_as(monkeypatch, tmp_path, managed = False)++ inspect(own.as_posix())++ assert read++def test_seed_hf_path_treats_an_unnamed_config_as_the_default(monkeypatch, tmp_path):seed_route = _load_seed_route(monkeypatch, tmp_path)configs = [diff --git a/studio/backend/tests/test_hub_data_files_guard.py b/studio/backend/tests/test_hub_data_files_guard.pynew file mode 100644index 00000000000..daefc2f9587--- /dev/null+++ b/studio/backend/tests/test_hub_data_files_guard.py@@ -0,0 +1,139 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import os+import subprocess+import sys+import textwrap+from pathlib import Path++import pytest++pytest.importorskip("datasets")++BACKEND = Path(__file__).resolve().parent.parent++# Each line names one way a Hub card reaches a host file, and whether datasets resolved it there.+PROBE = """+import os+import pathlib+import sys+from utils.security import hub_data_files+{before}+import datasets.data_files+import datasets.load+from datasets import DownloadConfig+from datasets.utils.metadata import MetadataConfigs+{after}+owner, local_base = sys.argv[1], sys.argv[2]+hub = "hf://datasets/someone/repo@main"+secret = os.path.join(owner, "private.csv")++def show(label, resolve):+ try:+ # private.csv is the only file of that name, so it marks the host file on any OS.+ print(label, "read" if "private.csv" in str(resolve()) else "other")+ except FileNotFoundError:+ print(label, "refused")++posix = pathlib.Path(secret).as_posix()+for label, pattern in (("ABSOLUTE", secret), ("FILE_URL", pathlib.Path(secret).as_uri()),+ ("LOCAL_URL", "local://" + posix), ("CHAINED", "simplecache::file://" + posix)):+ show(label, lambda: datasets.data_files.resolve_pattern(pattern, base_path = hub))++def card(data_dir):+ configs, _ = datasets.load.create_builder_configs_from_metadata_configs(+ "datasets.packaged_modules.csv.csv",+ MetadataConfigs({{"default": {{"data_dir": data_dir, "data_files": "private.csv"}}}}),+ base_path = hub,+ )+ configs[0]._resolve_data_files(base_path = hub, download_config = DownloadConfig())+ return dict(configs[0].data_files)++show("DATA_DIR", lambda: card(owner))+# A cached Hub snapshot is loaded as a local folder; its card is still the Hub's.+snapshot = sys.argv[3]+show("SNAPSHOT", lambda: datasets.data_files.resolve_pattern(secret, base_path = snapshot))+climb = os.path.relpath(secret, snapshot)+show("TRAVERSAL", lambda: datasets.data_files.resolve_pattern(climb, base_path = snapshot))+# An owner's own folder merely named like a snapshot, outside the Hub cache, stays local.+lookalike = sys.argv[4]+show("LOOKALIKE", lambda: datasets.data_files.resolve_pattern(secret, base_path = lookalike))+own_rows = datasets.data_files.resolve_pattern("train.csv", base_path = snapshot)+print("SNAPSHOT_OWN", "read" if own_rows and own_rows[0].endswith("train.csv") else "other")+# A local dataset may still name its own files by absolute path.+show(+ "LOCAL_DATASET", lambda: datasets.data_files.resolve_pattern(secret, base_path = local_base)+)+wrapped = getattr(datasets.data_files.resolve_pattern, "__wrapped__", None)+print("DOUBLE", getattr(wrapped, "_unsloth_hub_card_guard", False))+print("FINDERS", sum(isinstance(f, hub_data_files._Finder) for f in sys.meta_path))+"""++HUB_ROUTES = (+ "ABSOLUTE",+ "FILE_URL",+ "LOCAL_URL",+ "CHAINED",+ "DATA_DIR",+ "SNAPSHOT",+ "TRAVERSAL",+)+++def _probe(tmp_path, before, after):+ owner = tmp_path / "owner"+ owner.mkdir()+ (owner / "private.csv").write_text("secret\n1\n")+ snapshot = tmp_path / "hub" / "datasets--org--repo" / "snapshots" / "abc123"+ snapshot.mkdir(parents = True)+ lookalike = tmp_path / "mine" / "datasets--archive" / "snapshots" / "current"+ lookalike.mkdir(parents = True)+ (snapshot / "train.csv").write_text("a\n1\n")+ code = textwrap.dedent(PROBE.format(before = before, after = after))+ out = subprocess.run(+ [sys.executable, "-c", code, str(owner), str(tmp_path), str(snapshot), str(lookalike)],+ cwd = BACKEND,+ capture_output = True,+ text = True,+ timeout = 300,+ # The full environment: Windows cannot load sockets without SYSTEMROOT and friends.+ env = {+ **os.environ,+ "PYTHONPATH": str(BACKEND),+ "HF_HUB_OFFLINE": "1",+ "HF_HUB_CACHE": str(tmp_path / "hub"),+ },+ )+ assert out.returncode == 0, out.stderr+ return dict(line.split(" ", 1) for line in out.stdout.splitlines() if " " in line)+++# Installed before datasets loads (Studio's startup), after it, and twice: one wrapper, one finder.+ "before,after",+ [+ ("hub_data_files.install()", ""),+ ("", "hub_data_files.install()"),+ ("hub_data_files.install(); hub_data_files.install()", "hub_data_files.install()"),+ ],+)+def test_a_hub_card_cannot_reach_a_host_file(tmp_path, before, after):+ seen = _probe(tmp_path, before, after)++ assert {route: seen[route] for route in HUB_ROUTES} == dict.fromkeys(HUB_ROUTES, "refused")+ assert seen["LOCAL_DATASET"] == "read"+ assert seen["SNAPSHOT_OWN"] == "read"+ assert seen["LOOKALIKE"] == "read"+ assert seen["DOUBLE"] == "False"+ assert seen["FINDERS"] == "1"++[email protected](sys.platform == "win32", reason = "records datasets' POSIX URL parsing")+def test_without_the_guard_datasets_reads_the_host_file(tmp_path):+ # The behaviour the guard exists for, so a datasets release that changes it shows up here.+ seen = _probe(tmp_path, "", "")++ # datasets 4.3.0 already refuses the chained form; the guard keeps refusing it if that changes.+ open_routes = [route for route in HUB_ROUTES if route != "CHAINED"]+ assert {route: seen[route] for route in open_routes} == dict.fromkeys(open_routes, "read")diff --git a/studio/backend/utils/security/hub_data_files.py b/studio/backend/utils/security/hub_data_files.pynew file mode 100644index 00000000000..25941de656e--- /dev/null+++ b/studio/backend/utils/security/hub_data_files.py@@ -0,0 +1,166 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Refuse Hub dataset cards that point at host files.++datasets joins a card's relative ``data_files`` and ``data_dir`` to the repo (``hf://datasets/<repo>@<rev>``)+but keeps an absolute path, a ``file://`` / ``local://`` URL or an absolute ``data_dir`` as given, so a Hub+repo whose README lists ``/home/owner/private.csv`` makes ``load_dataset("org/repo")`` read that host file.+No real card does that, and it would let anyone who can name a Hub dataset to Studio read host files+through a preview or a load. A Hub card may name relative paths and hf / http(s) URLs only.++``install()`` patches ``datasets`` once it is imported, without importing it at startup; it is idempotent.+"""++import functools+import importlib.abc+import importlib.util+import os+import re+import sys++_HUB_URL_PROTOCOLS = frozenset({"hf", "http", "https"})+_PATCHES = {+ "datasets.data_files": ("resolve_pattern", "_pattern_guard"),+ "datasets.load": ("create_builder_configs_from_metadata_configs", "_card_guard"),+}+_MARK = "_unsloth_hub_card_guard"+# A cached Hub snapshot is loaded as a local folder, but its card is still the Hub's.+_SNAPSHOT = re.compile(r"[\\/]datasets--[^\\/]+[\\/]snapshots[\\/]")+++def _hub_cache_roots() -> list[str]:+ roots = [os.environ.get("HF_HUB_CACHE")]+ try:+ from huggingface_hub import constants+ roots.append(constants.HF_HUB_CACHE)+ except Exception: # noqa: BLE001 - a root we cannot read is simply not one+ pass+ try:+ from utils.hf_cache_settings import active_hf_hub_cache+ roots.append(active_hf_hub_cache())+ except Exception: # noqa: BLE001+ pass+ return [os.path.realpath(root) for root in roots if root]+++def _hub_base(base_path) -> bool:+ text = str(base_path or "")+ if text.startswith("hf://"):+ return True+ # Spelling alone is not enough: an owner's own folder may be named like a snapshot.+ if not _SNAPSHOT.search(text + "/"):+ return False+ real = os.path.realpath(text)+ return any(real.startswith(root.rstrip(os.sep) + os.sep) for root in _hub_cache_roots())+++def _stays_on_the_hub(value, *, urls: bool) -> bool:+ from datasets.utils.file_utils import is_relative_path+ from fsspec.core import split_protocol++ text = str(value)+ if "::" in text:+ return False+ protocol, _ = split_protocol(text)+ if protocol is None:+ # Relative, but ".." would climb out of a cached snapshot onto the host.+ return is_relative_path(text) and ".." not in re.split(r"[\\/]", text)+ return urls and protocol in _HUB_URL_PROTOCOLS+++def _refuse(value) -> None:+ raise FileNotFoundError(f"Unable to find '{value}': a Hub dataset cannot point at a local file")+++def _patterns(data_files):+ if isinstance(data_files, dict):+ for value in data_files.values():+ yield from _patterns(value)+ elif isinstance(data_files, (list, tuple)):+ for value in data_files:+ yield from _patterns(value)+ elif data_files is not None:+ yield data_files+++def _pattern_guard(resolve_pattern):+ @functools.wraps(resolve_pattern)+ def guarded(pattern, base_path, *args, **kwargs):+ if _hub_base(base_path) and not _stays_on_the_hub(pattern, urls = True):+ _refuse(pattern)+ return resolve_pattern(pattern, base_path, *args, **kwargs)++ return guarded+++def _card_guard(create_builder_configs):+ @functools.wraps(create_builder_configs)+ def guarded(+ module_path,+ metadata_configs,+ base_path = None,+ *args,+ **kwargs,+ ):+ # The card's own values, while their Hub origin is still known: a later join drops it.+ if _hub_base(base_path):+ for params in (metadata_configs or {}).values():+ data_dir = params.get("data_dir")+ if data_dir and not _stays_on_the_hub(data_dir, urls = False):+ _refuse(data_dir)+ for pattern in _patterns(params.get("data_files")):+ if not _stays_on_the_hub(pattern, urls = True):+ _refuse(pattern)+ return create_builder_configs(module_path, metadata_configs, base_path, *args, **kwargs)++ return guarded+++def _patch(module) -> None:+ attribute, wrapper = _PATCHES[module.__name__]+ original = getattr(module, attribute)+ if not getattr(original, _MARK, False):+ guarded = globals()[wrapper](original)+ setattr(guarded, _MARK, True)+ setattr(module, attribute, guarded)+++class _Loader(importlib.abc.Loader):+ def __init__(self, loader):+ self._loader = loader++ def create_module(self, spec):+ return self._loader.create_module(spec)++ def exec_module(self, module):+ self._loader.exec_module(module)+ _patch(module)+++class _Finder(importlib.abc.MetaPathFinder):+ def find_spec(+ self,+ name,+ path,+ target = None,+ ):+ if name not in _PATCHES:+ return None+ sys.meta_path.remove(self)+ try:+ spec = importlib.util.find_spec(name)+ finally:+ sys.meta_path.insert(0, self)+ if spec is None or spec.loader is None:+ return spec+ spec.loader = _Loader(spec.loader)+ return spec+++def install() -> None:+ for name in _PATCHES:+ if name in sys.modules:+ _patch(sys.modules[name])+ if not any(isinstance(finder, _Finder) for finder in sys.meta_path):+ sys.meta_path.insert(0, _Finder())