Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/key_value/aio/stores/filetree/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ async def get_entry(self, *, key: str) -> ManagedEntry | None:
if not await key_path.exists():
return None

data_dict: dict[str, Any] = await read_file(file=key_path)
try:
data_dict: dict[str, Any] = await read_file(file=key_path)
except FileNotFoundError:
# Another actor removed the entry between the exists() check and the read.
# Report a miss, the same as delete_entry does when the file is already gone.
return None

return self.serialization_adapter.load_dict(data=data_dict)

Expand Down
20 changes: 20 additions & 0 deletions tests/stores/filetree/test_filetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ async def unlink_after_external_removal(path: AsyncPath) -> None:
assert await store.delete(collection="test", key="race_key") is False
assert await store.get(collection="test", key="race_key") is None

async def test_get_returns_none_when_file_disappears_after_exists_check(
self,
store: FileTreeStore,
monkeypatch: pytest.MonkeyPatch,
):
"""get should report a miss when another actor removes the file mid-read."""
await store.put(collection="test", key="race_key", value={"data": "value"})

original_exists = AsyncPath.exists

async def exists_then_external_removal(path: AsyncPath) -> bool:
result = await original_exists(path)
if Path(path).name == "race_key.json":
Path(path).unlink()
return result

monkeypatch.setattr(AsyncPath, "exists", exists_then_external_removal)

assert await store.get(collection="test", key="race_key") is None


class TestFileTreeStorePathTraversal:
"""Test suite for FileTreeStore path traversal security."""
Expand Down