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.7 KB
Raw Blame
# allow: no-sut-import — a static census over the production SOURCE of
# src/local_deep_research (grep-style contract); the SUT it guards is the
# absence of any URL-reaching unstructured call site.
"""Tripwire for the unstructured URL-partitioning SSRF (GHSA-4mvj-m6j5-pmf7).

unstructured 0.18.32 — the newest release that installs on Python 3.14 — is
affected by GHSA-4mvj-m6j5-pmf7 / CVE-2026-71428 (CRITICAL): the ``url=``
argument of ``partition()``, ``partition_html()`` and ``partition_md()`` is
fetched with ``requests.get()`` and no host validation, giving a full-read
SSRF (loopback admin APIs, internal services, cloud metadata endpoints).

Every release carrying the fix (0.24.0+, up to and including 0.27.6) declares
``Requires-Python: <3.14``, and this project's ``requires-python`` is
``>=3.12,<3.15`` with a Python 3.14.8 production image — so no fixed version
can be locked until upstream ships 3.14 support (tracked in the
``.trivyignore`` / ``.grype.yaml`` suppression entries).

This file pins the compensating control instead: the vulnerable sinks are only
reachable when a caller hands unstructured a *URL*. This codebase never does —

* ``es_utils.index_file`` calls ``partition(filename=file_path)`` on a
  PathValidator-vetted local path, and
* every other touchpoint goes through the langchain-community
  ``Unstructured*Loader`` family, which passes ``file_path`` (never ``url``;
  ``UnstructuredURLLoader`` is the URL-taking variant and is not used).

The grep-style contract below fails the build if any of that changes: a new
call site that feeds a URL into unstructured would re-open the SSRF while the
scanner alerts are suppressed.

Remove this tripwire (and the suppression entries) together when the lock can
move to unstructured >= 0.24.0.
"""

from __future__ import annotations

import re
import tomllib
from pathlib import Path

import pytest
from packaging.version import Version

REPO_ROOT = Path(__file__).resolve().parents[2]
SRC = REPO_ROOT / "src" / "local_deep_research"

# Importing an URL-taking unstructured entry point at all counts as a
# violation: the SSRF lives inside partition_html/partition_md regardless of
# which keyword they are called with.
FORBIDDEN_IMPORTS = (
    "unstructured.partition.html",
    "unstructured.partition.md",
    "unstructured.partition.api",
)

# partition() itself is used legitimately (filename=), so the *call* is only
# forbidden when a url= keyword is passed to it — in source form that is
# ``url=`` appearing inside a partition(...) call. A call-scoped regex keeps
# false positives (e.g. ``url=`` kwargs on unrelated functions such as
# requests.get) out.
_PARTITION_URL_KWARG = re.compile(
    r"\bpartition\s*\((?:[^()]|\([^()]*\))*?\burl\s*=", re.DOTALL
)

# The langchain loader that feeds URLs to unstructured.partition.
FORBIDDEN_LOADERS = ("UnstructuredURLLoader",)

# Files that are allowed to mention the forbidden identifiers in prose
# (docstrings/comments explaining this exact suppression).
EXEMPT_FILES = (
    # this test file itself
    Path(__file__).name,
)


def _violations(text: str, rel: str) -> list[str]:
    offenders = []
    for needle in FORBIDDEN_IMPORTS:
        if needle in text:
            offenders.append(
                f"{rel}: imports {needle!r} (URL-based partition entry point)"
            )
    for needle in FORBIDDEN_LOADERS:
        if needle in text:
            offenders.append(
                f"{rel}: references {needle!r} (feeds URLs to unstructured)"
            )
    if _PARTITION_URL_KWARG.search(text):
        offenders.append(
            f"{rel}: calls partition(... url=...) — full-read SSRF in unstructured "
            "<0.24 (GHSA-4mvj-m6j5-pmf7); pass filename= of a validated local path"
        )
    return offenders


def test_no_url_reaches_the_unstructured_partitioner() -> None:
    """Grep-style contract: the SSRF sinks must stay unreachable in src/."""
    modules = sorted(SRC.rglob("*.py"))
    assert len(modules) > 100, "source tree not found"

    offenders: list[str] = []
    for path in modules:
        if path.name in EXEMPT_FILES:
            continue
        offenders += _violations(
            path.read_text(encoding="utf-8"), str(path.relative_to(SRC))
        )

    assert offenders == [], (
        "A code path hands a URL to unstructured while the GHSA-4mvj-m6j5-pmf7 "
        "full-read SSRF is only suppressed (no fixed release supports Python "
        "3.14). Partition a PathValidator-vetted local file instead, or "
        "re-evaluate the suppression:\n" + "\n".join(offenders)
    )


def test_locked_unstructured_still_needs_suppression() -> None:
    """Remove the exceptions when the lock actually contains a fixed release.

    The compatible-release requirement ``~=0.18`` already permits 0.24.0.
    Read every locked variant; this offline check cannot discover new releases
    or establish their Python compatibility.
    """
    lock = tomllib.loads((REPO_ROOT / "pdm.lock").read_text(encoding="utf-8"))
    versions = [
        Version(package["version"])
        for package in lock.get("package", [])
        if package.get("name") == "unstructured"
    ]
    assert versions, (
        "unstructured missing from pdm.lock; remove its suppressions"
    )
    assert all(version < Version("0.24.0") for version in versions), (
        f"pdm.lock contains fixed unstructured {versions}: remove the "
        "GHSA-4mvj-m6j5-pmf7 .trivyignore/.grype.yaml/osv-scanner.toml "
        "suppressions and this temporary tripwire. From 0.24.1 also remove "
        "the GHSA-v5mq-3xhg-98m9 / CVE-2026-104250 osv-scanner.toml, "
        ".trivyignore and .grype.yaml ignores, its three entries "
        "in .github/release-security-exceptions.json (9736 under "
        "CVE-2026-104250, 9737 and 9738 under "
        "GHSA-v5mq-3xhg-98m9-unstructured), the 'Accepted findings also "
        "suppressed in the scanner' subsection in .github/SECURITY_ALERTS.md, "
        "the TestUnstructuredScannerSuppressions tests in "
        "tests/deployment/test_deployment_surface_contracts.py and "
        "test_unstructured_html_output_tripwire.py"
    )


@pytest.mark.parametrize(
    "versions", [("0.24.0",), ("0.27.6",), ("0.18.32", "0.24.0"), ()]
)
def test_removal_guard_rejects_a_fixed_or_removed_lock(
    tmp_path, monkeypatch, versions
):
    # The requirement is unchanged while the resolved dependency changes.
    (tmp_path / "pyproject.toml").write_text(
        '[project]\ndependencies = ["unstructured~=0.18"]\n'
    )
    (tmp_path / "pdm.lock").write_text(
        "".join(
            f'[[package]]\nname = "unstructured"\nversion = "{version}"\n'
            for version in versions
        )
    )
    monkeypatch.setitem(globals(), "REPO_ROOT", tmp_path)
    with pytest.raises(AssertionError, match="remove"):
        test_locked_unstructured_still_needs_suppression()