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 browser: mark the panel's fetches to unsloth.ai so its Cloudflare can skip the bot challenge (#12834)

Daniel Han4e309219bb0f
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 9e92c7ce991..82d5d322121 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -15797,9 +15797,13 @@ def _fetch_url_raw(
raw_bytes_max: int | None = None,
post_data: bytes | None = None,
meta_out: dict | None = None,
+ host_headers = None,
) -> tuple[str | None, "str | bytes", str]:
"""Fetch a URL with SSRF protection; return ``(error, body_text, content_type)``.
+ ``host_headers(host)`` adds headers for one hop, chosen by the host that hop goes to, so a
+ redirect to another site does not carry them.
+
``post_data`` sends a urlencoded POST (kept on 307/308, dropped on other redirects).
``meta_out`` receives the final ``url``, ``charset`` and ``filename`` (Content-Disposition) of a
successful binary-mode fetch, and
@@ -15893,6 +15897,8 @@ def _fetch_url_raw(
}
if extra_headers:
headers.update(extra_headers)
+ if host_headers is not None:
+ headers.update(host_headers(current_host))
if pending_post is not None:
headers.setdefault("Content-Type", "application/x-www-form-urlencoded")
req = urllib.request.Request(request_url, headers = headers, data = pending_post)
diff --git a/studio/backend/routes/browser.py b/studio/backend/routes/browser.py
index 47c42ef4913..2dea769850f 100644
--- a/studio/backend/routes/browser.py
+++ b/studio/backend/routes/browser.py
@@ -1134,6 +1134,16 @@ def _prepare_page(page: str, url: str) -> tuple[str, str, Optional[dict]]:
return _META_TAG_RE.sub(strip_meta, page), base_url, refresh
+# unsloth.ai's Cloudflare skips its bot challenge for requests carrying this. Only its own hosts get
+# it: the challenge can't be solved from the proxied frame, and other sites have no use for it.
+_STUDIO_HEADERS = {"X-Unsloth-Studio": "1"}
+
+
+def _studio_headers(host: str) -> dict:
+ host = host.lower().rstrip(".")
+ return _STUDIO_HEADERS if host == "unsloth.ai" or host.endswith(".unsloth.ai") else {}
+
+
def _fetch(
request: BrowserFetchRequest, cancel_event: threading.Event
) -> tuple[Optional[str], bytes, str, dict]:
@@ -1151,6 +1161,7 @@ def _fetch(
post_data = (request.body or "").encode() if request.method == "POST" else None,
meta_out = meta,
cancel_event = cancel_event,
+ host_headers = _studio_headers,
)
return error, body if isinstance(body, bytes) else b"", content_type, meta
diff --git a/studio/backend/tests/test_browser_routes.py b/studio/backend/tests/test_browser_routes.py
index 3300e9428b2..eecd5862267 100644
--- a/studio/backend/tests/test_browser_routes.py
+++ b/studio/backend/tests/test_browser_routes.py
@@ -249,6 +249,16 @@ def test_fetch_arguments(monkeypatch):
)
+def test_only_unsloth_hosts_get_the_studio_header(monkeypatch):
+ calls = _fetch(monkeypatch, (None, b"<html></html>", "text/html"))
+ _call()
+ headers = calls[0]["host_headers"]
+ for host in ("unsloth.ai", "www.unsloth.ai", "docs.UNSLOTH.ai", "unsloth.ai."):
+ assert headers(host) == {"X-Unsloth-Studio": "1"}, host
+ for host in ("example.com", "notunsloth.ai", "unsloth.ai.example.com", "unsloth.aix"):
+ assert headers(host) == {}, host
+
+
@pytest.mark.parametrize(
"result, detail",
[
diff --git a/studio/backend/tests/test_web_fetch_extraction.py b/studio/backend/tests/test_web_fetch_extraction.py
index ea26582cb74..805bb6cf2a0 100644
--- a/studio/backend/tests/test_web_fetch_extraction.py
+++ b/studio/backend/tests/test_web_fetch_extraction.py
@@ -1521,6 +1521,41 @@ def test_fetch_url_raw_overall_deadline_aborts_across_redirects(monkeypatch):
assert hops["n"] < 5
+def test_fetch_url_raw_host_headers_follow_each_hop(monkeypatch):
+ # A header chosen for unsloth.ai must not ride a redirect to another site.
+ import urllib.request
+ from urllib.error import HTTPError
+
+ import core.inference.tools as tools_mod
+
+ sent = []
+
+ class _Opener:
+ def open(
+ self,
+ req,
+ timeout = None,
+ ):
+ sent.append((req.get_header("Host"), req.get_header("X-unsloth-studio")))
+ raise HTTPError(
+ req.full_url, 302, "Found", {"Location": "https://example.com/next"}, None
+ )
+
+ monkeypatch.setattr(
+ tools_mod,
+ "_validate_and_resolve_host",
+ lambda host, port: (True, "", ["203.0.113.7"]),
+ )
+ monkeypatch.setattr(urllib.request, "build_opener", lambda *handlers: _Opener())
+
+ tools_mod._fetch_url_raw(
+ "https://unsloth.ai/",
+ host_headers = lambda host: {"X-Unsloth-Studio": "1"} if host == "unsloth.ai" else {},
+ )
+ assert sent[0] == ("unsloth.ai", "1")
+ assert sent[1] == ("example.com", None)
+
+
def test_fetch_url_raw_cancel_event_aborts_before_network(monkeypatch):
# A set cancel_event (client disconnected) stops the fetch before it opens any
# socket, so a dropped stream cannot leave a tool blocking on the wire.