Skip to content

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

fix(security): prevent Git submodule SSRF (#3108)

Qin Haojiecbdab573539d
diff --git a/openviking/parse/accessors/git_accessor.py b/openviking/parse/accessors/git_accessor.py
index 718f02258..d3c8e715b 100644
--- a/openviking/parse/accessors/git_accessor.py
+++ b/openviking/parse/accessors/git_accessor.py
@@ -16,7 +16,7 @@ import urllib.request
import zipfile
from pathlib import Path, PurePosixPath
from typing import Any, Optional, Tuple, Union
-from urllib.parse import unquote, urlparse
+from urllib.parse import quote, unquote, urlparse
from openviking.utils import is_github_url, is_gitlab_url, parse_code_hosting_url
from openviking.utils.code_hosting_utils import (
@@ -337,9 +337,8 @@ class GitAccessor(DataAccessor):
user_msg = (
"Git command failed: authentication error. Check your SSH keys or credentials."
)
- raise RuntimeError(
- f"{user_msg} Command: git {' '.join(args[1:])}. Details: {error_msg}"
- )
+ logger.warning(f"[GitAccessor] {user_msg} Details: {error_msg}")
+ raise RuntimeError(user_msg)
return stdout.decode().strip()
async def _has_commit(self, repo_dir: str, commit: str) -> bool:
@@ -376,7 +375,7 @@ class GitAccessor(DataAccessor):
"clone",
"--depth",
"1",
- "--recursive",
+ "--no-recurse-submodules",
]
if branch and not commit:
clone_args.extend(["--branch", branch])
@@ -444,10 +443,8 @@ class GitAccessor(DataAccessor):
# Strip .git suffix for the archive URL
repo_slug = repo_raw[:-4] if repo_raw.endswith(".git") else repo_raw
- if branch:
- zip_url = f"https://github.com/{owner}/{repo_slug}/archive/{branch}.zip"
- else:
- zip_url = f"https://github.com/{owner}/{repo_slug}/archive/HEAD.zip"
+ ref = quote(branch or "HEAD", safe="/")
+ zip_url = f"https://github.com/{owner}/{repo_slug}/archive/{ref}.zip"
logger.info(f"[GitAccessor] Downloading GitHub ZIP: {zip_url}")
diff --git a/openviking/parse/parsers/code/code.py b/openviking/parse/parsers/code/code.py
index 0541280c0..5efe4bbf8 100644
--- a/openviking/parse/parsers/code/code.py
+++ b/openviking/parse/parsers/code/code.py
@@ -467,7 +467,7 @@ class CodeRepositoryParser(BaseParser):
"clone",
"--depth",
"1",
- "--recursive",
+ "--no-recurse-submodules",
]
if branch and not commit:
clone_args.extend(["--branch", branch])
diff --git a/tests/unit/test_accessors_git.py b/tests/unit/test_accessors_git.py
index 0c89ab5fd..25bb74f1c 100644
--- a/tests/unit/test_accessors_git.py
+++ b/tests/unit/test_accessors_git.py
@@ -4,7 +4,7 @@
from pathlib import Path
from types import SimpleNamespace
-from unittest.mock import patch
+from unittest.mock import AsyncMock, patch
import pytest
@@ -152,3 +152,43 @@ class TestGitAccessor:
def test_cannot_handle_other_urls(self, accessor: GitAccessor, source: str) -> None:
"""GitAccessor should not handle non-git URLs or files."""
assert accessor.can_handle(source) is False
+
+ async def test_git_clone_does_not_fetch_submodules(
+ self, accessor: GitAccessor, tmp_path: Path
+ ) -> None:
+ with patch.object(accessor, "_run_git", new_callable=AsyncMock) as run_git:
+ await accessor._git_clone("https://github.com/volcengine/OpenViking.git", str(tmp_path))
+
+ clone_args = run_git.await_args.args[0]
+ assert "--no-recurse-submodules" in clone_args
+ assert "--recursive" not in clone_args
+
+ async def test_github_archive_encodes_fragment_in_ref(
+ self, accessor: GitAccessor, tmp_path: Path
+ ) -> None:
+ with patch(
+ "openviking.parse.accessors.git_accessor.urllib.request.urlopen",
+ side_effect=OSError("stop before network"),
+ ) as urlopen:
+ with pytest.raises(RuntimeError):
+ await accessor._github_zip_download(
+ "https://github.com/example/repo", "test#ssrf", str(tmp_path)
+ )
+
+ request = urlopen.call_args.args[0]
+ assert request.full_url == "https://github.com/example/repo/archive/test%23ssrf.zip"
+
+ async def test_git_error_does_not_expose_remote_stderr(self, accessor: GitAccessor) -> None:
+ process = SimpleNamespace(
+ returncode=1,
+ communicate=AsyncMock(return_value=(b"", b"remote: internal metadata")),
+ )
+ with patch(
+ "openviking.parse.accessors.git_accessor.asyncio.create_subprocess_exec",
+ new=AsyncMock(return_value=process),
+ ):
+ with pytest.raises(RuntimeError) as exc_info:
+ await accessor._run_git(["git", "clone", "https://github.com/example/repo"])
+
+ assert str(exc_info.value) == "Git command failed."
+ assert "internal metadata" not in str(exc_info.value)