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
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Trust order: rightmost X-Forwarded-For only if UNSLOTH_STUDIO_TRUST_FORWARDED (leftmost is
spoofable; enable only behind an appending proxy); CF-Connecting-IP only via loopback tunnel;
relayed:<peer>; socket peer"""
from __future__ import annotations
import ipaddress
import os
import re
from urllib.parse import urlparse
_TRUST_FORWARDED_ENV = "UNSLOTH_STUDIO_TRUST_FORWARDED"
def _trust_forwarded_for() -> bool:
return os.environ.get(_TRUST_FORWARDED_ENV, "").strip().lower() in {"1", "true", "yes"}
def trust_forwarded_headers() -> bool:
return _trust_forwarded_for()
def _is_loopback(host: str | None) -> bool:
try:
return bool(host) and ipaddress.ip_address(host).is_loopback
except ValueError:
return False
def _normalize_addr(value: str | None) -> str | None:
"""Parse an ``X-Forwarded-For`` entry into a bare, validated IP (strip port/brackets)."""
raw = (value or "").strip().strip('"')
if not raw:
return None
if raw.startswith("["):
raw = raw[1:].split("]", 1)[0]
elif raw.count(":") == 1:
raw = raw.split(":", 1)[0]
try:
return ipaddress.ip_address(raw).compressed
except ValueError:
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:
return "_unknown"
peer = request.client.host if request.client else None
if _trust_forwarded_for():
# Rightmost hop = what the trusted proxy saw; the leftmost is spoofable.
xff = header_fields(request.headers, "x-forwarded-for")
if xff:
normalized = _normalize_addr(xff.rsplit(",", 1)[-1])
if normalized:
return normalized
if _is_loopback(peer):
if request_from_managed_tunnel(
getattr(request, "scope", None), _request_app_state(request)
):
cf = _normalize_addr(request.headers.get("cf-connecting-ip"))
if cf:
return cf
if names_a_forwarded_client(request):
return f"relayed:{peer}"
return peer or "_unknown"
def _request_app_state(request):
try:
return request.app.state
except Exception:
return None
def _managed_tunnel_hostname(app_state) -> str | None:
url = getattr(app_state, "cloudflare_url", None) if app_state is not None else None
if not isinstance(url, str) or not url:
return None
try:
host = urlparse(url).hostname
except ValueError:
return None
return host.lower().rstrip(".") if host else None
def request_from_managed_tunnel(scope, app_state) -> bool:
"""Live published tunnel, one valid ``CF-Connecting-IP``, Host naming it. Caller checks the peer is loopback."""
expected = _managed_tunnel_hostname(app_state)
if not expected or not isinstance(scope, dict):
return False
cf_values = []
host_values = []
for name, value in scope.get("headers") or ():
if name == b"cf-connecting-ip":
cf_values.append(value)
elif name == b"host":
host_values.append(value)
if len(cf_values) != 1 or len(host_values) != 1:
return False
try:
if _normalize_addr(cf_values[0].decode("latin-1")) is None:
return False
request_host = urlparse("//" + host_values[0].decode("latin-1")).hostname
except (UnicodeDecodeError, ValueError):
return False
return bool(request_host) and request_host.lower().rstrip(".") == expected
def _is_loopback_ip(host: str | None) -> bool:
if not host or "%" in host: # a scope id (::1%eth0) is never a plain loopback
return False
try:
ip = ipaddress.ip_address(host)
except (TypeError, ValueError):
return False
mapped = getattr(ip, "ipv4_mapped", None)
return ip.is_loopback or (mapped is not None and mapped.is_loopback)
# A loopback peer with these headers is a proxy relaying a remote client.
_PROXIED_CLIENT_HEADERS = (
"cf-connecting-ip",
"forwarded",
"x-forwarded-for",
"x-forwarded-host",
"x-real-ip",
)
def _host_header_is_loopback(host_header: str | None) -> bool:
"""Raw Host header, so a bad Host cannot fall back to the (loopback) ASGI server address."""
if not host_header:
return False
host = host_header.strip()
if host.startswith("["): # [IPv6] or [IPv6]:port
end = host.find("]")
if end == -1 or (host[end + 1 :] and not host[end + 1 :].startswith(":")):
return False # unclosed bracket or junk after ] (e.g. [::1]evil)
host = host[1:end]
elif host.count(":") == 1: # host:port
host = host.split(":", 1)[0]
host = host.lower().rstrip(".")
return host == "localhost" or _is_loopback_ip(host)
_PROXY_HOSTS_ENV = "UNSLOTH_STUDIO_PROXY_HOSTS"
_DNS_NAME = re.compile(r"[a-z0-9-]+(\.[a-z0-9-]+)*")
def _authority_hostname(value: str | None) -> str | None:
host = (value or "").strip().lower()
if "://" in host: # tolerate a pasted URL in the setting
host = host.split("://", 1)[1].split("/", 1)[0]
if host.count(":") == 1:
host, port = host.split(":", 1)
if not port.isdigit():
return None
host = host.rstrip(".")
# Names only: a wildcard would admit any rebound name.
if host == "localhost" or not _DNS_NAME.fullmatch(host):
return None
try:
ipaddress.ip_address(host)
except ValueError:
return host
return None
def trusted_proxy_hosts() -> frozenset[str]:
raw = os.environ.get(_PROXY_HOSTS_ENV, "")
names = (_authority_hostname(entry) for entry in raw.replace(",", " ").split())
return frozenset(name for name in names if name)
def request_via_trusted_proxy(scope) -> bool:
"""Local reverse proxy for an allowlisted name. Host must be loopback or allowlisted: a DNS-rebound page
sends its own name as Host, so it cannot borrow the allowlist via X-Forwarded-Host."""
allowed = trusted_proxy_hosts()
if not allowed or not isinstance(scope, dict):
return False
server = scope.get("server")
client = scope.get("client")
if not server or not client or not _is_loopback_ip(server[0]):
return False
# UNSLOTH_STUDIO_TRUST_FORWARDED makes uvicorn replace the loopback peer with X-Forwarded-For.
if not _is_loopback_ip(client[0]) and not trust_forwarded_headers():
return False
host_values = []
forwarded_hosts = []
for name, value in scope.get("headers") or ():
if name == b"host":
host_values.append(value.decode("latin-1"))
elif name == b"x-forwarded-host":
forwarded_hosts.extend(p for p in value.decode("latin-1").split(",") if p.strip())
if len(host_values) != 1:
return False
host_allowed = _authority_hostname(host_values[0]) in allowed
if not host_allowed and not _host_header_is_loopback(host_values[0]):
return False
if any(_authority_hostname(part) not in allowed for part in forwarded_hosts):
return False
return host_allowed or bool(forwarded_hosts)
# 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")
_RELAY_HEADERS = frozenset(_PROXIED_CLIENT_HEADERS + _PROXY_HOP_HEADERS)
def names_a_forwarded_client(request) -> bool:
"""Whether a proxy or tunnel marked this request as relayed."""
return not _RELAY_HEADERS.isdisjoint(k.lower() for k in request.headers.keys())
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.client
if client is None or not _is_loopback_ip(client.host):
return False
if names_a_forwarded_client(request):
return False
return _host_header_is_loopback(request.headers.get("host"))