* Keep server-side tools enabled under --secure and on every bind --secure binds loopback and exposes Studio only through an authenticated Cloudflare HTTPS tunnel, but it was grouped with a raw 0.0.0.0 bind and force-disabled all server-side tools (web search, Python, terminal). The process tool policy overrode the client's enable_tools request, so the model was never told the tools existed and answered in plain text. The plain 'unsloth studio' command had no way to re-enable and printed nothing. Tools now default on for every bind. The bind host and --secure no longer change the tool policy; only an explicit --enable-tools/--disable-tools forces it on or off. Both 'unsloth studio' and 'unsloth studio run' accept the flags and the startup banner states the resolved policy. - run.py: replace _apply_default_tool_policy(host, secure) with _apply_cli_tool_policy(enable_tools); add an enable_tools kwarg to run_server and --enable-tools/--disable-tools to the argparse. - _tool_policy.py: resolve_tool_policy defaults to on for every host and no longer prompts on a network bind. - studio.py: drop the secure-as-public tool gating, add the flags to the plain command, and reword the startup banner. - Update and extend the secure-flag and tool-policy tests. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add tool-policy notice to plain server banner and refresh run --help Follow-up to PR review: - run.py: the plain 'unsloth studio' / --secure / direct run.py path went through _emit_startup_output without any tool-policy line, so a network-reachable launch was silent about code execution now that tools default on. Thread enable_tools through _emit_startup_output / _emit_secure_startup_output and print a one-line policy notice, followed by a single stop hint. - studio.py: the 'unsloth studio run' --enable-tools/--disable-tools and --yes help still described the removed loopback-on/network-off default and the confirmation prompt; reword to match the new policy. - Add tests for the banner notice and the refreshed help text. * Update CI tool-policy resolver tests for default-on behavior tests/python/test_unsloth_run_tool_policy_resolver.py still asserted the removed network-bind policy (0.0.0.0 and LAN IP default off, explicit enable prompts and aborts on a declined prompt), so it failed the Python CI jobs. Rewrite the truth table: every bind defaults on, explicit on/off always wins, and the resolver never prompts (yes/silent/prompt kept for compatibility). * Trim comments for the tool-policy change Shorten the verbose docstrings and block comments added for --secure tool handling; keep the security-relevant intent. Verified comment-only via an AST diff (code unchanged). * Add deterministic test that server-side tools execute under --secure Drive the GGUF agentic tool loop with a fake llama-server stream and let the real execute_tool run: python counts 1..100, terminal returns a UTC datetime, and web_search runs through real _web_search with only the ddgs network boundary mocked. A policy assertion pins that the post-fix --secure path (policy None + per-request enable_tools) is what keeps these executions reachable. No model, GPU, or live network; runs in the existing backend CI. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Align _emit_startup_output banner test with the moved stop hint The tool-policy notice now prints between the access banner and the stop hint, so the stop hint is emitted once at the end instead of inline in the banner (include_stop_hint is False and print_studio_stop_hint runs once). Update the plain-localhost case to match; the mismatch and wildcard cases already asserted this wiring. --------- Co-authored-by: Michael Han <michaelhan2050@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
355 lines
12 KiB
Python
355 lines
12 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
|
|
|
|
"""Tests for the `--secure/--not-secure` Studio flag: option registration,
|
|
re-exec/run_server forwarding, the forced 127.0.0.1 bind, and rejection
|
|
alongside --no-cloudflare or before a subcommand. Modeled on
|
|
test_studio_cloudflare_flag.py."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from typer.testing import CliRunner
|
|
|
|
_REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
if str(_REPO_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(_REPO_ROOT))
|
|
|
|
|
|
def _studio():
|
|
from unsloth_cli.commands import studio as _studio_mod
|
|
return _studio_mod
|
|
|
|
|
|
_BASE = ["--model", "unsloth/Qwen3-1.7B-GGUF"]
|
|
|
|
|
|
# ── option registration ──────────────────────────────────────────────
|
|
|
|
|
|
def test_run_exposes_secure_option_default_off():
|
|
import inspect
|
|
|
|
opt = inspect.signature(_studio().run).parameters["secure"].default
|
|
decls = set(getattr(opt, "param_decls", []) or [])
|
|
assert "--secure/--not-secure" in decls
|
|
assert getattr(opt, "default", None) is False
|
|
|
|
|
|
def test_studio_default_exposes_secure_option_default_off():
|
|
import inspect
|
|
|
|
opt = inspect.signature(_studio().studio_default).parameters["secure"].default
|
|
decls = set(getattr(opt, "param_decls", []) or [])
|
|
assert "--secure/--not-secure" in decls
|
|
assert getattr(opt, "default", None) is False
|
|
|
|
|
|
# ── re-exec capture plumbing (mirrors test_studio_cloudflare_flag.py) ─
|
|
|
|
|
|
class _ExecCaptured(SystemExit):
|
|
def __init__(self, argv):
|
|
super().__init__(0)
|
|
self.argv = list(argv)
|
|
|
|
|
|
def _install_run_reexec_capture(monkeypatch):
|
|
studio_mod = _studio()
|
|
captured = []
|
|
monkeypatch.setattr(sys, "prefix", "/nonexistent/outer/venv")
|
|
fake_venv = Path("/fake/studio/venv/unsloth_studio")
|
|
monkeypatch.setattr(studio_mod, "_studio_venv_python", lambda: fake_venv / "bin" / "python")
|
|
fake_bin = fake_venv / "bin" / "unsloth"
|
|
real_is_file = Path.is_file
|
|
monkeypatch.setattr(
|
|
Path,
|
|
"is_file",
|
|
lambda self: True if str(self) == str(fake_bin) else real_is_file(self),
|
|
)
|
|
from unsloth_cli import _tool_policy as _tp_mod
|
|
|
|
monkeypatch.setattr(
|
|
_tp_mod,
|
|
"resolve_tool_policy",
|
|
lambda host, flag, yes, silent: False if flag is None else bool(flag),
|
|
)
|
|
monkeypatch.setattr(sys, "platform", "linux")
|
|
|
|
def fake_execvp(file, argv):
|
|
captured.append(list(argv))
|
|
raise _ExecCaptured(argv)
|
|
|
|
monkeypatch.setattr(studio_mod.os, "execvp", fake_execvp)
|
|
return captured
|
|
|
|
|
|
def _invoke_run(monkeypatch, args):
|
|
import typer as _typer
|
|
|
|
captured = _install_run_reexec_capture(monkeypatch)
|
|
app = _typer.Typer()
|
|
app.command(
|
|
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
|
|
)(_studio().run)
|
|
CliRunner().invoke(app, args, catch_exceptions = True)
|
|
return captured
|
|
|
|
|
|
def _invoke_studio_default(monkeypatch, args):
|
|
import typer as _typer
|
|
|
|
studio_mod = _studio()
|
|
captured = []
|
|
monkeypatch.setattr(sys, "prefix", "/nonexistent/outer/venv")
|
|
monkeypatch.setattr(studio_mod, "_ensure_studio_env_exported", lambda: None)
|
|
fake_venv = Path("/fake/studio/venv/unsloth_studio")
|
|
monkeypatch.setattr(studio_mod, "_studio_venv_python", lambda: fake_venv / "bin" / "python")
|
|
monkeypatch.setattr(studio_mod, "_find_run_py", lambda: Path("/fake/studio/run.py"))
|
|
monkeypatch.setattr(studio_mod, "_find_frontend_dist", lambda: None)
|
|
monkeypatch.setattr(sys, "platform", "linux")
|
|
|
|
def fake_execvp(file, argv):
|
|
captured.append(list(argv))
|
|
raise _ExecCaptured(argv)
|
|
|
|
monkeypatch.setattr(studio_mod.os, "execvp", fake_execvp)
|
|
app = _typer.Typer()
|
|
app.command()(studio_mod.studio_default)
|
|
CliRunner().invoke(app, args, catch_exceptions = True)
|
|
return captured
|
|
|
|
|
|
# ── re-exec forwarding ────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"user_flag,expected,unexpected",
|
|
[
|
|
(None, "--not-secure", "--secure"), # default off
|
|
("--secure", "--secure", "--not-secure"),
|
|
("--not-secure", "--not-secure", "--secure"),
|
|
],
|
|
)
|
|
def test_run_reexec_forwards_secure_polarity(monkeypatch, user_flag, expected, unexpected):
|
|
extras = [user_flag] if user_flag else []
|
|
captured = _invoke_run(monkeypatch, _BASE + extras)
|
|
assert len(captured) == 1, captured
|
|
argv = captured[0]
|
|
assert expected in argv and unexpected not in argv, argv
|
|
|
|
|
|
def test_run_secure_forces_localhost_in_reexec(monkeypatch):
|
|
# `unsloth studio run -H 0.0.0.0 --secure` must re-exec with --host 127.0.0.1.
|
|
captured = _invoke_run(monkeypatch, _BASE + ["-H", "0.0.0.0", "--secure"])
|
|
assert len(captured) == 1, captured
|
|
argv = captured[0]
|
|
assert "--secure" in argv
|
|
assert argv[argv.index("--host") + 1] == "127.0.0.1", argv
|
|
|
|
|
|
def test_studio_default_reexec_forwards_secure(monkeypatch):
|
|
captured = _invoke_studio_default(monkeypatch, ["-H", "0.0.0.0", "--secure"])
|
|
assert len(captured) == 1, captured
|
|
argv = captured[0]
|
|
assert "--secure" in argv
|
|
# studio_default also forces the loopback bind under --secure.
|
|
assert argv[argv.index("--host") + 1] == "127.0.0.1", argv
|
|
|
|
|
|
# ── in-venv path forwards secure + forced host into run_server ────────
|
|
|
|
|
|
class _RunServerCaptured(SystemExit):
|
|
def __init__(self, kwargs):
|
|
super().__init__(0)
|
|
self.kwargs = dict(kwargs)
|
|
|
|
|
|
def test_run_in_venv_passes_secure_and_forces_host(monkeypatch):
|
|
import types
|
|
|
|
studio_mod = _studio()
|
|
fake_venv = Path("/fake/studio/venv/unsloth_studio")
|
|
monkeypatch.setattr(sys, "prefix", str(fake_venv))
|
|
monkeypatch.setattr(studio_mod, "STUDIO_HOME", fake_venv.parent)
|
|
|
|
from unsloth_cli import _tool_policy as _tp_mod
|
|
|
|
monkeypatch.setattr(
|
|
_tp_mod,
|
|
"resolve_tool_policy",
|
|
lambda host, flag, yes, silent: False if flag is None else bool(flag),
|
|
)
|
|
|
|
captured: dict = {}
|
|
|
|
def fake_run_server(**kwargs):
|
|
captured.update(kwargs)
|
|
raise _RunServerCaptured(kwargs)
|
|
|
|
fake_backend_run = sys.modules.setdefault(
|
|
"studio.backend.run", types.ModuleType("studio.backend.run")
|
|
)
|
|
fake_backend_run.run_server = fake_run_server
|
|
fake_backend_run._resolve_external_ip = lambda: "127.0.0.1"
|
|
monkeypatch.setattr(studio_mod, "_RUN_MODULE", fake_backend_run)
|
|
|
|
import typer as _typer
|
|
|
|
app = _typer.Typer()
|
|
app.command(
|
|
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
|
|
)(studio_mod.run)
|
|
CliRunner().invoke(app, _BASE + ["-H", "0.0.0.0", "--secure"], catch_exceptions = True)
|
|
|
|
assert captured.get("secure") is True, captured
|
|
assert captured.get("host") == "127.0.0.1", captured
|
|
|
|
|
|
# ── --secure + --no-cloudflare is rejected ───────────────────────────
|
|
|
|
|
|
def test_run_secure_rejects_no_cloudflare(monkeypatch):
|
|
studio_mod = _studio()
|
|
import typer as _typer
|
|
|
|
app = _typer.Typer()
|
|
app.command(
|
|
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
|
|
)(studio_mod.run)
|
|
result = CliRunner().invoke(app, _BASE + ["--secure", "--no-cloudflare"])
|
|
assert result.exit_code == 2, result.output
|
|
|
|
|
|
def test_studio_default_rejects_secure_with_subcommand():
|
|
import typer as _typer
|
|
|
|
studio_mod = _studio()
|
|
app = _typer.Typer()
|
|
app.add_typer(studio_mod.studio_app, name = "studio")
|
|
result = CliRunner().invoke(app, ["studio", "--secure", "run", "--model", "X"])
|
|
assert result.exit_code == 2, result.output
|
|
combined = (result.output or "") + (getattr(result, "stderr", "") or "")
|
|
assert "--secure" in combined, combined
|
|
|
|
|
|
# ── secure resolves tools against the loopback bind (tools stay ON) ──
|
|
|
|
|
|
def test_run_secure_resolves_tools_against_loopback(monkeypatch):
|
|
# --secure is a loopback bind behind an authenticated tunnel, so tools resolve
|
|
# against 127.0.0.1 (ON): the child gets --enable-tools, not --disable-tools.
|
|
studio_mod = _studio()
|
|
monkeypatch.setattr(sys, "prefix", "/nonexistent/outer/venv")
|
|
fake_venv = Path("/fake/studio/venv/unsloth_studio")
|
|
monkeypatch.setattr(studio_mod, "_studio_venv_python", lambda: fake_venv / "bin" / "python")
|
|
fake_bin = fake_venv / "bin" / "unsloth"
|
|
real_is_file = Path.is_file
|
|
monkeypatch.setattr(
|
|
Path,
|
|
"is_file",
|
|
lambda self: True if str(self) == str(fake_bin) else real_is_file(self),
|
|
)
|
|
monkeypatch.setattr(sys, "platform", "linux")
|
|
|
|
from unsloth_cli import _tool_policy as _tp_mod
|
|
|
|
calls = []
|
|
|
|
def rec(host, flag, yes, silent):
|
|
calls.append(host)
|
|
return True if flag is None else bool(flag) # default ON everywhere
|
|
|
|
monkeypatch.setattr(_tp_mod, "resolve_tool_policy", rec)
|
|
|
|
captured = []
|
|
|
|
def fake_execvp(file, argv):
|
|
captured.append(list(argv))
|
|
raise _ExecCaptured(argv)
|
|
|
|
monkeypatch.setattr(studio_mod.os, "execvp", fake_execvp)
|
|
|
|
import typer as _typer
|
|
|
|
app = _typer.Typer()
|
|
app.command(
|
|
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
|
|
)(studio_mod.run)
|
|
CliRunner().invoke(app, _BASE + ["-H", "0.0.0.0", "--secure"], catch_exceptions = True)
|
|
|
|
# Resolved against the forced-loopback bind, not the public 0.0.0.0 exposure.
|
|
assert calls and calls[0] == "127.0.0.1", calls
|
|
assert len(captured) == 1, captured
|
|
assert "--enable-tools" in captured[0] and "--disable-tools" not in captured[0], captured[0]
|
|
|
|
|
|
def test_run_secure_enable_tools_no_auto_yes(monkeypatch):
|
|
# No prompt now, so a secure --enable-tools forwards --enable-tools but not
|
|
# --yes (only an explicit --yes is forwarded).
|
|
captured = _invoke_run(monkeypatch, _BASE + ["-H", "0.0.0.0", "--secure", "--enable-tools"])
|
|
assert len(captured) == 1, captured
|
|
argv = captured[0]
|
|
assert "--enable-tools" in argv, argv
|
|
assert "--yes" not in argv, argv
|
|
|
|
|
|
# ── plain `unsloth studio` exposes + forwards --enable-tools/--disable-tools ──
|
|
|
|
|
|
def test_studio_default_exposes_enable_tools_option_default_none():
|
|
import inspect
|
|
|
|
opt = inspect.signature(_studio().studio_default).parameters["enable_tools"].default
|
|
decls = set(getattr(opt, "param_decls", []) or [])
|
|
assert "--enable-tools/--disable-tools" in decls
|
|
assert opt.default is None # tri-state: omitted -> leave policy unset (tools on)
|
|
|
|
|
|
def test_studio_default_forwards_disable_tools(monkeypatch):
|
|
captured = _invoke_studio_default(monkeypatch, ["--disable-tools"])
|
|
assert len(captured) == 1, captured
|
|
assert "--disable-tools" in captured[0] and "--enable-tools" not in captured[0], captured[0]
|
|
|
|
|
|
def test_studio_default_forwards_enable_tools(monkeypatch):
|
|
captured = _invoke_studio_default(monkeypatch, ["--enable-tools"])
|
|
assert len(captured) == 1, captured
|
|
assert "--enable-tools" in captured[0] and "--disable-tools" not in captured[0], captured[0]
|
|
|
|
|
|
def test_studio_default_no_tool_flag_omits_both(monkeypatch):
|
|
# No flag -> neither flag forwarded; run.py leaves the policy unset (tools on).
|
|
captured = _invoke_studio_default(monkeypatch, [])
|
|
assert len(captured) == 1, captured
|
|
assert "--enable-tools" not in captured[0] and "--disable-tools" not in captured[0], captured[0]
|
|
|
|
|
|
def test_studio_default_rejects_enable_tools_with_subcommand():
|
|
import typer as _typer
|
|
|
|
studio_mod = _studio()
|
|
app = _typer.Typer()
|
|
app.add_typer(studio_mod.studio_app, name = "studio")
|
|
result = CliRunner().invoke(app, ["studio", "--enable-tools", "run", "--model", "X"])
|
|
assert result.exit_code == 2, result.output
|
|
combined = (result.output or "") + (getattr(result, "stderr", "") or "")
|
|
assert "--enable-tools" in combined, combined
|
|
|
|
|
|
def test_run_tool_help_reflects_default_on_everywhere():
|
|
# Help must match the new policy (tools on everywhere, no prompt).
|
|
import inspect
|
|
|
|
params = inspect.signature(_studio().run).parameters
|
|
tools_help = params["enable_tools"].default.help or ""
|
|
assert "on for every bind" in tools_help, tools_help
|
|
assert "0.0.0.0" not in tools_help, tools_help
|
|
|
|
yes_help = params["yes"].default.help or ""
|
|
assert "Skip the 0.0.0.0" not in yes_help, yes_help
|
|
assert "no longer prompts" in yes_help, yes_help
|