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: login limiter and keyless access behind a reverse proxy (#14459)
Daniel Han71099f2e8207
diff --git a/studio/backend/routes/auth.py b/studio/backend/routes/auth.pyindex 1f989f80f8e..e1616b1f40a 100644--- a/studio/backend/routes/auth.py+++ b/studio/backend/routes/auth.py@@ -280,16 +280,16 @@ def _client_ip(request: Request | None) -> str:if request is None:return "_unknown"if _trust_forwarded_for():+ # Rightmost entry only: the trusted proxy appends the client it saw, and everything left of+ # it is the caller's own text, so keying on it would let each guess pick a fresh bucket.xff = request.headers.get("x-forwarded-for", "")if xff:- # First entry is the originating client.- normalized = _normalize_forwarded_addr(xff.split(",", 1)[0])+ normalized = _normalize_forwarded_addr(xff.rsplit(",", 1)[-1])if normalized:return normalizedfwd = request.headers.get("forwarded", "")if fwd:- # First element only; multi-element headers can't fork buckets.- normalized = _forwarded_for_from_element(fwd.split(",", 1)[0])+ normalized = _forwarded_for_from_element(fwd.rsplit(",", 1)[-1])if normalized:return normalizedreturn (request.client.host if request.client else None) or "_unknown"diff --git a/studio/backend/tests/test_keyless_loopback_proxy.py b/studio/backend/tests/test_keyless_loopback_proxy.pynew file mode 100644index 00000000000..ad04cdc297b--- /dev/null+++ b/studio/backend/tests/test_keyless_loopback_proxy.py@@ -0,0 +1,61 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A same-host reverse proxy reaches the backend from loopback. When it marks the request as relayed,+keyless `full` and `inference` must not read that as the local browser. With nginx's default `proxy_pass` it+names nobody and Host is the upstream address, which this check cannot tell from a direct client."""++import types++import pytest+from starlette.requests import Request++from utils import keyless_api_access as kl+++def _request(+ peer,+ host,+ headers = (),+):+ raw = [(b"host", host.encode())] + [(k.encode(), v.encode()) for k, v in headers]+ state = types.SimpleNamespace(bind_host = "127.0.0.1", secure = False, cloudflare_url = None)+ return Request(+ {+ "type": "http",+ "method": "POST",+ "path": "/api/accounts",+ "root_path": "",+ "headers": raw,+ "client": (peer, 50000),+ "server": ("127.0.0.1", 8888),+ "scheme": "http",+ "query_string": b"",+ "app": types.SimpleNamespace(state = state),+ }+ )+++_RELAY_HEADERS = [+ ("x-forwarded-for", "203.0.113.9"),+ ("x-real-ip", "203.0.113.9"),+ ("forwarded", "for=203.0.113.9"),+ ("x-forwarded-host", "studio.example.com"),+ ("via", "1.1 proxy"),+ ("x-forwarded-proto", "https"),+ ("x-forwarded-server", "proxy.example.com"),+]++[email protected]("scope", ["full", "inference"])[email protected]("header", _RELAY_HEADERS)+def test_keyless_refuses_a_loopback_request_relayed_by_a_proxy(header, scope):+ assert (+ kl.keyless_transport_allowed(_request("127.0.0.1", "127.0.0.1:8888", [header]), scope)+ is False+ )++[email protected]("scope", ["full", "inference"])+def test_keyless_still_serves_a_direct_loopback_client(scope):+ assert kl.keyless_transport_allowed(_request("127.0.0.1", "127.0.0.1:8888"), scope) is Truediff --git a/studio/backend/tests/test_login_forwarded_client_ip.py b/studio/backend/tests/test_login_forwarded_client_ip.pynew file mode 100644index 00000000000..69aecb7dc98--- /dev/null+++ b/studio/backend/tests/test_login_forwarded_client_ip.py@@ -0,0 +1,68 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Behind a trusted proxy the login limiter keys on the hop the proxy appended. The leftmost+X-Forwarded-For entry is whatever the client sent, so keying on it gives every guess a fresh bucket."""++import asyncio++import pytest+from fastapi import HTTPException+from starlette.datastructures import Headers++from routes import auth as auth_routes++[email protected](autouse = True)+def _clean_limiter(monkeypatch):+ for table in (auth_routes._LOGIN_BUCKETS, auth_routes._LOGIN_IP_BUCKETS):+ table.clear()+ for shard in auth_routes._LOGIN_IP_OVERFLOW:+ shard.clear()+ monkeypatch.setenv("UNSLOTH_STUDIO_TRUST_FORWARDED", "1")+ yield+ for table in (auth_routes._LOGIN_BUCKETS, auth_routes._LOGIN_IP_BUCKETS):+ table.clear()+ for shard in auth_routes._LOGIN_IP_OVERFLOW:+ shard.clear()+++class _Request:+ def __init__(+ self,+ peer,+ headers = None,+ ):+ self.client = type("Client", (), {"host": peer})()+ self.headers = Headers(headers or {})+++def test_a_spoofed_leftmost_forwarded_for_does_not_pick_a_fresh_bucket(monkeypatch):+ from models.auth import AuthLoginRequest++ monkeypatch.setattr(auth_routes.policy, "installation_is_multi_user", lambda: False)+ monkeypatch.setattr(auth_routes.storage, "get_user_and_secret", lambda username: None)+ monkeypatch.setattr(auth_routes.hashing, "equalize_login_work", lambda password: None)+ statuses = []+ for guess in range(auth_routes._LOGIN_MAX_FAILS + 3):+ # The proxy appends the real client; everything left of it is the caller's own text.+ request = _Request("127.0.0.1", {"x-forwarded-for": f"10.9.{guess}.1, 198.51.100.7"})+ with pytest.raises(HTTPException) as exc:+ asyncio.run(+ auth_routes.login(+ AuthLoginRequest(username = "nobody", password = "wrong-password"), request+ )+ )+ statuses.append(exc.value.status_code)+ assert statuses[: auth_routes._LOGIN_MAX_FAILS] == [401] * auth_routes._LOGIN_MAX_FAILS+ assert statuses[-1] == 429+++def test_forwarded_header_takes_the_proxy_appended_element():+ request = _Request("127.0.0.1", {"forwarded": "for=10.9.9.9, for=198.51.100.42;proto=https"})+ assert auth_routes._client_ip(request) == "198.51.100.42"+++def test_a_single_proxy_hop_still_names_the_client():+ request = _Request("127.0.0.1", {"x-forwarded-for": "198.51.100.7"})+ assert auth_routes._client_ip(request) == "198.51.100.7"diff --git a/studio/backend/tests/test_login_rate_limit.py b/studio/backend/tests/test_login_rate_limit.pyindex 5ccd99af1c8..b30a903f2a7 100644--- a/studio/backend/tests/test_login_rate_limit.py+++ b/studio/backend/tests/test_login_rate_limit.py@@ -78,11 +78,12 @@ class TestClientIp:# Proxy header is spoofable; without the opt-in, trust the direct connection.assert _client_ip(req) == "127.0.0.1"- def test_honours_first_xff_when_trust_on(self, env_trust_proxy):+ def test_honours_rightmost_xff_when_trust_on(self, env_trust_proxy):+ # The trusted proxy appends the real client; the entries left of it are the client's own.from routes.auth import _client_ipreq = _FakeRequest("127.0.0.1",- {"x-forwarded-for": "198.51.100.7, 10.0.0.1"},+ {"x-forwarded-for": "10.0.0.1, 198.51.100.7"},)assert _client_ip(req) == "198.51.100.7"@@ -107,12 +108,12 @@ class TestClientIp:def test_xff_strips_ipv4_port(self, env_trust_proxy):from routes.auth import _client_ip- req = _FakeRequest("127.0.0.1", {"x-forwarded-for": "198.51.100.7:50001, 10.0.0.1"})+ req = _FakeRequest("127.0.0.1", {"x-forwarded-for": "10.0.0.1, 198.51.100.7:50001"})assert _client_ip(req) == "198.51.100.7"def test_xff_strips_bracketed_ipv6_port(self, env_trust_proxy):from routes.auth import _client_ip- req = _FakeRequest("127.0.0.1", {"x-forwarded-for": "[2001:db8::1]:50001, 10.0.0.1"})+ req = _FakeRequest("127.0.0.1", {"x-forwarded-for": "10.0.0.1, [2001:db8::1]:50001"})assert _client_ip(req) == "2001:db8::1"def test_forwarded_strips_ipv4_port(self, env_trust_proxy):@@ -125,13 +126,13 @@ class TestClientIp:req = _FakeRequest("127.0.0.1", {"forwarded": 'for="[2001:db8::1]:50001";proto=https'})assert _client_ip(req) == "2001:db8::1"- def test_forwarded_isolates_first_element(self, env_trust_proxy):- # Pick the first Forwarded element only, else suffix variations create- # attacker-controlled buckets.+ def test_forwarded_isolates_last_element(self, env_trust_proxy):+ # The last Forwarded element is the trusted proxy's; earlier ones are client-controlled+ # and would let a caller pick a fresh bucket per guess.from routes.auth import _client_ipreq = _FakeRequest("127.0.0.1",- {"forwarded": "for=198.51.100.42, for=10.0.0.1;proto=https"},+ {"forwarded": "for=10.0.0.1, for=198.51.100.42;proto=https"},)assert _client_ip(req) == "198.51.100.42"diff --git a/studio/backend/utils/client_ip.py b/studio/backend/utils/client_ip.pyindex 3505eb2c838..4ccdab7d7d4 100644--- a/studio/backend/utils/client_ip.py+++ b/studio/backend/utils/client_ip.py@@ -109,6 +109,18 @@ def _host_header_is_loopback(host_header: str | None) -> bool:return host == "localhost" or _is_loopback_ip(host)+# Hop headers a proxy adds without naming the client (Apache's X-Forwarded-Server, an nginx block that+# only sets X-Forwarded-Proto). Kept apart because Colab's tolerated-header check walks the tuple above.+_PROXY_HOP_HEADERS = ("via", "x-forwarded-port", "x-forwarded-proto", "x-forwarded-server")+++def names_a_forwarded_client(request) -> bool:+ """Whether a proxy or tunnel marked this request as relayed."""+ return any(+ request.headers.get(h) is not None for h in _PROXIED_CLIENT_HEADERS + _PROXY_HOP_HEADERS+ )++def is_direct_local_request(request) -> bool:"""A direct loopback connection to a loopback Host: not relayed by a proxy or tunnel, not DNS rebinding."""client = request.clientdiff --git a/studio/backend/utils/keyless_api_access.py b/studio/backend/utils/keyless_api_access.pyindex 09db469b457..22a97986fe4 100644--- a/studio/backend/utils/keyless_api_access.py+++ b/studio/backend/utils/keyless_api_access.py@@ -380,6 +380,11 @@ def _full_scope_transport_allowed(request: Any, app_state: Any) -> bool:if not request_is_loopback(request):return False+ from utils.client_ip import names_a_forwarded_client++ # The loopback socket is a same-host proxy's when it marks the request as relayed.+ if names_a_forwarded_client(request):+ return Falsebind_host = getattr(app_state, "bind_host", None)scope = getattr(request, "scope", {})server = scope.get("server")@@ -503,7 +508,9 @@ def keyless_transport_allowed(request: Any, scope: str) -> bool:return Trueif _public_tunnel_active(app_state):return False- return request_is_loopback(request)+ from utils.client_ip import names_a_forwarded_client++ return request_is_loopback(request) and not names_a_forwarded_client(request)def keyless_request_allowed(request: Any) -> bool: