Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: dataset image guard keeps owner flows working (opt-in, WSL paths, unencoded spaces) (#14966)

Daniel Hanfb104d2b57ff
diff --git a/studio/backend/tests/test_vlm_conversion_row_urls.py b/studio/backend/tests/test_vlm_conversion_row_urls.py
index c60edd36868..64ff23fdbed 100644
--- a/studio/backend/tests/test_vlm_conversion_row_urls.py
+++ b/studio/backend/tests/test_vlm_conversion_row_urls.py
@@ -326,6 +326,8 @@ def test_chained_non_hub_urls_are_refused(monkeypatch, path):
],
)
def test_network_share_paths_are_refused(monkeypatch, path):
+ from utils.datasets import remote_images
+ monkeypatch.setattr(remote_images, "_WINDOWS", True)
with pytest.raises(OSError, match = "network share"):
_decode_without_io(monkeypatch, path)
@@ -347,6 +349,9 @@ def test_local_and_hub_paths_still_reach_datasets(monkeypatch, path):
def test_string_rows_naming_a_network_share_never_touch_it(monkeypatch):
+ from utils.datasets import remote_images
+
+ monkeypatch.setattr(remote_images, "_WINDOWS", True)
import PIL.Image
from utils.datasets import format_conversion
@@ -386,3 +391,146 @@ def test_guard_applies_when_datasets_loads_after_this_module():
)
assert result.returncode == 0, result.stderr[-2000:]
assert "GUARDED_LAZILY" in result.stdout
+
+
+ "path",
+ [
+ "\\\\wsl$\\Ubuntu\\home\\u\\a.png",
+ "\\\\wsl.localhost\\Ubuntu\\home\\u\\a.png",
+ "file://wsl.localhost/Ubuntu/home/u/a.png",
+ "\\\\?\\C:\\data\\a.png",
+ ],
+)
+def test_wsl_and_drive_paths_are_local_on_windows(monkeypatch, path):
+ from utils.datasets import remote_images
+ monkeypatch.setattr(remote_images, "_WINDOWS", True)
+ with pytest.raises(AssertionError, match = "reached"):
+ _decode_without_io(monkeypatch, path)
+
+
+def test_double_slash_paths_are_ordinary_paths_off_windows(monkeypatch):
+ from utils.datasets import remote_images
+ monkeypatch.setattr(remote_images, "_WINDOWS", False)
+ with pytest.raises(AssertionError, match = "reached"):
+ _decode_without_io(monkeypatch, "//data/images/a.png")
+
+
+def test_refusals_name_the_opt_in(monkeypatch):
+ with pytest.raises(OSError, match = "UNSLOTH_ALLOW_PRIVATE_URL_FETCH=1"):
+ _decode_without_io(monkeypatch, "zip://a.png::https://attacker.example/x.zip")
+
+
+def _single_user(monkeypatch, single = True):
+ from auth import policy
+ monkeypatch.setattr(policy, "installation_has_managed_accounts", lambda: not single)
+
+
+def test_the_opt_in_restores_private_hosts_and_archives(monkeypatch, loopback_server):
+ # The owner's own LAN image server or non-Hub archive loads the way datasets loads it.
+ base, hits = loopback_server
+ monkeypatch.setenv("UNSLOTH_ALLOW_PRIVATE_URL_FETCH", "1")
+ _single_user(monkeypatch)
+ ds = Dataset.from_list([{"image": f"{base}/lan.png", "caption": "x"}])
+ out = convert_to_vlm_format(ds, instruction = "Describe.", text_column = "caption")
+ assert isinstance(out[0]["messages"][0]["content"][1]["image"], Image.Image)
+ assert isinstance(_typed_image_dataset(f"{base}/typed.png")[0]["image"], Image.Image)
+ assert len(hits) >= 2
+ with pytest.raises(AssertionError, match = "reached"):
+ _decode_without_io(monkeypatch, "zip://a.png::https://images.example.org/x.zip")
+
+
[email protected]("known_size", [True, False])
+def test_the_opt_in_keeps_the_image_size_cap(monkeypatch, known_size):
+ from utils.datasets import remote_images
+ from utils.datasets.format_conversion import _fetch_row_image
+
+ body = _png()
+
+ class Handler(BaseHTTPRequestHandler):
+ protocol_version = "HTTP/1.0" # no Content-Length: the body ends when the server closes
+
+ def do_GET(self):
+ self.send_response(200)
+ if known_size:
+ self.send_header("Content-Length", str(len(body)))
+ self.end_headers()
+ self.wfile.write(body)
+
+ def log_message(self, *args):
+ pass
+
+ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
+ threading.Thread(target = server.serve_forever, daemon = True).start()
+ base = f"http://127.0.0.1:{server.server_address[1]}"
+ try:
+ monkeypatch.setenv("UNSLOTH_ALLOW_PRIVATE_URL_FETCH", "1")
+ _single_user(monkeypatch)
+ assert isinstance(_fetch_row_image(f"{base}/small.png"), Image.Image)
+ monkeypatch.setattr(remote_images, "_MAX_IMAGE_BYTES", 16)
+ with pytest.raises(OSError, match = "larger than"):
+ _fetch_row_image(f"{base}/big.png")
+ finally:
+ server.shutdown()
+ server.server_close()
+
+
+def test_opted_in_requests_are_quoted_idna_encoded_and_time_bounded(monkeypatch):
+ import urllib.request
+
+ from utils.datasets.remote_images import open_opted_in_url
+
+ seen = []
+
+ def fake_urlopen(request, timeout):
+ seen.append((request.full_url, request.get_method(), timeout))
+ raise OSError("stop")
+
+ monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen)
+ for kwargs in ({}, {"method": "HEAD", "timeout": 3}):
+ with pytest.raises(OSError, match = "stop"):
+ open_opted_in_url("https://u@例子.测试:8443/my images/a.png", **kwargs)
+ assert seen == [
+ ("https://[email protected]:8443/my%20images/a.png", "GET", 30),
+ ("https://[email protected]:8443/my%20images/a.png", "HEAD", 3),
+ ]
+
+
+def test_unencoded_spaces_are_quoted_before_the_guard(monkeypatch):
+ calls = _recording_fetch(monkeypatch, _png())
+ _typed_image_dataset("https://example.com/my images/a b.png")[0]
+ assert calls == ["https://example.com/my%20images/a%20b.png"]
+
+
+def test_the_opt_in_is_ignored_once_managed_accounts_exist(monkeypatch, loopback_server):
+ # A managed account's row must not reach the owner's LAN through the owner's opt-in.
+ base, hits = loopback_server
+ monkeypatch.setenv("UNSLOTH_ALLOW_PRIVATE_URL_FETCH", "1")
+ _single_user(monkeypatch, single = False)
+ with pytest.raises(OSError):
+ _typed_image_dataset(f"{base}/typed.png")[0]
+ assert hits == []
+
+
+def test_an_internationalized_host_is_not_percent_encoded():
+ from utils.datasets.remote_images import guard_url
+ assert guard_url("https://例子.测试/my images/a.png") == "https://例子.测试/my%20images/a.png"
+ assert (
+ guard_url("https://example.com/a.png?x[]=1&b=c d")
+ == "https://example.com/a.png?x[]=1&b=c%20d"
+ )
+
+
+def test_only_private_refusals_suggest_the_opt_in(monkeypatch):
+ from core.inference import tools
+ from utils.datasets.remote_images import fetch_untrusted_image_bytes
+ for error, hinted in (
+ ("Blocked: refusing to fetch non-public address 10.0.0.1.", True),
+ ("Failed to fetch URL: HTTP 404", False),
+ ):
+ monkeypatch.setattr(
+ tools, "_fetch_url_raw", lambda url, error = error, **kwargs: (error, b"", "")
+ )
+ with pytest.raises(OSError) as refused:
+ fetch_untrusted_image_bytes("https://example.com/a.png")
+ assert ("UNSLOTH_ALLOW_PRIVATE_URL_FETCH" in str(refused.value)) is hinted
diff --git a/studio/backend/tests/test_vlm_probe_untrusted_rows.py b/studio/backend/tests/test_vlm_probe_untrusted_rows.py
index e9398ae8e7f..266ee508765 100644
--- a/studio/backend/tests/test_vlm_probe_untrusted_rows.py
+++ b/studio/backend/tests/test_vlm_probe_untrusted_rows.py
@@ -71,6 +71,9 @@ def test_public_image_urls_go_through_the_guarded_fetch(monkeypatch):
def test_network_share_paths_are_not_probed(monkeypatch):
+ from utils.datasets import remote_images
+
+ monkeypatch.setattr(remote_images, "_WINDOWS", True)
import os
def no_touch(*args, **kwargs):
@@ -84,3 +87,36 @@ def test_network_share_paths_are_not_probed(monkeypatch):
}
detect_vlm_dataset_structure([row])
+
+
+def test_the_opt_in_still_probes_so_a_working_column_wins(monkeypatch):
+ import urllib.request
+ from types import SimpleNamespace
+
+ from auth import policy
+
+ monkeypatch.setenv("UNSLOTH_ALLOW_PRIVATE_URL_FETCH", "1")
+ monkeypatch.setattr(policy, "installation_has_managed_accounts", lambda: False)
+
+ class _Ok:
+ status = 200
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *a):
+ return False
+
+ def fake_urlopen(request, timeout = None):
+ assert " " not in request.full_url
+ if "good" in request.full_url:
+ return _Ok()
+ raise OSError("HTTP 404")
+
+ monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen)
+ row = {
+ "image": "http://192.168.1.5/bad.png",
+ "image_filename": "http://192.168.1.5/my images/good.png",
+ "caption": "a cat",
+ }
+ assert detect_vlm_dataset_structure([row])["image_column"] == "image_filename"
diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.py
index aa7d3536318..510fb548469 100644
--- a/studio/backend/utils/datasets/format_conversion.py
+++ b/studio/backend/utils/datasets/format_conversion.py
@@ -7,7 +7,12 @@ import os
from .cells import cell_text
from .iterable import is_streaming_dataset
-from .remote_images import fetch_untrusted_image_bytes, is_network_share
+from .remote_images import (
+ fetch_opted_in_image_bytes,
+ fetch_untrusted_image_bytes,
+ is_network_share,
+ private_sources_allowed,
+)
from loggers import get_logger
logger = get_logger(__name__)
@@ -28,8 +33,11 @@ def _own_image_path(path, account):
from utils.account_context import run_as
from utils.security.dataset_media import require_readable_media
- if isinstance(path, str) and is_network_share(path):
- raise OSError(f"Refusing an image path on a network share: {path}")
+ if isinstance(path, str) and is_network_share(path) and not private_sources_allowed():
+ raise OSError(
+ f"Refusing an image path on a network share: {path}. "
+ "Set UNSLOTH_ALLOW_PRIVATE_URL_FETCH=1 to load images from shares you trust."
+ )
run_as(account, require_readable_media, path)
return path
@@ -399,7 +407,9 @@ def _fetch_row_image(url):
from io import BytesIO
from PIL import Image
- return Image.open(BytesIO(fetch_untrusted_image_bytes(url))).convert("RGB")
+
+ fetch = fetch_opted_in_image_bytes if private_sources_allowed() else fetch_untrusted_image_bytes
+ return Image.open(BytesIO(fetch(url))).convert("RGB")
def convert_to_vlm_format(
diff --git a/studio/backend/utils/datasets/format_detection.py b/studio/backend/utils/datasets/format_detection.py
index 4e891f41f4e..93f6b67602e 100644
--- a/studio/backend/utils/datasets/format_detection.py
+++ b/studio/backend/utils/datasets/format_detection.py
@@ -974,9 +974,24 @@ def detect_vlm_dataset_structure(dataset):
if not isinstance(sample_value, str):
return True
+ from .remote_images import (
+ guard_url,
+ is_network_share,
+ open_opted_in_url,
+ private_sources_allowed,
+ )
+
if not sample_value.startswith(("http://", "https://")):
- from .remote_images import is_network_share
- return not is_network_share(sample_value) and os.path.exists(sample_value)
+ if is_network_share(sample_value) and not private_sources_allowed():
+ return False
+ return os.path.exists(sample_value)
+ if private_sources_allowed():
+ # Still probe, so a broken column loses to a working one.
+ try:
+ with open_opted_in_url(sample_value, method = "HEAD", timeout = 3) as response:
+ return response.status < 400
+ except Exception:
+ return False
try:
import time
@@ -984,7 +999,7 @@ def detect_vlm_dataset_structure(dataset):
from core.inference.tools import _fetch_url_raw
error, _, _ = _fetch_url_raw(
- sample_value,
+ guard_url(sample_value),
timeout = 3,
deadline = time.monotonic() + 3,
method = "HEAD",
diff --git a/studio/backend/utils/datasets/remote_images.py b/studio/backend/utils/datasets/remote_images.py
index 050f594fd62..52fafa1ab03 100644
--- a/studio/backend/utils/datasets/remote_images.py
+++ b/studio/backend/utils/datasets/remote_images.py
@@ -6,6 +6,7 @@
import functools
import importlib.abc
import importlib.util
+import os
import sys
_MAX_IMAGE_BYTES = 64 * 1024 * 1024
@@ -34,23 +35,73 @@ _DATASETS_OWN_SCHEMES = {
}
+# Same opt-in as unsloth_zoo's media fetch: trusted LAN hosts, shares and archives load as datasets loads them.
+ALLOW_PRIVATE_ENV = "UNSLOTH_ALLOW_PRIVATE_URL_FETCH"
+_OPT_IN_HINT = f"Set {ALLOW_PRIVATE_ENV}=1 to load images from private hosts or shares you trust."
+
+
+def private_sources_allowed():
+ """Single-user installs only, so a managed account's row cannot reach the owner's LAN; fails closed."""
+ if os.environ.get(ALLOW_PRIVATE_ENV) != "1":
+ return False
+ try:
+ from auth.policy import installation_has_managed_accounts
+ return not installation_has_managed_accounts()
+ except Exception: # noqa: BLE001 - unreadable account store keeps the guard on
+ return False
+
+
def _scheme(url):
return url.split("://", 1)[0].lower() if "://" in url else ""
+_LOCAL_SHARE_HOSTS = ("", "localhost", "wsl$", "wsl.localhost")
+_WINDOWS = os.name == "nt"
+
+
def is_network_share(path):
- """UNC and network ``file:`` paths: Windows would open an SMB connection to the host."""
+ """Windows UNC / network ``file:`` paths (SMB); ``\\\\wsl$`` and non-Windows ``//x`` are local."""
+ if not _WINDOWS:
+ return False
p = path.replace("\\", "/")
if p[:5].lower() == "file:":
p = p[5:]
if p.startswith("//"):
host = p[2:].split("/", 1)[0]
- if host.lower() not in ("", "localhost"):
+ if host.lower() not in _LOCAL_SHARE_HOSTS:
return True
p = p[2 + len(host) :]
if p.startswith(("//?/", "//./")):
- return p[4:].upper().startswith("UNC/")
- return p.startswith("//")
+ p = p[4:]
+ if not p.upper().startswith("UNC/"):
+ return False
+ p = "//" + p[4:]
+ return p.startswith("//") and p[2:].split("/", 1)[0].lower() not in _LOCAL_SHARE_HOSTS
+
+
+def guard_url(url):
+ """Percent-encode the path, query and fragment as a browser would; the host is left for IDNA."""
+ from urllib.parse import quote, urlsplit, urlunsplit
+
+ try:
+ parts = urlsplit(url.strip())
+ except ValueError:
+ return url
+ keep = "/?#[]@!$&'()*+,;=%~:"
+ return urlunsplit(
+ (
+ parts.scheme,
+ parts.netloc,
+ quote(parts.path, safe = keep),
+ quote(parts.query, safe = keep),
+ quote(parts.fragment, safe = keep),
+ )
+ )
+
+
+def _private_refusal(error):
+ lowered = error.lower()
+ return error.startswith("Blocked:") and ("non-public" in lowered or "local" in lowered)
def fetch_untrusted_image_bytes(url):
@@ -60,17 +111,58 @@ def fetch_untrusted_image_bytes(url):
from core.inference.tools import _fetch_url_raw
error, body, _ = _fetch_url_raw(
- url,
+ guard_url(url),
timeout = _IMAGE_TIMEOUT_S,
extra_headers = {"Accept": "image/*"},
deadline = time.monotonic() + _IMAGE_TIMEOUT_S * 2,
raw_bytes_max = _MAX_IMAGE_BYTES,
)
if error is not None:
- raise OSError(error)
+ raise OSError(f"{error} {_OPT_IN_HINT}" if _private_refusal(error) else error)
return body
+def open_opted_in_url(
+ url,
+ method = "GET",
+ timeout = _IMAGE_TIMEOUT_S,
+):
+ """The owner's opt-in: no address checks, but quoted, IDNA-encoded and time bounded."""
+ import urllib.request
+ from urllib.parse import urlsplit, urlunsplit
+
+ parts = urlsplit(guard_url(url))
+ userinfo, at, hostport = parts.netloc.rpartition("@")
+ if not hostport.isascii(): # never an IPv6 literal, so the first colon starts the port
+ name, colon, port = hostport.partition(":")
+ hostport = name.encode("idna").decode("ascii") + colon + port
+ url = urlunsplit(parts._replace(netloc = userinfo + at + hostport))
+ request = urllib.request.Request(url, method = method, headers = {"Accept": "image/*"})
+ return urllib.request.urlopen(request, timeout = timeout)
+
+
+def fetch_opted_in_image_bytes(url):
+ """The owner's opt-in fetch, with the guarded fetch's size cap and deadline."""
+ import time
+
+ deadline = time.monotonic() + _IMAGE_TIMEOUT_S * 2
+ chunks, total = [], 0
+ with open_opted_in_url(url) as response:
+ if int(response.headers.get("Content-Length") or 0) > _MAX_IMAGE_BYTES:
+ total = _MAX_IMAGE_BYTES + 1
+ while total <= _MAX_IMAGE_BYTES:
+ if time.monotonic() > deadline:
+ raise OSError(f"Timed out fetching image: {url}")
+ chunk = response.read1(1 << 16)
+ if not chunk:
+ break
+ chunks.append(chunk)
+ total += len(chunk)
+ if total > _MAX_IMAGE_BYTES:
+ raise OSError(f"Image is larger than {_MAX_IMAGE_BYTES >> 20} MB: {url}")
+ return b"".join(chunks)
+
+
def install_guarded_image_decode(hf_image = None):
"""``datasets.Image`` decodes a bytes-less URL cell by fetching it itself, wherever a row is
read (detection, previews, conversion, streaming, nested features). Route those fetches through
@@ -98,22 +190,26 @@ def install_guarded_image_decode(hf_image = None):
token_per_repo_id = None,
):
path = value.get("path") if isinstance(value, dict) else None
- if isinstance(path, str) and value.get("bytes") is None:
+ if isinstance(path, str) and value.get("bytes") is None and not private_sources_allowed():
# fsspec opens every hop of a chained path, so each one is checked.
hops = path.split("::")
for hop in hops:
if is_network_share(hop):
- raise OSError(f"Refusing an image path on a network share: {path}")
+ raise OSError(
+ f"Refusing an image path on a network share: {path}. {_OPT_IN_HINT}"
+ )
if _scheme(hop) not in _DATASETS_OWN_SCHEMES:
# ftp://, sftp://, smb:// ... would reach fsspec with no address checks.
- raise OSError(f"Refusing an image URL with an unsupported scheme: {path}")
+ raise OSError(
+ f"Refusing an image URL with an unsupported scheme: {path}. {_OPT_IN_HINT}"
+ )
# Exact origin, not a prefix: huggingface.co.attacker.example would match a prefix.
if any(
_scheme(hop) in ("http", "https") and _origin(hop) != _origin(config.HF_ENDPOINT)
for hop in hops
):
if len(hops) > 1:
- raise OSError(f"Refusing a remote archive image path: {path}")
+ raise OSError(f"Refusing a remote archive image path: {path}. {_OPT_IN_HINT}")
value = {"path": None, "bytes": fetch_untrusted_image_bytes(path)}
return original(self, value, token_per_repo_id)