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

‎git/refs/symbolic.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fromgit.typesimportPathLike
2+
frompathlibimportPath
23
importos
34

45
fromgit.compatimportdefenc
@@ -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-
withopen(os.path.join(repodir, str(ref_path)), "rt", encoding="UTF-8") asfp:
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+
ifnormalized_repodirnotinnormalized_ref.parents:
180+
raiseValueError(f"Reference at {normalized_ref} is outside the repo directory")
181+
182+
withopen(normalized_ref, "rt", encoding="UTF-8") asfp:
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)