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: show a site's own error page in the browser panel (#12986)

Nilay53f8ff0a7dd1
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 3396b84122e..7d1b706eecb 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -15665,16 +15665,11 @@ def _resolve_with_budget(hostname, port, deadline, cancel_event):
def _read_capped_body(resp, max_bytes, timeout, deadline, cancel_event):
- """Read up to ``max_bytes``, enforcing the overall budget between chunks. A single
- ``resp.read(max_bytes)`` can block for the whole transfer if the server dribbles bytes just
- inside each socket-inactivity timeout, so the body is read in chunks with the budget
- re-checked (and the socket timeout re-tightened toward the deadline) each round. The joined
- bytes are identical to one capped read. Returns ``(error_or_None, body_bytes)``."""
- # Best-effort handle on the underlying socket so its timeout tightens as the deadline nears; absent on test
- # doubles, where the between-chunk budget check still bounds the read.
- sock = getattr(getattr(getattr(resp, "fp", None), "raw", None), "_sock", None)
- # A buffered read(n) keeps receiving until n bytes arrive, so a drip never reaches the
- # budget check; read1 returns after one receive.
+ """read at most ``max_bytes`` within the overall budget and return ``(error_or_None, body_bytes)``."""
+ # HTTPError wraps the socket; tighten its deadline when present, while chunk checks bound test doubles without one
+ fp = getattr(resp, "fp", None)
+ sock = getattr(getattr(getattr(fp, "fp", fp), "raw", None), "_sock", None)
+ # use read1 because buffered read(n) can keep receiving until n bytes arrive and bypass the budget check
read = getattr(resp, "read1", None) or resp.read
chunks = []
remaining = max_bytes
@@ -15798,35 +15793,13 @@ def _fetch_url_raw(
post_data: bytes | None = None,
meta_out: dict | None = None,
host_headers = None,
+ error_page: bool = False,
) -> 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``, ``filename`` (Content-Disposition),
- ``allow_origin`` (Access-Control-Allow-Origin), ``cache_control`` and ``age`` of a successful
- binary-mode fetch, and
- ``bot_check`` on HTTP errors.
-
- ``raw_bytes_max`` switches to binary mode: the body is returned as ``bytes`` untouched (no PDF
- or text handling) and refused past that many bytes. The same scheme, host, redirect and budget
- gates apply either way.
-
- ``error`` is a user-facing message string when the fetch failed, else ``None``. Blocks
- private/loopback/link-local targets and caps the download size. No input reaches the caller as
- an exception: the URL is model-supplied, so every malformed form resolves to one of these
- strings.
-
- ``deadline`` is an optional ``time.monotonic`` cutoff for the whole fetch (redirect hops and
- body read included) and ``cancel_event`` aborts it when the caller goes away; both default off.
- """
+ """fetch with SSRF protection; binary reads stay capped, HTML error pages require binary mode, per-hop headers do not cross redirects, and deadlines cover redirects and body reads."""
from urllib.parse import urlparse
from .web_access_policy import check_url_access
- # Before the policy gate: it requires an http(s) scheme, so a bare host would be refused there and never reach the
- # fetch.
+ # normalize before the policy gate because a bare host would otherwise fail its http(s) scheme check.
url = _normalize_url_scheme(url)
allowed, reason, canonical_host = check_url_access(url, website_policy)
if not allowed:
@@ -15853,6 +15826,7 @@ def _fetch_url_raw(
current_host = canonical_host
ua = random.choice(_USER_AGENTS)
pending_post = post_data
+ http_error = None
for _hop in range(5):
budget_error = _fetch_budget_exceeded(deadline, cancel_event)
@@ -15904,40 +15878,46 @@ def _fetch_url_raw(
headers.setdefault("Content-Type", "application/x-www-form-urlencoded")
req = urllib.request.Request(request_url, headers = headers, data = pending_post)
try:
- # Cap the socket timeout at the time left on the overall deadline so a single slow hop cannot outlast
- # the whole fetch budget.
+ # cap the socket timeout at the remaining deadline so one slow hop cannot outlast the fetch budget.
resp = opener.open(req, timeout = _fetch_hop_timeout(timeout, deadline))
except _HTTPError as e:
if e.code not in (301, 302, 303, 307, 308):
if meta_out is not None:
meta_out["bot_check"] = _is_bot_check(e.code, e.headers)
- return f"Failed to fetch URL: HTTP {e.code} {getattr(e, 'reason', '')}", "", ""
- location = e.headers.get("Location")
- if not location:
- return "Failed to fetch URL: redirect missing Location header.", "", ""
- current_url = urljoin(current_url, location)
- # 307/308 keep the POST; other redirects turn it into a GET.
- if e.code not in (307, 308):
- pending_post = None
- hop_error, current_host, pinned_ips = _redirect_hop(
- current_url,
- website_policy,
- deadline,
- cancel_event,
- )
- if hop_error is not None:
- return hop_error, "", ""
- continue
+ http_error = f"Failed to fetch URL: HTTP {e.code} {getattr(e, 'reason', '')}"
+ declared = e.headers.get("Content-Type") and e.headers.get_content_type()
+ if (
+ not error_page
+ or raw_bytes_max is None
+ or declared not in (None, "", "text/html", "application/xhtml+xml")
+ ):
+ return http_error, "", ""
+ resp = e
+ else:
+ location = e.headers.get("Location")
+ if not location:
+ return "Failed to fetch URL: redirect missing Location header.", "", ""
+ current_url = urljoin(current_url, location)
+ # 307/308 preserve POST; other redirects switch to GET.
+ if e.code not in (307, 308):
+ pending_post = None
+ hop_error, current_host, pinned_ips = _redirect_hop(
+ current_url,
+ website_policy,
+ deadline,
+ cancel_event,
+ )
+ if hop_error is not None:
+ return hop_error, "", ""
+ continue
- # get_content_type() defaults to "text/plain" when the header is absent (RFC 2045); report "" instead so
- # callers can tell a missing header apart from a server that really declared text/plain.
+ # get_content_type() defaults missing headers to "text/plain" per RFC 2045; use "" to distinguish them.
if resp.headers.get("Content-Type") is None:
content_type = ""
else:
content_type = (resp.headers.get_content_type() or "").lower()
- # Success: read the capped body enforcing the budget between chunks (see _read_capped_body), so a
- # slow-drip server can't stretch a single resp.read past the deadline.
+ # chunked reads recheck the fetch budget between chunks.
declared_pdf = raw_bytes_max is None and content_type == "application/pdf"
if raw_bytes_max is not None:
read_limit = raw_bytes_max + 1
@@ -15955,8 +15935,7 @@ def _fetch_url_raw(
if body_error is not None:
return body_error, "", ""
- # A missing or wrong PDF MIME type is common: once the initial text-sized read identifies PDF magic,
- # finish the bounded download to reach the EOF xref.
+ # missing or wrong PDF MIME types require a bounded tail read to reach the EOF xref.
if raw_bytes_max is not None:
if len(raw_bytes) > raw_bytes_max:
return f"(content exceeds the {raw_bytes_max} byte limit)", "", content_type
@@ -15967,7 +15946,7 @@ def _fetch_url_raw(
meta_out["allow_origin"] = resp.headers.get("Access-Control-Allow-Origin")
meta_out["cache_control"] = resp.headers.get("Cache-Control")
meta_out["age"] = resp.headers.get("Age")
- return None, raw_bytes, content_type
+ return http_error, raw_bytes, content_type
if not declared_pdf and len(raw_bytes) == max_bytes and _has_pdf_magic(raw_bytes):
tail_error, tail = _read_capped_body(
resp,
diff --git a/studio/backend/routes/browser.py b/studio/backend/routes/browser.py
index 47f525faf78..5194267fe53 100644
--- a/studio/backend/routes/browser.py
+++ b/studio/backend/routes/browser.py
@@ -1106,6 +1106,7 @@ class BrowserFetchRequest(BaseModel):
body: Optional[str] = Field(default = None, max_length = 1024 * 1024)
# favicon callers can lower the 50 MB fetch cap.
max_bytes: Optional[int] = Field(default = None, ge = 1, le = _MAX_BROWSER_FETCH_BYTES)
+ error_page: bool = False
_BOMS = (
@@ -1447,12 +1448,13 @@ def _fetch(
meta_out = meta,
cancel_event = cancel_event,
host_headers = _studio_headers,
+ error_page = request.error_page,
)
return error, body if isinstance(body, bytes) else b"", content_type, meta
def _attachment_name(meta: dict) -> Optional[str]:
- """The server's name for a download, as a bare file name."""
+ """server-provided download name reduced to a bare file name."""
name = meta.get("filename")
if not isinstance(name, str):
return None
@@ -1468,9 +1470,13 @@ def _build_response(
meta: dict,
cancel_event: Optional[threading.Event] = None,
) -> Response:
- """Build the panel's response. Runs in the fetch pool to keep large pages off the event loop."""
- if error is not None:
- # The host only: a page address can carry a sign-in token.
+ """build the panel response in the fetch pool to keep large pages off the event loop."""
+ looks_html = not content_type and body[:512].lstrip().lower().startswith(
+ (b"<!doctype html", b"<html")
+ )
+ is_html = content_type in _HTML_TYPES or looks_html
+ if error is not None and (meta.get("bot_check") or not (is_html and body.strip())):
+ # log the host only because page URLs can carry sign-in tokens.
try:
host = urlsplit(url).hostname
except ValueError:
@@ -1481,10 +1487,7 @@ def _build_response(
raise HTTPException(status_code = 502, detail = error)
final_url = meta.get("url") or url
- looks_html = not content_type and body[:512].lstrip().lower().startswith(
- (b"<!doctype html", b"<html")
- )
- if content_type in _HTML_TYPES or looks_html:
+ if is_html:
if len(body) > _MAX_BROWSER_HTML_BYTES:
raise HTTPException(
status_code = 502,
diff --git a/studio/backend/tests/test_browser_routes.py b/studio/backend/tests/test_browser_routes.py
index eb33b8ce463..b670a23fc73 100644
--- a/studio/backend/tests/test_browser_routes.py
+++ b/studio/backend/tests/test_browser_routes.py
@@ -761,3 +761,122 @@ def test_a_redirected_module_is_not_cached(monkeypatch):
for _ in range(2):
assert "ready()" in browser_mod._inline_module_scripts(page, "https://example.com/")
assert fetched == ["https://example.com/latest.js"] * 2
+
+
+def _serve(
+ monkeypatch,
+ *parts,
+ gap = 0,
+):
+ import socket
+
+ from core.inference import tools
+
+ server = socket.create_server(("127.0.0.1", 0))
+ server.settimeout(10)
+
+ def respond():
+ with server:
+ while True:
+ try:
+ conn, _ = server.accept()
+ except OSError:
+ return
+ with conn:
+ conn.recv(65536)
+ try:
+ for part in parts:
+ conn.sendall(part)
+ time.sleep(gap)
+ except OSError:
+ pass
+
+ threading.Thread(target = respond, daemon = True).start()
+ monkeypatch.setattr(
+ tools, "_validate_and_resolve_host", lambda host, port: (True, "", ["127.0.0.1"])
+ )
+ return f"http://example.com:{server.getsockname()[1]}/missing"
+
+
+def _error_response(
+ code,
+ body,
+ headers = None,
+):
+ headers = {
+ "Content-Type": "text/html; charset=utf-8",
+ "Content-Length": str(len(body)),
+ **(headers or {}),
+ }
+ head = "".join(f"{name}: {value}\r\n" for name, value in headers.items())
+ return f"HTTP/1.1 {code} Not Found\r\n{head}\r\n".encode() + body
+
+
+_SITE_404 = b"<html><head><title>Page not found</title></head><body>SITE_404_PAGE</body></html>"
+
+
+def test_a_sites_own_error_page_is_shown_in_a_tab(monkeypatch):
+ from core.inference import tools
+
+ url = _serve(monkeypatch, _error_response(404, _SITE_404))
+ response = _call(url = url, error_page = True)
+ assert response.headers["x-unsloth-browser-kind"] == "html"
+ payload = json.loads(response.body)
+ assert payload["url"] == url
+ assert "SITE_404_PAGE" in payload["html"]
+ assert tools._fetch_page_text(url, timeout = 5) == "Failed to fetch URL: HTTP 404 Not Found"
+
+
[email protected]("request_fields", [{}, {"max_bytes": 256 * 1024}])
+def test_a_download_or_icon_of_an_error_page_fails(monkeypatch, request_fields):
+ url = _serve(monkeypatch, _error_response(404, _SITE_404))
+ with pytest.raises(HTTPException) as caught:
+ _call(url = url, **request_fields)
+ assert caught.value.status_code == 502
+ assert caught.value.detail == "Failed to fetch URL: HTTP 404 Not Found"
+
+
+def test_a_slow_error_page_stops_at_the_fetch_deadline(monkeypatch):
+ monkeypatch.setattr(browser_mod, "_FETCH_TIMEOUT_S", 1.5)
+ head = _error_response(404, b"<html>" + b"x" * 9)[:-9]
+ url = _serve(monkeypatch, head, *[b"x"] * 9, gap = 1)
+ started = time.monotonic()
+ with pytest.raises(HTTPException) as caught:
+ _call(url = url, error_page = True)
+ assert caught.value.status_code == 502
+ assert time.monotonic() - started < 1.8
+
+
+def test_an_error_that_is_not_a_page_fails_without_reading_it(monkeypatch):
+ monkeypatch.setattr(browser_mod, "_FETCH_TIMEOUT_S", 1.5)
+ head = _error_response(503, b"x" * 9, {"Content-Type": "application/octet-stream"})[:-9]
+ url = _serve(monkeypatch, head, *[b"x"] * 9, gap = 1)
+ started = time.monotonic()
+ with pytest.raises(HTTPException) as caught:
+ _call(url = url, error_page = True)
+ assert caught.value.detail == "Failed to fetch URL: HTTP 503 Not Found"
+ assert time.monotonic() - started < 1
+
+
+ "code, body, headers, detail",
+ [
+ (404, b"", {}, "HTTP 404 Not Found"),
+ (404, b"no such page", {"Content-Type": "text/plain"}, "HTTP 404 Not Found"),
+ (
+ 403,
+ b"<html>challenge</html>",
+ {"Server": "cloudflare"},
+ {"message": "Failed to fetch URL: HTTP 403 Not Found", "botCheck": True},
+ ),
+ ],
+)
+def test_error_pages_that_cannot_be_shown_stay_errors(monkeypatch, code, body, headers, detail):
+ url = _serve(monkeypatch, _error_response(code, body, headers))
+ with pytest.raises(HTTPException) as caught:
+ _call(url = url, error_page = True)
+ assert caught.value.status_code == 502
+ if isinstance(detail, dict):
+ assert caught.value.detail == detail
+ else:
+ assert caught.value.detail == f"Failed to fetch URL: {detail}"
diff --git a/studio/frontend/src/features/browser/api.ts b/studio/frontend/src/features/browser/api.ts
index e99c6d203fa..b90066c6ea1 100644
--- a/studio/frontend/src/features/browser/api.ts
+++ b/studio/frontend/src/features/browser/api.ts
@@ -12,19 +12,21 @@ export type BrowserPage =
html: string;
refresh: { delay: number; url: string } | null;
}
- | { kind: "raw"; url: string; blob: Blob; contentType: string; /** The server's download name. */ fileName?: string };
+ | { kind: "raw"; url: string; blob: Blob; contentType: string; /** the server's download name. */ fileName?: string };
export type BrowserRequest = {
url: string;
method?: "GET" | "POST";
body?: string;
- /** Refuse bodies past this size (favicons); the backend's own cap otherwise. */
+ /** refuse bodies past this size (favicons); otherwise use the backend cap. */
maxBytes?: number;
+ /** show the site's HTTP error page; tab loads only. */
+ errorPage?: boolean;
};
let annotateCode: Promise<string> | null = null;
-/** The page's annotate code (`_ANNOTATE_JS`), fetched on first use and kept; a failure tries again next time. */
+/** the page annotate code (`_ANNOTATE_JS`), cached after success so failed fetches can retry. */
export function annotateScript(): Promise<string> {
annotateCode ??= authFetch("/api/browser/annotate.js")
.then((response) => {
@@ -47,7 +49,7 @@ export class BrowserFetchError extends Error {
}
}
-/** Fetch a page via the backend, which can load sites that refuse framing. */
+/** fetch through the backend for sites that refuse framing. */
export async function fetchBrowserPage(request: BrowserRequest, signal: AbortSignal): Promise<BrowserPage> {
const response = await authFetch("/api/browser/fetch", {
method: "POST",
@@ -57,6 +59,7 @@ export async function fetchBrowserPage(request: BrowserRequest, signal: AbortSig
method: request.method ?? "GET",
body: request.body ?? null,
max_bytes: request.maxBytes ?? null,
+ error_page: request.errorPage ?? false,
}),
signal,
});
diff --git a/studio/frontend/src/features/browser/tab-view.tsx b/studio/frontend/src/features/browser/tab-view.tsx
index 04b69a78e9d..45157524777 100644
--- a/studio/frontend/src/features/browser/tab-view.tsx
+++ b/studio/frontend/src/features/browser/tab-view.tsx
@@ -284,16 +284,16 @@ function WebPage({
}
const controller = new AbortController();
updateTab(tab.id, { loading: true });
- fetchBrowserPage({ url, method, body }, controller.signal)
+ fetchBrowserPage({ url, method, body, errorPage: true }, controller.signal)
.then((page) => {
cachePage(entry, page);
setState({ status: "ready", page });
show(page);
- // A file the panel can't show downloads, as in a browser; fresh loads only, so returning to the tab doesn't ask again.
+ // unsupported files download only on fresh loads, so revisiting the tab does not prompt again.
if (page.kind === "raw") {
const name = page.fileName ?? fileNameFromUrl(page.url);
if (!canShowFile(name, page.contentType)) {
- // The sending page, else the address asked for (not the redirect target), so another site's "allow" can't cover it.
+ // use the sender or requested address, not the redirect target, so another site's permission cannot apply.
void saveBrowserDownload({ blob: page.blob, name, contentType: page.contentType, url: page.url, site: entry.from ?? url });
if (entry.kind === "web" && entry.from) useBrowserStore.getState().leaveDownload(tab.id, entry);
}