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: check the weight index of a module path that only reads as the root (#16212)

Daniel Hanc917be491848
diff --git a/studio/backend/tests/test_account_path_confinement.py b/studio/backend/tests/test_account_path_confinement.py
index 84b5626d327..e19a2a30c53 100644
--- a/studio/backend/tests/test_account_path_confinement.py
+++ b/studio/backend/tests/test_account_path_confinement.py
@@ -276,6 +276,85 @@ def test_a_dotdot_through_a_link_that_stays_in_the_folder_passes(tmp_path, files
assert escaping_weight_references(folder)
+@symlinks
+def test_a_module_path_that_reads_as_the_root_but_lands_elsewhere_has_its_index_read(tmp_path):
+ from utils.security.file_security import escaping_weight_references
+
+ folder = _write(
+ tmp_path / "m",
+ {"modules.json": [{"idx": 0, "name": "0", "path": "link/..", "type": "x"}]},
+ )
+ (folder / "deep" / "child").mkdir(parents = True)
+ (folder / "link").symlink_to(folder / "deep" / "child", target_is_directory = True)
+ index = {"weight_map": {"w": "../../victim.safetensors"}}
+ _write(folder / "deep", {"model.safetensors.index.json": index})
+ assert escaping_weight_references(folder) == ["link/../../../victim.safetensors"]
+ _write(
+ folder / "deep", {"model.safetensors.index.json": {"weight_map": {"w": "s.safetensors"}}}
+ )
+ assert escaping_weight_references(folder) == []
+
+
+@symlinks
+def test_a_module_index_climbing_back_into_the_folder_from_where_it_lands_passes(tmp_path):
+ from utils.security.file_security import escaping_weight_references
+
+ folder = _write(
+ tmp_path / "m",
+ {"modules.json": [{"idx": 0, "name": "0", "path": "link/..", "type": "x"}]},
+ )
+ (folder / "model.safetensors").write_bytes(b"")
+ (folder / "deep" / "child").mkdir(parents = True)
+ (folder / "link").symlink_to(folder / "deep" / "child", target_is_directory = True)
+ index = {"weight_map": {"w": "../model.safetensors"}}
+ _write(folder / "deep", {"model.safetensors.index.json": index})
+ assert escaping_weight_references(folder) == []
+
+
+@symlinks
+def test_a_module_index_shard_linking_outside_from_where_it_lands_is_flagged(tmp_path):
+ from utils.security.file_security import escaping_weight_references
+
+ victim = tmp_path / "victim"
+ victim.mkdir()
+ folder = _write(
+ tmp_path / "m",
+ {"modules.json": [{"idx": 0, "name": "0", "path": "link/..", "type": "x"}]},
+ )
+ (folder / "deep" / "child").mkdir(parents = True)
+ (folder / "link").symlink_to(folder / "deep" / "child", target_is_directory = True)
+ (folder / "deep" / "out").symlink_to(victim, target_is_directory = True)
+ for shard, flagged in (
+ ("out/s.safetensors", "link/../out/s.safetensors"),
+ ("/abs/s.safetensors", "/abs/s.safetensors"),
+ ("C:/s.safetensors", "C:/s.safetensors"),
+ ):
+ _write(folder / "deep", {"model.safetensors.index.json": {"weight_map": {"w": shard}}})
+ assert escaping_weight_references(folder) == [flagged]
+
+
+@symlinks
+def test_a_module_path_climbing_through_a_link_into_a_subfolder_is_judged_where_it_lands(tmp_path):
+ from utils.security.file_security import escaping_weight_references
+
+ victim = tmp_path / "victim"
+ (victim / "child").mkdir(parents = True)
+ folder = _write(
+ tmp_path / "m",
+ {"modules.json": [{"idx": 0, "name": "0", "path": "link/../x", "type": "x"}]},
+ )
+ (folder / "deep" / "child").mkdir(parents = True)
+ (folder / "deep" / "x").mkdir()
+ (folder / "link").symlink_to(folder / "deep" / "child", target_is_directory = True)
+ (folder / "deep" / "x" / "out").symlink_to(victim / "child", target_is_directory = True)
+ index = {"weight_map": {"w": "out/../s.safetensors"}}
+ _write(folder / "deep" / "x", {"model.safetensors.index.json": index})
+ assert escaping_weight_references(folder) == ["link/../x/out/../s.safetensors"]
+ index = {"weight_map": {"w": "s.safetensors"}}
+ _write(folder / "deep" / "x", {"model.safetensors.index.json": index})
+ assert escaping_weight_references(folder) == []
+
+
@symlinks
def test_a_hub_cache_snapshot_with_blob_links_passes(tmp_path):
from utils.security.file_security import escaping_weight_references
@@ -499,6 +578,7 @@ if os.name != "nt":
_DOT_NAMED_MODULE_PATHS.append(".\\...")
[email protected](sys.platform == "win32", reason = "Win32 strips trailing dots from folder names")
@pytest.mark.parametrize("path", _DOT_NAMED_MODULE_PATHS)
def test_a_dot_named_module_folder_index_outside_the_model_is_flagged(tmp_path, path):
from utils.security.file_security import escaping_weight_references
diff --git a/studio/backend/utils/security/file_security.py b/studio/backend/utils/security/file_security.py
index a91a3c255f2..cdf972adde2 100644
--- a/studio/backend/utils/security/file_security.py
+++ b/studio/backend/utils/security/file_security.py
@@ -108,6 +108,27 @@ def _climbs_through_link(folder: Path, name: str) -> bool:
return not any(_within(landing, root) for root in roots)
+def _lands_outside(folder: Path, module: Path, name: str) -> bool:
+ spelled = name.replace("\\", "/")
+ if spelled.startswith("/") or name[1:2] == ":":
+ return True
+ try:
+ landing = os.path.realpath(os.path.join(os.path.realpath(module), name))
+ roots = _confinement_roots(folder)
+ except (OSError, ValueError):
+ return True
+ return not any(_within(landing, root) for root in roots)
+
+
+def _same_folder(path: Path, folder: Path) -> bool:
+ try:
+ return os.path.normcase(os.path.realpath(path)) == os.path.normcase(
+ os.path.realpath(folder)
+ )
+ except (OSError, ValueError):
+ return False
+
+
def _escapes(folder: Path, name: str) -> bool:
return _escapes_folder(name) or _climbs_through_link(folder, name)
@@ -166,6 +187,7 @@ def escaping_weight_references(folder) -> list:
except (OSError, ValueError):
return []
names = _weight_names(folder)
+ outside = []
try:
modules = json.loads((folder / "modules.json").read_text(encoding = "utf-8-sig"))
except (OSError, ValueError):
@@ -186,14 +208,22 @@ def escaping_weight_references(folder) -> list:
if any(_escapes_folder(spelled) for spelled in spellings):
names.append(sub)
continue
- if spellings:
+ if not _same_folder(folder / sub, folder / os.path.normpath(sub)):
+ # link/.. or link/../x lands beside the link's target: judge names from there.
+ outside.extend(
+ _in_subfolder(sub, name)
+ for name in _weight_names(folder / sub)
+ if _lands_outside(folder, folder / sub, name)
+ )
+ elif spellings:
module_names = _weight_names(folder / sub)
for spelled in spellings:
names.extend(_in_subfolder(spelled, name) for name in module_names)
# Spark-TTS / BiCodec load their transformers model from LLM/.
if (folder / "LLM").is_dir():
names.extend(_in_subfolder("LLM", name) for name in _weight_names(folder / "LLM"))
- return [name for name in dict.fromkeys(names) if _escapes(folder, name)]
+ names = [name for name in names if _escapes(folder, name)]
+ return list(dict.fromkeys(names + outside))
def _normalize_repo_path(path: str) -> str: