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: install the dataset image URL guard when datasets loads, not at startup (#14888)

Daniel Hanaab980fe7789
diff --git a/studio/backend/tests/test_vlm_conversion_row_urls.py b/studio/backend/tests/test_vlm_conversion_row_urls.py
index 510efdb7691..c60edd36868 100644
--- a/studio/backend/tests/test_vlm_conversion_row_urls.py
+++ b/studio/backend/tests/test_vlm_conversion_row_urls.py
@@ -361,3 +361,28 @@ def test_string_rows_naming_a_network_share_never_touch_it(monkeypatch):
with pytest.raises((OSError, ValueError)) as err:
convert_to_vlm_format(ds, instruction = "Describe.", text_column = "caption")
assert "touched" not in str(err.value)
+
+
+def test_guard_applies_when_datasets_loads_after_this_module():
+ """Startup imports this module without datasets; the guard must still land on first use."""
+ import subprocess
+ import sys
+ from pathlib import Path
+
+ snippet = (
+ "import sys\n"
+ "import utils.datasets.remote_images\n"
+ "assert 'datasets' not in sys.modules, 'importing remote_images loaded datasets'\n"
+ "from datasets.features import image\n"
+ "assert getattr(image.Image.decode_example, '_unsloth_guarded', False)\n"
+ "print('GUARDED_LAZILY')\n"
+ )
+ result = subprocess.run(
+ [sys.executable, "-c", snippet],
+ cwd = Path(__file__).resolve().parents[1],
+ capture_output = True,
+ text = True,
+ timeout = 300,
+ )
+ assert result.returncode == 0, result.stderr[-2000:]
+ assert "GUARDED_LAZILY" in result.stdout
diff --git a/studio/backend/utils/datasets/remote_images.py b/studio/backend/utils/datasets/remote_images.py
index 33936c4232b..050f594fd62 100644
--- a/studio/backend/utils/datasets/remote_images.py
+++ b/studio/backend/utils/datasets/remote_images.py
@@ -4,6 +4,9 @@
"""Dataset image URLs are untrusted row content: fetch them only through the SSRF guard."""
import functools
+import importlib.abc
+import importlib.util
+import sys
_MAX_IMAGE_BYTES = 64 * 1024 * 1024
_IMAGE_TIMEOUT_S = 30
@@ -68,13 +71,14 @@ def fetch_untrusted_image_bytes(url):
return body
-def install_guarded_image_decode():
+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
the guard; Hub URLs keep datasets' own token-aware fetch. Idempotent."""
try:
from datasets import config
- from datasets.features import image as hf_image
+ if hf_image is None:
+ from datasets.features import image as hf_image
except Exception:
return
original = hf_image.Image.decode_example
@@ -117,4 +121,44 @@ def install_guarded_image_decode():
hf_image.Image.decode_example = decode_example
-install_guarded_image_decode()
+_IMAGE_MODULE = "datasets.features.image"
+
+
+class _GuardOnLoad(importlib.abc.Loader):
+ def __init__(self, loader):
+ self._loader = loader
+
+ def create_module(self, spec):
+ return self._loader.create_module(spec)
+
+ def exec_module(self, module):
+ self._loader.exec_module(module)
+ install_guarded_image_decode(module)
+
+
+class _GuardOnImport(importlib.abc.MetaPathFinder):
+ def find_spec(
+ self,
+ name,
+ path,
+ target = None,
+ ):
+ if name != _IMAGE_MODULE:
+ return None
+ sys.meta_path.remove(self)
+ try:
+ spec = importlib.util.find_spec(name)
+ finally:
+ sys.meta_path.insert(0, self)
+ if spec is None or spec.loader is None:
+ return spec
+ spec.loader = _GuardOnLoad(spec.loader)
+ return spec
+
+
+# Importing datasets here pulled pandas, pyarrow and torch into every Studio start, so the guard is
+# applied when datasets' image module loads instead (or now, if something already loaded it).
+if _IMAGE_MODULE in sys.modules:
+ install_guarded_image_decode(sys.modules[_IMAGE_MODULE])
+elif not any(isinstance(finder, _GuardOnImport) for finder in sys.meta_path):
+ sys.meta_path.insert(0, _GuardOnImport())