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

Code before:
    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 = (
                    "The tar file ({}) has a file ({}) trying to install "
                    "outside target directory ({})"
                )
                raise InstallationError(message.format(filename, member.name, exc))

Review comment (suggestion):
I do not like this exception control flow where we are catching KeyError's and then raising different KeyError's.

I suggest this function return `None` or a linkname as `str`, where `None` represents no invalid link targets and the linkname represents the first invalid linkname.

Code after:
    """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)
            except Exception as exc:
                # Some corrupt tar files seem to produce this
                # (specifically bad symlinks)
                logger.warning(