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.

Studio: say only localhost loses keyless access while a tunnel is up (#9690)

Daniel Han7b9bb03957f1
diff --git a/studio/backend/tests/test_keyless_tunnel_exposure.py b/studio/backend/tests/test_keyless_tunnel_exposure.py
new file mode 100644
index 00000000000..7bca875a00b
--- /dev/null
+++ b/studio/backend/tests/test_keyless_tunnel_exposure.py
@@ -0,0 +1,123 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Why a tunnel on a wildcard bind closes loopback but not the LAN listener.
+
+The managed tunnel targets loopback on a wildcard bind, so tunnelled traffic is
+indistinguishable from a real local client there and never presents a LAN address.
+Closing LAN too would cost legitimate LAN clients their access and still miss the case
+it looks like it covers: an externally run cloudflared or ngrok sets neither
+``app_state.cloudflare_url`` nor ``_remote_connector_active``, so Studio never sees it.
+An explicit interface bind can be the tunnel origin instead; these tests cover the
+wildcard bind and its separate private LAN listener.
+"""
+
+from __future__ import annotations
+
+import secrets
+from types import SimpleNamespace
+
+import pytest
+from starlette.requests import Request
+
+from auth import storage
+from utils import host_policy
+from utils.keyless_api_access import (
+ _reset_scope_cache,
+ keyless_request_allowed,
+ set_keyless_api_access,
+)
+
+
[email protected](autouse = True)
+def isolated_auth_db(tmp_path, monkeypatch):
+ monkeypatch.setattr(storage, "DB_PATH", tmp_path / "auth.db")
+ monkeypatch.setattr(storage, "_BOOTSTRAP_PW_PATH", tmp_path / ".bootstrap_password")
+ monkeypatch.setattr(storage, "_bootstrap_password", None)
+ monkeypatch.setattr(storage, "_api_key_pbkdf2_salt_cache", None)
+ storage._reset_api_key_hash_cache()
+ _reset_scope_cache()
+ monkeypatch.setattr(host_policy, "_remote_connector_active", False, raising = False)
+ monkeypatch.setattr(host_policy, "_lan_connector_active", True, raising = False)
+ storage.create_initial_user(
+ username = storage.DEFAULT_ADMIN_USERNAME,
+ password = "human-password-123",
+ jwt_secret = secrets.token_urlsafe(64),
+ )
+ yield
+ storage._reset_api_key_hash_cache()
+ _reset_scope_cache()
+
+
[email protected](autouse = True)
+def live_lan_listener(monkeypatch):
+ import lan_access
+ monkeypatch.setattr(
+ lan_access,
+ "lan_listener_status",
+ lambda: {"running": True, "port": 8888, "addresses": ["192.168.1.24"], "error": None},
+ )
+
+
+LAN = {"server": ("192.168.1.24", 8888), "client": ("192.168.1.90", 51000)}
+LOOPBACK = {"server": ("127.0.0.1", 8000), "client": ("127.0.0.1", 51000)}
+
+
+def request_for(endpoints, *, cloudflare_url = None):
+ state = SimpleNamespace(
+ bind_host = "0.0.0.0",
+ secure = False,
+ remote_access_is_colab = False,
+ lan_access_is_colab = False,
+ lan_access_secure_launch = False,
+ cloudflare_url = cloudflare_url,
+ )
+ return Request(
+ {
+ "type": "http",
+ "method": "POST",
+ "path": "/v1/chat/completions",
+ "root_path": "",
+ "query_string": b"",
+ "scheme": "http",
+ "headers": [],
+ "server": endpoints["server"],
+ "client": endpoints["client"],
+ "app": SimpleNamespace(state = state),
+ }
+ )
+
+
+def test_a_published_tunnel_closes_loopback_and_leaves_the_lan_listener_alone(monkeypatch):
+ set_keyless_api_access("inference")
+ assert keyless_request_allowed(request_for(LOOPBACK)) is True
+ assert keyless_request_allowed(request_for(LAN)) is True
+
+ for signal in ("cloudflare_url", "connector"):
+ if signal == "cloudflare_url":
+ loopback = request_for(LOOPBACK, cloudflare_url = "https://demo.trycloudflare.com")
+ lan = request_for(LAN, cloudflare_url = "https://demo.trycloudflare.com")
+ else:
+ monkeypatch.setattr(host_policy, "_remote_connector_active", True, raising = False)
+ loopback, lan = request_for(LOOPBACK), request_for(LAN)
+ # loopback is ambiguous while a tunnel terminates on it
+ assert keyless_request_allowed(loopback) is False, signal
+ # on a wildcard bind the managed tunnel does not arrive with a LAN address
+ assert keyless_request_allowed(lan) is True, signal
+ monkeypatch.setattr(host_policy, "_remote_connector_active", False, raising = False)
+
+
+def test_full_scope_ignores_the_lan_listener_whatever_the_tunnel_state():
+ """`full` is loopback-only, so the asymmetry above cannot widen it."""
+ set_keyless_api_access("full")
+ assert keyless_request_allowed(request_for(LAN)) is False
+ assert (
+ keyless_request_allowed(request_for(LAN, cloudflare_url = "https://demo.trycloudflare.com"))
+ is False
+ )
+ assert (
+ keyless_request_allowed(
+ request_for(LOOPBACK, cloudflare_url = "https://demo.trycloudflare.com")
+ )
+ is False
+ )
diff --git a/studio/frontend/src/features/settings/components/keyless-api-access-section.tsx b/studio/frontend/src/features/settings/components/keyless-api-access-section.tsx
index 86a4f76161a..00400f5d65f 100644
--- a/studio/frontend/src/features/settings/components/keyless-api-access-section.tsx
+++ b/studio/frontend/src/features/settings/components/keyless-api-access-section.tsx
@@ -30,7 +30,7 @@ function activeExposureWarning(
exposure: KeylessApiAccessSettings["exposure"],
): string {
if (exposure === "public_url") {
- return " A public URL or tunnel is active, so keyless access remains disabled, including on localhost, until it is stopped.";
+ return " A public URL or tunnel is active, so keyless access is disabled on localhost until it is stopped.";
}
if (exposure === "colab") {
return " This Colab runtime cannot receive keyless access.";
diff --git a/tests/studio/test_usage_examples_model_source_contract.py b/tests/studio/test_usage_examples_model_source_contract.py
index ec21c56164d..b19358cf5a2 100644
--- a/tests/studio/test_usage_examples_model_source_contract.py
+++ b/tests/studio/test_usage_examples_model_source_contract.py
@@ -233,5 +233,5 @@ def test_keyless_examples_match_transport_tool_and_full_scope_policy():
section = KEYLESS_SECTION_TSX.read_text(encoding = "utf-8")
assert "[cloudflareUrl, onSettingsChange]" in section
assert "delete" in section[section.find(" full: {") : section.find(" tools: {")]
- assert "including on localhost" in section
+ assert "disabled on localhost" in section
assert "read the files and settings in Unsloth" not in section