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: harden S3 dataset keys, uv fallback, header reads and auth body cap (#12936)

Daniel Hancebca113b6df
diff --git a/install.sh b/install.sh
index 2701e0f7701..8c2c93e9486 100755
--- a/install.sh
+++ b/install.sh
@@ -4117,8 +4117,9 @@ if ! command -v uv >/dev/null 2>&1 || ! _uv_version_ok uv; then
:
else
# Unpinned hosts keep the path they have always had: a wrong triple breaks the install outright, which costs more than the fallback's score.
+ # The versioned installer installs the same pinned release (and checks its per-asset sha256), not whatever is latest.
_uv_tmp=$(mktemp)
- if download "https://astral.sh/uv/install.sh" "$_uv_tmp"; then
+ if download "https://astral.sh/uv/$UV_PINNED_VERSION/install.sh" "$_uv_tmp"; then
run_maybe_quiet sh "$_uv_tmp" </dev/null || _uv_refreshed=false
else
_uv_refreshed=false
diff --git a/studio/backend/core/inference/diffusion_comfy_quant.py b/studio/backend/core/inference/diffusion_comfy_quant.py
index c0d407446b7..087991c4e4f 100644
--- a/studio/backend/core/inference/diffusion_comfy_quant.py
+++ b/studio/backend/core/inference/diffusion_comfy_quant.py
@@ -114,7 +114,7 @@ _MAX_COMFY_QUANT_BYTES = 1 << 20
def _read_json_tensor(path: str, entry: dict, base: int) -> Any:
start, end = entry["data_offsets"]
- if not 0 <= int(end) - int(start) <= _MAX_COMFY_QUANT_BYTES:
+ if int(start) < 0 or not 0 <= int(end) - int(start) <= _MAX_COMFY_QUANT_BYTES:
raise ValueError(f"comfy_quant declaration spans {int(end) - int(start)} bytes")
with open(path, "rb") as handle:
handle.seek(base + int(start))
diff --git a/studio/backend/core/inference/video_minimax_h3_comfy.py b/studio/backend/core/inference/video_minimax_h3_comfy.py
index 0b049a74824..f111f0c657b 100644
--- a/studio/backend/core/inference/video_minimax_h3_comfy.py
+++ b/studio/backend/core/inference/video_minimax_h3_comfy.py
@@ -9,10 +9,8 @@ ref2va) is not in the tensors, so it comes from the file name."""
from __future__ import annotations
import functools
-import json
import os
import re
-import struct
from typing import Any, Optional
H3_COMFY_TABLE_KEY = "adaln_t_table"
@@ -111,9 +109,9 @@ def h3_comfy_keep_dtype(key: str, pruned: bool = True) -> Optional[Any]:
def h3_comfy_curve_metadata(path: str) -> Optional[dict]:
"""``apply_h3_adaln_curve`` metadata for a pruned (curve-form) ComfyUI H3 file, from its header; None for a
dense one (no ``adaln_t_table``)."""
- with open(path, "rb") as handle:
- size = struct.unpack("<Q", handle.read(8))[0]
- header = json.loads(handle.read(size))
+ from .diffusion_comfy_quant import _read_header
+
+ header, _ = _read_header(path)
table = header.get(H3_COMFY_TABLE_KEY)
if not isinstance(table, dict) or len(table.get("shape") or ()) != 2:
return None
diff --git a/studio/backend/core/training/s3_dataset.py b/studio/backend/core/training/s3_dataset.py
index 3d057fa90e4..709fe23404c 100644
--- a/studio/backend/core/training/s3_dataset.py
+++ b/studio/backend/core/training/s3_dataset.py
@@ -151,13 +151,38 @@ def _validate_single_extension_family(keys: list[str]) -> None:
)
+def _contained_local_path(
+ target_dir: str,
+ parts: list[str],
+ pathmod = os.path,
+) -> str:
+ """Join S3 key ``parts`` under ``target_dir``; refuse absolute, drive or escaping keys (Windows joins honour them)."""
+ if any(pathmod.isabs(part) or pathmod.splitdrive(part)[0] for part in parts):
+ raise ValueError(
+ "S3 key is an absolute path and cannot be downloaded into the dataset directory."
+ )
+ # Win32 trims trailing dots and spaces, so ".. " can act as "..".
+ if any(
+ seg.rstrip(" .") == ""
+ for part in parts
+ for seg in part.replace("\\", "/").split("/")
+ if seg
+ ):
+ raise ValueError("S3 key has a dot-only path segment and cannot be downloaded.")
+ root = pathmod.normcase(pathmod.normpath(pathmod.abspath(target_dir)))
+ resolved = pathmod.normcase(pathmod.normpath(pathmod.join(root, *parts)))
+ if resolved == root or not resolved.startswith(pathmod.join(root, "")):
+ raise ValueError("S3 key resolves outside the dataset directory and cannot be downloaded.")
+ return pathmod.join(target_dir, *parts)
+
+
def _unique_local_path(target_dir: str, filename: str, used_paths: set[str]) -> str:
"""Return an unused flattened path for an S3 object basename."""
stem, ext = os.path.splitext(filename)
- candidate = os.path.join(target_dir, filename)
+ candidate = _contained_local_path(target_dir, [filename])
suffix = 1
while candidate in used_paths or os.path.exists(candidate):
- candidate = os.path.join(target_dir, f"{stem}_{suffix}{ext}")
+ candidate = _contained_local_path(target_dir, [f"{stem}_{suffix}{ext}"])
suffix += 1
used_paths.add(candidate)
return candidate
@@ -197,7 +222,7 @@ def _download_structured(
f"S3 key {key!r} contains '..' or empty path segments and cannot "
"be downloaded into the dataset directory."
)
- local_path = os.path.join(target_dir, *parts)
+ local_path = _contained_local_path(target_dir, parts)
os.makedirs(os.path.dirname(local_path) or target_dir, exist_ok = True)
_download_one(client, bucket, key, local_path, cancel_callback)
local_by_key[key] = local_path
diff --git a/studio/backend/main.py b/studio/backend/main.py
index fc337bd5667..dcec3718896 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -1384,6 +1384,8 @@ _BODY_PROTECTED_PREFIXES = (
"/api/export",
"/api/library",
"/api/browser",
+ # Unauthenticated (login, refresh): every route takes a few hundred bytes of JSON.
+ "/api/auth",
"/mcp",
)
_DATASET_UPLOAD_PASSTHROUGH_PREFIXES = (
diff --git a/studio/backend/tests/test_diffusion_header_bounds.py b/studio/backend/tests/test_diffusion_header_bounds.py
new file mode 100644
index 00000000000..d33007e371a
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_header_bounds.py
@@ -0,0 +1,84 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Header readers must not let a downloaded checkpoint name how many bytes Studio buffers."""
+
+from __future__ import annotations
+
+import json
+import struct
+
+import pytest
+
+from core.inference import diffusion_comfy_quant as cq
+from core.inference import video_minimax_h3_comfy as h3c
+
+
+def _safetensors(
+ path,
+ header: dict,
+ payload: bytes = b"",
+) -> str:
+ raw = json.dumps(header).encode("utf-8")
+ path.write_bytes(struct.pack("<Q", len(raw)) + raw + payload)
+ return str(path)
+
+
+def test_an_oversized_comfy_quant_declaration_is_refused(tmp_path):
+ payload = json.dumps("a" * (2 * 1024 * 1024)).encode("utf-8")
+ path = _safetensors(
+ tmp_path / "big.safetensors",
+ {
+ "x.comfy_quant": {
+ "dtype": "U8",
+ "shape": [len(payload)],
+ "data_offsets": [0, len(payload)],
+ }
+ },
+ payload,
+ )
+ scan = cq.scan_comfy_quant(path)
+ assert scan is not None
+ assert scan.problems == ["x: unreadable comfy_quant declaration"]
+
+
+def test_reversed_comfy_quant_offsets_are_refused(tmp_path):
+ path = _safetensors(
+ tmp_path / "rev.safetensors",
+ {"x.comfy_quant": {"dtype": "U8", "shape": [4], "data_offsets": [8, 4]}},
+ b"{} ",
+ )
+ assert cq.scan_comfy_quant(path).problems == ["x: unreadable comfy_quant declaration"]
+
+
+def test_a_normal_comfy_quant_declaration_still_reads(tmp_path):
+ payload = json.dumps({"format": "int8_tensorwise"}).encode("utf-8")
+ path = _safetensors(
+ tmp_path / "ok.safetensors",
+ {
+ "x.comfy_quant": {
+ "dtype": "U8",
+ "shape": [len(payload)],
+ "data_offsets": [0, len(payload)],
+ }
+ },
+ payload,
+ )
+ scan = cq.scan_comfy_quant(path)
+ assert not any("unreadable" in p for p in scan.problems)
+
+
+def test_h3_curve_metadata_refuses_an_implausible_header_size(tmp_path):
+ path = tmp_path / "h3.safetensors"
+ path.write_bytes(struct.pack("<Q", 300 * 1024 * 1024) + b"{}")
+ with pytest.raises(ValueError, match = "implausible"):
+ h3c.h3_comfy_curve_metadata(str(path))
+
+
+def test_h3_curve_metadata_reads_a_normal_header(tmp_path):
+ path = _safetensors(
+ tmp_path / "dense.safetensors",
+ {"w": {"dtype": "F32", "shape": [1], "data_offsets": [0, 4]}},
+ b"\0" * 4,
+ )
+ assert h3c.h3_comfy_curve_metadata(path) is None
diff --git a/studio/backend/tests/test_middleware.py b/studio/backend/tests/test_middleware.py
index 64eb232060e..fc746704e8b 100644
--- a/studio/backend/tests/test_middleware.py
+++ b/studio/backend/tests/test_middleware.py
@@ -306,6 +306,17 @@ class TestMaxBodyMiddleware:
f"/api/browser{path}".startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES
), path
+ def test_auth_posts_are_capped_before_auth(self, main_module):
+ # login / refresh / desktop-login are reachable without a session.
+ from routes.auth import router
+
+ posts = [route.path for route in router.routes if "POST" in route.methods]
+ assert posts
+ for path in posts:
+ assert any(
+ f"/api/auth{path}".startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES
+ ), path
+
def test_diffusion_dataset_json_subroutes_keep_default_cap(self, main_module):
# The exact-path passthrough must NOT sweep in the JSON sub-routes under the same prefix: a prefix match would let a large
# caption/import body bypass the default JSON cap and be buffered up to the far larger upload limit.
diff --git a/studio/backend/tests/test_s3_audio_dataset.py b/studio/backend/tests/test_s3_audio_dataset.py
index e02dae08ea7..e3488538adf 100644
--- a/studio/backend/tests/test_s3_audio_dataset.py
+++ b/studio/backend/tests/test_s3_audio_dataset.py
@@ -237,3 +237,35 @@ def test_a_key_with_dotdot_segments_cannot_escape_the_download_dir(monkeypatch,
escaped = tmp_path / "evil.wav"
assert not escaped.exists()
assert all("evil" not in key for (_b, key, _p) in client.downloaded)
+
+
+ "parts",
+ [
+ ["C:evil.txt"],
+ ["C:\\Users\\victim\\Startup\\evil.bat"],
+ ["\\\\server\\share\\evil.wav"],
+ ["audio", "..\\..\\evil.wav"],
+ ["\\evil.wav"],
+ ["datasets", ".. ", "evil.wav"],
+ ["datasets", ". .", "evil.wav"],
+ ["datasets\\.. \\evil.wav"],
+ ],
+)
+def test_windows_shaped_keys_cannot_escape_the_download_dir(parts):
+ import ntpath
+ with pytest.raises(ValueError, match = "S3 key"):
+ s3_dataset._contained_local_path("C:\\data\\download", parts, pathmod = ntpath)
+
+
+ "parts", [["train.parquet"], ["audio", "a.wav"], ["audio", "x..y.wav"], ["a\\b.wav"]]
+)
+def test_ordinary_keys_stay_inside_the_download_dir(parts, tmp_path):
+ import ntpath
+ assert s3_dataset._contained_local_path(str(tmp_path), parts) == os.path.join(
+ str(tmp_path), *parts
+ )
+ if "\\" not in parts[-1]:
+ win = s3_dataset._contained_local_path("C:\\data\\download", parts, pathmod = ntpath)
+ assert win == ntpath.join("C:\\data\\download", *parts)
diff --git a/studio/setup.sh b/studio/setup.sh
index 8ef35200b11..da7874311b6 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -3167,9 +3167,9 @@ elif {
if _setup_install_uv_pinned || { [ "$_SIUP_UNFETCHED" = true ] && _mirror_switch uvbin && _setup_install_uv_pinned; }; then
_SETUP_UV_PINNED_OK=true
elif _is_verbose; then
- _setup_http_get https://astral.sh/uv/install.sh | sh
+ _setup_http_get "https://astral.sh/uv/$_SETUP_UV_PINNED_VERSION/install.sh" | sh
else
- _setup_http_get https://astral.sh/uv/install.sh | sh > /dev/null 2>&1
+ _setup_http_get "https://astral.sh/uv/$_SETUP_UV_PINNED_VERSION/install.sh" | sh > /dev/null 2>&1
fi
}; then
# Only for astral's installer, which writes to ~/.local/bin. The pinned path already put its
diff --git a/tests/sh/test_uv_pinned_release.sh b/tests/sh/test_uv_pinned_release.sh
index 6bb74e6865e..6a0ca482afc 100755
--- a/tests/sh/test_uv_pinned_release.sh
+++ b/tests/sh/test_uv_pinned_release.sh
@@ -30,7 +30,7 @@ fi
# The pinned attempt must precede the fallback, or the fallback is what actually runs.
_pinned_at=$(grep -n 'if _uv_install_pinned[ ;]' "$INSTALL_SH" | head -1 | cut -d: -f1)
-_fallback_at=$(grep -n 'download "https://astral.sh/uv/install.sh"' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_fallback_at=$(grep -n 'download "https://astral.sh/uv/$UV_PINNED_VERSION/install.sh"' "$INSTALL_SH" | head -1 | cut -d: -f1)
if [ -n "$_pinned_at" ] && [ -n "$_fallback_at" ] && [ "$_pinned_at" -lt "$_fallback_at" ]; then
ok "the pinned path is tried before the astral fallback"
else
diff --git a/tests/studio/test_installer_av_shapes.py b/tests/studio/test_installer_av_shapes.py
index 211572196c5..966ebf45da2 100644
--- a/tests/studio/test_installer_av_shapes.py
+++ b/tests/studio/test_installer_av_shapes.py
@@ -322,13 +322,14 @@ def test_no_remote_script_is_executed_in_process(name: str) -> None:
def test_no_remote_script_is_piped_into_a_shell_first(name: str) -> None:
# The astral fallback stays reachable for unpinned hosts, but must never be tried first.
text = _text(name)
- if "astral.sh/uv/install.sh" not in text:
+ match = re.search(r"astral\.sh/uv/(\$\w+/)?install\.sh", text)
+ if match is None:
return
pinned = min(
(m.start() for m in re.finditer(r"_(setup_install_uv_pinned|uv_install_pinned)\b", text)),
default = None,
)
- fallback = text.index("astral.sh/uv/install.sh")
+ fallback = match.start()
assert pinned is not None, f"{name} has no pinned uv path"
assert pinned < fallback, f"{name} reaches the piped fallback before the pinned release"