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: fetch VLM dataset row image URLs through the SSRF-guarded fetch (#14701)

Daniel Han866fed8fd9ed
diff --git a/studio/backend/tests/test_vlm_conversion_row_urls.py b/studio/backend/tests/test_vlm_conversion_row_urls.py
new file mode 100644
index 00000000000..510efdb7691
--- /dev/null
+++ b/studio/backend/tests/test_vlm_conversion_row_urls.py
@@ -0,0 +1,363 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""VLM conversion must download row image URLs through the SSRF guard, never from private addresses."""
+
+import io
+import sys
+import threading
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+from pathlib import Path
+
+import pytest
+from datasets import Dataset
+from PIL import Image
+
+_BACKEND_ROOT = Path(__file__).resolve().parents[1]
+if str(_BACKEND_ROOT) not in sys.path:
+ sys.path.insert(0, str(_BACKEND_ROOT))
+
+from utils.datasets import format_conversion, remote_images # noqa: E402
+from utils.datasets.format_conversion import ( # noqa: E402
+ convert_sharegpt_with_images_to_vlm_format,
+ convert_to_vlm_format,
+)
+
+
+def _png() -> bytes:
+ out = io.BytesIO()
+ Image.new("RGB", (4, 4), "red").save(out, format = "PNG")
+ return out.getvalue()
+
+
+def loopback_server():
+ hits = []
+ body = _png()
+
+ class Handler(BaseHTTPRequestHandler):
+ def do_GET(self):
+ hits.append(self.path)
+ self.send_response(200)
+ self.send_header("Content-Type", "image/png")
+ 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()
+ yield f"http://127.0.0.1:{server.server_address[1]}", hits
+ server.shutdown()
+ server.server_close()
+
+
[email protected](autouse = True)
+def no_llm_warning(monkeypatch):
+ from utils.datasets import llm_assist
+ def refuse(*args, **kwargs):
+ raise RuntimeError("no LLM in tests")
+
+ monkeypatch.setattr(llm_assist, "llm_generate_dataset_warning", refuse)
+
+
+def _run(fn, *args, **kwargs):
+ try:
+ return fn(*args, **kwargs)
+ except ValueError:
+ return []
+
+
+def test_simple_vlm_conversion_does_not_fetch_loopback(loopback_server):
+ base, hits = loopback_server
+ ds = Dataset.from_list([{"image": f"{base}/a.png", "caption": "a red square"}])
+
+ converted = _run(convert_to_vlm_format, ds, instruction = "Describe.", text_column = "caption")
+
+ assert hits == []
+ assert converted == []
+
+
+def test_sharegpt_vlm_conversion_does_not_fetch_loopback(loopback_server):
+ base, hits = loopback_server
+ ds = Dataset.from_list(
+ [
+ {
+ "image": f"{base}/b.png",
+ "conversations": [
+ {"from": "human", "value": "<image> What is this?"},
+ {"from": "gpt", "value": "A red square."},
+ ],
+ }
+ ]
+ )
+
+ converted = _run(convert_sharegpt_with_images_to_vlm_format, ds)
+
+ assert hits == []
+ assert converted == []
+
+
+def test_public_row_urls_go_through_the_guarded_fetch(monkeypatch):
+ from core.inference import tools
+
+ calls = []
+ body = _png()
+
+ def fake_fetch(url, **kwargs):
+ calls.append((url, kwargs.get("raw_bytes_max")))
+ return None, body, "image/png"
+
+ monkeypatch.setattr(tools, "_fetch_url_raw", fake_fetch)
+ ds = Dataset.from_list([{"image": "https://example.com/c.png", "caption": "a red square"}])
+
+ converted = convert_to_vlm_format(ds, instruction = "Describe.", text_column = "caption")
+
+ assert [url for url, _ in calls] == ["https://example.com/c.png"]
+ assert calls[0][1] == remote_images._MAX_IMAGE_BYTES
+ image = converted[0]["messages"][0]["content"][1]["image"]
+ assert image.size == (4, 4)
+
+
+def test_guard_refusals_skip_the_row(monkeypatch):
+ from core.inference import tools
+
+ monkeypatch.setattr(
+ tools, "_fetch_url_raw", lambda url, **kwargs: ("Blocked: private address.", "", "")
+ )
+ ds = Dataset.from_list(
+ [
+ {"image": "https://example.com/ok.png", "caption": "x"},
+ ]
+ )
+
+ with pytest.raises(ValueError):
+ convert_to_vlm_format(ds, instruction = "Describe.", text_column = "caption")
+
+
+def _typed_image_dataset(url):
+ from datasets import Image as ImageFeature
+ ds = Dataset.from_dict({"image": [url], "text": ["a red square"]})
+ return ds.cast_column("image", ImageFeature())
+
+
+def _recording_fetch(monkeypatch, body = None):
+ from core.inference import tools
+
+ calls = []
+
+ def fake_fetch(url, **kwargs):
+ calls.append(url)
+ if body is None:
+ return "Blocked: refusing to fetch non-public address 10.0.0.1.", b"", ""
+ return None, body, "image/png"
+
+ monkeypatch.setattr(tools, "_fetch_url_raw", fake_fetch)
+ return calls
+
+
+def test_image_typed_url_rows_do_not_fetch_loopback(loopback_server):
+ # datasets decodes an Image()-typed cell holding only a URL by fetching it on access.
+ base, hits = loopback_server
+ ds = _typed_image_dataset(f"{base}/typed.png")
+
+ with pytest.raises((OSError, ValueError)):
+ convert_to_vlm_format(ds, instruction = "Describe.")
+ with pytest.raises(OSError):
+ ds[0]
+ assert hits == []
+
+
+def test_image_typed_url_rows_use_the_guarded_fetch(monkeypatch):
+ calls = _recording_fetch(monkeypatch, _png())
+ ds = _typed_image_dataset("https://example.com/typed.png")
+
+ simple = convert_to_vlm_format(ds, instruction = "Describe.")
+ sharegpt = convert_sharegpt_with_images_to_vlm_format(
+ ds.add_column(
+ "conversations",
+ [[{"from": "human", "value": "<image> hi"}, {"from": "gpt", "value": "ok"}]],
+ )
+ )
+
+ assert set(calls) == {"https://example.com/typed.png"}
+ assert isinstance(simple[0]["messages"][0]["content"][1]["image"], Image.Image)
+ assert len(sharegpt) == 1
+
+
+def test_nested_and_streamed_image_features_are_guarded(loopback_server):
+ from datasets import Image as ImageFeature
+ from datasets import Sequence
+
+ base, hits = loopback_server
+ nested = Dataset.from_dict({"images": [[f"{base}/n.png"]]}).cast_column(
+ "images", Sequence(ImageFeature())
+ )
+ with pytest.raises(OSError):
+ nested[0]
+ with pytest.raises(OSError):
+ next(iter(_typed_image_dataset(f"{base}/s.png").to_iterable_dataset()))
+ assert hits == []
+
+
+def test_embedded_and_hub_images_keep_datasets_own_decode(monkeypatch):
+ calls = _recording_fetch(monkeypatch)
+ from datasets import Image as ImageFeature
+
+ ds = Dataset.from_dict({"image": [{"bytes": _png(), "path": None}], "text": ["x"]})
+ ds = ds.cast_column("image", ImageFeature())
+ out = convert_to_vlm_format(ds, instruction = "Describe.")
+
+ assert isinstance(out[0]["messages"][0]["content"][1]["image"], Image.Image)
+ assert calls == []
+
+
+def test_detection_does_not_decode_typed_url_rows(loopback_server):
+ from utils.datasets.format_detection import detect_vlm_dataset_structure
+
+ base, hits = loopback_server
+ with pytest.raises(OSError):
+ detect_vlm_dataset_structure(_typed_image_dataset(f"{base}/detect.png"))
+ assert hits == []
+
+
+def test_typed_url_rows_keep_the_exif_orientation_datasets_applies(monkeypatch):
+ rotated = io.BytesIO()
+ exif = Image.Exif()
+ exif[0x0112] = 6 # rotate 90 CW on display
+ Image.new("RGB", (4, 2), "red").save(rotated, format = "JPEG", exif = exif)
+ _recording_fetch(monkeypatch, rotated.getvalue())
+
+ out = convert_to_vlm_format(
+ _typed_image_dataset("https://example.com/r.jpg"), instruction = "Describe."
+ )
+ assert out[0]["messages"][0]["content"][1]["image"].size == (2, 4)
+
+
+def test_guarded_decode_is_installed_once():
+ from datasets.features import image as hf_image
+
+ from utils.datasets import remote_images
+
+ before = hf_image.Image.decode_example
+ remote_images.install_guarded_image_decode()
+ assert hf_image.Image.decode_example is before
+ assert getattr(before, "_unsloth_guarded", False)
+
+
+ "url",
+ [
+ "https://huggingface.co.attacker.example/a.png",
+ "https://[email protected]/a.png",
+ ],
+)
+def test_lookalike_hub_hosts_still_go_through_the_guard(monkeypatch, url):
+ calls = _recording_fetch(monkeypatch)
+ with pytest.raises(OSError):
+ _typed_image_dataset(url)[0]
+ assert calls == [url]
+
+
+ "url", ["ftp://127.0.0.1/a.png", "sftp://10.0.0.1/a.png", "smb://host/share/a.png"]
+)
+def test_other_remote_schemes_are_refused_before_fsspec(monkeypatch, url):
+ import fsspec
+
+ def no_open(*args, **kwargs):
+ raise AssertionError("refused scheme reached fsspec")
+
+ monkeypatch.setattr(fsspec, "open", no_open)
+ with pytest.raises(OSError, match = "unsupported scheme"):
+ _typed_image_dataset(url)[0]
+
+
+def _decode_without_io(monkeypatch, path):
+ """Decode one bytes-less cell; any open that slips past the guard raises AssertionError."""
+ from datasets import Image as ImageFeature
+ from datasets.features import image as hf_image
+
+ def no_open(*args, **kwargs):
+ raise AssertionError(f"reached fsspec / PIL with {args[:1]}")
+
+ monkeypatch.setattr(hf_image, "xopen", no_open)
+ import PIL.Image
+
+ monkeypatch.setattr(PIL.Image, "open", no_open)
+ return ImageFeature().decode_example({"path": path, "bytes": None})
+
+
+ "path",
+ [
+ "ftp://10.0.0.5/image.png::/tmp/x",
+ "zip://a.png::ftp://10.0.0.5/x.zip",
+ "zip://a.png::smb://host/share/x.zip",
+ ],
+)
+def test_every_chained_hop_is_checked(monkeypatch, path):
+ with pytest.raises(OSError, match = "unsupported scheme"):
+ _decode_without_io(monkeypatch, path)
+
+
+ "path",
+ ["https://attacker.example/x.zip::/tmp/x", "zip://a.png::https://attacker.example/x.zip"],
+)
+def test_chained_non_hub_urls_are_refused(monkeypatch, path):
+ calls = _recording_fetch(monkeypatch, _png())
+ with pytest.raises(OSError, match = "remote archive"):
+ _decode_without_io(monkeypatch, path)
+ assert calls == []
+
+
+ "path",
+ [
+ "\\\\attacker\\share\\a.png",
+ "//attacker/share/a.png",
+ "file:////attacker/share/a.png",
+ "file://attacker/share/a.png",
+ "\\\\?\\UNC\\attacker\\share\\a.png",
+ "zip://a.png::\\\\attacker\\share\\x.zip",
+ ],
+)
+def test_network_share_paths_are_refused(monkeypatch, path):
+ with pytest.raises(OSError, match = "network share"):
+ _decode_without_io(monkeypatch, path)
+
+
+ "path",
+ [
+ "/data/images/a.png",
+ "C:\\data\\a.png",
+ "file:///data/a.png",
+ "file://localhost/data/a.png",
+ "zip://a.png::https://huggingface.co/datasets/o/r/resolve/main/a.zip",
+ "gzip://a.png::hf://datasets/o/r/a.png.gz",
+ ],
+)
+def test_local_and_hub_paths_still_reach_datasets(monkeypatch, path):
+ with pytest.raises(AssertionError, match = "reached"):
+ _decode_without_io(monkeypatch, path)
+
+
+def test_string_rows_naming_a_network_share_never_touch_it(monkeypatch):
+ import PIL.Image
+
+ from utils.datasets import format_conversion
+
+ def no_touch(*args, **kwargs):
+ raise AssertionError(f"touched {args[:1]}")
+
+ monkeypatch.setattr(PIL.Image, "open", no_touch)
+ monkeypatch.setattr(format_conversion.os.path, "exists", no_touch)
+ ds = Dataset.from_list([{"image": "\\\\attacker\\share\\a.png", "caption": "x"}])
+
+ with pytest.raises((OSError, ValueError)) as err:
+ convert_to_vlm_format(ds, instruction = "Describe.", text_column = "caption")
+ assert "touched" not in str(err.value)
diff --git a/studio/backend/tests/test_vlm_probe_untrusted_rows.py b/studio/backend/tests/test_vlm_probe_untrusted_rows.py
new file mode 100644
index 00000000000..e9398ae8e7f
--- /dev/null
+++ b/studio/backend/tests/test_vlm_probe_untrusted_rows.py
@@ -0,0 +1,86 @@
+# 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 VLM image-column probe must not reach private addresses from untrusted row URLs."""
+
+import socket
+import sys
+import urllib.request
+from pathlib import Path
+
+import pytest
+
+_BACKEND_ROOT = Path(__file__).resolve().parents[1]
+if str(_BACKEND_ROOT) not in sys.path:
+ sys.path.insert(0, str(_BACKEND_ROOT))
+
+from utils.datasets.format_detection import detect_vlm_dataset_structure # noqa: E402
+
+
+def dials(monkeypatch):
+ seen = []
+
+ def no_dial(address, *args, **kwargs):
+ seen.append(address)
+ raise OSError("dial refused in test")
+
+ def no_urlopen(req, *args, **kwargs):
+ seen.append(getattr(req, "full_url", req))
+ raise OSError("urlopen refused in test")
+
+ monkeypatch.setattr(socket, "create_connection", no_dial)
+ monkeypatch.setattr(urllib.request, "urlopen", no_urlopen)
+ return seen
+
+
+ "url",
+ ["http://127.0.0.1:9/a.png", "http://169.254.169.254/latest/a.png", "http://[::1]/a.png"],
+)
+def test_private_image_urls_are_never_dialled(dials, url):
+ # Two scored-below-75 URL columns force the probe pass.
+ row = {"image": url, "photo": "http://10.0.0.1/b.png", "caption": "a cat on a mat"}
+
+ detect_vlm_dataset_structure([row])
+
+ assert dials == []
+
+
+def test_public_image_urls_go_through_the_guarded_fetch(monkeypatch):
+ from core.inference import tools
+
+ calls = []
+
+ def fake_fetch(url, **kwargs):
+ calls.append((url, kwargs.get("method")))
+ return (None, b"", "") if "good" in url else ("Failed to fetch URL: HTTP 404", "", "")
+
+ monkeypatch.setattr(tools, "_fetch_url_raw", fake_fetch)
+ row = {
+ "image": "https://example.com/bad.png",
+ "image_filename": "https://example.com/good.png",
+ "caption": "a cat",
+ }
+
+ assert detect_vlm_dataset_structure([row])["image_column"] == "image_filename"
+ assert calls == [
+ ("https://example.com/bad.png", "HEAD"),
+ ("https://example.com/good.png", "HEAD"),
+ ]
+
+
+def test_network_share_paths_are_not_probed(monkeypatch):
+ import os
+
+ def no_touch(*args, **kwargs):
+ raise AssertionError(f"probed {args[:1]}")
+
+ monkeypatch.setattr(os.path, "exists", no_touch)
+ row = {
+ "image": "\\\\attacker\\share\\a.png",
+ "photo": "//attacker/share/b.png",
+ "caption": "a cat",
+ }
+
+ detect_vlm_dataset_structure([row])
diff --git a/studio/backend/tests/test_vlm_text_only_rows.py b/studio/backend/tests/test_vlm_text_only_rows.py
index 412bf89592a..565fbcb18cd 100644
--- a/studio/backend/tests/test_vlm_text_only_rows.py
+++ b/studio/backend/tests/test_vlm_text_only_rows.py
@@ -29,26 +29,24 @@ def test_rows_without_an_image_are_left_out_of_vision_training():
assert all(isinstance(image, Image.Image) for image in images)
-def test_rows_without_an_image_do_not_count_as_broken_urls(tmp_path):
- import threading
- from functools import partial
- from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
-
- Image.new("RGB", (4, 4)).save(tmp_path / "a.png")
- server = ThreadingHTTPServer(
- ("127.0.0.1", 0), partial(SimpleHTTPRequestHandler, directory = str(tmp_path))
+def test_rows_without_an_image_do_not_count_as_broken_urls(monkeypatch):
+ import io
+
+ from core.inference import tools
+
+ png = io.BytesIO()
+ Image.new("RGB", (4, 4)).save(png, format = "PNG")
+ # Row URLs go through the SSRF-guarded fetch, which refuses loopback, so stub it.
+ monkeypatch.setattr(
+ tools, "_fetch_url_raw", lambda url, **kwargs: (None, png.getvalue(), "image/png")
)
- threading.Thread(target = server.serve_forever, daemon = True).start()
- url = f"http://127.0.0.1:{server.server_address[1]}/a.png"
- try:
- ds = Dataset.from_dict(
- {
- "image": [url, None, None, url, None],
- "text": ["first", "none", "none", "second", "none"],
- }
- )
- out = format_conversion.convert_to_vlm_format(ds, instruction = "Describe.")
- finally:
- server.shutdown()
+ url = "https://example.com/a.png"
+ ds = Dataset.from_dict(
+ {
+ "image": [url, None, None, url, None],
+ "text": ["first", "none", "none", "second", "none"],
+ }
+ )
+ out = format_conversion.convert_to_vlm_format(ds, instruction = "Describe.")
assert [sample["messages"][1]["content"][0]["text"] for sample in out] == ["first", "second"]
diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.py
index 86df81b743a..aa7d3536318 100644
--- a/studio/backend/utils/datasets/format_conversion.py
+++ b/studio/backend/utils/datasets/format_conversion.py
@@ -7,6 +7,7 @@ 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 loggers import get_logger
logger = get_logger(__name__)
@@ -27,6 +28,8 @@ 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}")
run_as(account, require_readable_media, path)
return path
@@ -392,6 +395,13 @@ def _format_eta(seconds):
return f"{h}h {m}m"
+def _fetch_row_image(url):
+ from io import BytesIO
+
+ from PIL import Image
+ return Image.open(BytesIO(fetch_untrusted_image_bytes(url))).convert("RGB")
+
+
def convert_to_vlm_format(
dataset,
instruction = None,
@@ -444,10 +454,7 @@ def convert_to_vlm_format(
if isinstance(image_data, str):
if image_data.startswith(("http://", "https://")):
- import fsspec
- from io import BytesIO
- with fsspec.open(image_data, "rb", expand = True) as f:
- image_data = Image.open(BytesIO(f.read())).convert("RGB")
+ image_data = _fetch_row_image(image_data)
elif _image_lookup is not None and image_data in _image_lookup:
# Bare filename → resolve via HF repo lookup
from huggingface_hub import hf_hub_download
@@ -508,6 +515,7 @@ def convert_to_vlm_format(
if (
not has_urls
and isinstance(first_image, str)
+ and not is_network_share(first_image)
and not os.path.exists(first_image)
and dataset_name
):
@@ -775,6 +783,7 @@ def convert_sharegpt_with_images_to_vlm_format(
if (
isinstance(first_image, str)
and not first_image.startswith(("http://", "https://"))
+ and not is_network_share(first_image)
and not os.path.exists(first_image)
and dataset_name
):
@@ -814,10 +823,7 @@ def convert_sharegpt_with_images_to_vlm_format(
return image_data
if isinstance(image_data, str):
if image_data.startswith(("http://", "https://")):
- import fsspec
- from io import BytesIO
- with fsspec.open(image_data, "rb", expand = True) as f:
- return Image.open(BytesIO(f.read())).convert("RGB")
+ return _fetch_row_image(image_data)
elif _image_lookup is not None and image_data in _image_lookup:
from huggingface_hub import hf_hub_download
from utils.hf_cache_settings import active_hf_hub_cache
diff --git a/studio/backend/utils/datasets/format_detection.py b/studio/backend/utils/datasets/format_detection.py
index fe29b161f86..4e891f41f4e 100644
--- a/studio/backend/utils/datasets/format_detection.py
+++ b/studio/backend/utils/datasets/format_detection.py
@@ -967,21 +967,30 @@ def detect_vlm_dataset_structure(dataset):
return 0
def _probe_image_candidate(col, sample_value):
- """Probe whether an image candidate is reachable (True unless definitely broken)."""
+ """Probe whether an image candidate is reachable (True unless definitely broken).
+ Row URLs are untrusted, so they go through the web tool's SSRF guard."""
import os
if not isinstance(sample_value, str):
return True
if not sample_value.startswith(("http://", "https://")):
- return os.path.exists(sample_value)
+ from .remote_images import is_network_share
+ return not is_network_share(sample_value) and os.path.exists(sample_value)
try:
- import urllib.request
+ import time
- req = urllib.request.Request(sample_value, method = "HEAD")
- resp = urllib.request.urlopen(req, timeout = 3)
- return resp.status < 400
+ from core.inference.tools import _fetch_url_raw
+
+ error, _, _ = _fetch_url_raw(
+ sample_value,
+ timeout = 3,
+ deadline = time.monotonic() + 3,
+ method = "HEAD",
+ raw_bytes_max = 0,
+ )
+ return error is None
except Exception:
return False
diff --git a/studio/backend/utils/datasets/remote_images.py b/studio/backend/utils/datasets/remote_images.py
new file mode 100644
index 00000000000..33936c4232b
--- /dev/null
+++ b/studio/backend/utils/datasets/remote_images.py
@@ -0,0 +1,120 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Dataset image URLs are untrusted row content: fetch them only through the SSRF guard."""
+
+import functools
+
+_MAX_IMAGE_BYTES = 64 * 1024 * 1024
+_IMAGE_TIMEOUT_S = 30
+# Left to datasets: local paths (no scheme), the Hub, and cloud object stores.
+_DATASETS_OWN_SCHEMES = {
+ "",
+ "file",
+ "hf",
+ "http",
+ "https",
+ "s3",
+ "gs",
+ "gcs",
+ "az",
+ "abfs",
+ "abfss",
+ # Archive / compression wrappers datasets chains onto another hop (zip://a.png::<archive>).
+ "zip",
+ "tar",
+ "gzip",
+ "bz2",
+ "xz",
+ "zstd",
+ "lz4",
+}
+
+
+def _scheme(url):
+ return url.split("://", 1)[0].lower() if "://" in url else ""
+
+
+def is_network_share(path):
+ """UNC and network ``file:`` paths: Windows would open an SMB connection to the host."""
+ 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"):
+ return True
+ p = p[2 + len(host) :]
+ if p.startswith(("//?/", "//./")):
+ return p[4:].upper().startswith("UNC/")
+ return p.startswith("//")
+
+
+def fetch_untrusted_image_bytes(url):
+ """Public addresses only, pinned to the resolved IP, every redirect re-checked, size capped."""
+ import time
+
+ from core.inference.tools import _fetch_url_raw
+
+ error, body, _ = _fetch_url_raw(
+ 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)
+ return body
+
+
+def install_guarded_image_decode():
+ """``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
+ the guard; Hub URLs keep datasets' own token-aware fetch. Idempotent."""
+ try:
+ from datasets import config
+ from datasets.features import image as hf_image
+ except Exception:
+ return
+ original = hf_image.Image.decode_example
+ if getattr(original, "_unsloth_guarded", False):
+ return
+
+ from urllib.parse import urlsplit
+
+ def _origin(url):
+ parts = urlsplit(url)
+ return parts.scheme.lower(), (parts.hostname or "").lower(), parts.port
+
+ @functools.wraps(original)
+ def decode_example(
+ self,
+ value,
+ 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:
+ # 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}")
+ 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}")
+ # 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}")
+ value = {"path": None, "bytes": fetch_untrusted_image_bytes(path)}
+ return original(self, value, token_per_repo_id)
+
+ decode_example._unsloth_guarded = True
+ hf_image.Image.decode_example = decode_example
+
+
+install_guarded_image_decode()