Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(embeddings): bound model artifact stamp traversal (#8210)

creed15e8f6a5c387
diff --git a/changelog.d/7468.bugfix.md b/changelog.d/7468.bugfix.md
new file mode 100644
index 0000000000..53f2eec0d2
--- /dev/null
+++ b/changelog.d/7468.bugfix.md
@@ -0,0 +1 @@
+Bound local embedding artifact scans and report oversized model trees instead of stalling concurrent embedding calls.
diff --git a/src/local_deep_research/embeddings/model_cache.py b/src/local_deep_research/embeddings/model_cache.py
index 32276b6f2d..25bb7d9bf1 100644
--- a/src/local_deep_research/embeddings/model_cache.py
+++ b/src/local_deep_research/embeddings/model_cache.py
@@ -12,6 +12,8 @@ import atexit
from copy import deepcopy
import gc
import hashlib
+from itertools import islice
+from os import scandir
from pathlib import Path
import re
from stat import S_ISDIR
@@ -26,6 +28,12 @@ from ..security.secure_logging import logger
_Result = TypeVar("_Result")
_Factory = Callable[..., Embeddings]
_CacheKey = tuple[tuple[_Factory, Any], str]
+_MAX_ARTIFACT_STAMP_ENTRIES = 512
+_ARTIFACT_STAMP_LIMIT_MESSAGE = (
+ f"Local embedding model artifact tree exceeds "
+ f"{_MAX_ARTIFACT_STAMP_ENTRIES} entries; remove unrelated files "
+ "or directory links"
+)
def _freeze(value: Any) -> Any:
@@ -83,8 +91,12 @@ def _artifact_stamp(kwargs: dict) -> str:
digest.update(str(root).encode())
paths = [root]
seen_directories = set()
+ visited = 0
while paths:
+ if visited >= _MAX_ARTIFACT_STAMP_ENTRIES:
+ raise ValueError(_ARTIFACT_STAMP_LIMIT_MESSAGE)
path = paths.pop()
+ visited += 1
try:
stat = path.stat()
except FileNotFoundError:
@@ -108,8 +120,20 @@ def _artifact_stamp(kwargs: dict) -> str:
if directory in seen_directories:
continue
seen_directories.add(directory)
- # nosemgrep: semgrep.rules.path-traversal-risk, reason: Lists an admitted model root and its linked module directories only to stat entries; no file contents are read or written
- paths.extend(sorted(path.iterdir(), reverse=True))
+ # Path.iterdir() materializes the whole directory before a
+ # caller can stop, so use lazy scandir and consume at most
+ # one entry beyond the remaining budget. Never return a
+ # partial stamp: unseen model bytes could change silently.
+ remaining = _MAX_ARTIFACT_STAMP_ENTRIES - visited
+ # nosemgrep: semgrep.rules.path-traversal-risk, reason: Lists an admitted model root and linked module directories only within a bounded budget; no file contents are read or written
+ with scandir(path) as entries:
+ children = [
+ path / entry.name
+ for entry in islice(entries, remaining + 1)
+ ]
+ if len(children) > remaining:
+ raise ValueError(_ARTIFACT_STAMP_LIMIT_MESSAGE)
+ paths.extend(sorted(children, reverse=True))
return digest.hexdigest()
diff --git a/tests/embeddings/test_model_cache.py b/tests/embeddings/test_model_cache.py
index 7e5dfbc8d5..0f0d7a097a 100644
--- a/tests/embeddings/test_model_cache.py
+++ b/tests/embeddings/test_model_cache.py
@@ -619,6 +619,26 @@ def test_retargeting_a_module_directory_invalidates_old_handles(
old.embed_query("old")
[email protected]("linked", [False, True])
+def test_large_artifact_tree_is_bounded(tmp_path, linked):
+ model = tmp_path / "local-model"
+ model.mkdir()
+ tree = (
+ tmp_path / "unrelated-large-tree" if linked else model / "0_Transformer"
+ )
+ tree.mkdir()
+ (model / "model.safetensors").write_bytes(b"weights")
+ if linked:
+ _model_symlink(model / "0_Transformer", tree, directory=True)
+ for number in range(600):
+ (tree / f"unrelated-{number:04d}").write_bytes(b"x")
+
+ with pytest.raises(ValueError, match="artifact tree exceeds 512 entries"):
+ model_cache._artifact_stamp(
+ {"model_name": str(model), "cache_folder": str(tmp_path)}
+ )
+
+
def test_caller_mutation_cannot_change_evicted_reload(models):
create, calls, _ = models
options = {"torch_dtype": "original"}