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.

6.2 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

"""llama-server runs with a per-launch API key by default, and every Studio request to it sends the key."""

import ast
import os
import stat
import sys
from pathlib import Path

import pytest

BACKEND = Path(__file__).resolve().parents[1]
if str(BACKEND) not in sys.path:
    sys.path.insert(0, str(BACKEND))

from core.inference import llama_cpp  # noqa: E402
from core.inference import llama_stats  # noqa: E402


@pytest.mark.parametrize(
    "env,expected",
    [
        ({}, True),
        ({"UNSLOTH_LLAMA_SERVER_API_KEY": "0"}, False),
        ({"UNSLOTH_LLAMA_SERVER_API_KEY": "1"}, True),
        ({"UNSLOTH_LLAMA_SERVER_API_KEY": "0", "UNSLOTH_DIRECT_STREAM": "1"}, True),
        ({"UNSLOTH_DIRECT_STREAM": "1"}, True),
    ],
)
def test_key_is_on_by_default(monkeypatch, env, expected):
    monkeypatch.delenv("UNSLOTH_LLAMA_SERVER_API_KEY", raising = False)
    monkeypatch.delenv("UNSLOTH_DIRECT_STREAM", raising = False)
    for key, value in env.items():
        monkeypatch.setenv(key, value)
    assert llama_cpp._llama_server_api_key_enabled() is expected


def test_key_file_is_per_backend_private_and_rewritten_on_relaunch(monkeypatch, tmp_path):
    import utils.paths.storage_roots as roots

    monkeypatch.setattr(roots, "auth_root", lambda: tmp_path)
    first = llama_cpp._write_direct_stream_key("one")
    second = llama_cpp._write_direct_stream_key("two", first)
    other = llama_cpp._write_direct_stream_key("three")
    assert second == first and other != first
    assert second.read_text() == "two" and other.read_text() == "three"
    assert second.name.startswith("llama_api_key_")
    if os.name == "posix":
        assert stat.S_IMODE(second.stat().st_mode) == 0o600


def test_stale_key_files_are_swept_on_a_new_launch(monkeypatch, tmp_path):
    import utils.paths.storage_roots as roots

    monkeypatch.setattr(roots, "auth_root", lambda: tmp_path)
    stale = tmp_path / "llama_api_key_dead"
    stale.write_text("old")
    os.utime(stale, (1, 1))
    recent = llama_cpp._write_direct_stream_key("live")
    current = llama_cpp._write_direct_stream_key("new")
    assert not stale.exists()
    assert recent.read_text() == "live" and current.read_text() == "new"


@pytest.mark.parametrize(
    "platform,dirname,via_env",
    [
        ("win32", "Galambos András", True),
        ("win32", "张伟", True),
        ("win32", "Galambos Andras", False),
        ("linux", "Galambos András", False),
        ("darwin", "张伟", False),
    ],
)
def test_key_goes_through_env_only_where_windows_cannot_open_the_file(
    monkeypatch, tmp_path, platform, dirname, via_env
):
    import utils.paths.storage_roots as roots

    auth = tmp_path / dirname / "auth"
    monkeypatch.setattr(roots, "auth_root", lambda: auth)
    monkeypatch.setattr(llama_cpp.sys, "platform", platform)
    argv, env, path = llama_cpp._llama_server_key_launch("k")
    if via_env:
        # #13123: Windows llama-server opens --api-key-file in the ANSI code page.
        assert (argv, env, path) == ([], {"LLAMA_API_KEY": "k"}, None)
        assert not auth.exists()
    else:
        assert argv == ["--api-key-file", str(path)] and env == {}
        assert path.parent == auth and path.read_text() == "k"


def test_launch_sets_the_env_key_after_the_inherited_one_is_scrubbed():
    src = Path(llama_cpp.__file__).read_text(encoding = "utf-8")
    assert src.index("_denied_scrubbed = scrub_denied_env(env)") < src.index("env.update(_key_env)")


def test_tool_guard_still_refuses_the_per_launch_key_file():
    from core.inference import tools

    assert tools._references_studio_credential("cat /tmp/x/llama_api_key_0123abcd")
    assert tools._references_studio_credential("cat /tmp/x/llama_api_key")
    # A basename or glob search finds the live file anywhere, so it is refused bare too.
    assert tools._references_studio_credential("find / -name 'llama_api_key_*' -exec cat {} +")
    # The unsuffixed name stays an ordinary identifier.
    assert not tools._references_studio_credential("print(llama_api_key)")


def test_stats_scrape_sends_the_key(monkeypatch):
    seen = {}

    class _Resp:
        status = 200

        def __enter__(self):
            return self

        def __exit__(self, *a):
            return False

        def read(self):
            return b""

    def _open(request, timeout = None):
        seen["auth"] = request.get_header("Authorization")
        return _Resp()

    logger = llama_stats.LlamaServerStatsLogger(
        "http://127.0.0.1:1", None, headers = {"Authorization": "Bearer k"}
    )
    # The scrape never goes through an ambient proxy, which would receive the bearer.
    assert not any(
        isinstance(h, llama_stats.urllib.request.ProxyHandler) and h.proxies
        for h in logger._opener.handlers
    )
    monkeypatch.setattr(logger._opener, "open", _open)
    logger._scrape()
    assert seen["auth"] == "Bearer k"


def test_capability_probe_reports_api_key_file_and_fails_open():
    src = Path(llama_cpp.__file__).read_text(encoding = "utf-8")
    assert '"supports_api_key_file": True' in src
    assert 'supports_api_key_file = _is_real("--api-key-file")' in src
    assert 'server_caps.get("supports_api_key_file", True)' in src


def test_inference_routes_reach_llama_server_only_through_one_owner():
    src = (BACKEND / "routes" / "inference.py").read_text(encoding = "utf-8")
    tree = ast.parse(src)
    owner = next(
        node
        for node in tree.body
        if isinstance(node, ast.ClassDef) and node.name == "_LlamaUpstream"
    )
    owned = {id(node) for node in ast.walk(owner)}
    stray = []
    for node in ast.walk(tree):
        if id(node) in owned:
            continue
        if isinstance(node, ast.Call) and getattr(node.func, "id", None) == (
            "_openai_passthrough_upstream_headers"
        ):
            stray.append(f"line {node.lineno}: {ast.unparse(node)[:120]}")
        if isinstance(node, ast.JoinedStr) and ".base_url}/" in ast.unparse(node):
            stray.append(f"line {node.lineno}: {ast.unparse(node)[:120]}")
    assert not stray, "llama-server requests built outside _LlamaUpstream:\n" + "\n".join(stray)