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: let browser panel pages call their own site's API (#13664)
oobabooga0cefbf4b125f
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex 525b07a65e6..fd73cbac0f8 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -16647,8 +16647,13 @@ def _fetch_url_raw(meta_out: dict | None = None,host_headers = None,error_page: bool = False,+ method: str | None = None,+ any_status: bool = False,+ on_redirect = None,) -> tuple[str | None, "str | bytes", str]:- """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."""+ """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.+ ``method`` replaces the GET/POST chosen from ``post_data``; with ``any_status`` (binary mode) every status returns its body, and ``meta_out`` gets the status and headers.+ ``on_redirect(url, method, has_body, headers)`` runs before a redirect is followed, with the redirect's headers; a string it returns is the error, and the hop is never sent."""from urllib.parse import urlparsefrom .web_access_policy import check_url_access@@ -16679,6 +16684,7 @@ def _fetch_url_raw(current_host = canonical_hostua = random.choice(_USER_AGENTS)pending_post = post_data+ pending_method = methodhttp_error = Nonefor _hop in range(5):@@ -16728,9 +16734,11 @@ def _fetch_url_raw(headers.update(extra_headers)if host_headers is not None:headers.update(host_headers(current_host))- if pending_post is not None:+ if pending_post is not None and pending_method is None:headers.setdefault("Content-Type", "application/x-www-form-urlencoded")- req = urllib.request.Request(request_url, headers = headers, data = pending_post)+ req = urllib.request.Request(+ request_url, headers = headers, data = pending_post, method = pending_method+ )try:# 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))@@ -16740,11 +16748,13 @@ def _fetch_url_raw(meta_out["bot_check"] = _is_bot_check(e.code, e.headers)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")- ):+ html_error_page = error_page and declared in (+ None,+ "",+ "text/html",+ "application/xhtml+xml",+ )+ if raw_bytes_max is None or not (any_status or html_error_page):return http_error, "", ""resp = eelse:@@ -16752,9 +16762,23 @@ def _fetch_url_raw(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):+ # Fetch's HTTP-redirect fetch: 301/302 turn a POST, and 303 anything but GET/HEAD,+ # into a bodiless GET; otherwise the method and body go to the next hop.+ sent = pending_method or ("GET" if pending_post is None else "POST")+ if (e.code in (301, 302) and sent == "POST") or (+ e.code == 303 and sent not in ("GET", "HEAD")+ ):pending_post = None+ pending_method = None+ if on_redirect is not None:+ refused = on_redirect(+ current_url,+ pending_method or ("GET" if pending_post is None else "POST"),+ pending_post is not None,+ e.headers,+ )+ if refused:+ return refused, "", ""hop_error, current_host, pinned_ips = _redirect_hop(current_url,website_policy,@@ -16814,6 +16838,9 @@ 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")+ meta_out["status"] = resp.getcode()+ meta_out["reason"] = str(getattr(resp, "reason", "") or "")+ meta_out["headers"] = list(resp.headers.items())return http_error, raw_bytes, content_typeif not declared_pdf and _has_pdf_magic(raw_bytes):tail_error, tail = _read_capped_body(diff --git a/studio/backend/routes/browser.py b/studio/backend/routes/browser.pyindex e916132861e..b6dd7a96c42 100644--- a/studio/backend/routes/browser.py+++ b/studio/backend/routes/browser.py@@ -35,6 +35,7 @@ from core.inference.tools import (_normalize_url_scheme,_sniff_meta_charset,)+from core.inference.web_access_policy import normalize_domainfrom loggers import get_loggerfrom routes.browser_network import BROWSER_NETWORK_JS@@ -62,6 +63,34 @@ _HTML_TYPES = frozenset({"text/html", "application/xhtml+xml"})# Own pool, so slow sites can't starve chat inference's default executor._FETCH_POOL = ThreadPoolExecutor(max_workers = 8, thread_name_prefix = "browser-fetch")_DISCONNECT_POLL_S = 0.25+# Pages' requests to their own sites (/request); a pool of their own, so a busy page can't hold up page loads.+_REQUEST_POOL = ThreadPoolExecutor(max_workers = 8, thread_name_prefix = "browser-request")+# Queued plus running; held until the worker ends, which a disconnect does not cut short, so a page+# that aborts and resends can't pile request bodies up behind a stalled site.+_PAGE_REQUEST_SLOTS = threading.BoundedSemaphore(64)+_MAX_PAGE_REQUEST_BODY = 1024 * 1024+_MAX_PAGE_RESPONSE_BYTES = 10 * 1024 * 1024+_HEADER_NAME_RE = re.compile(r"[!#$%&'*+.^_`|~0-9A-Za-z-]{1,256}")+# Fetch's forbidden methods and request headers: no page can send them, so neither does this.+_FORBIDDEN_METHODS = frozenset({"CONNECT", "TRACE", "TRACK"})+_FORBIDDEN_REQUEST_HEADERS = frozenset(+ "accept-charset accept-encoding access-control-request-headers access-control-request-method "+ "connection content-length cookie cookie2 date dnt expect host keep-alive origin referer "+ "set-cookie te trailer transfer-encoding upgrade user-agent via".split()+)+# Fetch drops these, and the request's Origin, when a redirect turns a write into a bodiless GET.+_REQUEST_BODY_HEADERS = frozenset(+ "content-encoding content-language content-location content-type".split()+)+# What a page reads of a CORS response besides the headers it names in Access-Control-Expose-Headers.+_CORS_SAFELISTED_RESPONSE_HEADERS = frozenset(+ "cache-control content-language content-length content-type expires last-modified pragma".split()+)+# Unreadable by any page (Set-Cookie) or about the hop, not the response. Content-Length and+# Content-Encoding stay: browsers show them with a decoded body too.+_HIDDEN_RESPONSE_HEADERS = frozenset(+ "set-cookie set-cookie2 connection keep-alive proxy-connection te trailer transfer-encoding upgrade".split()+)# Quoted values are bounded and a bare "<" ends a tag, so stripping stays linear on hostile pages._TAG_BODY = r"""(?:[^<>"']|"[^"]{0,4096}"|'[^']{0,4096}')*>"""@@ -713,7 +742,7 @@ _FRAME_HTML = r"""<!doctype html>}// Only the parent may drive the shell, once.if (!data || data.type !== "unsloth:browser-html" || typeof data.html !== "string") return;- const cfg = { url: data.url || null, ownBase: data.ownBase === true, refresh: data.refresh || null, zoom: Number(data.zoom) || 1, muted: data.muted === true };+ const cfg = { url: data.url || null, ownBase: data.ownBase === true, anyOrigin: data.anyOrigin === true, refresh: data.refresh || null, zoom: Number(data.zoom) || 1, muted: data.muted === true };const base = data.base ? `<base href="${escapeAttr(data.base)}">` : "";const script = `<script>(${boot.toString()})(${JSON.stringify(cfg).replace(/</g, "\\u003c")});<\/script>`;page = document.createElement("iframe");@@ -1542,6 +1571,8 @@ def _build_response("url": final_url,"base": base_url,"ownBase": own_base is not None,+ # Its site answers any origin, the sandbox's too: the page's requests to it stay native.+ "anyOrigin": (meta.get("allow_origin") or "").strip() in ("*", "null"),"refresh": refresh,"html": page,}@@ -1573,22 +1604,278 @@ def _build_response()+class BrowserPageRequest(BaseModel):+ # The page's address as the panel loaded it; the request must be to its origin.+ page: str = Field(..., min_length = 1, max_length = 8192)+ url: str = Field(..., min_length = 1, max_length = 8192)+ method: str = Field(default = "GET", pattern = r"^[A-Za-z]{1,16}$")+ headers: list[tuple[str, str]] = Field(default_factory = list, max_length = 64)+ # Base64.+ body: Optional[str] = Field(default = None, max_length = 4 * (_MAX_PAGE_REQUEST_BODY // 3 + 1))+ # Fetch's redirect mode: "error" fails at a redirect, "manual" stops there (an opaque redirect).+ redirect: Literal["follow", "error", "manual"] = "follow"+ # Fetch's integrity metadata; a body that doesn't match is a network error.+ integrity: str = Field(default = "", max_length = 4096)+++def _origin(url: str) -> Optional[tuple[str, str, int]]:+ """``(scheme, host, port)``, the host named as _fetch_url_raw names it to host_headers."""+ try:+ parts = urlsplit(url)+ scheme = parts.scheme.lower()+ if scheme not in ("http", "https") or not parts.hostname:+ return None+ return (+ scheme,+ normalize_domain(parts.hostname),+ parts.port or (443 if scheme == "https" else 80),+ )+ except (TypeError, ValueError):+ return None+++def _cors_safelisted(name: str, value: str) -> bool:+ """Fetch's CORS-safelisted request-header, for the headers a bodiless GET carries."""+ lowered = name.lower()+ unsafe = any(+ (ord(char) < 0x20 and char != "\t") or char in '"():<>?@[\\]{}\x7f' for char in value+ )+ if len(value) > 128:+ return False+ if lowered == "accept":+ return not unsafe+ if lowered in ("accept-language", "content-language"):+ return bool(re.fullmatch(r"[0-9A-Za-z *,.;=-]*", value))+ if lowered == "range":+ return bool(re.fullmatch(r"bytes=[0-9]+-[0-9]*", value))+ if lowered == "content-type":+ essence = value.split(";", 1)[0].strip().lower()+ return not unsafe and essence in (+ "application/x-www-form-urlencoded",+ "multipart/form-data",+ "text/plain",+ )+ return False+++def _allowed_header(name: str, value: str) -> bool:+ lowered = name.lower()+ return (+ bool(_HEADER_NAME_RE.fullmatch(name))+ and lowered not in _FORBIDDEN_REQUEST_HEADERS+ and not lowered.startswith(("proxy-", "sec-"))+ and "\r" not in value+ and "\n" not in value+ )+++def _page_request(request: BrowserPageRequest, cancel_event: threading.Event) -> dict:+ """The page's request, made as its browser would make a same-origin one; every status is the page's."""+ origin = _origin(request.page)+ if origin is None or _origin(request.url) != origin:+ raise HTTPException(status_code = 403, detail = "Only requests to the page's own site.")+ method = request.method.upper()+ if method in _FORBIDDEN_METHODS:+ raise HTTPException(status_code = 400, detail = f"{method} is not allowed.")+ body = None+ if request.body is not None and method not in ("GET", "HEAD"):+ try:+ body = base64.b64decode(request.body, validate = True)+ except ValueError:+ raise HTTPException(status_code = 400, detail = "The body is not base64.")+ if len(body) > _MAX_PAGE_REQUEST_BODY:+ raise HTTPException(status_code = 413, detail = "The body is too large.")+ # setRequestHeader combines a repeated name, in any case, into one value, as Headers does.+ own: dict[str, str] = {}+ named: dict[str, str] = {}+ for name, value in request.headers:+ if not _allowed_header(name, value):+ continue+ first = named.setdefault(name.lower(), name)+ own[first] = f"{own[first]}, {value}" if first in own else value+ scheme, host, port = origin+ default_port = 443 if scheme == "https" else 80+ serialized_origin = f"{scheme}://" + (f"[{host}]" if ":" in host else host)+ serialized_origin += "" if port == default_port else f":{port}"+ # Typed addresses can keep non-ASCII text, which no header can carry; a browser sends it encoded.+ page = urlsplit(request.page)+ path = (page.path or "/") + (f"?{page.query}" if page.query else "")+ # What a browser adds to a same-origin request: Referer (the default policy keeps the whole+ # address), fetch metadata, and Origin on anything but GET and HEAD.+ added = {+ "Referer": serialized_origin + quote(path, safe = ":/?#[]@!$&'()*+,;=%~"),+ "Sec-Fetch-Site": "same-origin",+ "Sec-Fetch-Mode": "cors",+ "Sec-Fetch-Dest": "empty",+ }+ if method not in ("GET", "HEAD"):+ added["Origin"] = serialized_origin+ if "accept" not in named:+ added["Accept"] = "*/*"+ if "accept-language" not in named:+ added["Accept-Language"] = "en-US,en;q=0.9"+ page_headers = {**added, **own}+ # Whether every hop so far stayed on the page's origin. Once one leaves it, only the page's+ # CORS-safelisted headers go on (Range, never Authorization), since the rest would need a preflight+ # this can't make; and the request is a CORS one carrying Origin: the page's, or "null" once a hop+ # went from one other origin to another (Fetch's redirect-taint).+ hops = {+ "same_origin": True,+ "stopped": False,+ "last": origin,+ "origin": serialized_origin,+ "rewritten": False,+ "redirected": False,+ }++ def allowed(allow_origin: Optional[str]) -> bool:+ return (allow_origin or "").strip() in ("*", hops["origin"])++ def on_redirect(url: str, hop_method: str, has_body: bool, headers) -> Optional[str]:+ if request.redirect != "follow":+ hops["stopped"] = True+ return "Redirected, and the request asked not to follow."+ # Mixed content: an https page never reads what came over plain http, so that hop is never sent.+ if scheme == "https" and urlsplit(url).scheme.lower() != "https":+ return "Redirected from https to an insecure address."+ # A browser CORS-checks each response from another site, a redirect too, before following it.+ if not hops["same_origin"] and not allowed(headers.get("Access-Control-Allow-Origin")):+ return "Redirected through a site that does not allow it."+ hops["redirected"] = True+ hops["rewritten"] = hops["rewritten"] or hop_method != method+ last, hops["last"] = hops["last"], _origin(url)+ if hops["last"] != last and last != origin:+ hops["origin"] = "null"+ if hops["last"] == origin and hops["same_origin"]:+ return None+ hops["same_origin"] = False+ # A browser preflights anything but a bare GET or HEAD to another origin; this can't, so a+ # write is refused before it is sent rather than after it reached the other site.+ if hop_method not in ("GET", "HEAD") or has_body:+ return "Redirected a write to another site."+ return None++ def host_headers(current_host: str) -> dict:+ own_headers = page_headers+ if hops["rewritten"]:+ own_headers = {+ name: value+ for name, value in own_headers.items()+ if name.lower() not in _REQUEST_BODY_HEADERS and name != "Origin"+ }+ if not hops["same_origin"]:+ own_headers = {+ **{+ name: value+ for name, value in own_headers.items()+ if _cors_safelisted(name, value)+ },+ "Origin": hops["origin"],+ }+ # The default referrer policy sends another site only the page's origin.+ own_headers["Referer"] = serialized_origin + "/"+ return {**_studio_headers(current_host), **own_headers}++ meta: dict = {}+ error, raw, _content_type = _fetch_url_raw(+ request.url,+ timeout = _FETCH_TIMEOUT_S,+ extra_headers = {"User-Agent": _BROWSER_UA},+ deadline = time.monotonic() + _FETCH_TIMEOUT_S,+ raw_bytes_max = _MAX_PAGE_RESPONSE_BYTES,+ post_data = body,+ meta_out = meta,+ cancel_event = cancel_event,+ host_headers = host_headers,+ method = method,+ any_status = True,+ on_redirect = on_redirect,+ )+ # "manual" gets an opaque redirect: no status, headers or body reach the page.+ if hops["stopped"] and request.redirect == "manual":+ return {+ "opaqueRedirect": True,+ "status": 0,+ "statusText": "",+ "url": request.url,+ "headers": [],+ "body": "",+ }+ status = meta.get("status")+ if not isinstance(status, int) or not isinstance(raw, bytes):+ raise HTTPException(status_code = 502, detail = error or "The request failed.")+ if not 200 <= status <= 599:+ raise HTTPException(status_code = 502, detail = f"HTTP {status}.")+ if not _integrity_ok(raw, request.integrity):+ raise HTTPException(status_code = 502, detail = "The body does not match its integrity.")+ final_url = meta.get("url") or request.url+ headers = [+ (name, value)+ for name, value in meta.get("headers") or []+ if name.lower() not in _HIDDEN_RESPONSE_HEADERS+ ]+ # Redirected through another site, it is a CORS request, as in a browser: read only with the+ # consent of the site that answered, and only the headers it exposes.+ if not hops["same_origin"]:+ if not allowed(meta.get("allow_origin")):+ raise HTTPException(status_code = 502, detail = "Redirected to another site.")+ exposed = {+ name.strip().lower()+ for header, value in headers+ if header.lower() == "access-control-expose-headers"+ for name in value.split(",")+ }+ # Credentials are never sent, so "*" exposes every header.+ if "*" not in exposed:+ headers = [+ (name, value)+ for name, value in headers+ if name.lower() in _CORS_SAFELISTED_RESPONSE_HEADERS or name.lower() in exposed+ ]+ return {+ "status": status,+ "statusText": meta.get("reason") or "",+ "url": final_url,+ "headers": [[name, value] for name, value in headers],+ "body": base64.b64encode(raw).decode("ascii"),+ # Fetch's response.redirected and type: a chain back to the page's origin stays "cors".+ "redirected": hops["redirected"],+ "cors": not hops["same_origin"],+ }++def _fetch_and_build(request: BrowserFetchRequest, cancel_event: threading.Event) -> Response:error, body, content_type, meta = _fetch(request, cancel_event)return _build_response(request.url, error, body, content_type, meta, cancel_event)[email protected]("/fetch")-async def browser_fetch(- request: BrowserFetchRequest,+async def _run_in_pool(+ pool: ThreadPoolExecutor,+ work,+ request,http_request: Request,- current_subject: str = Depends(get_current_subject),+ slots = None,):- """Fetch a page: HTML as JSON for the shell, anything else raw with its content type."""- request.url = _normalize_url_scheme(request.url.strip())+ """``work(request, cancel_event)`` in ``pool``, cancelled when the client goes away; ``slots`` bounds the calls queued or running."""+ if slots is not None and not slots.acquire(blocking = False):+ raise HTTPException(status_code = 503, detail = "Too many requests in flight.")cancel_event = threading.Event()loop = asyncio.get_running_loop()- task = loop.run_in_executor(_FETCH_POOL, _fetch_and_build, request, cancel_event)+ if slots is not None:+ held = work++ def work(request, cancel_event):+ try:+ return held(request, cancel_event)+ finally:+ slots.release()++ try:+ task = loop.run_in_executor(pool, work, request, cancel_event)+ except BaseException:+ if slots is not None:+ slots.release()+ raisetry:while True:done, _ = await asyncio.wait({task}, timeout = _DISCONNECT_POLL_S)@@ -1601,6 +1888,29 @@ async def browser_fetch(cancel_event.set()[email protected]("/fetch")+async def browser_fetch(+ request: BrowserFetchRequest,+ http_request: Request,+ current_subject: str = Depends(get_current_subject),+):+ """Fetch a page: HTML as JSON for the shell, anything else raw with its content type."""+ request.url = _normalize_url_scheme(request.url.strip())+ return await _run_in_pool(_FETCH_POOL, _fetch_and_build, request, http_request)++[email protected]("/request")+async def browser_page_request(+ request: BrowserPageRequest,+ http_request: Request,+ current_subject: str = Depends(get_current_subject),+):+ """A page's fetch or XHR to its own site, which its opaque-origin sandbox can't make (browser_network.py)."""+ return await _run_in_pool(+ _REQUEST_POOL, _page_request, request, http_request, slots = _PAGE_REQUEST_SLOTS+ )++@router.get("/print", include_in_schema = False)async def browser_print():"""Print shell; static and unauthenticated like ``/frame``."""diff --git a/studio/backend/routes/browser_network.py b/studio/backend/routes/browser_network.pyindex 8147bf9ae67..8132098be1c 100644--- a/studio/backend/routes/browser_network.py+++ b/studio/backend/routes/browser_network.py@@ -1,9 +1,10 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-"""The page shell's fetch and XMLHttpRequest, wrapped to count the page's requests in flight, so the-browser agent can wait for content a page fetches after load before it reads the page.-Runs inside ``boot`` in routes/browser.py."""+"""The page shell's fetch and XMLHttpRequest. Wrapped to count the page's requests in flight, so the+browser agent can wait for content a page fetches after load before it reads the page; and to send+the page's requests to its own site through the panel, as the sandbox's opaque origin makes them+cross-origin. Runs inside ``boot`` in routes/browser.py."""BROWSER_NETWORK_JS = r"""const nativeFetch = window.fetch.bind(window);@@ -28,24 +29,278 @@ BROWSER_NETWORK_JS = r"""return reading.finally(track());};}++ // To the browser the sandbox's origin is opaque, so even the page's own site is another origin,+ // and an API of it that sends no CORS headers can't be read. Requests to the page's origin go+ // through the panel to Studio's backend (POST /api/browser/request), which makes them as the+ // same-origin requests they are. Requests to other sites stay the browser's. When the page's+ // site answers any origin (cfg.anyOrigin), its writes and XHRs stay the browser's too, streamed+ // and unbuffered; a fetch GET still falls back to the panel for an API that doesn't.+ const pageOrigin = (() => { try { return cfg.url ? new URL(cfg.url).origin : null; } catch { return null; } })();+ const ownSite = (url) => { try { return pageOrigin !== null && new URL(url).origin === pageOrigin; } catch { return false; } };+ const replies = new Map();+ let lastRequest = 0;+ window.addEventListener("message", (event) => {+ const data = event.data;+ if (event.source !== parent || !data || data.type !== "unsloth:browser-command" || data.command !== "response") return;+ replies.get(data.id)?.(data);+ }, true);+ // The panel's { status, statusText, url, headers, body }. A failure rejects as fetch's network+ // error does; an abort rejects with the signal's reason, and the panel stops the request.+ const relay = (request, signal) => new Promise((resolve, reject) => {+ if (signal.aborted) { reject(signal.reason); return; }+ const id = "q" + ++lastRequest;+ const settle = () => { replies.delete(id); signal.removeEventListener("abort", onAbort); };+ const onAbort = () => { settle(); post({ type: "requestAbort", id }); reject(signal.reason); };+ signal.addEventListener("abort", onAbort);+ replies.set(id, (reply) => {+ settle();+ if (reply.ok === true) resolve(reply);+ else reject(new TypeError("Failed to fetch"));+ });+ post({ type: "request", id, ...request });+ });+ const bodiless = (method) => method === "GET" || method === "HEAD";+ // A GET or HEAD tries the browser first: where the site already answers the sandbox's origin+ // (an API sending ACAO null or *), streams, long polls and large bodies stay as they were. One+ // the browser can't read goes to the panel, and that address goes there directly from then on.+ const needsRelay = new Set();+ const address = (url) => { const parts = new URL(url); return parts.origin + parts.pathname; };+ const fromBrowser = async (request) => {+ if (!bodiless(request.method) || needsRelay.has(address(request.url))) return null;+ try { return await nativeFetch(request); }+ catch (error) {+ if (request.signal.aborted) throw error;+ needsRelay.add(address(request.url));+ return null;+ }+ };+ const NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);+ // The panel's limit (frame-message.ts, routes/browser.py); strings counted at 3 bytes a unit.+ const MAX_RELAY_BODY = 1024 * 1024;+ const bodySize = (body) => typeof body === "string" ? body.length * 3+ : body instanceof Blob ? body.size+ : body instanceof ArrayBuffer || ArrayBuffer.isView(body) ? body.byteLength+ // Multipart adds a boundary and headers per entry: 512 bytes each, plus names, is ample.+ : body instanceof FormData ? [...body.entries()].reduce((total, [name, value]) => total + 512 + name.length * 3+ + (typeof value === "string" ? value.length * 3 : value.size + (value.name?.length ?? 0) * 3), 0)+ : body instanceof URLSearchParams ? body.toString().length * 3+ : body instanceof Document ? (body.documentElement?.outerHTML?.length ?? 0) * 3+ : 0;+ const withoutHash = (url) => url.split("#")[0];+ // What a fetched response has and a constructed one lacks, kept through clone(): "basic" on+ // the page's origin, "cors" once a redirect led through another site.+ const asFetched = (response, url, redirected, type) => {+ const clone = response.clone;+ return Object.defineProperties(response, {+ url: { value: url },+ redirected: { value: redirected },+ type: { value: type },+ clone: { value: () => asFetched(clone.call(response), url, redirected, type) },+ });+ };+ // The body of a copy, read only up to the relay's limit: undefined past it, so a large upload+ // is never buffered whole before it goes to the browser.+ const readCapped = async (request) => {+ const reader = request.clone().body?.getReader();+ if (!reader) return new ArrayBuffer(0);+ // The request's signal doesn't reach a copy's reader: an abort cancels it here, so a stalled+ // stream can't keep the fetch pending.+ const signal = request.signal;+ const onAbort = () => reader.cancel(signal.reason).catch(() => {});+ signal.addEventListener("abort", onAbort);+ const chunks = [];+ let size = 0;+ try {+ for (;;) {+ if (signal.aborted) throw signal.reason;+ const { done, value } = await reader.read();+ if (signal.aborted) throw signal.reason;+ if (done) break;+ size += value.byteLength;+ if (size > MAX_RELAY_BODY) { reader.cancel().catch(() => {}); return undefined; }+ chunks.push(value);+ }+ } finally {+ signal.removeEventListener("abort", onAbort);+ }+ const bytes = new Uint8Array(size);+ let at = 0;+ for (const chunk of chunks) { bytes.set(chunk, at); at += chunk.byteLength; }+ return bytes.buffer;+ };+ const relayFetch = async (request) => {+ const body = bodiless(request.method) ? null : await readCapped(request);+ // Past the relay's body limit a write stays the browser's, as before relaying existed.+ if (body === undefined) return nativeFetch(request);+ const reply = await relay(+ { url: request.url, method: request.method, headers: [...request.headers], body, redirect: request.redirect, integrity: request.integrity },+ request.signal,+ );+ // redirect: "manual" stopped at a redirect, which a page sees only as an opaque one.+ if (reply.opaqueRedirect === true) return asFetched(Response.error(), withoutHash(request.url), false, "opaqueredirect");+ const headers = new Headers();+ for (const [name, value] of reply.headers) try { headers.append(name, value); } catch {}+ const content = request.method === "HEAD" || NULL_BODY_STATUSES.has(reply.status) ? null : reply.body;+ let response;+ // A reason phrase a Response refuses is left out; HTTP/2 has none anyway.+ try { response = new Response(content, { status: reply.status, statusText: reply.statusText, headers }); }+ catch { response = new Response(content, { status: reply.status, headers }); }+ const url = withoutHash(reply.url);+ return asFetched(response, url, reply.redirected === true, reply.cors === true ? "cors" : "basic");+ };window.fetch = (input, init) => {const done = track();- return nativeFetch(input, init).finally(done);+ // Relative addresses resolve against the page's <base>, as the browser's would. Other inputs+ // (a Request from another frame) are left to the browser.+ let url = null;+ try {+ if (input instanceof Request) url = input.url;+ else if (typeof input === "string" || input instanceof URL) url = new URL(input, document.baseURI).href;+ } catch {}+ if (!ownSite(url)) return nativeFetch(input, init).finally(done);+ let request;+ try { request = new Request(input instanceof Request ? input : url, init); } catch (error) { done(); return Promise.reject(error); }+ if (cfg.anyOrigin && !bodiless(request.method)) return nativeFetch(request).finally(done);+ return fromBrowser(request).then((response) => response ?? relayFetch(request)).finally(done);+ };++ // An XHR to the page's site stays OPENED while the panel makes it, then the browser loads the+ // answer from a blob: URL, so events, responseType and decoding are the browser's own; status,+ // headers and URL read as the site's. A synchronous XHR can't wait, so it stays the browser's.+ const relayed = new WeakMap();+ const headerMap = (list) => {+ const merged = new Map();+ for (const [name, value] of list) {+ const key = name.toLowerCase();+ merged.set(key, merged.has(key) ? merged.get(key) + ", " + value : value);+ }+ return merged;+ };+ const nativeReadyState = (xhr) => Reflect.get(NativeXHR.prototype, "readyState", xhr);+ // The site's answer, once the browser has loaded its headers.+ const answered = (xhr) => {+ const state = relayed.get(xhr);+ return state?.reply && nativeReadyState(xhr) >= 2 ? state.reply : null;};window.XMLHttpRequest = class extends NativeXHR {+ constructor() {+ super();+ // send() fires loadstart; the blob: load's own would be a second. Added before any of the+ // page's listeners, so stopping it here keeps it from all of them.+ this.addEventListener("loadstart", (event) => {+ if (event.isTrusted && relayed.get(this)?.reply) event.stopImmediatePropagation();+ }, true);+ }// open() ends a request in flight without a loadend, so its count ends here too.open(...args) {this._done?.();+ relayed.get(this)?.controller?.abort();+ relayed.delete(this);super.open(...args);+ const url = args.length > 1 ? resolve(String(args[1])) : null;+ const async = args.length < 3 || Boolean(args[2]);+ if (async && ownSite(url) && !cfg.anyOrigin) relayed.set(this, { method: String(args[0]).toUpperCase(), url, headers: [] });+ }+ setRequestHeader(name, value) {+ super.setRequestHeader(name, value);+ relayed.get(this)?.headers.push([String(name), String(value)]);}send(body) {+ const state = relayed.get(this);+ // As the browser's: one already sent, or ended, can't be sent again before open().+ if (state && (state.controller || state.readyState !== undefined)) {+ throw new DOMException("The object's state must be OPENED.", "InvalidStateError");+ }const done = track();// Listening first: a synchronous request fires loadend inside send().this.addEventListener("loadend", done, { once: true });- // A send the browser refuses (one already in flight) leaves that one counted.- try { super.send(body); } catch (error) { this.removeEventListener("loadend", done); done(); throw error; }+ if (state && bodySize(body) <= MAX_RELAY_BODY) this.#relay(state, body);+ else {+ relayed.delete(this);+ // A send the browser refuses (one already in flight) leaves that one counted.+ try { super.send(body); } catch (error) { this.removeEventListener("loadend", done); done(); throw error; }+ }if (this._done !== done) this._done?.();this._done = done;}+ #relay(state, body) {+ const controller = (state.controller = new AbortController());+ // XHR's request error steps: DONE, then readystatechange, the error's event and loadend; an+ // abort leaves it UNSENT.+ const fail = (type) => {+ if (relayed.get(this) !== state || state.reply || state.readyState !== undefined) return;+ clearTimeout(timer);+ controller.abort();+ state.readyState = 4;+ this.dispatchEvent(new Event("readystatechange"));+ this.dispatchEvent(new ProgressEvent(type));+ this.dispatchEvent(new ProgressEvent("loadend"));+ if (type === "abort") state.readyState = 0;+ };+ const timer = this.timeout > 0 ? setTimeout(() => fail("timeout"), this.timeout) : 0;+ state.fail = fail;+ this.dispatchEvent(new ProgressEvent("loadstart"));+ // The body as bytes, with the Content-Type the browser would give it.+ const bytes = bodiless(state.method) || body == null ? Promise.resolve(null) : (async () => {+ // XHR serializes a Document, which Fetch's bodies don't take, as UTF-8 HTML or XML.+ const html = body instanceof Document && body.contentType === "text/html";+ const content = !(body instanceof Document) ? body+ : html ? (body.doctype ? "<!DOCTYPE " + body.doctype.name + ">" : "") + (body.documentElement?.outerHTML ?? "")+ : new XMLSerializer().serializeToString(body);+ const request = new Request(state.url, { method: "POST", body: content });+ const type = !(body instanceof Document) ? request.headers.get("content-type")+ : (html ? "text/html" : "application/xml") + ";charset=UTF-8";+ const own = state.headers.filter(([name]) => name.toLowerCase() === "content-type");+ if (!own.length) { if (type) state.headers.push(["Content-Type", type]); }+ // Sent as UTF-8, so the browser relabels the page's charset as UTF-8, other parameters kept.+ else if (typeof content === "string" || content instanceof URLSearchParams) {+ for (const header of own) header[1] = header[1].replace(/(;\s*charset=)("?)[^;"]*/i, "$1$2UTF-8");+ }+ return request.arrayBuffer();+ })();+ // The browser's answer, when it can read one, is read whole like the panel's.+ const browserReply = async () => {+ let request;+ try { request = new Request(state.url, { method: state.method, headers: state.headers, signal: controller.signal }); } catch { return null; }+ const response = await fromBrowser(request);+ if (!response) return null;+ return { status: response.status, statusText: response.statusText, url: response.url, headers: [...response.headers], body: await response.arrayBuffer() };+ };+ bytes+ .then(async (content) => (content === null && (await browserReply()))+ || relay({ url: state.url, method: state.method, headers: state.headers, body: content, redirect: "follow" }, controller.signal))+ .then((reply) => {+ if (relayed.get(this) !== state || state.readyState !== undefined) return;+ clearTimeout(timer);+ const type = headerMap(reply.headers).get("content-type");+ const link = URL.createObjectURL(new Blob([reply.body], type ? { type } : {}));+ this.addEventListener("loadend", () => URL.revokeObjectURL(link), { once: true });+ state.reply = reply;+ super.open("GET", link);+ super.send();+ }, () => fail("error"));+ }+ abort() {+ const state = relayed.get(this);+ if (state?.fail && !state.reply) state.fail("abort");+ else super.abort();+ }+ get readyState() { return relayed.get(this)?.readyState ?? super.readyState; }+ get status() { return answered(this)?.status ?? super.status; }+ get statusText() { return answered(this)?.statusText ?? super.statusText; }+ get responseURL() { const reply = answered(this); return reply ? withoutHash(reply.url) : super.responseURL; }+ getResponseHeader(name) {+ const reply = answered(this);+ return reply ? (headerMap(reply.headers).get(String(name).toLowerCase()) ?? null) : super.getResponseHeader(name);+ }+ getAllResponseHeaders() {+ const reply = answered(this);+ if (!reply) return super.getAllResponseHeaders();+ return [...headerMap(reply.headers)]+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))+ .map(([name, value]) => name + ": " + value + "\r\n")+ .join("");+ }};"""diff --git a/studio/backend/tests/test_browser_page_request.py b/studio/backend/tests/test_browser_page_request.pynew file mode 100644index 00000000000..dd01674b30d--- /dev/null+++ b/studio/backend/tests/test_browser_page_request.py@@ -0,0 +1,686 @@+# 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 page's requests to its own site (POST /api/browser/request): made as a browser makes a+same-origin request, through the SSRF-guarded fetcher, and only to the page's own origin."""++from __future__ import annotations++import base64+import hashlib+import io+import sys+import threading+from email.message import Message+from pathlib import Path+from urllib.error import HTTPError+from urllib.parse import urlsplit++import pytest+from fastapi import HTTPException+from pydantic import ValidationError++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++import routes.browser as browser_mod+from core.inference import tools++PAGE = "https://site.test/models#top"+++def _message(headers: dict) -> Message:+ message = Message()+ for name, value in headers.items():+ message[name] = value+ return message+++class _Resp:+ reason = "OK"++ def __init__(self, status: int, body: bytes, headers: dict):+ self._status = status+ self._body = body+ self.headers = _message(headers)++ def getcode(self) -> int:+ return self._status++ def read(self, n: int | None = None) -> bytes:+ chunk, self._body = (self._body, b"") if n is None else (self._body[:n], self._body[n:])+ return chunk++ def close(self) -> None:+ pass+++def _serve(monkeypatch, routes: dict) -> list[dict]:+ """``routes``: "host/path" -> (status, body, headers); returns what each hop sent."""+ sent = []++ class Opener:+ def open(+ self,+ req,+ timeout = None,+ ):+ key = f"{req.get_header('Host')}{urlsplit(req.full_url).path}"+ sent.append(+ {+ "url": key,+ "method": req.get_method(),+ "data": req.data,+ "headers": {name.lower(): value for name, value in req.header_items()},+ }+ )+ status, body, headers = routes[key]+ if status >= 300:+ raise HTTPError(req.full_url, status, "Moved", _message(headers), io.BytesIO(body))+ return _Resp(status, body, headers)++ monkeypatch.setattr(+ tools, "_validate_and_resolve_host", lambda host, port: (True, "", ["93.184.216.34"])+ )+ monkeypatch.setattr(tools.urllib.request, "build_opener", lambda *a, **k: Opener())+ return sent+++def _request(**fields) -> dict:+ return browser_mod._page_request(+ browser_mod.BrowserPageRequest(page = PAGE, **fields), threading.Event()+ )+++def test_a_same_origin_request_comes_back_as_the_site_sent_it(monkeypatch):+ sent = _serve(+ monkeypatch,+ {+ "site.test/models-json": (+ 200,+ b'{"models": []}',+ {"Content-Type": "application/json", "X-Total": "3", "Set-Cookie": "s=1"},+ )+ },+ )+ answer = _request(+ url = "https://site.test/models-json?search=q", headers = [["X-Requested-With", "page"]]+ )+ assert answer["status"] == 200+ assert answer["redirected"] is False and answer["cors"] is False+ assert answer["url"] == "https://site.test/models-json?search=q"+ assert base64.b64decode(answer["body"]) == b'{"models": []}'+ headers = dict(answer["headers"])+ # Same-origin: every header but Set-Cookie is the page's to read.+ assert headers["X-Total"] == "3"+ assert "Set-Cookie" not in headers+ hop = sent[0]+ assert hop["method"] == "GET"+ assert hop["headers"]["x-requested-with"] == "page"+ # What a browser adds, the fragment left out of Referer; a GET has no Origin.+ assert hop["headers"]["referer"] == "https://site.test/models"+ assert hop["headers"]["sec-fetch-site"] == "same-origin"+ assert "origin" not in hop["headers"]+++def test_an_error_status_is_the_pages_to_handle(monkeypatch):+ _serve(+ monkeypatch,+ {"site.test/api": (404, b'{"error": "none"}', {"Content-Type": "application/json"})},+ )+ answer = _request(url = "https://site.test/api")+ assert answer["status"] == 404+ assert base64.b64decode(answer["body"]) == b'{"error": "none"}'+++def test_a_put_carries_its_body_type_and_origin(monkeypatch):+ sent = _serve(monkeypatch, {"site.test/api": (200, b"ok", {})})+ _request(+ url = "https://site.test/api",+ method = "put",+ headers = [["Content-Type", "application/json"]],+ body = base64.b64encode(b'{"a":1}').decode(),+ )+ hop = sent[0]+ assert hop["method"] == "PUT"+ assert hop["data"] == b'{"a":1}'+ assert hop["headers"]["origin"] == "https://site.test"+ assert hop["headers"]["content-type"] == "application/json"+++ "page, referer",+ [+ (+ "https://site.test/models?note=日本#top",+ "https://site.test/models?note=%E6%97%A5%E6%9C%AC",+ ),+ ("https://пример.рф/вики", "https://xn--e1afmkfd.xn--p1ai/%D0%B2%D0%B8%D0%BA%D0%B8"),+ ],+)+def test_a_typed_non_ascii_address_is_sent_encoded_as_the_referer(monkeypatch, page, referer):+ host = urlsplit(referer).hostname+ sent = _serve(monkeypatch, {f"{host}/api": (200, b"ok", {})})+ browser_mod._page_request(+ browser_mod.BrowserPageRequest(page = page, url = f"https://{host}/api"),+ threading.Event(),+ )+ assert sent[0]["headers"]["referer"] == referer+++def test_an_ipv6_pages_origin_is_bracketed(monkeypatch):+ sent = _serve(monkeypatch, {"[2606:4700:4700::1111]:8443/api": (200, b"ok", {})})+ browser_mod._page_request(+ browser_mod.BrowserPageRequest(+ page = "https://[2606:4700:4700::1111]:8443/",+ url = "https://[2606:4700:4700::1111]:8443/api",+ method = "POST",+ ),+ threading.Event(),+ )+ assert sent[0]["headers"]["origin"] == "https://[2606:4700:4700::1111]:8443"+++ "url",+ [+ "https://other.test/api",+ "http://site.test/api",+ "https://site.test:8443/api",+ "ftp://site.test/x",+ ],+)+def test_only_the_pages_own_origin(monkeypatch, url):+ sent = _serve(monkeypatch, {})+ with pytest.raises(HTTPException) as raised:+ _request(url = url)+ assert raised.value.status_code == 403+ assert sent == []+++def test_headers_no_page_could_send_are_dropped(monkeypatch):+ sent = _serve(monkeypatch, {"site.test/api": (200, b"", {})})+ _request(+ url = "https://site.test/api",+ headers = [+ ["Cookie", "a=1"],+ ["Host", "evil.test"],+ ["Origin", "https://evil.test"],+ ["Sec-Fetch-Site", "none"],+ ["Proxy-Authorization", "x"],+ ["Bad Name", "x"],+ ["X-Split", "a\r\nInjected: 1"],+ ["X-Ok", "1"],+ ],+ )+ headers = sent[0]["headers"]+ assert "cookie" not in headers+ assert "proxy-authorization" not in headers+ assert "x-split" not in headers and "injected" not in headers+ assert headers["host"] == "site.test"+ assert headers["sec-fetch-site"] == "same-origin"+ assert headers["x-ok"] == "1"+++def test_a_repeated_header_is_combined_as_setrequestheader_does(monkeypatch):+ sent = _serve(monkeypatch, {"site.test/api": (200, b"", {})})+ _request(+ url = "https://site.test/api",+ headers = [["Accept", "text/html"], ["accept", "application/json"], ["X-Tag", "a"]],+ )+ headers = sent[0]["headers"]+ assert headers["accept"] == "text/html, application/json"+ assert headers["x-tag"] == "a"+++def test_a_body_that_does_not_match_its_integrity_is_a_network_error(monkeypatch):+ _serve(monkeypatch, {"site.test/app.wasm": (200, b"wasm", {})})+ good = "sha256-" + base64.b64encode(hashlib.sha256(b"wasm").digest()).decode()+ assert (+ base64.b64decode(_request(url = "https://site.test/app.wasm", integrity = good)["body"])+ == b"wasm"+ )+ bad = "sha256-" + base64.b64encode(hashlib.sha256(b"other").digest()).decode()+ with pytest.raises(HTTPException) as raised:+ _request(url = "https://site.test/app.wasm", integrity = bad)+ assert raised.value.status_code == 502+++def test_forbidden_methods_are_refused(monkeypatch):+ sent = _serve(monkeypatch, {})+ with pytest.raises(HTTPException) as raised:+ _request(url = "https://site.test/api", method = "TRACE")+ assert raised.value.status_code == 400+ assert sent == []+++def test_a_redirect_to_another_site_needs_its_cors_consent(monkeypatch):+ sent = _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": "https://other.test/data"}),+ "other.test/data": (200, b"secret", {"Content-Type": "text/plain"}),+ },+ )+ with pytest.raises(HTTPException) as raised:+ _request(url = "https://site.test/go", headers = [["Authorization", "Bearer page"]])+ assert raised.value.status_code == 502+ # The page's own headers stayed with its own site.+ assert sent[0]["headers"]["authorization"] == "Bearer page"+ assert "authorization" not in sent[1]["headers"]+++def test_a_redirect_to_a_site_that_allows_any_origin_is_read(monkeypatch):+ _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": "https://other.test/data"}),+ "other.test/data": (200, b"public", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ answer = _request(url = "https://site.test/go")+ assert answer["url"] == "https://other.test/data"+ assert base64.b64decode(answer["body"]) == b"public"+ assert answer["redirected"] is True and answer["cors"] is True++[email protected]("allow", [None, "null"])+def test_a_chain_back_to_the_pages_origin_stays_cors(monkeypatch, allow):+ # Back on the page's origin after another site, the request is tainted: Origin is "null".+ _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": "https://sso.test/bounce"}),+ "sso.test/bounce": (+ 302,+ b"",+ {"Location": "https://site.test/home", "Access-Control-Allow-Origin": "*"},+ ),+ "site.test/home": (+ 200,+ b"home",+ {"Access-Control-Allow-Origin": allow} if allow else {},+ ),+ },+ )+ if allow is None:+ with pytest.raises(HTTPException):+ _request(url = "https://site.test/go")+ return+ answer = _request(url = "https://site.test/go")+ assert answer["url"] == "https://site.test/home"+ assert answer["redirected"] is True and answer["cors"] is True+++ "expose, readable",+ [+ (None, {"Content-Type"}),+ ("X-Request-Id", {"Content-Type", "X-Request-Id"}),+ ("*", {"Content-Type", "X-Request-Id", "X-Internal-Token"}),+ ],+)+def test_a_response_from_another_site_shows_only_the_headers_it_exposes(+ monkeypatch, expose, readable+):+ headers = {+ "Access-Control-Allow-Origin": "*",+ "Content-Type": "text/plain",+ "X-Request-Id": "r1",+ "X-Internal-Token": "secret",+ }+ if expose:+ headers["Access-Control-Expose-Headers"] = expose+ _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": "https://cdn.test/data"}),+ "cdn.test/data": (200, b"public", headers),+ },+ )+ shown = {name for name, _value in _request(url = "https://site.test/go")["headers"]}+ assert shown - {"Access-Control-Allow-Origin", "Access-Control-Expose-Headers"} == readable++[email protected]("allow, readable", [("https://site.test", True), ("null", False)])+def test_a_redirect_to_another_site_carries_the_pages_origin(monkeypatch, allow, readable):+ # A first hop off the page's origin is not tainted: Origin is the page's, which the site must allow.+ sent = _serve(+ monkeypatch,+ {+ "site.test/file": (302, b"", {"Location": "https://bucket.test/file"}),+ "bucket.test/file": (200, b"data", {"Access-Control-Allow-Origin": allow}),+ },+ )+ if readable:+ assert base64.b64decode(_request(url = "https://site.test/file")["body"]) == b"data"+ else:+ with pytest.raises(HTTPException):+ _request(url = "https://site.test/file")+ assert "origin" not in sent[0]["headers"]+ assert sent[1]["headers"]["origin"] == "https://site.test"+++def test_a_second_hop_between_other_sites_sends_a_null_origin(monkeypatch):+ sent = _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": "https://a.test/go"}),+ "a.test/go": (+ 302,+ b"",+ {+ "Location": "https://b.test/data",+ "Access-Control-Allow-Origin": "https://site.test",+ },+ ),+ "b.test/data": (200, b"public", {"Access-Control-Allow-Origin": "null"}),+ },+ )+ assert base64.b64decode(_request(url = "https://site.test/go")["body"]) == b"public"+ assert [hop["headers"].get("origin") for hop in sent] == [None, "https://site.test", "null"]+++def test_a_same_origin_redirect_still_shows_every_header(monkeypatch):+ _serve(+ monkeypatch,+ {+ "site.test/old": (301, b"", {"Location": "/new"}),+ "site.test/new": (200, b"", {"X-Internal-Token": "mine"}),+ },+ )+ answer = _request(url = "https://site.test/old")+ assert ["X-Internal-Token", "mine"] in answer["headers"]+ assert answer["redirected"] is True and answer["cors"] is False+++def test_redirect_error_fails_at_the_redirect(monkeypatch):+ sent = _serve(+ monkeypatch,+ {"site.test/a": (302, b"", {"Location": "/b"}), "site.test/b": (200, b"", {})},+ )+ with pytest.raises(HTTPException) as raised:+ _request(url = "https://site.test/a", redirect = "error")+ assert raised.value.status_code == 502+ assert [hop["url"] for hop in sent] == ["site.test/a"]+++def test_redirect_manual_stops_at_an_opaque_redirect(monkeypatch):+ sent = _serve(+ monkeypatch,+ {+ "site.test/a": (302, b"", {"Location": "/b", "X-Secret": "1"}),+ "site.test/b": (200, b"", {}),+ },+ )+ answer = _request(url = "https://site.test/a", redirect = "manual")+ assert answer["opaqueRedirect"] is True+ assert answer["status"] == 0 and answer["headers"] == [] and answer["body"] == ""+ assert [hop["url"] for hop in sent] == ["site.test/a"]++[email protected]("target", ["https://site.test:8443/data"])+def test_the_pages_headers_stay_with_its_origin_not_just_its_host(monkeypatch, target):+ # Same host, another port: another origin, so Authorization is not forwarded.+ host_path = target.split("://", 1)[1]+ sent = _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": target}),+ host_path: (200, b"public", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ _request(url = "https://site.test/go", headers = [["Authorization", "Bearer page"]])+ assert sent[0]["headers"]["authorization"] == "Bearer page"+ assert "authorization" not in sent[1]["headers"]+ assert sent[1]["headers"].get("referer") == "https://site.test/"++[email protected]("target", ["http://site.test/data", "http://other.test/data"])+def test_an_https_page_never_follows_a_redirect_to_plain_http(monkeypatch, target):+ sent = _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": target}),+ target.split("://", 1)[1]: (200, b"injected", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ with pytest.raises(HTTPException) as caught:+ _request(url = "https://site.test/go")+ assert caught.value.status_code == 502+ assert [hop["url"] for hop in sent] == ["site.test/go"]++[email protected]("allow", [None, "*"])+def test_a_redirect_from_another_site_is_cors_checked_before_it_is_followed(monkeypatch, allow):+ # site -> a.test -> b.test: a.test's redirect must allow the page too, as in a browser.+ sent = _serve(+ monkeypatch,+ {+ "site.test/go": (302, b"", {"Location": "https://a.test/go"}),+ "a.test/go": (+ 302,+ b"",+ {+ "Location": "https://b.test/data",+ **({"Access-Control-Allow-Origin": allow} if allow else {}),+ },+ ),+ "b.test/data": (200, b"public", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ if allow is None:+ with pytest.raises(HTTPException):+ _request(url = "https://site.test/go")+ assert [hop["url"] for hop in sent] == ["site.test/go", "a.test/go"]+ else:+ assert base64.b64decode(_request(url = "https://site.test/go")["body"]) == b"public"+++def test_a_post_redirected_as_a_get_to_another_site_drops_its_content_type(monkeypatch):+ sent = _serve(+ monkeypatch,+ {+ "site.test/upload": (303, b"", {"Location": "https://cdn.test/result"}),+ "cdn.test/result": (200, b"done", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ _request(+ url = "https://site.test/upload",+ method = "POST",+ headers = [["Content-Type", "text/plain"]],+ body = base64.b64encode(b"x").decode(),+ )+ assert "content-type" not in sent[1]["headers"]+ assert sent[1]["headers"]["origin"] == "https://site.test"+++def test_safelisted_headers_follow_a_redirect_to_another_site(monkeypatch):+ # A video's Range goes on to the CDN, as in a browser; Authorization and custom headers don't.+ sent = _serve(+ monkeypatch,+ {+ "site.test/video": (302, b"", {"Location": "https://cdn.test/video"}),+ "cdn.test/video": (206, b"part", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ answer = _request(+ url = "https://site.test/video",+ headers = [+ ["Range", "bytes=0-3"],+ ["Accept", "video/mp4"],+ ["Authorization", "Bearer page"],+ ["X-Custom", "1"],+ ],+ )+ assert answer["status"] == 206+ cdn = sent[1]["headers"]+ assert cdn["range"] == "bytes=0-3" and cdn["accept"] == "video/mp4"+ assert cdn["origin"] == "https://site.test"+ assert "authorization" not in cdn and "x-custom" not in cdn+ # Origin-only, as the default referrer policy sends it to another site.+ assert cdn["referer"] == "https://site.test/"+++ "name, value, safe",+ [+ ("Range", "bytes=0-", True),+ ("Range", "bytes=-500", False),+ ("Range", "bytes=0-1,5-6", False),+ ("Accept-Language", "en-US,en;q=0.9", True),+ ("Accept", "application/json", True),+ ("Accept", "a:b", False),+ ("Content-Type", "text/plain;charset=UTF-8", True),+ ("Content-Type", "application/json", False),+ ("Content-Type", 'text/plain;a="b"', False),+ ],+)+def test_cors_safelisted_request_headers(name, value, safe):+ assert browser_mod._cors_safelisted(name, value) is safe++[email protected]("status, method", [(307, "DELETE"), (308, "PUT"), (307, "POST")])+def test_a_write_redirected_to_another_site_is_never_sent_there(monkeypatch, status, method):+ sent = _serve(+ monkeypatch,+ {+ "site.test/api": (status, b"", {"Location": "https://other.test/api"}),+ "other.test/api": (200, b"", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ with pytest.raises(HTTPException) as raised:+ _request(url = "https://site.test/api", method = method, body = base64.b64encode(b"x").decode())+ assert raised.value.status_code == 502+ assert [hop["url"] for hop in sent] == ["site.test/api"]+++def test_a_post_redirected_as_a_get_may_go_to_another_site(monkeypatch):+ # 303 turns it into a bodiless GET, which a browser sends to another site without a preflight.+ sent = _serve(+ monkeypatch,+ {+ "site.test/upload": (303, b"", {"Location": "https://cdn.test/result"}),+ "cdn.test/result": (200, b"done", {"Access-Control-Allow-Origin": "*"}),+ },+ )+ answer = _request(+ url = "https://site.test/upload", method = "POST", body = base64.b64encode(b"x").decode()+ )+ assert base64.b64decode(answer["body"]) == b"done"+ assert sent[1]["method"] == "GET" and sent[1]["data"] is None+++def test_a_write_redirected_as_a_get_drops_its_body_headers(monkeypatch):+ sent = _serve(+ monkeypatch,+ {+ "site.test/form": (303, b"", {"Location": "/done"}),+ "site.test/done": (200, b"ok", {}),+ },+ )+ _request(+ url = "https://site.test/form",+ method = "POST",+ headers = [["Content-Type", "application/json"], ["X-Csrf", "t"]],+ body = base64.b64encode(b"{}").decode(),+ )+ first, second = (hop["headers"] for hop in sent)+ assert first["content-type"] == "application/json" and first["origin"] == "https://site.test"+ assert sent[1]["method"] == "GET"+ assert "content-type" not in second and "origin" not in second+ assert second["x-csrf"] == "t"+++ "status, method, kept",+ [+ (301, "POST", False),+ (302, "POST", False),+ (303, "PUT", False),+ (303, "HEAD", True),+ (301, "PUT", True),+ (307, "POST", True),+ (308, "DELETE", True),+ ],+)+def test_redirects_change_the_method_as_browsers_do(monkeypatch, status, method, kept):+ sent = _serve(+ monkeypatch,+ {"site.test/a": (status, b"", {"Location": "/b"}), "site.test/b": (200, b"", {})},+ )+ _request(url = "https://site.test/a", method = method, body = base64.b64encode(b"x").decode())+ second = sent[1]+ if kept:+ assert second["method"] == method+ assert second["data"] == (None if method == "HEAD" else b"x")+ else:+ assert second["method"] == "GET"+ assert second["data"] is None+++def test_a_form_post_without_a_method_keeps_its_old_redirect(monkeypatch):+ # The panel's own form posts (no method given) still become a GET on 302.+ sent = _serve(+ monkeypatch,+ {"site.test/form": (302, b"", {"Location": "/done"}), "site.test/done": (200, b"", {})},+ )+ tools._fetch_url_raw("https://site.test/form", timeout = 5, post_data = b"q=1", raw_bytes_max = 100)+ assert [hop["method"] for hop in sent] == ["POST", "GET"]+ assert sent[0]["headers"]["content-type"] == "application/x-www-form-urlencoded"+++def test_a_body_past_the_cap_is_refused():+ with pytest.raises(ValidationError):+ browser_mod.BrowserPageRequest(+ page = PAGE,+ url = "https://site.test/api",+ method = "POST",+ body = "A" * (4 * (browser_mod._MAX_PAGE_REQUEST_BODY // 3 + 2)),+ )+++class _Client:+ def __init__(self, disconnected = False):+ self.disconnected = disconnected++ async def is_disconnected(self):+ return self.disconnected+++def test_page_requests_hold_a_slot_until_their_worker_ends(monkeypatch):+ import asyncio++ slots = threading.BoundedSemaphore(1)+ monkeypatch.setattr(browser_mod, "_PAGE_REQUEST_SLOTS", slots)+ release, finished = threading.Event(), threading.Event()++ def stalled(request, cancel_event):+ release.wait(10)+ finished.set()+ return {}++ monkeypatch.setattr(browser_mod, "_page_request", stalled)+ request = browser_mod.BrowserPageRequest(page = PAGE, url = PAGE)++ def call(client):+ return asyncio.run(+ browser_mod.browser_page_request(request, client, current_subject = "user")+ )++ # The page went away, but its worker is still stuck on the site: the slot stays taken.+ with pytest.raises(HTTPException) as caught:+ call(_Client(disconnected = True))+ assert caught.value.status_code == 499+ with pytest.raises(HTTPException) as caught:+ call(_Client())+ assert caught.value.status_code == 503+ release.set()+ assert finished.wait(10)+ for _ in range(100):+ if slots.acquire(blocking = False):+ break+ threading.Event().wait(0.05)+ else:+ pytest.fail("the slot was never given back")diff --git a/studio/backend/tests/test_browser_routes.py b/studio/backend/tests/test_browser_routes.pyindex fdf6b121926..f30677837b5 100644--- a/studio/backend/tests/test_browser_routes.py+++ b/studio/backend/tests/test_browser_routes.py@@ -106,6 +106,7 @@ def test_the_shells_base_follows_the_router_only_without_a_page_base():# Chromium, as this suite has no browser to run the shell in.shell = browser_mod._FRAME_HTMLassert "ownBase: data.ownBase === true" in shell+ assert "anyOrigin: data.anyOrigin === true" in shellassert 'cfg.url && !cfg.ownBase ? document.querySelector("base") : null' in shellassert shell.index("pageUrl = next;") < shell.index('routedBase?.setAttribute("href", pageUrl);'@@ -230,9 +231,23 @@ def test_html_comes_back_as_json_with_its_final_url(monkeypatch):payload = json.loads(response.body)assert payload["url"] == payload["base"] == "https://example.com/final"assert payload["ownBase"] is False+ assert payload["anyOrigin"] is Falseassert "<title>x</title>" in payload["html"]+ "allow, any_origin", [("*", True), ("null", True), ("https://a.test", False)]+)+def test_a_page_whose_site_answers_any_origin_says_so(monkeypatch, allow, any_origin):+ # The shell then leaves the page's own-site requests to the browser, which can read them.+ _fetch(+ monkeypatch,+ (None, b"<html></html>", "text/html"),+ {"url": "https://example.com/", "charset": "utf-8", "allow_origin": allow},+ )+ assert json.loads(_call(url = "example.com").body)["anyOrigin"] is any_origin++def test_a_page_base_equal_to_its_url_is_still_its_own(monkeypatch):# <base href="/"> on the site root: the shell must keep it fixed when the router moves._fetch(diff --git a/studio/frontend/src/features/browser/api.ts b/studio/frontend/src/features/browser/api.tsindex d5e17718ae6..d1e4be4237c 100644--- a/studio/frontend/src/features/browser/api.ts+++ b/studio/frontend/src/features/browser/api.ts@@ -3,6 +3,7 @@import { authFetch } from "@/features/auth";import { apiUrl } from "@/lib/api-base";+import type { PageRequest } from "./frame-message";export type BrowserPage =| {@@ -11,6 +12,8 @@ export type BrowserPage =base: string;/** The page declared its <base>, so it stays put when the page's router moves. */ownBase?: boolean;+ /** Its site sent Access-Control-Allow-Origin * or null, so the sandbox reads its requests itself. */+ anyOrigin?: boolean;html: string;refresh: { delay: number; url: string } | null;}@@ -94,6 +97,53 @@ export async function fetchBrowserPage(request: BrowserRequest, signal: AbortSig};}+export type PageResponse = {+ status: number;+ statusText: string;+ url: string;+ headers: Array<[string, string]>;+ body: ArrayBuffer;+ /** A redirect the request said to stop at ("manual"): the page gets an opaque redirect. */+ opaqueRedirect?: boolean;+ redirected?: boolean;+ /** The chain went through another site, so it reads as a CORS response. */+ cors?: boolean;+};++function toBase64(bytes: Uint8Array): string {+ let binary = "";+ for (let at = 0; at < bytes.length; at += 0x8000) binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000));+ return btoa(binary);+}++function fromBase64(text: string): ArrayBuffer {+ const binary = atob(text);+ const bytes = new Uint8Array(binary.length);+ for (let at = 0; at < binary.length; at++) bytes[at] = binary.charCodeAt(at);+ return bytes.buffer;+}++/** A page's request to its own site, made by the backend for the page at `page`; throws when it fails. */+export async function requestForPage(page: string, request: PageRequest, signal: AbortSignal): Promise<PageResponse> {+ const response = await authFetch("/api/browser/request", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ page,+ url: request.url,+ method: request.method,+ headers: request.headers,+ redirect: request.redirect,+ integrity: request.integrity,+ body: request.body ? toBase64(new Uint8Array(request.body)) : null,+ }),+ signal,+ });+ if (!response.ok) throw new Error(`HTTP ${response.status}`);+ const answer = (await response.json()) as Omit<PageResponse, "body"> & { body: string };+ return { ...answer, body: fromBase64(answer.body) };+}+export function browserFrameUrl(loadId: string): string {return apiUrl(`/api/browser/frame?v=${encodeURIComponent(loadId)}`);}diff --git a/studio/frontend/src/features/browser/frame-message.ts b/studio/frontend/src/features/browser/frame-message.tsindex 97e9a1e4f0d..91e014391ab 100644--- a/studio/frontend/src/features/browser/frame-message.ts+++ b/studio/frontend/src/features/browser/frame-message.ts@@ -27,7 +27,25 @@ export type FrameMessage =| { type: "zoom"; direction: 1 | -1 | 0; wheel: boolean }| { type: "annotate"; event: AnnotateEvent }/** The browser agent's answer to command `id` (agent/page-agent.ts); the panel checks `result`. */- | { type: "agent"; id: string; installed: boolean; result: Record<string, unknown> | null };+ | { type: "agent"; id: string; installed: boolean; result: Record<string, unknown> | null }+ /** A fetch or XHR to the page's own site, for the panel to make (routes/browser_network.py);+ * `request` is null for one the panel won't make (malformed, too large), which then fails. */+ | { type: "request"; id: string; request: PageRequest | null }+ | { type: "requestAbort"; id: string };++export type RedirectMode = "follow" | "error" | "manual";++export type PageRequest = {+ url: string;+ method: string;+ headers: Array<[string, string]>;+ body: ArrayBuffer | null;+ redirect: RedirectMode;+ /** Fetch's integrity metadata, checked against the body. */+ integrity: string;+};++const REDIRECT_MODES = new Set<unknown>(["follow", "error", "manual"]);export type AnnotateRect = { left: number; top: number; width: number; height: number };@@ -59,6 +77,10 @@ const MAX_QUOTE_CHARS = 300;export const MAX_MARKS = 500;// Far past any screen, so a page can't make the panel draw something huge.const MAX_COORD = 1_000_000;+// The request endpoint's limits.+const MAX_REQUEST_HEADERS = 64;+const MAX_HEADER_NAME_CHARS = 256;+const MAX_REQUEST_BODY_BYTES = 1024 * 1024;const text = (value: unknown, max: number): string | null =>typeof value === "string" && value.length <= max ? value : null;@@ -116,6 +138,31 @@ function annotateEvent(message: Record<string, unknown>): AnnotateEvent | null {}}+function headerList(value: unknown): Array<[string, string]> | null {+ if (!Array.isArray(value) || value.length > MAX_REQUEST_HEADERS) return null;+ const headers: Array<[string, string]> = [];+ for (const entry of value) {+ const name = Array.isArray(entry) && entry.length === 2 ? text(entry[0], MAX_HEADER_NAME_CHARS) : null;+ const content = name === null ? null : text(entry[1], MAX_URL_CHARS);+ if (name === null || content === null) return null;+ headers.push([name, content]);+ }+ return headers;+}++function pageRequest(message: Record<string, unknown>): PageRequest | null {+ const url = text(message.url, MAX_URL_CHARS);+ const method = text(message.method, 16);+ const headers = headerList(message.headers);+ const body = message.body;+ if (!url || !method || !/^[A-Za-z]+$/.test(method) || !headers) return null;+ if (body !== null && !(body instanceof ArrayBuffer && body.byteLength <= MAX_REQUEST_BODY_BYTES)) return null;+ const redirect = message.redirect ?? "follow";+ const integrity = text(message.integrity ?? "", 4096);+ if (!REDIRECT_MODES.has(redirect) || integrity === null) return null;+ return { url, method, headers, body, redirect: redirect as RedirectMode, integrity };+}+/** A message from a page, checked field by field: pages can post anything. */export function parseFrameMessage(data: unknown): FrameMessage | null {if (!data || typeof data !== "object") return null;@@ -177,6 +224,12 @@ export function parseFrameMessage(data: unknown): FrameMessage | null {: null;return { type: "agent", id, installed: message.installed === true, result };}+ case "request":+ case "requestAbort": {+ const id = text(message.id, 64);+ if (!id) return null;+ return message.type === "request" ? { type: "request", id, request: pageRequest(message) } : { type: "requestAbort", id };+ }default:return null;}diff --git a/studio/frontend/src/features/browser/page-frame.tsx b/studio/frontend/src/features/browser/page-frame.tsxindex 2030df3bce9..60913bb8b93 100644--- a/studio/frontend/src/features/browser/page-frame.tsx+++ b/studio/frontend/src/features/browser/page-frame.tsx@@ -2,8 +2,9 @@// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0import { useEffect, useRef, useState } from "react";-import { browserFrameUrl } from "./api";+import { browserFrameUrl, type PageResponse, requestForPage } from "./api";import { type AnnotateEvent, type FrameMessage, parseFrameMessage } from "./frame-message";+import { pageRequests } from "./page-requests";export type { FrameMessage };@@ -21,7 +22,8 @@ export type FrameCommand =| { command: "annotate"; on: boolean; color?: string }| { command: "annotateForget"; id: number }| { command: "annotateNumbers"; numbers: Array<[number, number]> }- | { command: "agent"; id: string; op: Record<string, unknown>; code?: string };+ | { command: "agent"; id: string; op: Record<string, unknown>; code?: string }+ | ({ command: "response"; id: string } & (({ ok: true } & PageResponse) | { ok: false }));type AgentReply = { installed: boolean; result: Record<string, unknown> | null };const agentRequests = new Map<string, (reply: AgentReply | null) => void>();@@ -107,6 +109,7 @@ export function PageFrame({url,base,ownBase = false,+ anyOrigin = false,refresh,title,tabId,@@ -121,6 +124,7 @@ export function PageFrame({url: string | null;base: string | null;ownBase?: boolean;+ anyOrigin?: boolean;refresh?: { delay: number; url: string } | null;title: string;onMessage: (message: FrameMessage) => void;@@ -128,9 +132,11 @@ export function PageFrame({const frameRef = useRef<HTMLIFrameElement | null>(null);const onMessageRef = useRef(onMessage);const tabIdRef = useRef(tabId);+ const urlRef = useRef(url);useEffect(() => {onMessageRef.current = onMessage;tabIdRef.current = tabId;+ urlRef.current = url;});const [loadId] = useState(() => String(++loadCounter));const postedRef = useRef(false);@@ -140,11 +146,28 @@ export function PageFrame({let loaded = false;let left = false;let lastUserAction = Number.NEGATIVE_INFINITY;+ // The page's requests to its own site, answered through the shell, which relays to the page.+ const requests = pageRequests(+ requestForPage,+ (id, answer) => {+ const command: FrameCommand = answer ? { command: "response", id, ok: true, ...answer } : { command: "response", id, ok: false };+ frameRef.current?.contentWindow?.postMessage({ type: "unsloth:browser-command", ...command }, "*", answer ? [answer.body] : []);+ },+ () => urlRef.current,+ );const listener = (event: MessageEvent) => {const frame = frameRef.current;if (!frame || event.source !== frame.contentWindow) return;let message = parseFrameMessage(event.data);if (!message) return;+ if (message.type === "request") {+ requests.request(message.id, message.request);+ return;+ }+ if (message.type === "requestAbort") {+ requests.abort(message.id);+ return;+ }if (message.type === "annotate") {annotateListeners.get(tabIdRef.current)?.(message.event);return;@@ -176,7 +199,10 @@ export function PageFrame({onMessageRef.current(message);};window.addEventListener("message", listener);- return () => window.removeEventListener("message", listener);+ return () => {+ window.removeEventListener("message", listener);+ requests.stop();+ };}, []);useEffect(() => {@@ -222,6 +248,7 @@ export function PageFrame({url,base,ownBase,+ anyOrigin,refresh: refresh ?? null,zoom: zoomRef.current,muted: mutedRef.current,diff --git a/studio/frontend/src/features/browser/page-requests.ts b/studio/frontend/src/features/browser/page-requests.tsnew file mode 100644index 00000000000..af8c4ffb4ef--- /dev/null+++ b/studio/frontend/src/features/browser/page-requests.ts@@ -0,0 +1,89 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import type { PageResponse } from "./api";+import type { PageRequest } from "./frame-message";++// Each one holds a connection to Studio until the site answers, and browsers keep 6 per host over+// HTTP/1.1; shared by every page so some stay free for Studio itself. Then a bounded queue per page.+const MAX_RUNNING = 3;+const MAX_QUEUED = 256;+let running = 0;+// Pages with requests waiting, in turn order. A page that starts one goes to the back, so one busy+// page can't take every slot another page is waiting for.+const turns = new Set<() => boolean>();++function schedule() {+ for (const startOne of turns) {+ if (running >= MAX_RUNNING) return;+ turns.delete(startOne);+ if (startOne()) turns.add(startOne);+ }+}++type Job = { id: string; request: PageRequest; controller: AbortController };++/** One page's requests to its own site (routes/browser_network.py), made by `send` for the page at+ * `pageUrl()`. Each gets one `reply`, null when it failed; one the page aborted gets none. */+export function pageRequests(+ send: (page: string, request: PageRequest, signal: AbortSignal) => Promise<PageResponse>,+ reply: (id: string, answer: PageResponse | null) => void,+ pageUrl: () => string | null,+) {+ const open = new Map<string, AbortController>();+ const queue: Job[] = [];+ /** Starts this page's next request; whether more are waiting. */+ const startOne = () => {+ while (queue.length) {+ const job = queue.shift() as Job;+ if (job.controller.signal.aborted) continue;+ const page = pageUrl();+ if (!page) {+ open.delete(job.id);+ reply(job.id, null);+ continue;+ }+ running++;+ send(page, job.request, job.controller.signal)+ .catch(() => null)+ .then((answer) => {+ running--;+ if (open.get(job.id) === job.controller) open.delete(job.id);+ if (!job.controller.signal.aborted) reply(job.id, answer);+ schedule();+ });+ break;+ }+ return queue.length > 0;+ };+ return {+ /** `request` null is one the panel won't make: it fails at once. */+ request(id: string, request: PageRequest | null) {+ if (!request || queue.length >= MAX_QUEUED) {+ reply(id, null);+ return;+ }+ const controller = new AbortController();+ open.get(id)?.abort();+ open.set(id, controller);+ queue.push({ id, request, controller });+ turns.add(startOne);+ schedule();+ },+ abort(id: string) {+ const controller = open.get(id);+ controller?.abort();+ open.delete(id);+ // A queued one leaves the queue, so aborted requests don't fill its bound.+ const at = queue.findIndex((job) => job.controller === controller);+ if (at >= 0) queue.splice(at, 1);+ },+ /** The page is gone: end all it asked for, queued or running. */+ stop() {+ for (const controller of open.values()) controller.abort();+ open.clear();+ queue.length = 0;+ turns.delete(startOne);+ },+ };+}diff --git a/studio/frontend/src/features/browser/tab-view.tsx b/studio/frontend/src/features/browser/tab-view.tsxindex b0b76fcee4b..91542f825e4 100644--- a/studio/frontend/src/features/browser/tab-view.tsx+++ b/studio/frontend/src/features/browser/tab-view.tsx@@ -353,6 +353,7 @@ function WebPage({url={page.url}base={page.base}ownBase={page.ownBase === true}+ anyOrigin={page.anyOrigin === true}refresh={page.refresh}title={tab.title || hostOf(page.url)}tabId={tab.id}diff --git a/studio/frontend/tests/browser-frame-message.test.ts b/studio/frontend/tests/browser-frame-message.test.tsindex 7bb5d5d2b3f..d214892dfd2 100644--- a/studio/frontend/tests/browser-frame-message.test.ts+++ b/studio/frontend/tests/browser-frame-message.test.ts@@ -77,3 +77,31 @@ test("a snapshot is markup or nothing", () => {assert.deepEqual(from({ type: "snapshot", html: 5 }), { type: "snapshot", html: null });assert.deepEqual(from({ type: "snapshot", html: "x".repeat(8 * 1024 * 1024 + 1) }), { type: "snapshot", html: null });});++test("a page's request is checked field by field, and one the panel won't make still gets an answer", () => {+ const body = new ArrayBuffer(4);+ const request = { url: "https://site.test/api", method: "POST", headers: [["X-A", "1"]], body, redirect: "manual", integrity: "sha256-x" };+ assert.deepEqual(from({ type: "request", id: "q1", ...request }), { type: "request", id: "q1", request });+ // Fetch's default redirect mode when the page names none.+ assert.deepEqual(from({ type: "request", id: "q1", ...request, body: null, redirect: undefined, integrity: undefined }), {+ type: "request",+ id: "q1",+ request: { ...request, body: null, redirect: "follow", integrity: "" },+ });+ // Without an id there is no one to answer.+ assert.equal(from({ type: "request", ...request }), null);+ for (const bad of [+ { body: new ArrayBuffer(1024 * 1024 + 1) },+ { body: "text" },+ { method: "GET POST" },+ { url: 7 },+ { headers: [["X-A"]] },+ { headers: [["X-A", 1]] },+ { headers: Array.from({ length: 65 }, () => ["X-A", "1"]) },+ { redirect: "skip" },+ ]) {+ assert.deepEqual(from({ type: "request", id: "q1", ...request, ...bad }), { type: "request", id: "q1", request: null });+ }+ assert.deepEqual(from({ type: "requestAbort", id: "q1" }), { type: "requestAbort", id: "q1" });+ assert.equal(from({ type: "requestAbort", id: 3 }), null);+});diff --git a/studio/frontend/tests/browser-network.test.ts b/studio/frontend/tests/browser-network.test.tsindex 4254ce5725e..67d39ff8594 100644--- a/studio/frontend/tests/browser-network.test.ts+++ b/studio/frontend/tests/browser-network.test.ts@@ -13,10 +13,29 @@ const source = readFileSync(new URL("../../backend/routes/browser_network.py", i/** A stand-in for the browser's own XHR: a request ends when the test fires its loadend. */class NativeXHR extends EventTarget {- open() {}- send() {}+ calls: unknown[][] = [];+ state = 0;+ get readyState() {+ return this.state;+ }+ get status() {+ return 0;+ }+ open(...args: unknown[]) {+ this.calls.push(["open", ...args]);+ this.state = 1;+ }+ setRequestHeader() {}+ send(...args: unknown[]) {+ this.calls.push(["send", ...args]);+ }+ abort() {+ this.calls.push(["abort"]);+ }}+class ProgressEvent extends Event {}+/** As a browser's: send() on a request already in flight throws InvalidStateError. */class StrictXHR extends NativeXHR {inFlight = false;@@ -33,22 +52,87 @@ class StrictXHR extends NativeXHR {}}-function setup(XHR: typeof NativeXHR = NativeXHR) {+/** Stand-ins for the page's Document and XMLSerializer, enough for an XHR's Document body. */+class PageDocument {+ contentType: string;+ markup: string;+ constructor(contentType: string, markup: string) {+ this.contentType = contentType;+ this.markup = markup;+ }+ get doctype() {+ return this.contentType === "text/html" ? { name: "html" } : null;+ }+ get documentElement() {+ return { outerHTML: this.markup };+ }+}+class PageXMLSerializer {+ serializeToString(document: PageDocument) {+ return `<?xml version="1.0"?>${document.markup}`;+ }+}++/** `pageUrl`: the page's address; with none (a local file) nothing goes through the panel.+ * `browserReads`: own-site addresses the sandbox can read itself (the site answers its origin);+ * the browser's fetch of any other own-site address fails, as CORS makes it. */+function setup(+ XHR: typeof NativeXHR = NativeXHR,+ pageUrl: string | null = null,+ anyOrigin = false,+ browserReads: (url: string) => boolean = () => false,+) {let now = 0;// The script patches its Response's body readers: a class of its own keeps that out of other tests.class PageResponse extends Response {}const answers: Array<(response: Response) => void> = [];+ const refused: string[] = [];const window = Object.assign(new EventTarget(), {- fetch: () => new Promise<Response>((resolve) => answers.push(resolve)),+ fetch: (input: unknown) =>+ new Promise<Response>((resolve, reject) => {+ const url = input instanceof Request ? input.url : String(input);+ const own = pageUrl !== null && new URL(url, pageUrl).origin === new URL(pageUrl).origin;+ if (own && !browserReads(url)) {+ refused.push(url);+ reject(new TypeError("Failed to fetch"));+ } else answers.push(resolve);+ }),XMLHttpRequest: XHR,});- runInNewContext(source, { window, Date: { now: () => now }, Response: PageResponse, Symbol, Object, Set });+ // What the page sent its parent, the shell.+ const posted: Array<Record<string, unknown>> = [];+ const parent = {};+ const base = pageUrl ?? "about:srcdoc";+ const resolve = (href: string) => {+ try {+ return new URL(href, base).href;+ } catch {+ return null;+ }+ };+ runInNewContext(source, {+ window, Date: { now: () => now }, Response: PageResponse, Symbol, Object, Set,+ cfg: { url: pageUrl, anyOrigin }, post: (message: Record<string, unknown>) => posted.push(message), resolve, parent,+ document: { baseURI: base }, URL, Request, Headers, Blob, Event, ProgressEvent, DOMException, AbortController,+ setTimeout, clearTimeout, Document: PageDocument, XMLSerializer: PageXMLSerializer, URLSearchParams,+ FormData, ArrayBuffer,+ });+ /** The panel's answer to request `id`, as the shell relays it. */+ const reply = (id: unknown, answer: Record<string, unknown>, source: unknown = parent) =>+ window.dispatchEvent(Object.assign(new Event("message"), {+ source,+ data: { type: "unsloth:browser-command", command: "response", id, ...answer },+ }));const pending = (since: number) => (window as unknown as Record<symbol, (since: number) => number>)[Symbol.for("unsloth.browser.pending")](since);- const page = window as unknown as { fetch: (url: string) => Promise<Response>; XMLHttpRequest: new () => NativeXHR };+ const page = window as unknown as { fetch: (url: string, init?: RequestInit) => Promise<Response>; XMLHttpRequest: new () => NativeXHR };return {window: page, pending, advance: (ms: number) => (now += ms),answer: (i: number, body = "{}") => answers[i](new PageResponse(body)),Response: PageResponse,+ native: answers,+ refused,+ posted,+ reply,};}@@ -121,3 +205,398 @@ test("a second send the browser refuses keeps counting the request in flight", (xhr.dispatchEvent(new Event("loadend"));assert.equal(t.pending(0), 0);});++const ok = (body: string, extra: Record<string, unknown> = {}) => ({+ ok: true,+ status: 200,+ statusText: "OK",+ url: "https://site.test/models-json?q=1",+ headers: [["Content-Type", "application/json"], ["X-Total", "3"]],+ body: new TextEncoder().encode(body).buffer,+ ...extra,+});+const tick = () => new Promise((resolve) => setTimeout(resolve, 0));+// Objects from the page script's realm, for deepEqual.+const plain = (value: unknown) => JSON.parse(JSON.stringify(value));++test("a fetch to the page's own site goes through the panel and reads as same-origin", async () => {+ const t = setup(NativeXHR, "https://site.test/models");+ const request = t.window.fetch("/models-json?q=1");+ await tick();+ assert.equal(t.native.length, 0);+ const sent = t.posted[0];+ assert.equal(sent.type, "request");+ // Resolved against the page's address, not the shell's.+ assert.equal(sent.url, "https://site.test/models-json?q=1");+ assert.equal(sent.method, "GET");+ assert.equal(sent.body, null);+ // Counted until answered, as a native request is.+ assert.equal(t.pending(0), 1);+ t.reply(sent.id, ok('{"models":[1]}'));+ const response = await request;+ assert.equal(t.pending(0), 0);+ assert.equal(response.status, 200);+ assert.equal(response.type, "basic");+ assert.equal(response.url, "https://site.test/models-json?q=1");+ assert.equal(response.redirected, false);+ // Same-origin: every header is readable, not only the CORS-safelisted ones.+ assert.equal(response.headers.get("x-total"), "3");+ assert.equal(response.clone().url, "https://site.test/models-json?q=1");+ assert.deepEqual(await response.json(), { models: [1] });+});++test("a page whose site answers any origin keeps its own requests, which the browser can read", async () => {+ const t = setup(NativeXHR, "https://site.test/", true, () => true);+ const own = t.window.fetch("/stream");+ assert.equal(t.native.length, 1);+ const xhr = new t.window.XMLHttpRequest();+ xhr.open("GET", "/api");+ xhr.send();+ await tick();+ assert.equal(t.posted.length, 0);+ assert.deepEqual(xhr.calls.at(-1), ["send", undefined]);+ t.answer(0);+ await own;+});++test("a page whose site answers any origin still relays a GET its API doesn't answer", async () => {+ const t = setup(NativeXHR, "https://site.test/", true, (url) => !url.endsWith("/same"));+ const read = t.window.fetch("/same");+ await tick();+ assert.equal(t.refused.length, 1);+ assert.equal(t.posted.length, 1);+ t.reply(t.posted[0].id, ok('{"a":1}'));+ assert.deepEqual(await (await read).json(), { a: 1 });+ // A write stays the browser's: its failure is the page's, with no panel request.+ await assert.rejects(t.window.fetch("/same", { method: "POST", body: "x" }), TypeError);+ assert.equal(t.posted.length, 1);+ assert.equal(t.refused.length, 2);+});++test("an own-site GET the browser can read stays the browser's, streamed as it was", async () => {+ const t = setup(NativeXHR, "https://site.test/", false, (url) => url.endsWith("/api/models"));+ const read = t.window.fetch("/api/models");+ await tick();+ assert.equal(t.native.length, 1);+ assert.equal(t.posted.length, 0);+ t.answer(0, "[1]");+ assert.deepEqual(await (await read).json(), [1]);+});++test("one the browser can't read goes to the panel, and that address goes there directly after", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const first = t.window.fetch("/models-json?search=a");+ await tick();+ assert.equal(t.refused.length, 1);+ assert.equal(t.posted.length, 1);+ t.reply(t.posted[0].id, ok("{}"));+ await first;+ const second = t.window.fetch("/models-json?search=ab");+ await tick();+ // Same address, another query: no second try in the browser.+ assert.equal(t.refused.length, 1);+ assert.equal(t.posted.length, 2);+ t.reply(t.posted[1].id, ok("{}"));+ await second;+});++test("a write never tries the browser first", async () => {+ const t = setup(NativeXHR, "https://site.test/", false, () => true);+ const write = t.window.fetch("/api", { method: "POST", body: "x" });+ await tick();+ assert.equal(t.native.length, 0);+ assert.equal(t.posted.length, 1);+ t.reply(t.posted[0].id, ok("{}"));+ await write;+});++test("a write past the panel's body limit stays the browser's, as before relaying", async () => {+ const t = setup(NativeXHR, "https://site.test/", false, () => true);+ const big = new Uint8Array(1024 * 1024 + 1);+ const write = t.window.fetch("/upload", { method: "POST", body: big });+ await tick();+ await tick();+ assert.equal(t.posted.length, 0);+ assert.equal(t.native.length, 1);+ t.answer(0);+ await write;+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ xhr.open("POST", "/upload");+ xhr.send(new Blob([big]));+ assert.equal(t.posted.length, 0);+ assert.equal(xhr.calls.at(-1)?.[0], "send");+ const form = new t.window.XMLHttpRequest() as PageXHR;+ form.open("POST", "/upload");+ form.send(new URLSearchParams({ q: "x".repeat(400 * 1024) }));+ assert.equal(t.posted.length, 0);+ assert.equal(form.calls.at(-1)?.[0], "send");+ // Empty values, but each multipart entry carries a boundary and headers.+ const fields = new FormData();+ for (let i = 0; i < 3000; i++) fields.append("f" + i, "");+ const multipart = new t.window.XMLHttpRequest() as PageXHR;+ multipart.open("POST", "/upload");+ multipart.send(fields);+ assert.equal(t.posted.length, 0);+ assert.equal(multipart.calls.at(-1)?.[0], "send");+});++test("aborting an upload whose stream stalls rejects the fetch at once", async () => {+ const t = setup(NativeXHR, "https://site.test/", false, () => true);+ const stream = new ReadableStream({ pull: () => new Promise(() => {}) });+ const controller = new AbortController();+ const write = t.window.fetch("/upload", { method: "POST", body: stream, duplex: "half", signal: controller.signal } as RequestInit);+ await tick();+ controller.abort();+ const outcome = await Promise.race([write.then(() => "resolved", () => "rejected"), new Promise((r) => setTimeout(() => r("pending"), 500))]);+ assert.equal(outcome, "rejected");+ assert.equal(t.posted.length, 0);+});++test("an upload past the limit is read only up to it before going to the browser", async () => {+ const t = setup(NativeXHR, "https://site.test/", false, () => true);+ let pulled = 0;+ const stream = new ReadableStream({+ pull(controller) {+ pulled++;+ controller.enqueue(new Uint8Array(64 * 1024));+ if (pulled >= 64) controller.close();+ },+ });+ const write = t.window.fetch("/upload", { method: "POST", body: stream, duplex: "half" } as RequestInit);+ for (let i = 0; i < 50 && t.native.length === 0; i++) await tick();+ assert.equal(t.posted.length, 0);+ assert.equal(t.native.length, 1);+ assert.ok(pulled < 32, `read ${pulled} of 64 chunks`);+ t.answer(0);+ await write;+});++test("an XHR GET the browser can read loads the browser's answer, with no panel request", async () => {+ const t = setup(NativeXHR, "https://site.test/", false, () => true);+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ xhr.open("GET", "/poll");+ xhr.send();+ await tick();+ assert.equal(t.posted.length, 0);+ t.answer(0, "late");+ await tick();+ await tick();+ const [open] = xhr.calls.slice(-2);+ assert.match(String(open[2]), /^blob:/);+});++test("requests to other sites, and from a page with no address, stay the browser's", async () => {+ const t = setup(NativeXHR, "https://site.test/models");+ const other = t.window.fetch("https://api.other.test/x");+ assert.equal(t.native.length, 1);+ assert.equal(t.posted.length, 0);+ t.answer(0);+ await other;+ const local = setup();+ const file = local.window.fetch("/x");+ assert.equal(local.native.length, 1);+ local.answer(0);+ await file;+});++test("a fetch body reaches the panel with the headers the browser would send", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ void t.window.fetch("/api", { method: "POST", headers: { "X-Test": "1" }, body: "hello", integrity: "sha256-abc" });+ await tick();+ await tick();+ const sent = t.posted[0];+ assert.equal(sent.method, "POST");+ assert.equal(sent.integrity, "sha256-abc");+ assert.equal(new TextDecoder().decode(sent.body as ArrayBuffer), "hello");+ assert.deepEqual(Object.fromEntries(sent.headers as Array<[string, string]>), {+ "content-type": "text/plain;charset=UTF-8",+ "x-test": "1",+ });+});++test("a failed relay is fetch's network error; an abort rejects and tells the panel", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const failing = t.window.fetch("/a");+ await tick();+ t.reply(t.posted[0].id, { ok: false });+ await assert.rejects(failing, (error: Error) => error.name === "TypeError");++ const controller = new AbortController();+ const aborted = t.window.fetch("/b", { signal: controller.signal});+ await tick();+ controller.abort();+ await assert.rejects(aborted, (error: Error) => error.name === "AbortError");+ assert.deepEqual(plain(t.posted.at(-1)), { type: "requestAbort", id: t.posted[1].id });+ assert.equal(t.pending(0), 0);+});++test("an answer from anything but the shell is ignored", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ let settled = false;+ void t.window.fetch("/a").then(() => (settled = true));+ await tick();+ t.reply(t.posted[0].id, ok("{}"), {});+ await tick();+ assert.equal(settled, false);+});++type PageXHR = NativeXHR & {+ readyState: number;+ status: number;+ responseURL: string;+ setRequestHeader: (name: string, value: string) => void;+ getResponseHeader: (name: string) => string | null;+ getAllResponseHeaders: () => string;+};++test("an XHR to the page's site loads the panel's answer and shows the site's status and headers", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ const events: string[] = [];+ for (const type of ["loadstart", "readystatechange", "load", "loadend"]) xhr.addEventListener(type, () => events.push(type));+ xhr.open("GET", "/api");+ xhr.setRequestHeader("X-Test", "1");+ xhr.send();+ await tick();+ assert.deepEqual(events, ["loadstart"]);+ const sent = t.posted[0];+ assert.deepEqual(plain(sent.headers), [["X-Test", "1"]]);+ // OPENED while the panel makes it, as one in flight.+ assert.equal(xhr.readyState, 1);+ t.reply(sent.id, ok("{}", { status: 404, statusText: "Not Found" }));+ await tick();+ // The browser now loads the answer from a blob: URL.+ const [open, send] = xhr.calls.slice(-2);+ assert.equal(open[0], "open");+ assert.match(String(open[2]), /^blob:/);+ assert.deepEqual(send, ["send"]);+ xhr.state = 4;+ assert.equal(xhr.status, 404);+ assert.equal(xhr.responseURL, "https://site.test/models-json?q=1");+ assert.equal(xhr.getResponseHeader("X-TOTAL"), "3");+ assert.equal(xhr.getAllResponseHeaders(), "content-type: application/json\r\nx-total: 3\r\n");+ assert.equal(t.pending(0), 1);+ xhr.dispatchEvent(new Event("loadend"));+ assert.equal(t.pending(0), 0);+});++test("an XHR the panel can't make ends as a network error", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ const events: string[] = [];+ for (const type of ["readystatechange", "error", "loadend"]) xhr.addEventListener(type, () => events.push(`${type}:${xhr.readyState}`));+ xhr.open("GET", "/api");+ xhr.send();+ await tick();+ t.reply(t.posted[0].id, { ok: false });+ await tick();+ assert.deepEqual(events, ["readystatechange:4", "error:4", "loadend:4"]);+ assert.equal(xhr.status, 0);+ assert.equal(t.pending(0), 0);+ assert.throws(() => xhr.send(), (error: Error) => error.name === "InvalidStateError");+});++test("aborting an XHR the panel is making ends it as the browser would", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ const events: string[] = [];+ for (const type of ["readystatechange", "abort", "loadend"]) xhr.addEventListener(type, () => events.push(`${type}:${xhr.readyState}`));+ xhr.open("GET", "/api");+ xhr.send();+ await tick();+ xhr.abort();+ assert.deepEqual(events, ["readystatechange:4", "abort:4", "loadend:4"]);+ assert.equal(xhr.readyState, 0);+ assert.deepEqual(plain(t.posted.at(-1)), { type: "requestAbort", id: t.posted[0].id });+ // A late answer changes nothing.+ t.reply(t.posted[0].id, ok("{}"));+ await tick();+ assert.equal(xhr.calls.filter(([name]) => name === "send").length, 0);+});++test("a synchronous XHR, which can't wait for the panel, stays the browser's", () => {+ const t = setup(NativeXHR, "https://site.test/");+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ xhr.open("GET", "/api", false);+ xhr.send();+ assert.equal(t.posted.length, 0);+ assert.deepEqual(xhr.calls.at(-1), ["send", undefined]);+});++test("a fetch's redirect mode reaches the panel, and manual stops at an opaque redirect", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const request = t.window.fetch("/login", { redirect: "manual"});+ await tick();+ assert.equal(t.posted[0].redirect, "manual");+ t.reply(t.posted[0].id, { ...ok(""), opaqueRedirect: true, status: 0, headers: [], url: "https://site.test/login" });+ const response = await request;+ assert.equal(response.type, "opaqueredirect");+ assert.equal(response.status, 0);+ assert.equal(response.url, "https://site.test/login");+ assert.equal([...response.headers].length, 0);+});++test("a response a redirect brought from another site reads as a CORS one", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const request = t.window.fetch("/download");+ await tick();+ t.reply(t.posted[0].id, ok("{}", { url: "https://cdn.test/file", redirected: true, cors: true }));+ const response = await request;+ assert.equal(response.type, "cors");+ assert.equal(response.redirected, true);+ assert.equal(response.url, "https://cdn.test/file");+});++test("a chain that went through another site and back is still a redirected CORS response", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const request = t.window.fetch("/models-json?q=1");+ await tick();+ t.reply(t.posted[0].id, ok("{}", { redirected: true, cors: true }));+ const response = await request;+ assert.equal(response.url, "https://site.test/models-json?q=1");+ assert.equal(response.type, "cors");+ assert.equal(response.redirected, true);+});++test("an XHR's Document body is sent serialized, as UTF-8 HTML or XML", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ for (const [document, markup, type] of [+ [new PageDocument("application/xml", "<envelope/>"), '<?xml version="1.0"?><envelope/>', "application/xml;charset=UTF-8"],+ [new PageDocument("text/html", "<html><body>hi</body></html>"), "<!DOCTYPE html><html><body>hi</body></html>", "text/html;charset=UTF-8"],+ ] as const) {+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ xhr.open("POST", "/soap");+ xhr.send(document as unknown as string);+ await tick();+ const sent = t.posted.at(-1) as Record<string, unknown>;+ assert.equal(new TextDecoder().decode(sent.body as ArrayBuffer), markup);+ assert.deepEqual(plain(sent.headers), [["Content-Type", type]]);+ }+});++test("an XHR's text body relabels the page's charset as the UTF-8 it is sent in", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ for (const [type, body, sent] of [+ ["text/plain;charset=iso-8859-1", "caf\u00e9", "text/plain;charset=UTF-8"],+ ['text/plain; charset="Shift_JIS"; foo=bar', "x", 'text/plain; charset="UTF-8"; foo=bar'],+ ["text/plain;Charset=latin1;format=flowed", new URLSearchParams("a=1"), "text/plain;Charset=UTF-8;format=flowed"],+ ["text/plain", "x", "text/plain"],+ ["application/octet-stream;charset=latin1", new Uint8Array([233]).buffer, "application/octet-stream;charset=latin1"],+ ] as const) {+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ xhr.open("POST", "/api");+ xhr.setRequestHeader("Content-Type", type);+ xhr.send(body as unknown as string);+ await tick();+ assert.deepEqual(plain((t.posted.at(-1) as Record<string, unknown>).headers), [["Content-Type", sent]]);+ }+});++test("an XHR always follows redirects, as the browser's does", async () => {+ const t = setup(NativeXHR, "https://site.test/");+ const xhr = new t.window.XMLHttpRequest() as PageXHR;+ xhr.open("GET", "/api");+ xhr.send();+ await tick();+ assert.equal(t.posted[0].redirect, "follow");+});diff --git a/studio/frontend/tests/browser-page-requests.test.ts b/studio/frontend/tests/browser-page-requests.test.tsnew file mode 100644index 00000000000..b671d856ad0--- /dev/null+++ b/studio/frontend/tests/browser-page-requests.test.ts@@ -0,0 +1,116 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import { test } from "node:test";+import type { PageResponse } from "../src/features/browser/api.ts";+import type { PageRequest } from "../src/features/browser/frame-message.ts";+import { pageRequests } from "../src/features/browser/page-requests.ts";++const request: PageRequest = { url: "https://site.test/api", method: "GET", headers: [], body: null, redirect: "follow", integrity: "" };+const answer: PageResponse = { status: 200, statusText: "OK", url: "https://site.test/api", headers: [], body: new ArrayBuffer(0) };+const tick = () => new Promise((resolve) => setTimeout(resolve, 0));++function setup(page: string | null = "https://site.test/models") {+ const calls: Array<{ page: string; signal: AbortSignal; finish: (ok: boolean) => void }> = [];+ const replies: Array<[string, PageResponse | null]> = [];+ const requests = pageRequests(+ (page, _request, signal) =>+ new Promise((resolve, reject) => calls.push({ page, signal, finish: (ok) => (ok ? resolve(answer) : reject(new Error("failed"))) })),+ (id, reply) => replies.push([id, reply]),+ () => page,+ );+ return { requests, calls, replies };+}++test("requests go out for the panel's page address and are answered once each", async () => {+ const t = setup();+ t.requests.request("a", request);+ t.requests.request("b", request);+ assert.equal(t.calls[0].page, "https://site.test/models");+ t.calls[0].finish(true);+ t.calls[1].finish(false);+ await tick();+ assert.deepEqual(t.replies, [["a", answer], ["b", null]]);+});++test("three run at once across every page and the rest wait their turn", async () => {+ const t = setup();+ const other = setup();+ for (let i = 0; i < 2; i++) t.requests.request(`r${i}`, request);+ for (let i = 0; i < 3; i++) other.requests.request(`o${i}`, request);+ assert.equal(t.calls.length + other.calls.length, 3);+ assert.equal(other.calls.length, 1);+ t.calls[0].finish(true);+ await tick();+ assert.equal(other.calls.length, 2);+ t.requests.stop();+ other.requests.stop();+ for (const call of [...t.calls, ...other.calls]) call.finish(false);+ await tick();+});++test("a page with many waiting can't keep another page waiting until its queue drains", async () => {+ const busy = setup();+ const other = setup();+ for (let i = 0; i < 6; i++) busy.requests.request(`b${i}`, request);+ for (let i = 0; i < 2; i++) other.requests.request(`o${i}`, request);+ assert.equal(busy.calls.length, 3);+ assert.equal(other.calls.length, 0);+ busy.calls[0].finish(true);+ busy.calls[1].finish(true);+ await tick();+ // The freed slots alternate between the two pages.+ assert.equal(busy.calls.length, 4);+ assert.equal(other.calls.length, 1);+ busy.requests.stop();+ other.requests.stop();+ for (const call of [...busy.calls, ...other.calls]) call.finish(false);+ await tick();+});++test("queued requests the page aborts leave the queue, so they can't fill it", async () => {+ const t = setup();+ for (let i = 0; i < 3; i++) t.requests.request(`run${i}`, request);+ for (let i = 0; i < 300; i++) {+ t.requests.request(`q${i}`, request);+ t.requests.abort(`q${i}`);+ }+ t.requests.request("last", request);+ // Queued behind the three running, not refused as over the bound.+ assert.deepEqual(t.replies, []);+ t.requests.stop();+ for (const call of t.calls) call.finish(false);+ await tick();+});++test("one the panel won't make, or with no page address to make it for, fails at once", () => {+ const t = setup();+ t.requests.request("bad", null);+ assert.deepEqual(t.replies, [["bad", null]]);+ const local = setup(null);+ local.requests.request("file", request);+ assert.equal(local.calls.length, 0);+ assert.deepEqual(local.replies, [["file", null]]);+});++test("an abort cancels the backend call and needs no answer", async () => {+ const t = setup();+ t.requests.request("a", request);+ t.requests.abort("a");+ assert.equal(t.calls[0].signal.aborted, true);+ t.calls[0].finish(false);+ await tick();+ assert.deepEqual(t.replies, []);+});++test("a page that goes ends everything it asked for, queued or running", async () => {+ const t = setup();+ for (let i = 0; i < 8; i++) t.requests.request(`r${i}`, request);+ t.requests.stop();+ assert.ok(t.calls.every((call) => call.signal.aborted));+ for (const call of t.calls) call.finish(false);+ await tick();+ assert.equal(t.calls.length, 3);+ assert.deepEqual(t.replies, []);+});