Skip to content

Commit 382b60a

Browse files
committed
This change adds a check during reference resolving to see if the requested reference is inside the current repository folder. If it's ouside, it raises an exception. This fixes CVE-2023-41040, which allows an attacker to access files outside the repository's directory.
1 parent 91b464c commit 382b60a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Diff for: git/refs/symbolic.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from git.types import PathLike
2+
from pathlib import Path
23
import os
34

45
from git.compat import defenc
@@ -171,7 +172,14 @@ def _get_ref_info_helper(
171172
tokens: Union[None, List[str], Tuple[str, str]] = None
172173
repodir = _git_dir(repo, ref_path)
173174
try:
174-
with open(os.path.join(repodir, str(ref_path)), "rt", encoding="UTF-8") as fp:
175+
# Make the path absolute, normalizing any up-level references and
176+
# separators
177+
normalized_ref = Path(os.path.abspath(os.path.join(repodir, str(ref_path))))
178+
normalized_repodir = Path(os.path.abspath(repodir))
179+
if normalized_repodir not in normalized_ref.parents:
180+
raise ValueError(f"Reference at {normalized_ref} is outside the repo directory")
181+
182+
with open(normalized_ref, "rt", encoding="UTF-8") as fp:
175183
value = fp.read().rstrip()
176184
# Don't only split on spaces, but on whitespace, which allows to parse lines like
177185
# 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo

0 commit comments

Comments
 (0)