Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.2 KB
Raw Blame
"""Run real pip transport regressions during both Docker root-stage builds.

Run in a fresh interpreter after installing the rebuilt pip, then again in a
new ensurepip-created virtual environment. This is a standalone build probe,
so the production root stage does not need pytest or application dependencies.
The negative TLS setting below is input to a mocked connection; no network
connection is made by these regression checks.
"""

import io
import json
import sys
from pathlib import Path


def verify_chunk_lines() -> None:
    """Both chunk-size and trailer reads must reject oversized lines."""
    import http.client

    from pip._vendor.urllib3.exceptions import ProtocolError
    from pip._vendor.urllib3.response import HTTPResponse

    class Socket:
        def __init__(self, data: bytes):
            self.data = data

        def makefile(self, *args, **kwargs):
            return io.BytesIO(self.data)

    def read(body: bytes) -> bytes:
        raw = http.client.HTTPResponse(
            Socket(
                b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" + body
            ),
            method="GET",
        )
        raw.begin()
        response = HTTPResponse(
            body=raw,
            headers=dict(raw.headers.items()),
            original_response=raw,
            preload_content=False,
            request_method="GET",
        )
        try:
            return b"".join(response.read_chunked())
        finally:
            response.close()

    if read(b"1\r\na\r\n0\r\n\r\n") != b"a":
        raise RuntimeError("ordinary chunked decoding failed")
    for body, message in (
        (b"1" * 65537 + b"\r\n", "chunk size line exceeded"),
        (
            b"1\r\na\r\n0\r\n" + b"x" * 65537 + b"\r\n",
            "chunk trailer line exceeded",
        ),
    ):
        try:
            read(body)
        except ProtocolError as error:
            if message not in str(error):
                raise RuntimeError("unexpected chunk rejection") from error
        else:
            raise RuntimeError("oversized chunk line was accepted")


def verify_deflate_eof() -> None:
    """A raw Deflate stream with trailing input must finish bounded reads."""
    import zlib

    from pip._vendor.urllib3.response import DeflateDecoder

    payload = b"hello" * 10
    decoder = DeflateDecoder()
    output = decoder.decompress(
        zlib.compress(payload)[2:-4] + b"trailer", max_length=8
    )
    for _ in range(32):
        if not decoder.has_unconsumed_tail:
            break
        output += decoder.decompress(b"", max_length=8)
    if decoder.has_unconsumed_tail or output != payload:
        raise RuntimeError(
            "Deflate failed to finish at EOF or changed decoded content"
        )


def verify_proxy_policy() -> None:
    """An origin's weaker TLS settings must not override proxy verification."""
    import ssl
    from collections import namedtuple
    from unittest.mock import Mock, patch

    from pip._vendor.urllib3 import connection
    from pip._vendor.urllib3._base_connection import ProxyConfig

    context = ssl.create_default_context()
    config = ProxyConfig(context, False, None, None)
    conn = connection.HTTPSConnection(
        "origin.invalid",
        cert_reqs=ssl.CERT_NONE,
        ca_certs="/origin-only",
        proxy_config=config,
    )
    wrapped = namedtuple("Wrapped", "socket is_verified")(Mock(), True)
    with patch.object(
        connection, "_ssl_wrap_socket_and_match_hostname", return_value=wrapped
    ) as wrap:
        conn._connect_tls_proxy("proxy.invalid", Mock())
    policy = wrap.call_args.kwargs
    if not (
        policy["cert_reqs"] == ssl.CERT_REQUIRED
        and policy["ssl_context"] is context
        and policy["ca_certs"] is None
    ):
        raise RuntimeError("origin TLS configuration overrode the proxy policy")


def verify_msgpack() -> None:
    """Exercise the pure-Python serializer used by pip's cache transport."""
    from pip._vendor import msgpack

    if msgpack.Packer.__module__ != "pip._vendor.msgpack.fallback":
        raise RuntimeError("pip did not retain pure-Python MessagePack")
    value = {"body": b"cached content", "metadata": [1, True, None]}
    packed = msgpack.packb(value, use_bin_type=True)
    if msgpack.unpackb(packed, raw=False) != value:
        raise RuntimeError("vendored MessagePack round-trip changed")
    unpacker = msgpack.Unpacker(raw=False)
    unpacker.feed(packed)
    if list(unpacker) != [value]:
        raise RuntimeError("vendored MessagePack streaming round-trip changed")


def verify_runtime(expected_version: str, expected_msgpack: str) -> None:
    import pip
    from pip._vendor import msgpack, urllib3
    from pip._vendor.urllib3.util.url import parse_url

    if urllib3.__version__ != expected_version:
        raise RuntimeError("installed pip did not load the refreshed urllib3")
    if msgpack.__version__ != expected_msgpack:
        raise RuntimeError("installed pip did not load the refreshed msgpack")
    if parse_url("https://bücher.example").host != "xn--bcher-kva.example":
        raise RuntimeError("vendored IDNA import failed")
    sbom = json.loads(
        (Path(pip.__file__).parent / "_vendor/bom.cdx.json").read_text()
    )
    manifest = [
        line.strip()
        for line in (Path(pip.__file__).parent / "_vendor/vendor.txt")
        .read_text(encoding="utf-8")
        .splitlines()
    ]
    for name, version in (
        ("urllib3", urllib3.__version__),
        ("msgpack", msgpack.__version__),
    ):
        components = [
            item for item in sbom["components"] if item["name"] == name
        ]
        reference = f"pkg:pypi/{name}@{version}"
        if (
            len(components) != 1
            or components[0]["version"] != version
            or components[0]["purl"] != reference
            or components[0]["bom-ref"] != reference
            or manifest.count(f"{name}=={version}") != 1
        ):
            raise RuntimeError(
                f"installed pip's inventory disagrees with its {name} code"
            )
    verify_chunk_lines()
    verify_deflate_eof()
    verify_proxy_policy()
    verify_msgpack()
    print(
        "pip's bundled code, inventory, transport and serialization checks passed"
    )


if __name__ == "__main__":
    verify_runtime(sys.argv[1], sys.argv[2])