unsloth/studio/backend/tests/test_preview_routes.py
Nilay e5cf956601
Studio: shareable per-checkpoint preview links (#6486)
* checkpoint preview endpoint

* harden new preview endpoints

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* address review

* Studio preview: pin adapter, guard streaming submit, robust copy-link

Harden the public per-checkpoint preview surface:

- Pin use_adapter=True in the preview payload sanitizer. Otherwise an
  unauthenticated /p caller can POST use_adapter=false, which calls
  disable_adapter_layers() on the shared in-memory model without restoring
  it; since load_model skips reloads for the same checkpoint, every later
  visitor (the page never sends the field) keeps getting base-model output
  instead of the fine-tuned checkpoint. Forcing it on also re-enables a
  previously disabled adapter and no-ops on merged checkpoints.
- Ignore preview-page submits while a response is streaming. The send
  button was disabled but the Enter handler still called requestSubmit(),
  so a second request could start before the first reply landed in msgs and
  reorder the chat history. Both the keydown and submit handlers now honor
  the disabled button.
- Keep the cloudflare-URL polling loop alive across transient startup fetch
  errors instead of letting one rejection halt it.
- Build the copy-link from a backend preview_ref (output dir relative to
  outputs_root, gated on previewability and the two-segment /p route limit)
  so a nested output dir no longer copies a basename-only link that 404s.
  Expose preview_ref on training run summaries.

Add route-level security tests (path traversal, payload sanitization,
asset containment, CSP header, HTML title escaping, streaming lock held
until drained) and preview_ref unit tests.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Studio preview: Safari-safe submit and adapter pin only for LoRA

Follow-ups from cross-browser and route simulations:

- Preview page: send the message from a shared send() helper called by both
  the form submit and the Enter key, instead of form.requestSubmit(). The
  latter throws on Safari < 16 and older iOS, which broke Enter-to-send there.
  Verified across Chromium, Firefox and WebKit with Playwright.
- Only pin use_adapter=True when the resolved checkpoint is a LoRA adapter
  (adapter_config.json present); for a merged checkpoint strip it to None.
  A merged model has no adapter to toggle, so forcing it on only produced a
  per-request "not a PeftModel" warning. The cross-request base-model
  contamination fix still holds for LoRA previews.

Add a merged-checkpoint test asserting use_adapter is stripped to None.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Studio preview: trim verbose comments

Tighten comments across the preview routes, page, checkpoint helpers, and tests
to short single-line notes; drop ones that just restate the code. No behavior
change (verified comment/docstring-only with comment_tools.py check).

* Harden preview routes for PR #6486

- Return a generic 400 detail on a rejected preview path so the public /p
  route never echoes the absolute install path (the real reason is logged
  server-side instead).
- Strip confirm_tool_calls, session_id and rag_scope in the preview payload
  sanitizer so the public surface stays inert regardless of the tool gate.
- Use Path.is_relative_to for the asset containment check, matching the rest
  of the codebase.
- Add img-src 'self' and font-src 'self' to the preview page CSP.
- Preview page: on a mid-stream error keep the streamed text, flag the break,
  and restore the prompt so the user can retry; drop the unused --font-sans var.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lee Jackson <130007945+Imagineer99@users.noreply.github.com>
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
2026-06-24 06:31:53 -07:00

293 lines
10 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Security smoke for the public /p preview routes.
Exercises the route layer with a real ``preview_router`` while stubbing the
expensive model calls (``load_model`` / ``openai_chat_completions``). Covers the
public-surface guarantees: path-traversal rejection, request sanitization
(tools / provider routing / use_adapter), asset-path containment, the page CSP
header + HTML escaping, and that the preview lock is held until a streaming
response is fully drained.
"""
import asyncio
import json
from pathlib import Path
import sys
import types as _types
import pytest
_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
if _BACKEND_DIR not in sys.path:
sys.path.insert(0, _BACKEND_DIR)
# Mirror test_preview.py: the real `loggers` package pulls in heavy handlers.
_loggers_stub = _types.ModuleType("loggers")
_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
sys.modules.setdefault("loggers", _loggers_stub)
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from fastapi.testclient import TestClient
import routes.preview as preview
from models.inference import ChatCompletionRequest
def _make_run(outputs: Path, name: str = "demorun") -> Path:
run = outputs / name
run.mkdir(parents = True)
(run / "adapter_config.json").write_text(
json.dumps({"base_model_name_or_path": "HuggingFaceTB/SmolLM-135M"})
)
ckpt = run / "checkpoint-1"
ckpt.mkdir()
(ckpt / "adapter_config.json").write_text("{}")
return run
@pytest.fixture
def captured():
return {}
@pytest.fixture
def client(tmp_path, monkeypatch, captured):
outputs = tmp_path / "outputs"
_make_run(outputs)
# resolve_preview_checkpoint -> resolve_output_dir -> outputs_root().
from utils.paths import storage_roots as _sr
monkeypatch.setattr(_sr, "outputs_root", lambda: outputs)
async def _fake_load_model(load_req, request, subject):
captured["load_path"] = load_req.model_path
return None
async def _fake_chat(payload, request, subject):
captured["payload"] = payload
return {"ok": True}
monkeypatch.setattr(preview, "load_model", _fake_load_model)
monkeypatch.setattr(preview, "openai_chat_completions", _fake_chat)
app = FastAPI()
app.include_router(preview.router, prefix = "/p")
app.dependency_overrides[preview.get_current_subject] = lambda: "admin"
# raise_server_exceptions=False so a 5xx surfaces as a response, not a throw.
return TestClient(app, raise_server_exceptions = False)
# ── Page rendering ────────────────────────────────────────────────────────
def test_page_renders_with_csp(client):
r = client.get("/p/demorun")
assert r.status_code == 200
assert "text/html" in r.headers["content-type"]
csp = r.headers.get("content-security-policy", "")
assert "default-src 'self'" in csp
assert "base-uri 'none'" in csp
def test_page_escapes_title(tmp_path, monkeypatch, captured):
outputs = tmp_path / "outputs"
# Run dir name carries an HTML-special char; the page must escape it.
_make_run(outputs, name = "a<b")
from utils.paths import storage_roots as _sr
monkeypatch.setattr(_sr, "outputs_root", lambda: outputs)
app = FastAPI()
app.include_router(preview.router, prefix = "/p")
c = TestClient(app, raise_server_exceptions = False)
r = c.get("/p/a%3Cb")
assert r.status_code == 200
assert "a<b" not in r.text
assert "a&lt;b" in r.text
def test_models_endpoint_shape(client):
r = client.get("/p/demorun/v1/models")
assert r.status_code == 200
body = r.json()
assert body["object"] == "list"
assert body["data"][0]["id"] == "demorun"
assert body["data"][0]["owned_by"] == "unsloth-studio"
def test_list_previews_builds_urls(client, monkeypatch):
monkeypatch.setattr(
preview,
"list_preview_targets",
lambda: [{"ref": "demorun", "is_latest": True}],
)
r = client.get("/p")
assert r.status_code == 200
data = r.json()["data"]
assert data[0]["url"].endswith("/p/demorun/v1")
# ── Path traversal / containment ────────────────────────────────────────────
@pytest.mark.parametrize(
"path",
[
"/p/..", # parent segment as run
"/p/%2e%2e/etc", # encoded traversal
"/p/..%2f..%2fetc/v1/models", # encoded slash traversal
"/p/does-not-exist", # unknown run
],
)
def test_traversal_and_missing_rejected(client, path):
r = client.get(path)
assert r.status_code in (400, 404), (path, r.status_code)
def test_chat_traversal_rejected(client):
r = client.post(
"/p/..%2f..%2fetc/v1/chat/completions",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code in (400, 404)
# ── Asset containment ────────────────────────────────────────────────────────
@pytest.mark.parametrize(
"asset",
[
"../../../../etc/passwd", # escapes dist
"secrets.txt", # non-allowlisted suffix
"nope.png", # allowlisted suffix but missing
],
)
def test_asset_path_contained(client, asset):
r = client.get(f"/p/_assets/{asset}")
assert r.status_code == 404
# ── Request sanitization ─────────────────────────────────────────────────────
def test_chat_payload_sanitized(client, captured):
r = client.post(
"/p/demorun/v1/chat/completions",
json = {
"messages": [{"role": "user", "content": "hi"}],
"tools": [{"type": "function", "function": {"name": "rm", "parameters": {}}}],
"enable_tools": True,
"enabled_tools": ["python"],
"mcp_enabled": True,
"bypass_permissions": True,
"provider_id": "p1",
"provider_type": "custom",
"provider_base_url": "http://evil.example/v1",
"external_model": "gpt-4o",
"use_adapter": False,
"confirm_tool_calls": True,
"session_id": "abc",
"rag_scope": {"project_id": "x"},
},
)
assert r.status_code == 200
p = captured["payload"]
assert isinstance(p, ChatCompletionRequest)
# Tools / code-exec off.
assert p.tools is None
assert p.enable_tools is False
assert p.enabled_tools is None
assert p.mcp_enabled is False
assert p.bypass_permissions is False
# Tool-loop levers neutralized regardless of the tool gate.
assert p.confirm_tool_calls is False
assert p.session_id is None
assert p.rag_scope is None
# Provider routing stripped so /p can't proxy an arbitrary endpoint.
assert p.provider_id is None
assert p.provider_type is None
assert p.provider_base_url is None
assert p.external_model is None
# Adapter pinned on for LoRA: a caller can't flip the shared backend to base.
assert p.use_adapter is True
# Loads the resolved checkpoint dir, not an attacker-supplied path.
assert captured["load_path"].endswith("demorun")
def test_merged_checkpoint_strips_use_adapter(tmp_path, monkeypatch, captured):
# Merged (non-LoRA) checkpoint: no adapter to toggle, so use_adapter -> None.
outputs = tmp_path / "outputs"
merged = outputs / "mergedrun"
merged.mkdir(parents = True)
(merged / "config.json").write_text(json.dumps({"_name_or_path": "some/base"}))
from utils.paths import storage_roots as _sr
monkeypatch.setattr(_sr, "outputs_root", lambda: outputs)
async def _fake_load(load_req, request, subject):
return None
async def _fake_chat(payload, request, subject):
captured["payload"] = payload
return {"ok": True}
monkeypatch.setattr(preview, "load_model", _fake_load)
monkeypatch.setattr(preview, "openai_chat_completions", _fake_chat)
app = FastAPI()
app.include_router(preview.router, prefix = "/p")
c = TestClient(app, raise_server_exceptions = False)
r = c.post(
"/p/mergedrun/v1/chat/completions",
json = {"messages": [{"role": "user", "content": "hi"}], "use_adapter": False},
)
assert r.status_code == 200
assert captured["payload"].use_adapter is None
# ── Streaming lock lifetime ──────────────────────────────────────────────────
def test_streaming_holds_lock_until_drained(tmp_path, monkeypatch, captured):
outputs = tmp_path / "outputs"
_make_run(outputs)
from utils.paths import storage_roots as _sr
monkeypatch.setattr(_sr, "outputs_root", lambda: outputs)
async def _fake_load_model(load_req, request, subject):
return None
async def _gen():
yield b"data: {}\n\n"
yield b"data: [DONE]\n\n"
async def _fake_chat(payload, request, subject):
return StreamingResponse(_gen())
monkeypatch.setattr(preview, "load_model", _fake_load_model)
monkeypatch.setattr(preview, "openai_chat_completions", _fake_chat)
async def _run():
assert not preview._preview_lock.locked()
payload = ChatCompletionRequest(messages = [{"role": "user", "content": "hi"}])
resp = await preview._serve_chat("demorun", None, payload, request = None)
# Lock must still be held: a second checkpoint must not swap the backend
# mid-stream.
assert preview._preview_lock.locked()
chunks = [c async for c in resp.body_iterator]
# Released only after the stream fully drains.
assert not preview._preview_lock.locked()
return chunks
chunks = asyncio.run(_run())
assert any(b"[DONE]" in c for c in chunks)
assert not preview._preview_lock.locked()