Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(security): prevent path traversal prefix collision and add tomllib fallback (#784)

Vijay Misal9ebf59ddf64c
diff --git a/backend/app/providers/local_storage.py b/backend/app/providers/local_storage.py
index 44dc0a4e..6f24022c 100644
--- a/backend/app/providers/local_storage.py
+++ b/backend/app/providers/local_storage.py
@@ -20,7 +20,9 @@ class LocalStorageProvider(StorageProvider):
def _full_path(self, storage_key: str) -> Path:
base = self._base_path().resolve()
full = (base / storage_key).resolve()
- if not str(full).startswith(str(base)):
+ try:
+ full.relative_to(base)
+ except ValueError:
raise ValueError("Invalid storage key")
return full
diff --git a/backend/tests/test_local_storage.py b/backend/tests/test_local_storage.py
index db4156b8..8caf083f 100644
--- a/backend/tests/test_local_storage.py
+++ b/backend/tests/test_local_storage.py
@@ -116,3 +116,12 @@ async def test_path_traversal_download_blocked(local_storage):
async def test_path_traversal_delete_blocked(local_storage):
with pytest.raises(ValueError, match="Invalid storage key"):
await local_storage.delete("../../etc/passwd")
+
+
+async def test_path_traversal_prefix_collision_blocked(local_storage, storage_dir):
+ # Test sibling folder sharing the same prefix name
+ sibling_evil = f"../{Path(storage_dir).name}_evil/stolen.txt"
+ with pytest.raises(ValueError, match="Invalid storage key"):
+ await local_storage.upload(sibling_evil, b"evil", "text/plain")
+