Skip to content

World's first open-source, agentic video production system. 12 production pipelines, 100+ tools, 700+ agent skill and production-knowledge files. Turn your AI coding assistant into a full video production studio.

fix(clip-cache): prevent path traversal in ingest, try_link, and eviction (#711)

Hugecf9f03c09f02
diff --git a/tests/tools/test_clip_cache.py b/tests/tools/test_clip_cache.py
index feb155d9..cb1907e5 100644
--- a/tests/tools/test_clip_cache.py
+++ b/tests/tools/test_clip_cache.py
@@ -435,3 +435,115 @@ def test_get_default_cache_returns_same_instance(monkeypatch, tmp_path):
b = get_default_cache()
assert a is b
reset_default_cache()
+
+
+# ----------------------------------------------------------------------
+# Path traversal defenses
+# ----------------------------------------------------------------------
+
+
+def test_ingest_rejects_path_traversal_clip_id(tmp_path):
+ cache = ClipCache(cache_dir=tmp_path / "cache")
+ src = _fake_clip(tmp_path / "source.mp4", 4000)
+
+ # A sensitive file outside cache_dir that must never be touched
+ outside_canary = tmp_path / "canary.mp4"
+ outside_canary.write_text("protected content")
+
+ # Attempting to ingest with a traversal clip_id aiming at the canary
+ traversal_id = "../canary"
+ ok = cache.ingest(traversal_id, src, _default_metadata(traversal_id))
+ assert ok is False
+ assert outside_canary.read_text() == "protected content"
+
+ # Various malformed or traversal identifiers
+ bad_clip_ids = [
+ "../escape",
+ "../../escape",
+ "/absolute/escape",
+ "dir/subclip",
+ "dir\\subclip",
+ "..\\windows_escape",
+ "clip:stream",
+ "clip\x00null",
+ "",
+ ".",
+ "..",
+ ]
+ for bad_id in bad_clip_ids:
+ assert cache.ingest(bad_id, src, _default_metadata()) is False
+
+ # Confirm cache manifest and cache dir have no escaped files
+ entries = cache._read_manifest()
+ assert len(entries) == 0
+
+
+def test_try_link_rejects_path_traversal_clip_id(tmp_path):
+ cache = ClipCache(cache_dir=tmp_path / "cache")
+ dest = tmp_path / "project" / "leaked.mp4"
+
+ # An outside file exists
+ _fake_clip(tmp_path / "outside.mp4", 3000)
+
+ # Attempting to link using path traversal in clip_id
+ assert cache.try_link("../outside", dest) is False
+ assert cache.try_link("../../outside", dest) is False
+ assert cache.try_link("/outside", dest) is False
+ assert not dest.exists()
+ assert cache.misses >= 3
+
+
+def test_manifest_prunes_traversal_entries(tmp_path):
+ cache_dir = tmp_path / "cache"
+ cache = ClipCache(cache_dir=cache_dir)
+
+ outside_victim = _fake_clip(tmp_path / "victim.mp4", 2000)
+
+ # Inject corrupt or malicious entries directly into the manifest
+ evil_traversal = CacheEntry(
+ clip_id="evil_traversal",
+ file_name="../victim.mp4",
+ size_bytes=2000,
+ added_at=100.0,
+ last_access_at=100.0,
+ )
+ evil_id = CacheEntry(
+ clip_id="../bad_id",
+ file_name="bad.mp4",
+ size_bytes=2000,
+ added_at=100.0,
+ last_access_at=100.0,
+ )
+ valid_entry = CacheEntry(
+ clip_id="valid_clip",
+ file_name="valid_clip.mp4",
+ size_bytes=2000,
+ added_at=200.0,
+ last_access_at=200.0,
+ )
+ _fake_clip(cache_dir / "valid_clip.mp4", 2000)
+
+ manifest_path = cache_dir / ClipCache.MANIFEST_NAME
+ with open(manifest_path, "w", encoding="utf-8") as f:
+ f.write(json.dumps(evil_traversal.to_dict()) + "\n")
+ f.write(json.dumps(evil_id.to_dict()) + "\n")
+ f.write(json.dumps(valid_entry.to_dict()) + "\n")
+
+ # _read_manifest should silently filter out traversal entries
+ entries = cache._read_manifest()
+ assert "evil_traversal" not in entries
+ assert "../bad_id" not in entries
+ assert "valid_clip" in entries
+ assert len(entries) == 1
+
+ # try_link must refuse to link any traversal file even if present in entries
+ dest = tmp_path / "project" / "dest.mp4"
+ assert cache.try_link("evil_traversal", dest) is False
+ assert not dest.exists()
+
+ # Eviction must never unlink outside files
+ cache.max_total_bytes = 1000 # force eviction on next ingest
+ src = _fake_clip(tmp_path / "new.mp4", 2000)
+ cache.ingest("new_clip", src, _default_metadata("new_clip"))
+ assert outside_victim.exists(), "outside victim file must not be unlinked during eviction"
+
diff --git a/tools/video/clip_cache.py b/tools/video/clip_cache.py
index c0ff7def..29cbe250 100644
--- a/tools/video/clip_cache.py
+++ b/tools/video/clip_cache.py
@@ -169,6 +169,44 @@ class CacheEntry:
)
+# ----------------------------------------------------------------------
+# Path confinement and safety helpers
+# ----------------------------------------------------------------------
+
+
+def _is_safe_clip_id(clip_id: str) -> bool:
+ """Validate that clip_id is safe and cannot escape the cache directory."""
+ if not clip_id or not isinstance(clip_id, str):
+ return False
+ if "\x00" in clip_id:
+ return False
+ if any(sep in clip_id for sep in ("/", "\\", ":")) or ".." in clip_id:
+ return False
+ if clip_id.strip() in {".", ".."}:
+ return False
+ return True
+
+
+def _safe_blob_path(cache_dir: Path, name: str) -> Optional[Path]:
+ """Resolve a blob path and ensure it stays strictly within cache_dir."""
+ if not name or not isinstance(name, str):
+ return None
+ if "\x00" in name:
+ return None
+ if any(sep in name for sep in ("/", "\\", ":")) or ".." in name:
+ return None
+ if name.strip() in {".", ".."}:
+ return None
+ try:
+ resolved_dir = cache_dir.resolve()
+ candidate = (cache_dir / name).resolve()
+ if candidate.is_relative_to(resolved_dir) and candidate != resolved_dir:
+ return candidate
+ except (ValueError, RuntimeError, OSError):
+ return None
+ return None
+
+
# ----------------------------------------------------------------------
# The cache itself
# ----------------------------------------------------------------------
@@ -277,6 +315,10 @@ class ClipCache:
try:
d = json.loads(line)
entry = CacheEntry.from_dict(d)
+ if not _is_safe_clip_id(entry.clip_id):
+ continue
+ if _safe_blob_path(self.cache_dir, entry.file_name) is None:
+ continue
entries[entry.clip_id] = entry
except (json.JSONDecodeError, KeyError, TypeError, ValueError):
continue
@@ -326,6 +368,10 @@ class ClipCache:
gone), the stale entry is pruned and the call reports a miss,
so the caller falls back to downloading fresh.
"""
+ if not _is_safe_clip_id(clip_id):
+ self.misses += 1
+ return False
+
dest = Path(dest)
with self._locked():
entries = self._read_manifest()
@@ -334,9 +380,9 @@ class ClipCache:
self.misses += 1
return False
- blob_path = self.cache_dir / entry.file_name
- if not blob_path.exists():
- # Drift — prune and miss.
+ blob_path = _safe_blob_path(self.cache_dir, entry.file_name)
+ if blob_path is None or not blob_path.exists():
+ # Drift or unsafe — prune and miss.
del entries[clip_id]
self._write_manifest(entries)
self.misses += 1
@@ -382,6 +428,9 @@ class ClipCache:
and had its access time bumped), ``False`` if ingest failed
(missing source, empty file, lock timeout, or link/copy fail).
"""
+ if not _is_safe_clip_id(clip_id):
+ return False
+
source_path = Path(source_path)
if not source_path.exists():
return False
@@ -398,12 +447,14 @@ class ClipCache:
entries = self._read_manifest()
# Already cached → just bump last_access and return.
- if clip_id in entries and (
- self.cache_dir / entries[clip_id].file_name
- ).exists():
- entries[clip_id].last_access_at = time.time()
- self._write_manifest(entries)
- return True
+ if clip_id in entries:
+ existing_blob = _safe_blob_path(
+ self.cache_dir, entries[clip_id].file_name
+ )
+ if existing_blob and existing_blob.exists():
+ entries[clip_id].last_access_at = time.time()
+ self._write_manifest(entries)
+ return True
# Make room.
self._evict_to_fit_locked(entries, size_bytes)
@@ -412,7 +463,9 @@ class ClipCache:
# as long as clip_ids are unique (they are — {source}_{source_id}).
ext = source_path.suffix or ""
blob_name = f"{clip_id}{ext}"
- blob_path = self.cache_dir / blob_name
+ blob_path = _safe_blob_path(self.cache_dir, blob_name)
+ if blob_path is None:
+ return False
# Clean any stale blob at the same path (drift or interrupted
# ingest from a previous run).
@@ -497,7 +550,10 @@ class ClipCache:
for victim in sorted_victims:
if current_bytes + needed_bytes <= self.max_total_bytes:
break
- blob_path = self.cache_dir / victim.file_name
+ blob_path = _safe_blob_path(self.cache_dir, victim.file_name)
+ if blob_path is None:
+ del entries[victim.clip_id]
+ continue
unlinked = False
try:
if blob_path.exists():