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
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Shared allowlist helpers for secure local model file access."""
from __future__ import annotations
from hub.services.models import account_access
from utils.paths.storage_roots import workspace_root
import os
from pathlib import Path
from typing import Optional
from loggers import get_logger
from hub.utils.paths import (
exports_root,
hf_default_cache_dir,
legacy_hf_cache_dir,
normalize_path,
outputs_root,
studio_root,
well_known_model_dirs,
)
from utils.paths.external_media import (
linux_external_mount_roots,
macos_volume_roots,
windows_drive_roots,
)
from hub.services.models.common import _safe_is_dir
from hub.services.models.local_inventory import _resolve_hf_cache_dir
logger = get_logger(__name__)
def _build_browse_allowlist(
media_roots: Optional[list[Path]] = None, drive_roots: Optional[list[Path]] = None
) -> list[Path]:
"""Walkable roots: HOME, HF caches, outputs, scan folders; each only if a real directory."""
if account_access.managed_account():
return [workspace_root().resolve()]
from hub.storage.scan_folders import list_scan_folders
candidates: list[Path] = []
def _add(p: Optional[Path | str]) -> None:
if p is None:
return
try:
p = Path(normalize_path(str(p))).expanduser()
resolved = p.resolve()
except (OSError, RuntimeError, ValueError):
return
if _safe_is_dir(resolved):
candidates.append(resolved)
_add(Path.home())
if media_roots is None:
media_roots = [*linux_external_mount_roots(), *macos_volume_roots()]
if drive_roots is None:
drive_roots = windows_drive_roots()
for p in media_roots:
_add(p)
for p in drive_roots:
_add(p)
_add(_resolve_hf_cache_dir())
try:
from utils.hf_cache_settings import known_hf_cache_homes
for cache_home in known_hf_cache_homes():
_add(cache_home)
except Exception: # noqa: BLE001 -- best-effort
pass
try:
_add(hf_default_cache_dir())
except Exception: # noqa: BLE001 -- best-effort
pass
try:
_add(legacy_hf_cache_dir())
except Exception: # noqa: BLE001 -- best-effort
pass
try:
_add(studio_root())
_add(outputs_root())
_add(exports_root())
except Exception as exc: # noqa: BLE001 -- best-effort
logger.debug("browse-folders: Unsloth roots unavailable: %s", exc)
try:
for folder in list_scan_folders():
p = folder.get("path")
if p:
_add(p)
except Exception as exc: # noqa: BLE001 -- best-effort
logger.debug("browse-folders: could not load scan folders: %s", exc)
try:
for p in well_known_model_dirs():
_add(p)
except Exception as exc: # noqa: BLE001 -- best-effort
logger.debug("browse-folders: well-known dirs unavailable: %s", exc)
seen: set[str] = set()
deduped: list[Path] = []
for p in candidates:
key = os.path.normcase(os.path.realpath(str(p)))
if key in seen:
continue
seen.add(key)
deduped.append(p)
return deduped
def _is_path_inside_allowlist(target: Path, allowed_roots: list[Path]) -> bool:
"""A bare POSIX / root must not authorize its descendants, or every absolute path would pass."""
try:
target_real = os.path.normcase(os.path.realpath(str(target)))
except OSError:
return False
for root in allowed_roots:
try:
root_real = os.path.normcase(os.path.realpath(str(root)))
except OSError:
continue
if target_real == root_real:
return True
drive, tail = os.path.splitdrive(root_real)
if os.path.dirname(root_real) == root_real and not drive:
# Bare POSIX root: must not authorize arbitrary descendants.
continue
if drive.startswith(("\\\\", "//")) and not tail:
# commonpath raises on a bare UNC share root, so use a boundary-safe prefix test.
if target_real.startswith(root_real.rstrip("\\/") + os.sep):
return True
continue
try:
if os.path.commonpath([target_real, root_real]) == root_real:
return True
except ValueError:
continue
return False