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: close managed-account and API-key gaps in owner-only routes (#12940)
Daniel Han3790884d059c
diff --git a/studio/backend/core/inference/llama_server_args.py b/studio/backend/core/inference/llama_server_args.pyindex 712da88552e..3644f47df82 100644--- a/studio/backend/core/inference/llama_server_args.py+++ b/studio/backend/core/inference/llama_server_args.py@@ -178,6 +178,8 @@ OWNER_ONLY_PATH_FLAGS: frozenset[str] = frozenset("--log-prompts-dir",# llama-server runs <dir>/ffmpeg to decode a video."--video-ffmpeg-dir",+ # Not a path: llama-server sends the model's tensors to these hosts.+ "--rpc",})diff --git a/studio/backend/routes/data_recipe/library.py b/studio/backend/routes/data_recipe/library.pyindex 895576dafb5..0b583f0bb70 100644--- a/studio/backend/routes/data_recipe/library.py+++ b/studio/backend/routes/data_recipe/library.py@@ -5,14 +5,19 @@from __future__ import annotations-from typing import Any+from typing import Annotated, Any-from fastapi import APIRouter, HTTPException+from fastapi import APIRouter, Depends, HTTPExceptionfrom pydantic import BaseModel, ConfigDict, Field+from auth.authentication import (+ authenticated_via_api_key,+ require_ui_session_for_local_commands,+)from storage import data_recipes_db as dbrouter = APIRouter(prefix = "/recipes")+ViaApiKey = Annotated[bool, Depends(authenticated_via_api_key)]_ID = Field(min_length = 1, max_length = 128)# JS millisecond timestamps; also keeps values inside SQLite INTEGER.@@ -48,13 +53,29 @@ class LegacyImportRequest(BaseModel):executions: list[ExecutionRecord] = Field(default_factory = list, max_length = 50_000)+def _payload_has_stdio_mcp(payload: Any) -> bool:+ """True when the recipe holds a stdio MCP provider anywhere; running it later starts that command."""+ stack = [payload]+ while stack:+ node = stack.pop()+ if isinstance(node, dict):+ if node.get("provider_type") == "stdio":+ return True+ stack.extend(node.values())+ elif isinstance(node, list):+ stack.extend(node)+ return False++@router.get("")def get_recipes():return {"recipes": db.list_recipes()}@router.post("/import")-def import_legacy_recipes(req: LegacyImportRequest):+def import_legacy_recipes(req: LegacyImportRequest, via_api_key: ViaApiKey = False):+ if any(_payload_has_stdio_mcp(r.payload) for r in req.recipes):+ require_ui_session_for_local_commands(via_api_key)return db.import_legacy([r.model_dump(exclude_none = True) for r in req.recipes],[e.model_dump() for e in req.executions],@@ -70,9 +91,15 @@ def get_recipe(recipe_id: str):@router.put("/{recipe_id}")-def put_recipe(recipe_id: str, recipe: SaveRecipeRequest):+def put_recipe(+ recipe_id: str,+ recipe: SaveRecipeRequest,+ via_api_key: ViaApiKey = False,+):if recipe.id != recipe_id:raise HTTPException(status_code = 400, detail = "ID mismatch")+ if _payload_has_stdio_mcp(recipe.payload):+ require_ui_session_for_local_commands(via_api_key)try:return db.upsert_recipe(recipe.model_dump(exclude_none = True, exclude = {"baseUpdatedAt"}),diff --git a/studio/backend/routes/external_import.py b/studio/backend/routes/external_import.pyindex ed689b41011..5127c0bf2eb 100644--- a/studio/backend/routes/external_import.py+++ b/studio/backend/routes/external_import.py@@ -49,7 +49,12 @@ def external_import_status(source: SourceKey, current_subject: str = Depends(getreturn ExternalImportStatus(available = bool(chats), chats = chats)[email protected]("/{source}", response_model = ExternalImportResult, dependencies = [OwnerOnly])+# Authenticate first: until the bearer is resolved the request carries the default (owner) account.+ "/{source}",+ response_model = ExternalImportResult,+ dependencies = [Depends(get_current_subject), OwnerOnly],+)def external_import(source: SourceKey, current_subject: str = Depends(get_current_subject)):try:summary = run_import(sources()[source])diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 9523381a068..8ea964141b1 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -8074,11 +8074,15 @@ def _owner_chosen_launch(or was the owner's). Replaying these is not a new path, so auto-switch loads and the UI resendingan inherited or echoed setting keep working."""from core.inference.llama_server_args import owner_only_path_args+ from utils.account_context import OWNERfrom utils.openai_auto_switch_settings import resolve_override_for_loadsources = []if identifier:- _, override = resolve_override_for_load(identifier, config_identifier, variant)+ # The owner's row only: a managed account's own saved override is not the owner's choice.+ _, override = run_as(+ OWNER, resolve_override_for_load, identifier, config_identifier, variant+ )sources.append(override.get("llama_extra_args"))intent = getattr(get_llama_cpp_backend(), "last_load_intent", None)if (diff --git a/studio/backend/routes/mcp_servers.py b/studio/backend/routes/mcp_servers.pyindex 597299a10a1..4d100b68be1 100644--- a/studio/backend/routes/mcp_servers.py+++ b/studio/backend/routes/mcp_servers.py@@ -503,7 +503,11 @@ async def update_mcp_server(@router.delete("/{server_id}", status_code = 204)@serialize_mcp_server_mutation-async def delete_mcp_server(server_id: str, current_subject: str = Depends(get_current_subject)):+async def delete_mcp_server(+ server_id: str,+ current_subject: str = Depends(get_current_subject),+ via_api_key: ViaApiKey = False,+):old = mcp_servers_db.get_server(server_id)if not old:raise HTTPException(status_code = 404, detail = "MCP server not found")@@ -511,6 +515,9 @@ async def delete_mcp_server(server_id: str, current_subject: str = Depends(get_craise HTTPException(status_code = 400, detail = "Managed integrations cannot be deleted; disable them instead.")+ # Same rule as update: an API key cannot touch a stdio row.+ if is_stdio(old["url"]):+ require_ui_session_for_local_commands(via_api_key)if old.get("use_oauth"):await clear_oauth_tokens_async(old["url"])mcp_servers_db.delete_server(server_id)diff --git a/studio/backend/routes/providers.py b/studio/backend/routes/providers.pyindex f00407ce763..768406818a0 100644--- a/studio/backend/routes/providers.py+++ b/studio/backend/routes/providers.py@@ -7,6 +7,7 @@ listing."""import asyncio+import hashlibimport jsonimport timeimport uuid@@ -1005,7 +1006,9 @@ async def list_provider_model_capabilities(except ValueError as exc:raise HTTPException(status_code = 400, detail = str(exc)) from None- cache_key = f"{payload.provider_type}\n{base_url}"+ # Per credential: one key's model list is not another caller's.+ key_digest = hashlib.sha256((api_key or "").encode("utf-8")).hexdigest()[:16]+ cache_key = f"{payload.provider_type}\n{base_url}\n{key_digest}"cached = _model_capability_cache.get(cache_key)if cached is not None and time.monotonic() - cached[0] < _MODEL_CAPABILITY_CACHE_TTL_SECONDS:return cached[1]diff --git a/studio/backend/tests/test_data_recipe_library.py b/studio/backend/tests/test_data_recipe_library.pyindex 96fb41d4cb7..6e9ad0acd0f 100644--- a/studio/backend/tests/test_data_recipe_library.py+++ b/studio/backend/tests/test_data_recipe_library.py@@ -4,12 +4,15 @@from fastapi import FastAPIfrom fastapi.testclient import TestClient+from auth.authentication import authenticated_via_api_keyfrom routes.data_recipe.library import routerdef _client() -> TestClient:app = FastAPI()app.include_router(router)+ # Mounted bare here; in the app it sits behind get_current_subject, a UI session by default.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falsereturn TestClient(app)diff --git a/studio/backend/tests/test_managed_account_route_gates.py b/studio/backend/tests/test_managed_account_route_gates.pynew file mode 100644index 00000000000..c5d3789a8bc--- /dev/null+++ b/studio/backend/tests/test_managed_account_route_gates.py@@ -0,0 +1,313 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Owner-only and UI-session-only gates that a managed account or an API key must not get past."""++from __future__ import annotations++import asyncio++import pytest+from fastapi import FastAPI, HTTPException+from fastapi.testclient import TestClient++from auth import policy+from auth.authentication import authenticated_via_api_key, get_current_subject+from utils.account_context import (+ AccountContext,+ OWNER,+ bind_account,+ is_owner_context,+ reset_account,+)++ALICE = AccountContext("a" * 32, "alice")++[email protected](autouse = True)+def multi_user(monkeypatch):+ monkeypatch.setattr(policy, "installation_is_multi_user", lambda: True)+ yield+++def _client(+ account,+ build,+ via_api_key = False,+):+ app = FastAPI()++ async def subject():+ token = bind_account(account)+ try:+ yield account.username+ finally:+ reset_account(token)++ app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: via_api_key+ build(app)+ return TestClient(app)+++# Conversation import reads the host owner's ~/.claude, ~/.codex and ~/.cursor.+++def _import_app(app):+ from routes import external_import+ app.include_router(external_import.router, prefix = "/api/import")+++def imports(monkeypatch):+ from routes import external_import++ ran = []++ def run_import(source):+ ran.append(source)+ return type("Summary", (), {})()++ monkeypatch.setattr(external_import, "run_import", run_import)+ return ran+++def test_a_managed_account_cannot_import_the_owners_conversations(imports):+ with _client(ALICE, _import_app) as client:+ assert client.post("/api/import/claude").status_code == 403+ assert imports == []+++def test_the_owner_still_imports_conversations(imports):+ with _client(OWNER, _import_app) as client:+ assert client.post("/api/import/claude").status_code == 200+ assert len(imports) == 1+++# --rpc: llama-server dials the named hosts and streams the model's tensors to them.++[email protected]("args", [["--rpc", "10.0.0.5:50052"], ["--rpc=10.0.0.5:50052"]])+def test_rpc_is_an_owner_only_flag(args):+ from core.inference.llama_server_args import owner_only_path_flags+ assert owner_only_path_flags(args) == ["--rpc"]+++def test_the_owner_can_still_pass_rpc():+ from core.inference.llama_server_args import validate_extra_args+ assert validate_extra_args(["--rpc", "10.0.0.5:50052"]) == ["--rpc", "10.0.0.5:50052"]+++def _launch_gate(monkeypatch, stored_by_account):+ """routes.inference._refuse_managed_custom_projector with override rows per account."""+ from routes import inference+ from utils import openai_auto_switch_settings++ def resolve(+ load_id,+ alias_id = None,+ variant = None,+ ):+ # As the real resolver: a managed account without its own row falls back to the owner's.+ row = stored_by_account.get("owner" if is_owner_context() else "managed")+ if not row and not is_owner_context():+ row = stored_by_account.get("owner")+ return (load_id, row) if row else (None, {})++ monkeypatch.setattr(openai_auto_switch_settings, "resolve_override_for_load", resolve)+ monkeypatch.setattr(+ inference,+ "get_llama_cpp_backend",+ lambda: type("Backend", (), {"last_load_intent": None})(),+ )++ def gate(extra_args, identifier = "org/model-GGUF"):+ token = bind_account(ALICE)+ try:+ return inference._refuse_managed_custom_projector(extra_args, identifier)+ finally:+ reset_account(token)++ return gate+++def test_a_managed_account_cannot_load_with_rpc(monkeypatch):+ gate = _launch_gate(monkeypatch, {})+ with pytest.raises(HTTPException) as refused:+ gate(["--rpc", "10.0.0.5:50052"])+ assert refused.value.status_code == 403+++def test_a_managed_accounts_own_override_is_not_the_owners_choice(monkeypatch):+ # A managed account can save per-model overrides; that row must not count as one the owner chose.+ path_args = ["--chat-template-file", "/etc/hostname"]+ gate = _launch_gate(monkeypatch, {"managed": {"llama_extra_args": path_args}})+ with pytest.raises(HTTPException) as refused:+ gate(path_args)+ assert refused.value.status_code == 403+++def test_a_path_the_owner_saved_still_replays_for_a_managed_account(monkeypatch):+ path_args = ["--chat-template-file", "/srv/templates/chat.jinja", "--rpc", "10.0.0.5:50052"]+ gate = _launch_gate(monkeypatch, {"owner": {"llama_extra_args": path_args}})+ assert gate(path_args) is None+++# Data Recipes: a saved recipe runs its stdio MCP command later, from the UI.++_STDIO_PAYLOAD = {+ "recipe": {+ "columns": [],+ "mcp_providers": [{"name": "x", "provider_type": "stdio", "command": "touch /tmp/pwned"}],+ }+}+_HTTP_PAYLOAD = {+ "recipe": {+ "columns": [],+ "mcp_providers": [+ {"name": "x", "provider_type": "streamable_http", "endpoint": "https://example.com/mcp"}+ ],+ }+}+++def recipe_store(monkeypatch):+ from routes.data_recipe import library++ saved = []+ monkeypatch.setattr(+ library.db, "upsert_recipe", lambda record, base: saved.append(record) or record+ )+ monkeypatch.setattr(+ library.db,+ "import_legacy",+ lambda recipes, executions: saved.extend(recipes) or {"recipes": len(recipes)},+ )+ return saved+++def _recipe_app(app):+ from routes.data_recipe import library+ app.include_router(library.router, prefix = "/api/data-recipe")+++def _recipe(payload):+ return {"id": "r1", "name": "r", "payload": payload, "createdAt": 1, "updatedAt": 1}+++def test_an_api_key_cannot_save_a_recipe_with_a_stdio_mcp_command(recipe_store):+ with _client(OWNER, _recipe_app, via_api_key = True) as client:+ assert (+ client.put("/api/data-recipe/recipes/r1", json = _recipe(_STDIO_PAYLOAD)).status_code+ == 403+ )+ imported = client.post(+ "/api/data-recipe/recipes/import", json = {"recipes": [_recipe(_STDIO_PAYLOAD)]}+ )+ assert imported.status_code == 403+ assert recipe_store == []+++def test_recipes_without_stdio_and_ui_sessions_still_save(recipe_store):+ with _client(OWNER, _recipe_app, via_api_key = True) as client:+ assert (+ client.put("/api/data-recipe/recipes/r1", json = _recipe(_HTTP_PAYLOAD)).status_code+ == 200+ )+ with _client(OWNER, _recipe_app, via_api_key = False) as client:+ assert (+ client.put("/api/data-recipe/recipes/r1", json = _recipe(_STDIO_PAYLOAD)).status_code+ == 200+ )+ imported = client.post(+ "/api/data-recipe/recipes/import", json = {"recipes": [_recipe(_STDIO_PAYLOAD)]}+ )+ assert imported.status_code == 200+ assert len(recipe_store) == 3+++# MCP servers: an API key cannot touch a stdio row, so it cannot delete one either.+++def mcp_rows(monkeypatch):+ from routes import mcp_servers++ rows = {+ "stdio": {"id": "stdio", "url": "npx -y some-server", "use_oauth": False},+ "http": {"id": "http", "url": "https://example.com/mcp", "use_oauth": False},+ }+ deleted = []+ monkeypatch.setattr(mcp_servers.mcp_servers_db, "get_server", lambda sid: rows.get(sid))+ monkeypatch.setattr(mcp_servers.mcp_servers_db, "delete_server", deleted.append)+ monkeypatch.setattr(mcp_servers, "invalidate_tool_cache", lambda sid: None)+ monkeypatch.setattr(mcp_servers, "close_mcp_sessions", lambda *a, **k: None)+ monkeypatch.setattr(mcp_servers, "parse_server_headers", lambda row: {})++ def delete(server_id, via_api_key):+ return asyncio.run(+ mcp_servers.delete_mcp_server(server_id, current_subject = "x", via_api_key = via_api_key)+ )++ return delete, deleted+++def test_an_api_key_cannot_delete_a_stdio_mcp_server(mcp_rows):+ delete, deleted = mcp_rows+ with pytest.raises(HTTPException) as refused:+ delete("stdio", True)+ assert refused.value.status_code == 403+ assert deleted == []+++def test_mcp_deletes_still_work_for_ui_sessions_and_http_rows(mcp_rows):+ delete, deleted = mcp_rows+ delete("http", True)+ delete("stdio", False)+ assert deleted == ["http", "stdio"]+++# Provider capability catalogs are per credential.+++def test_capability_cache_does_not_answer_one_key_with_anothers_catalog(monkeypatch):+ from models.providers import ProviderModelsRequest+ from routes import providers as providers_route++ fetched = []++ class Client:+ def __init__(self, **kwargs):+ self.api_key = kwargs["api_key"]++ async def list_models(self):+ fetched.append(self.api_key)+ return [{"id": f"model-for-{self.api_key}"}]++ async def close(self):+ pass++ keys = iter(["sk-alice", "sk-bob"])+ monkeypatch.setattr(providers_route, "ExternalProviderClient", Client)+ monkeypatch.setattr(+ providers_route, "resolve_provider_api_key_or_400", lambda *a, **k: next(keys)+ )+ monkeypatch.setattr(+ providers_route,+ "provider_model_capabilities",+ lambda provider_type, models: [{"id": m["id"]} for m in models],+ )+ providers_route._model_capability_cache.clear()+ try:+ for _ in range(2):+ asyncio.run(+ providers_route.list_provider_model_capabilities(+ ProviderModelsRequest(provider_type = "openrouter"),+ _current_subject = "x",+ via_api_key = False,+ )+ )+ finally:+ providers_route._model_capability_cache.clear()+ assert fetched == ["sk-alice", "sk-bob"]