Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
facutuesca committed Sep 5, 2023
1 parent 91b464c commit b2d3d01
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion git/refs/symbolic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from git.types import PathLike
from pathlib import Path
import os

from git.compat import defenc
Expand Down Expand Up @@ -171,7 +172,13 @@ def _get_ref_info_helper(
tokens: Union[None, List[str], Tuple[str, str]] = None
repodir = _git_dir(repo, ref_path)
try:
with open(os.path.join(repodir, str(ref_path)), "rt", encoding="UTF-8") as fp:
# Make path absolute, resolving any symlinks, and check that we are still
# inside the repository
full_ref_path = Path(repodir, str(ref_path)).resolve(strict=True)
if Path(repodir) not in full_ref_path.parents:
raise ValueError(f"Reference at {full_ref_path} is outside the repo directory")

with open(full_ref_path, "rt", encoding="UTF-8") as fp:
value = fp.read().rstrip()
# Don't only split on spaces, but on whitespace, which allows to parse lines like
# 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo
Expand Down

0 comments on commit b2d3d01

Please sign in to comment.