Skip to content

Commit 8e227a9

Browse files
authored
Merge pull request #13777 from sethmlarson/commonpath
Use os.path.commonpath() instead of commonprefix()
2 parents f5315ad + 4c651b7 commit 8e227a9

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

news/+1ee322a1.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use a path-segment prefix comparison, not char-by-char.

src/pip/_internal/utils/unpacking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def is_within_directory(directory: str, target: str) -> bool:
8383
abs_directory = os.path.abspath(directory)
8484
abs_target = os.path.abspath(target)
8585

86-
prefix = os.path.commonprefix([abs_directory, abs_target])
86+
prefix = os.path.commonpath([abs_directory, abs_target])
8787
return prefix == abs_directory
8888

8989

tests/unit/test_utils_unpacking.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ def test_unpack_tar_unicode(tmpdir: Path) -> None:
412412
(("parent/", "parent/sub"), True),
413413
# Test target outside parent
414414
(("parent/", "parent/../sub"), False),
415+
# Test target sub-string of parent
416+
(("parent/child", "parent/childfoo"), False),
415417
],
416418
)
417419
def test_is_within_directory(args: tuple[str, str], expected: bool) -> None:

0 commit comments

Comments
 (0)