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.

9.3 KB
Raw Blame
"""Revert-catching tests for the patched-v2 dependency floors.

Why these exist: the security floors in ``pyproject.toml`` ship in the
published wheel metadata, but nothing downstream re-asserts them — pip only
checks resolvability (2.32.x still resolves), and OSV scans ``pdm.lock``,
which already held patched versions before the floors existed via dev
overrides. Lowering or deleting a floor is therefore a silently revertible
change. Each test fails loudly when its floor moves, and names the
advisories the floor remediates so a future re-baseline must justify itself
here rather than in a diff nobody reads.
"""

# allow: no-sut-import — asserts on repository metadata (pyproject.toml),
# not on the package under test.

from __future__ import annotations

import tomllib
from pathlib import Path

import pytest
from packaging.requirements import Requirement
from packaging.version import Version

REPO_ROOT = Path(__file__).resolve().parents[2]
PYPROJECT = REPO_ROOT / "pyproject.toml"


def _requirements(section: str, key: str) -> dict[str, Requirement]:
    with PYPROJECT.open("rb") as fh:
        data = tomllib.load(fh)
    raw = data[section][key]
    parsed = {Requirement(r).name: Requirement(r) for r in raw}
    assert parsed, f"{section}.{key} in {PYPROJECT} is empty — floors vanished"
    return parsed


def _project_dependencies() -> dict[str, Requirement]:
    return _requirements("project", "dependencies")


def _dev_group() -> dict[str, Requirement]:
    return _requirements("dependency-groups", "dev")


def _assert_floor(req: Requirement, *, floor_excluded: str, floor: str) -> None:
    """Require an unconditional floor at ``floor``.

    The specifier must admit ``floor`` and no version below it, which
    includes ``floor_excluded`` and every prerelease of ``floor``.
    """
    # An environment marker makes the floor conditional: pip skips this
    # requirement where the marker is false (for example on Python 3.13+
    # under ``python_version < '3.13'``), while every check below would
    # still pass.
    assert req.marker is None, (
        f"{req.name} floor applies only where {req.marker} holds: a "
        f"security floor must be unconditional (requirement: {req})"
    )
    spec = req.specifier
    # The highest lower bound must reach the floor. A probe version cannot
    # catch every partial downgrade: datasets>=5.0.1rc1 rejects 5.0.1.dev0
    # yet admits 5.0.1rc1, which is below the patched 5.0.1.
    lower_bounds = [
        Version(clause.version)
        for clause in spec
        if clause.operator in {">=", ">", "~=", "=="}
        and not clause.version.endswith(".*")
    ]
    assert lower_bounds and max(lower_bounds) >= Version(floor), (
        f"{req.name} admits versions below {floor}: the patched floor was "
        f"lowered (current specifier: {spec})"
    )
    assert not spec.contains(floor_excluded, prereleases=True), (
        f"{req.name} accepts {floor_excluded}: the patched floor was lowered "
        f"(current specifier: {spec})"
    )
    # A historical bad version alone misses a partial downgrade, such as
    # datasets>=5.0.0 when the patched floor is 5.0.1. Explicitly considering
    # prereleases tests the lower boundary even if no adjacent release exists.
    assert not spec.contains(f"{floor}.dev0", prereleases=True), (
        f"{req.name} accepts a version below {floor}: the patched floor was "
        f"lowered (current specifier: {spec})"
    )
    assert spec.contains(floor, prereleases=True), (
        f"{req.name} rejects {floor}: the patched floor moved above it "
        f"(current specifier: {spec})"
    )


def _assert_window(
    req: Requirement, *, floor_excluded: str, floor: str, cap: str
) -> None:
    """Accept the patched floor while preserving the compatibility cap."""
    _assert_floor(req, floor_excluded=floor_excluded, floor=floor)
    spec = req.specifier
    assert not spec.contains(cap, prereleases=True), (
        f"{req.name} accepts {cap}: the compat cap was dropped "
        f"(current specifier: {spec})"
    )


def test_requests_floor_is_patched_in_published_metadata():
    """requests>=2.33,<3 — CVE-2026-25645 (2.32.x ships the vulnerable path).

    Published metadata must enforce the floor; the pre-floor policy relied on
    a dev-only PDM override, so release wheels could resolve 2.32.x.
    """
    req = _project_dependencies()["requests"]
    _assert_window(req, floor_excluded="2.32.5", floor="2.33.0", cap="3.0.0")


def test_arxiv_floor_requires_the_4x_api_port():
    """arxiv>=4.0.1,<5 — the 2.4 line caps requests~=2.32.0 (CVE-2026-25645
    exposure) and uses the removed feedparser/download_pdf stack this PR
    ports away from.
    """
    req = _project_dependencies()["arxiv"]
    _assert_window(req, floor_excluded="2.4.1", floor="4.0.1", cap="5.0.0")


def test_pypdf_floor_covers_2026_advisory_batch():
    """Require the reviewed 6.21.4 patch while retaining the 6.x cap."""
    req = _project_dependencies()["pypdf"]
    _assert_window(req, floor_excluded="6.21.3", floor="6.21.4", cap="7.0.0")


def test_urllib3_floor_covers_2026_advisory_batch():
    """urllib3~=2.8 — CVE-2026-44431/CVE-2026-44432; 2.8.0 also fixes
    GHSA-8988-9cw3-xx77, GHSA-gh4c-6fx4-qh6g, GHSA-vxq7-64xx-v4gw.
    """
    req = _project_dependencies()["urllib3"]
    _assert_window(req, floor_excluded="2.7.0", floor="2.8.0", cap="3.0.0")


def test_brotli_minimum_is_preserved():
    req = _project_dependencies()["brotli"]
    _assert_floor(req, floor_excluded="1.2.0", floor="1.2.1")


def test_filelock_minimum_is_preserved():
    req = _project_dependencies()["filelock"]
    _assert_floor(req, floor_excluded="4.2.0", floor="4.2.1")


def test_patchright_minimum_is_preserved():
    req = _project_dependencies()["patchright"]
    _assert_window(req, floor_excluded="1.63.0", floor="1.64.3", cap="2.0.0")


def test_playwright_minimum_is_preserved():
    req = _project_dependencies()["playwright"]
    _assert_window(req, floor_excluded="1.64.0", floor="1.64.1", cap="2.0.0")


def test_setuptools_minimum_is_preserved():
    req = _project_dependencies()["setuptools"]
    _assert_floor(req, floor_excluded="84.0.0", floor="84.1.0")


def test_brotlicffi_minimum_preserves_its_interpreter_scope():
    req = _project_dependencies()["brotlicffi"]
    assert str(req.marker) == 'platform_python_implementation != "CPython"'
    _assert_floor(
        Requirement(f"{req.name}{req.specifier}"),
        floor_excluded="1.2.0.2",
        floor="1.2.1.0",
    )


def test_fsspec_floor_covers_reference_template_cve():
    """fsspec>=2026.6.0 — CVE-2026-104851 (reference-filesystem template
    handling); 2026.2.0 must not resolve.
    """
    req = _project_dependencies()["fsspec"]
    _assert_floor(req, floor_excluded="2026.2.0", floor="2026.6.0")


def test_datasets_floor_caps_folder_metadata_traversal():
    """datasets>=5.0.1,<6 — CVE-2026-66007 folder-metadata path containment."""
    req = _project_dependencies()["datasets"]
    _assert_window(req, floor_excluded="4.8.5", floor="5.0.1", cap="6.0.0")


def test_dev_virtualenv_minimum_is_preserved():
    req = _dev_group()["virtualenv"]
    _assert_window(
        req, floor_excluded="21.14.12", floor="21.14.13", cap="22.0.0"
    )


def test_pillow_floor_covers_2026_decoder_advisories():
    """Pillow>=13.0.0 — CVE-2026-25990 (PSD OOB write, fixed 12.1.1), then
    CVE-2026-112390 … CVE-2026-112406 and CVE-2026-112891 … CVE-2026-112902
    (GHSA-8g5g-8vmr-9782 heap overflow et al., fixed only in 13.0.0).
    12.3.0 must not resolve.
    """
    req = _project_dependencies()["Pillow"]
    _assert_floor(req, floor_excluded="12.3.0", floor="13.0.0")


def test_certifi_floor_covers_emsign_root_advisory():
    """certifi>=2026.10.19 — PYSEC-2026-4320 / GHSA-6q48-6c7w-9gw2
    (critical: emSign root CAs with compromised intermediate CA keys);
    2026.7.22 must not resolve.
    """
    req = _project_dependencies()["certifi"]
    _assert_floor(req, floor_excluded="2026.7.22", floor="2026.10.19")


def test_nltk_floor_covers_ghsa_8mgp():
    """nltk>=3.10.4 — GHSA-8mgp-746c-j5xp. nltk is transitive via
    unstructured, so without this floor a non-lock install could resolve
    3.10.3; the OSV exception was removed on the strength of it.
    """
    req = _project_dependencies()["nltk"]
    _assert_floor(req, floor_excluded="3.10.3", floor="3.10.4")


@pytest.mark.parametrize(
    "name",
    [
        "requests",
        "arxiv",
        "pypdf",
        "urllib3",
        "fsspec",
        "datasets",
        "Pillow",
        "certifi",
        "nltk",
    ],
)
def test_floored_packages_are_declared_dependencies(name: str) -> None:
    """Keep the patched packages in the published dependency set."""
    assert name in _project_dependencies(), (
        f"{name} no longer declared in [project.dependencies] — floor removed"
    )


@pytest.mark.parametrize(
    "line",
    [
        "datasets>=5.0.1,<6; python_version < '3.13'",
        "datasets>=5.0.1rc1,<6",
    ],
    ids=["environment-marker", "prerelease-floor"],
)
def test_floor_guard_rejects_weakened_floors(line: str) -> None:
    """The guard itself rejects a conditional floor and a floor lowered to
    a prerelease of the patched release."""
    with pytest.raises(AssertionError):
        _assert_window(
            Requirement(line),
            floor_excluded="4.8.5",
            floor="5.0.1",
            cap="6.0.0",
        )