unsloth/studio/backend/tests/test_preview_routes.py
Daniel Han 80d3434d61
Studio: require signed capability tokens for /p preview links (#6666)
* Studio: require signed capability tokens for /p preview links

The public /p preview routes added in #6486 run model load and chat
generation as the admin user with no authentication. The only gate is the
preview ref, a deterministic outputs-root path (run or run/checkpoint) that
is guessable rather than secret. On a network-reachable Studio (--secure
tunnel or -H 0.0.0.0), an unauthenticated caller who guesses a ref can
consume GPU and probe a private fine-tuned checkpoint.

Make the share link an unguessable, revocable capability:

- Sign the canonical ref with a dedicated server-side secret (HMAC-SHA256,
  stored in app_secrets, independent of the JWT/login secret).
- Require a valid token on every /p chat, models, and page request before
  resolving a checkpoint or loading a model; missing or invalid tokens get a
  generic 404 so the surface never confirms a ref exists.
- Accept the token via ?k= (browser link and preview page) or
  Authorization: Bearer (OpenAI-compatible clients).
- Rotate the secret to revoke every outstanding link
  (POST /api/settings/preview-links/rotate).
- Clamp preview generation (max_tokens/max_completion_tokens <= 1024, n = 1)
  and set Referrer-Policy: no-referrer on the page so the token is not
  leaked via Referer.

Training history hands the authenticated owner the signed token, and the
copy-link button builds /p/{ref}?k={sig}.

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

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

* Studio: honor a lower caller token limit in the preview clamp

Codex review: when only the legacy max_tokens was sent, the clamp left
max_completion_tokens at the 1024 default, and _effective_max_tokens prefers
max_completion_tokens, so a request like max_tokens=16 could still generate up
to 1024 tokens. Derive one effective limit (max_completion_tokens wins, else the
legacy max_tokens) and pin both fields to it so a caller's lower limit is kept.

* Studio: add preview kill switch, rate limit, and revoke-links UI

Follow-ups to the /p preview capability work:

- Public-sharing kill switch: a persisted setting (default on) gates the public
  /p surface. When off, every preview request 404s even with a valid token, and
  the owner UI stops offering share links. GET/PUT /api/settings/preview-sharing;
  enforced in _verify_or_404.
- Per-IP rate limit on the preview chat route: a coarse in-process sliding-window
  limiter (20 req/min/IP) returns 429 + Retry-After before the GPU lock is taken.
  Client IP honors X-Forwarded-For only when UNSLOTH_STUDIO_TRUST_FORWARDED is
  set, matching the login limiter's trust model.
- Settings UI: a "Preview sharing" section with the public-sharing toggle and a
  "Revoke all preview links" button (confirm dialog) that rotates the secret.

Tests cover the kill switch (404 when off), the 429 path, the sliding window,
client-IP trust behavior, and the setting default.

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

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

* Studio: fix preview-fields sharing arg and refresh sigs after revoke

Codex review:
- P1: get_training_run_detail and update_training_run called _preview_fields
  with only output_dir after it gained a required sharing_on parameter, raising
  a 500 TypeError once get_run succeeded. Pass get_preview_sharing_enabled() at
  both sites; add a detail-endpoint regression test.
- P2: after rotating the preview secret from settings, the history grid still
  held stale preview_sig values, so a freshly copied link would 404. Emit
  emitTrainingRunsChanged() after a successful revoke so the grid refetches
  freshly signed refs.

* Studio: harden preview sharing controls (Codex review)

- Fail closed: a read failure on the preview-sharing kill switch now returns
  False instead of defaulting to enabled, so an unavailable settings DB can't
  reopen the public surface. A missing key still defaults to enabled.
- Per-IP rate limit behind the managed Cloudflare tunnel: client_ip now honors
  CF-Connecting-IP when the socket peer is loopback, so tunneled visitors are
  keyed by their real IP instead of collapsing onto the local cloudflared peer.
- GET /p no longer mints key/share_url when sharing is disabled; it returns
  sharing_enabled=false so clients don't distribute links that 404.
- Settings UI: toggling public sharing emits the training-runs-changed event so
  the history grid shows/hides Copy preview link without a manual refresh.

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

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

* Studio: harden preview rate limiter and IP keying (Opus review)

From a two-agent review of the PR:

- Rate limiter no longer evicts an active bucket when the table is full: a flood
  of distinct keys could otherwise cycle out a throttled bucket and reset its
  counter. Evict only aged-out buckets; if the table is full of live clients,
  fail closed (deny the new key) instead.
- client_ip keys on the rightmost (proxy-appended) X-Forwarded-For hop when the
  trust env is set; the leftmost is client-spoofable. Documented the
  append/overwrite-proxy assumption.
- _verify_or_404 checks the capability token before the kill-switch DB read, so
  unauthenticated /p spam can't be used as an unbounded settings-DB sink and the
  response is identical regardless of the sharing on/off state.

Tests: nested run/checkpoint happy path + wrong-ref rejection, the eviction
fail-closed behavior, and route-level coverage for the rotate / preview-sharing
settings endpoints.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-06-25 21:40:48 -07:00

496 lines
18 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: HMAC capability gating (a valid ``?k=`` token or
Bearer credential is required; missing/invalid/wrong-ref tokens 404 before any
model load), path-traversal rejection, request sanitization (tools / provider
routing / use_adapter / generation clamp), asset-path containment, the page CSP
+ no-referrer headers and 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
import utils.preview_token as preview_token
from models.inference import ChatCompletionRequest
# A fixed secret keeps signing deterministic and avoids touching auth.db.
_TEST_SECRET = b"unit-test-preview-secret-0123456789"
def _use_test_secret(monkeypatch) -> None:
monkeypatch.setattr(preview_token, "get_or_create_preview_link_secret", lambda: _TEST_SECRET)
def _sig(ref: str) -> str:
"""Valid capability token for ``ref`` under the patched test secret."""
return preview_token.sign_preview_ref(ref)
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)
_use_test_secret(monkeypatch)
# Public sharing on by default; reset the per-IP rate buckets each test.
monkeypatch.setattr(preview, "get_preview_sharing_enabled", lambda: True)
import utils.preview_rate_limit as _rl
_rl.reset()
# 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(f"/p/demorun?k={_sig('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
# Token rides in the query string; keep it out of the Referer header.
assert r.headers.get("referrer-policy") == "no-referrer"
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")
_use_test_secret(monkeypatch)
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)
# Sign the decoded canonical ref ("a<b"), not the %-encoded path segment.
r = c.get(f"/p/a%3Cb?k={_sig('a<b')}")
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(f"/p/demorun/v1/models?k={_sig('demorun')}")
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")
# The listing hands the authenticated owner a usable capability.
assert data[0]["key"] == _sig("demorun")
assert data[0]["share_url"].endswith(f"/p/demorun?k={_sig('demorun')}")
def test_list_previews_omits_capability_when_sharing_disabled(client, monkeypatch):
monkeypatch.setattr(
preview,
"list_preview_targets",
lambda: [{"ref": "demorun", "is_latest": True}],
)
monkeypatch.setattr(preview, "get_preview_sharing_enabled", lambda: False)
r = client.get("/p")
assert r.status_code == 200
body = r.json()
# Don't hand out credentials that 404; signal the disabled state instead.
assert body["sharing_enabled"] is False
assert body["data"][0]["key"] is None
assert body["data"][0]["share_url"] is None
# ── 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(
f"/p/demorun/v1/chat/completions?k={_sig('demorun')}",
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
# Generation cost capped on this public surface (no override sent -> ceiling).
assert p.max_tokens == preview._PREVIEW_MAX_OUTPUT_TOKENS
assert p.max_completion_tokens == preview._PREVIEW_MAX_OUTPUT_TOKENS
assert p.n == 1
# 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"}))
_use_test_secret(monkeypatch)
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(
f"/p/mergedrun/v1/chat/completions?k={_sig('mergedrun')}",
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()
# ── Capability gating ────────────────────────────────────────────────────────
def test_chat_without_token_404_and_no_load(client, captured):
r = client.post(
"/p/demorun/v1/chat/completions",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 404
# Verified before any model work: nothing loaded, nothing generated.
assert "load_path" not in captured
assert "payload" not in captured
def test_chat_with_invalid_token_404(client, captured):
r = client.post(
"/p/demorun/v1/chat/completions?k=not-a-valid-token",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 404
assert "load_path" not in captured
def test_token_for_other_ref_rejected(client, captured):
# A capability minted for a different ref must not unlock demorun.
r = client.post(
f"/p/demorun/v1/chat/completions?k={_sig('otherrun')}",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 404
assert "load_path" not in captured
def test_models_without_token_404(client):
assert client.get("/p/demorun/v1/models").status_code == 404
def test_page_without_token_404(client):
assert client.get("/p/demorun").status_code == 404
def test_checkpoint_route_with_valid_sig(client, captured):
# Nested ref: the signed/verified/resolved canonical ref is "run/checkpoint".
sig = _sig("demorun/checkpoint-1")
r = client.post(
f"/p/demorun/checkpoint-1/v1/chat/completions?k={sig}",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 200
assert captured["load_path"].endswith("checkpoint-1")
def test_checkpoint_token_does_not_unlock_bare_run(client, captured):
# A token minted for the nested checkpoint must not unlock the run ref.
r = client.post(
f"/p/demorun/v1/chat/completions?k={_sig('demorun/checkpoint-1')}",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 404
assert "load_path" not in captured
def test_bearer_token_accepted(client, captured):
# OpenAI-compatible clients pass the capability as the api_key (Bearer header).
r = client.post(
"/p/demorun/v1/chat/completions",
headers = {"Authorization": f"Bearer {_sig('demorun')}"},
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 200
assert captured["load_path"].endswith("demorun")
def test_generation_clamp_caps_overrides(client, captured):
r = client.post(
f"/p/demorun/v1/chat/completions?k={_sig('demorun')}",
json = {
"messages": [{"role": "user", "content": "hi"}],
"max_tokens": 999999,
"max_completion_tokens": 888888,
"n": 64,
},
)
assert r.status_code == 200
p = captured["payload"]
assert p.max_tokens == preview._PREVIEW_MAX_OUTPUT_TOKENS
assert p.max_completion_tokens == preview._PREVIEW_MAX_OUTPUT_TOKENS
assert p.n == 1
def test_generation_clamp_honors_lower_legacy_max_tokens(client, captured):
# A caller asking for fewer tokens via the legacy field must not be bumped up
# to the ceiling: _effective_max_tokens prefers max_completion_tokens, so both
# fields have to carry the lower value.
r = client.post(
f"/p/demorun/v1/chat/completions?k={_sig('demorun')}",
json = {"messages": [{"role": "user", "content": "hi"}], "max_tokens": 16},
)
assert r.status_code == 200
p = captured["payload"]
assert p.max_tokens == 16
assert p.max_completion_tokens == 16
def test_generation_clamp_honors_lower_completion_tokens(client, captured):
r = client.post(
f"/p/demorun/v1/chat/completions?k={_sig('demorun')}",
json = {"messages": [{"role": "user", "content": "hi"}], "max_completion_tokens": 32},
)
assert r.status_code == 200
p = captured["payload"]
assert p.max_tokens == 32
assert p.max_completion_tokens == 32
# ── Public-sharing kill switch ───────────────────────────────────────────────
def test_chat_blocked_when_sharing_disabled(client, monkeypatch, captured):
# Admin turned public sharing off: even a valid token 404s, with no model load.
monkeypatch.setattr(preview, "get_preview_sharing_enabled", lambda: False)
r = client.post(
f"/p/demorun/v1/chat/completions?k={_sig('demorun')}",
json = {"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == 404
assert "load_path" not in captured
def test_page_blocked_when_sharing_disabled(client, monkeypatch):
monkeypatch.setattr(preview, "get_preview_sharing_enabled", lambda: False)
assert client.get(f"/p/demorun?k={_sig('demorun')}").status_code == 404
# ── Rate limiting ────────────────────────────────────────────────────────────
def test_chat_rate_limited_returns_429(client, monkeypatch):
import utils.preview_rate_limit as rl
monkeypatch.setattr(rl, "_MAX_REQUESTS", 2)
rl.reset()
url = f"/p/demorun/v1/chat/completions?k={_sig('demorun')}"
body = {"messages": [{"role": "user", "content": "hi"}]}
assert client.post(url, json = body).status_code == 200
assert client.post(url, json = body).status_code == 200
r = client.post(url, json = body)
assert r.status_code == 429
assert r.headers.get("retry-after")