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: read every forwarded header field behind a trusted proxy (#14534)
Daniel Hane9cf337cc870
diff --git a/studio/backend/routes/auth.py b/studio/backend/routes/auth.pyindex e1616b1f40a..85bd6db0be5 100644--- a/studio/backend/routes/auth.py+++ b/studio/backend/routes/auth.py@@ -282,12 +282,14 @@ def _client_ip(request: Request | None) -> str: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", "")+ from utils.client_ip import header_fields++ xff = header_fields(request.headers, "x-forwarded-for")if xff:normalized = _normalize_forwarded_addr(xff.rsplit(",", 1)[-1])if normalized:return normalized- fwd = request.headers.get("forwarded", "")+ fwd = header_fields(request.headers, "forwarded")if fwd:normalized = _forwarded_for_from_element(fwd.rsplit(",", 1)[-1])if normalized:diff --git a/studio/backend/tests/test_login_forwarded_client_ip.py b/studio/backend/tests/test_login_forwarded_client_ip.pyindex 69aecb7dc98..67e57799691 100644--- a/studio/backend/tests/test_login_forwarded_client_ip.py+++ b/studio/backend/tests/test_login_forwarded_client_ip.py@@ -66,3 +66,25 @@ def test_forwarded_header_takes_the_proxy_appended_element():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"++[email protected]("name", [b"x-forwarded-for", b"forwarded"])+def test_a_proxy_appended_header_field_is_the_one_keyed_on(name):+ def hop(addr):+ return addr.encode() if name == b"x-forwarded-for" else b"for=" + addr.encode()++ request = _Request("10.0.0.5")+ for spoofed in ("198.51.100.1", "198.51.100.2"):+ request.headers = Headers(raw = [(name, hop(spoofed)), (name, hop("203.0.113.9"))])+ assert auth_routes._client_ip(request) == "203.0.113.9"+++def test_shared_client_ip_reads_a_proxy_appended_header_field(monkeypatch):+ from utils import client_ip as client_ip_mod++ monkeypatch.setattr(client_ip_mod, "_trust_forwarded_for", lambda: True)+ request = _Request("10.0.0.5")+ request.headers = Headers(+ raw = [(b"x-forwarded-for", b"198.51.100.1"), (b"x-forwarded-for", b"203.0.113.9")]+ )+ assert client_ip_mod.client_ip(request) == "203.0.113.9"diff --git a/studio/backend/utils/client_ip.py b/studio/backend/utils/client_ip.pyindex 4ccdab7d7d4..fce297cb705 100644--- a/studio/backend/utils/client_ip.py+++ b/studio/backend/utils/client_ip.py@@ -52,6 +52,14 @@ def _normalize_addr(value: str | None) -> str | None:return None+def header_fields(headers, name: str) -> str:+ """All fields of a repeated header, comma-joined (``get`` returns only the first; HAProxy appends a field)."""+ getlist = getattr(headers, "getlist", None)+ if getlist is not None:+ return ",".join(getlist(name))+ return headers.get(name, "") or ""++def client_ip(request) -> str:"""Best-effort client IP, or ``"_unknown"`` when it can't be determined."""if request is None:@@ -59,7 +67,7 @@ def client_ip(request) -> str:peer = request.client.host if request.client else Noneif _trust_forwarded_for():# Rightmost hop = what the trusted proxy saw; the leftmost is spoofable.- xff = request.headers.get("x-forwarded-for", "")+ xff = header_fields(request.headers, "x-forwarded-for")if xff:normalized = _normalize_addr(xff.rsplit(",", 1)[-1])if normalized: