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.

9.9 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Per-user approval cache skips only the dialog, honored only on matching fingerprint, rules
version and SHA; gate always re-scans, never stores CRITICAL; errors mean ask again, never auto-
approve."""

from __future__ import annotations

import contextlib
import json
import os
import pathlib
import threading
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Optional

from loggers import get_logger
from utils.paths import storage_roots
from hub.services.models import account_access
from utils.account_context import current_account_id, is_owner_context
from utils.security.remote_code_scan import CRITICAL, SCAN_RULES_VERSION

logger = get_logger(__name__)

_SCHEMA_VERSION = 1
_lock = threading.RLock()

# Re-exported so the gate can compare a stored approval's ruleset to the live one.
SCANNER_VERSION = SCAN_RULES_VERSION


@dataclass
class StoredApproval:
    commit_sha: Optional[str]
    fingerprint: str
    max_severity: Optional[str]
    approved_at: str
    scanner_version: int = 0


def cache_disabled() -> bool:
    return os.environ.get("UNSLOTH_TRC_APPROVAL_CACHE_DISABLE", "").lower() in ("1", "true", "yes")


def _store_path():
    if is_owner_context():
        return storage_roots.studio_root() / "security" / "remote_code_approvals.json"
    return storage_roots.account_path("security/remote_code_approvals.json")


def _env_offline() -> bool:
    return os.environ.get("HF_HUB_OFFLINE", "").lower() in ("1", "true", "yes") or os.environ.get(
        "TRANSFORMERS_OFFLINE", ""
    ).lower() in ("1", "true", "yes")


def approval_target_key(targets) -> str:
    """Stable key for the combined load unit (a LoRA pins adapter + base together), using
    the same casing normalization as the fingerprint so identity never disagrees."""
    from utils.security.consent import _fingerprint_target_key

    keys = sorted(_fingerprint_target_key(t) for t in dict.fromkeys(targets) if t)
    return "\x1f".join(keys)


def _load() -> dict:
    """Parsed store, or an empty skeleton on any error (fail-safe = re-prompt)."""
    try:
        with open(_store_path(), encoding = "utf-8-sig") as f:
            data = json.load(f)
        # Validate the shape too: a hand-edited non-dict ``subjects`` must fail safe, not crash.
        if (
            isinstance(data, dict)
            and data.get("version") == _SCHEMA_VERSION
            and isinstance(data.get("subjects"), dict)
        ):
            return data
    except FileNotFoundError:
        pass
    except Exception as exc:
        logger.warning("Could not read remote-code approvals (%s); ignoring", exc)
    return {"version": _SCHEMA_VERSION, "subjects": {}}


def _save(data: dict) -> None:
    """Atomic write (tmp + os.replace), best-effort 0600."""
    path = _store_path()
    storage_roots.ensure_dir(path.parent)
    tmp = path.parent / f".{path.name}.tmp-{os.getpid()}"
    try:
        with open(tmp, "w", encoding = "utf-8") as f:
            json.dump(data, f, indent = 2)
        try:
            os.chmod(tmp, 0o600)
        except OSError:
            pass
        os.replace(tmp, path)
    except Exception as exc:
        logger.warning("Could not write remote-code approvals (%s)", exc)
        try:
            tmp.unlink(missing_ok = True)
        except OSError:
            pass


@contextlib.contextmanager
def _file_lock():
    """Cross-process lock around the read-modify-write, since two processes could clobber an entry written by the other; a no-op when OS locking is unavailable."""
    path = _store_path()
    try:
        storage_roots.ensure_dir(path.parent)
        fd = os.open(str(path.parent / f"{path.name}.lock"), os.O_CREAT | os.O_RDWR, 0o600)
    except Exception:
        yield
        return
    try:
        try:
            if os.name == "nt":
                import msvcrt
                msvcrt.locking(fd, msvcrt.LK_LOCK, 1)
            else:
                import fcntl
                fcntl.flock(fd, fcntl.LOCK_EX)
        except Exception:
            pass
        yield
    finally:
        try:
            if os.name == "nt":
                import msvcrt
                with contextlib.suppress(Exception):
                    msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
            else:
                import fcntl
                fcntl.flock(fd, fcntl.LOCK_UN)
        finally:
            os.close(fd)


def lookup(subject: str, target_key: str) -> Optional[StoredApproval]:
    """The stored approval for (subject, target_key), or None. A CRITICAL entry (e.g. a
    hand-edited store) is refused so it can never seed an approval."""
    if account_access.managed_account():
        subject = current_account_id()
    if not subject or cache_disabled():
        return None
    with _lock:
        subj = _load().get("subjects", {}).get(subject, {})
    entry = subj.get(target_key) if isinstance(subj, dict) else None
    if not isinstance(entry, dict) or not entry.get("fingerprint"):
        return None
    if entry.get("max_severity") == CRITICAL:
        return None
    return StoredApproval(
        commit_sha = entry.get("commit_sha"),
        fingerprint = entry["fingerprint"],
        max_severity = entry.get("max_severity"),
        approved_at = entry.get("approved_at", ""),
        scanner_version = entry.get("scanner_version", 0),
    )


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,
    *,
    commit_sha: Optional[str],
    fingerprint: str,
    max_severity: Optional[str],
    scanner_version: int = SCANNER_VERSION,
) -> None:
    """Persist a user's explicit approval. CRITICAL is never stored."""
    if account_access.managed_account():
        subject = current_account_id()
    if not subject or not fingerprint or cache_disabled() or max_severity == CRITICAL:
        return
    with _lock, _file_lock():
        data = _load()
        subjects = data.setdefault("subjects", {})
        subj = subjects.get(subject)
        if not isinstance(subj, dict):
            subj = subjects[subject] = {}
        subj[target_key] = {
            "commit_sha": commit_sha,
            "fingerprint": fingerprint,
            "max_severity": max_severity,
            "scanner_version": scanner_version,
            "approved_at": datetime.now(timezone.utc).isoformat(),
        }
        _save(data)


def forget(subject: str, target_key: str) -> None:
    """Drop an approval (e.g. the user declined / discarded the download)."""
    if account_access.managed_account():
        subject = current_account_id()
    if not subject:
        return
    with _lock, _file_lock():
        data = _load()
        subj = data.get("subjects", {}).get(subject)
        if isinstance(subj, dict) and subj.pop(target_key, None) is not None:
            _save(data)


def clear() -> None:
    """Test helper: drop the on-disk store."""
    with _lock:
        try:
            _store_path().unlink(missing_ok = True)
        except OSError:
            pass


def resolve_commit_sha(target: str, hf_token: Optional[str] = None) -> Optional[str]:
    """Not cached: a moved branch would reuse stale consent. None means fall back, never fail open."""
    hf_token = account_access.account_hf_token(hf_token)
    from utils.paths import is_local_path

    try:
        if is_local_path(target) or _env_offline():
            return None
        from huggingface_hub import HfApi
        return HfApi().model_info(target, token = hf_token).sha
    except Exception as exc:
        logger.debug("Could not resolve commit sha for '%s': %s", target, exc)
        return None


def resolve_combined_sha(targets, hf_token: Optional[str] = None) -> Optional[str]:
    """Combined SHA over the primary targets; None if ANY is unresolvable. A cheap secondary
    gate only -- the fingerprint (which also covers external auto_map repos) stays
    authoritative, so a None here just falls back to the fingerprint, never weakens it."""
    from utils.security.consent import _fingerprint_target_key

    parts = []
    for target in dict.fromkeys(targets):
        if not target:
            continue
        sha = resolve_commit_sha(target, hf_token)
        if sha is None:
            return None
        parts.append(f"{_fingerprint_target_key(target)}={sha}")
    return "\x1f".join(sorted(parts)) if parts else None