Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(client): skip symlinks in directory uploads (#2374)

Hao Zhee0cf9e0f8613
diff --git a/openviking_cli/client/http.py b/openviking_cli/client/http.py
index f9d566289..1ec35fdff 100644
--- a/openviking_cli/client/http.py
+++ b/openviking_cli/client/http.py
@@ -39,7 +39,7 @@ from openviking_cli.exceptions import (
)
from openviking_cli.retrieve.types import FindResult
from openviking_cli.session.user_id import UserIdentifier
-from openviking_cli.utils import run_async
+from openviking_cli.utils import get_logger, run_async
from openviking_cli.utils.config.ovcli_config import load_ovcli_config
from openviking_cli.utils.uri import VikingURI
@@ -67,6 +67,8 @@ ERROR_CODE_TO_EXCEPTION = {
"UNKNOWN": OpenVikingError,
}
+logger = get_logger(__name__)
+
class _HTTPObserver:
"""Observer proxy for HTTP mode.
@@ -344,15 +346,26 @@ class AsyncHTTPClient(BaseClient):
if not dir_path.is_dir():
raise ValueError(f"Path {dir_path} is not a directory")
+ root = dir_path.resolve()
temp_dir = tempfile.gettempdir()
zip_path = Path(temp_dir) / f"temp_upload_{uuid.uuid4().hex}.zip"
+ entry_count = 0
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
for file_path in dir_path.rglob("*"):
+ if file_path.is_symlink():
+ continue
if file_path.is_file():
- arcname = file_path.relative_to(dir_path)
- arcname = str(arcname).replace("\\", "/")
+ # Defense-in-depth: drop files whose real path escapes the selected root
+ # if traversal behavior changes or this walk implementation is replaced.
+ if not file_path.resolve().is_relative_to(root):
+ continue
+ arcname = str(file_path.relative_to(dir_path)).replace("\\", "/")
zipf.write(file_path, arcname=arcname)
+ entry_count += 1
+
+ if entry_count == 0:
+ logger.warning("Created empty directory upload archive for %s", dir_path)
return str(zip_path)
diff --git a/tests/client/test_rebuild_clients.py b/tests/client/test_rebuild_clients.py
index 5cd8202ef..18005f808 100644
--- a/tests/client/test_rebuild_clients.py
+++ b/tests/client/test_rebuild_clients.py
@@ -1,5 +1,7 @@
import asyncio
import threading
+import zipfile
+from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
@@ -20,6 +22,61 @@ def clear_ovcli_config(monkeypatch):
monkeypatch.setattr(http_module, "load_ovcli_config", lambda: None)
+def test_async_http_client_zip_directory_skips_symlinked_entries(tmp_path):
+ root = tmp_path / "upload"
+ root.mkdir()
+ (root / "keep.txt").write_text("keep", encoding="utf-8")
+ nested = root / "nested"
+ nested.mkdir()
+ (nested / "nested.txt").write_text("nested", encoding="utf-8")
+ outside = tmp_path / "outside.txt"
+ outside.write_text("outside", encoding="utf-8")
+
+ try:
+ (root / "inside-link.txt").symlink_to(root / "keep.txt")
+ (root / "outside-link.txt").symlink_to(outside)
+ (root / "dir-link").symlink_to(tmp_path, target_is_directory=True)
+ except OSError as exc:
+ pytest.skip(f"symlinks are not available in this environment: {exc}")
+
+ client = AsyncHTTPClient(url="http://localhost:1933")
+ zip_path = Path(client._zip_directory(str(root)))
+ try:
+ with zipfile.ZipFile(zip_path) as zipf:
+ names = sorted(zipf.namelist())
+ finally:
+ zip_path.unlink(missing_ok=True)
+
+ assert names == ["keep.txt", "nested/nested.txt"]
+
+
+def test_async_http_client_zip_directory_warns_when_archive_is_empty(tmp_path):
+ root = tmp_path / "upload"
+ root.mkdir()
+ outside = tmp_path / "outside.txt"
+ outside.write_text("outside", encoding="utf-8")
+
+ try:
+ (root / "outside-link.txt").symlink_to(outside)
+ except OSError as exc:
+ pytest.skip(f"symlinks are not available in this environment: {exc}")
+
+ client = AsyncHTTPClient(url="http://localhost:1933")
+ with patch.object(http_module.logger, "warning") as mock_warning:
+ zip_path = Path(client._zip_directory(str(root)))
+ try:
+ with zipfile.ZipFile(zip_path) as zipf:
+ names = sorted(zipf.namelist())
+ finally:
+ zip_path.unlink(missing_ok=True)
+
+ assert names == []
+ mock_warning.assert_called_once_with(
+ "Created empty directory upload archive for %s",
+ root,
+ )
+
+
async def test_async_openviking_reindex_forwards_to_local_client(tmp_path):
client = AsyncOpenViking(path=str(tmp_path))
with patch.object(client, "_ensure_initialized", new_callable=AsyncMock) as mock_init: