0001-Don-t-split-git-references-on-unicode-separators.patch 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. From 62300cf398faacdd0e490b0a1400dec2558612bf Mon Sep 17 00:00:00 2001
  2. From: Pradyun Gedam <pradyunsg@users.noreply.github.com>
  3. Date: Sat, 24 Apr 2021 10:13:15 +0100
  4. Subject: [PATCH] Don't split git references on unicode separators
  5. Previously, maliciously formatted tags could be used to hijack a
  6. commit-based pin. Using the fact that the split here allowed for
  7. all of unicode's whitespace characters as separators -- which git allows
  8. as a part of a tag name -- it is possible to force a different revision
  9. to be installed; if an attacker gains access to the repository.
  10. This change stops splitting the string on unicode characters, by forcing
  11. the splits to happen on newlines and ASCII spaces.
  12. (cherry picked from commit ca832b2836e0bffa7cf95589acdcd71230f5834e)
  13. Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
  14. ---
  15. src/pip/_internal/vcs/git.py | 10 ++++++++--
  16. 1 file changed, 8 insertions(+), 2 deletions(-)
  17. diff --git a/src/pip/_internal/vcs/git.py b/src/pip/_internal/vcs/git.py
  18. index 7483303a9..d706064e7 100644
  19. --- a/src/pip/_internal/vcs/git.py
  20. +++ b/src/pip/_internal/vcs/git.py
  21. @@ -137,9 +137,15 @@ class Git(VersionControl):
  22. output = cls.run_command(['show-ref', rev], cwd=dest,
  23. show_stdout=False, on_returncode='ignore')
  24. refs = {}
  25. - for line in output.strip().splitlines():
  26. + # NOTE: We do not use splitlines here since that would split on other
  27. + # unicode separators, which can be maliciously used to install a
  28. + # different revision.
  29. + for line in output.strip().split("\n"):
  30. + line = line.rstrip("\r")
  31. + if not line:
  32. + continue
  33. try:
  34. - sha, ref = line.split()
  35. + sha, ref = line.split(" ", maxsplit=2)
  36. except ValueError:
  37. # Include the offending line to simplify troubleshooting if
  38. # this error ever occurs.
  39. --
  40. 2.20.1