Repository: pypa/pip
File: src/pip/_internal/utils/unpacking.py (Python)

Code before:
        tar.close()


def _untar_without_filter(
    filename: str,
    location: str,
    tar: tarfile.TarFile,
    leading: bool,
) -> None:
    """Fallback for Python without tarfile.data_filter"""

    def _check_link_target(tar: tarfile.TarFile, tarinfo: tarfile.TarInfo) -> None:
        linkname = "/".join(
            filter(None, (os.path.dirname(tarinfo.name), tarinfo.linkname))
        )

        linkname = os.path.normpath(linkname)

        try:
            tar.getmember(linkname)
        except KeyError:
            if "\\" in linkname or "/" in linkname:
                if "\\" in linkname:
                    linkname = linkname.replace("\\", "/")
                else:
                    linkname = linkname.replace("/", "\\")
                try:
                    tar.getmember(linkname)
                except KeyError:
                    raise KeyError(linkname)
            else:
                raise KeyError(linkname)

    for member in tar.getmembers():
        fn = member.name
        if leading:
            fn = split_leading_dir(fn)[1]
        path = os.path.join(location, fn)
        if not is_within_directory(location, path):
            message = (
                "The tar file ({}) has a file ({}) trying to install "
                "outside target directory ({})"
            )
            raise InstallationError(message.format(filename, path, location))
        if member.isdir():
            ensure_dir(path)
        elif member.issym():
            try:
                _check_link_target(tar, member)
            except KeyError as exc:
                message = (

Review comment (suggestion):
You need to add an explanation of what you are doing here and why, I don't understand why these replaces exist or what they are being used to test.

Code after:
        tar.close()


def is_symlink_target_in_tar(tar: tarfile.TarFile, tarinfo: tarfile.TarInfo) -> bool:
    """Check if the file pointed to by the symbolic link is in the tar archive"""
    linkname = os.path.join(os.path.dirname(tarinfo.name), tarinfo.linkname)

    linkname = os.path.normpath(linkname)
    linkname = linkname.replace("\\", "/")

    try:
        tar.getmember(linkname)
        return True
    except KeyError:
        return False


def _untar_without_filter(
    filename: str,
    location: str,
    tar: tarfile.TarFile,
    leading: bool,
) -> None:
    """Fallback for Python without tarfile.data_filter"""
    # NOTE: This function can be removed once pip requires CPython ≥ 3.12.​
    # PEP 706 added tarfile.data_filter, made tarfile extraction operations more secure.
    # This feature is fully supported from CPython 3.12 onward.
    for member in tar.getmembers():
        fn = member.name
        if leading:
            fn = split_leading_dir(fn)[1]
        path = os.path.join(location, fn)
        if not is_within_directory(location, path):
            message = (
                "The tar file ({}) has a file ({}) trying to install "
                "outside target directory ({})"
            )
            raise InstallationError(message.format(filename, path, location))
        if member.isdir():
            ensure_dir(path)
        elif member.issym():
            if not is_symlink_target_in_tar(tar, member):
                message = (
                    "The tar file ({}) has a file ({}) trying to install "
                    "outside target directory ({})"
                )
                raise InstallationError(
                    message.format(filename, member.name, member.linkname)
                )
            try:
                tar._extract_member(member, path)