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
"""The Docker image ships the NLTK data the bounded upload parser needs.

The parser child sets ``AUTO_DOWNLOAD_NLTK=false`` (isolated_loader), so
unstructured's Word, PowerPoint, ``.xlsx`` and HTML text classification
finds ``punkt_tab`` and ``averaged_perceptron_tagger_eng`` installed or the
upload fails. These static checks pin how the image gets that data without
building it: the ``nltk-data`` stage runs scripts/install_nltk_data.py, and
the ``ldr`` stage copies the result to a directory on NLTK's default search
path, readable by ``ldruser``. The script itself is exercised offline with
archives built here.
"""

from __future__ import annotations

import hashlib
import importlib.util
import io
import re
import stat
import zipfile
from pathlib import Path

import pytest

from local_deep_research.security import isolated_loader

REPO_ROOT = Path(__file__).resolve().parents[2]
DOCKERFILE = REPO_ROOT / "Dockerfile"
SCRIPT = REPO_ROOT / "scripts" / "install_nltk_data.py"
# On NLTK's default nltk.data.path on every non-Windows host, for every user.
IMAGE_NLTK_DATA = "/usr/local/share/nltk_data"


def _load_script():
    spec = importlib.util.spec_from_file_location("install_nltk_data", SCRIPT)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def _logical_lines(text: str) -> list[str]:
    # Docker drops comment lines, also inside a continued instruction, and
    # joins lines that end in a backslash.
    lines, current = [], ""
    for raw in text.splitlines():
        stripped = raw.strip()
        if stripped.startswith("#") or (not stripped and current):
            continue
        if stripped.endswith("\\"):
            current += stripped[:-1] + " "
            continue
        current += stripped
        if current.strip():
            lines.append(current.strip())
        current = ""
    return lines


def _stages() -> list[tuple[str, list[tuple[str, str]]]]:
    stages: list[tuple[str, list[tuple[str, str]]]] = []
    for line in _logical_lines(DOCKERFILE.read_text()):
        keyword, _, argument = line.partition(" ")
        keyword = keyword.upper()
        if keyword == "FROM":
            words = argument.split()
            name = words[2] if len(words) == 3 else f"#{len(stages)}"
            stages.append((name.lower(), []))
        elif stages:
            stages[-1][1].append((keyword, argument.strip()))
    return stages


def _stage(name: str) -> list[tuple[str, str]]:
    found = [body for stage, body in _stages() if stage == name]
    assert len(found) == 1, f"expected one {name!r} stage"
    return found[0]


def test_script_installs_exactly_the_packages_the_parser_needs():
    script = _load_script()
    assert (
        tuple(package.name for package in script.PACKAGES)
        == isolated_loader.NLTK_DATA_PACKAGES
    )
    # unstructured looks each one up in a fixed category. Read its source
    # rather than import it: importing it downloads missing data.
    spec = importlib.util.find_spec("unstructured.nlp.tokenize")
    if spec is None or spec.origin is None:
        pytest.skip("unstructured is not installed")
    source = " ".join(Path(spec.origin).read_text().split())
    for package in script.PACKAGES:
        assert (
            f'package_category="{package.subdir}", '
            f'package_name="{package.name}"' in source
        ) or (
            f'package_name="{package.name}", '
            f'package_category="{package.subdir}"' in source
        ), f"unstructured no longer looks up {package.subdir}/{package.name}"


def test_script_pins_a_commit_and_every_archive_hash():
    script = _load_script()
    assert re.fullmatch(r"[0-9a-f]{40}", script.NLTK_DATA_COMMIT)
    for package in script.PACKAGES:
        assert re.fullmatch(r"[0-9a-f]{64}", package.sha256)
        assert package.size > 0
        url = script.package_url(package)
        assert url.startswith("https://raw.githubusercontent.com/nltk/")
        assert f"/{script.NLTK_DATA_COMMIT}/" in url
        assert "gh-pages" not in url


def test_dockerfile_builds_the_data_and_ships_it_to_the_ldr_stage():
    stages = [name for name, _ in _stages()]
    # ldr is what `docker build` builds without --target, and it can only
    # copy from a stage defined before it.
    assert stages[-1] == "ldr"
    assert stages.index("nltk-data") < stages.index("ldr")

    builder = _stage("nltk-data")
    copies = [arg for keyword, arg in builder if keyword == "COPY"]
    assert copies == ["scripts/install_nltk_data.py /tmp/install_nltk_data.py"]
    runs = [arg for keyword, arg in builder if keyword == "RUN"]
    assert any(
        re.match(r"python3 -I /tmp/install_nltk_data\.py /nltk_data(\s|$)", run)
        for run in runs
    ), runs

    ldr = _stage("ldr")
    shipped = [
        arg
        for keyword, arg in ldr
        if keyword == "COPY" and "--from=nltk-data" in arg.split()
    ]
    # No --chown: the data stays root-owned and read-only to ldruser.
    assert shipped == [f"--from=nltk-data /nltk_data/ {IMAGE_NLTK_DATA}/"]
    # NLTK_DATA would add a path; the default one must stay in effect.
    assert not any(
        keyword == "ENV" and "NLTK_DATA" in arg for keyword, arg in ldr
    )


def test_image_directory_is_on_nltks_default_search_path():
    # nltk/data.py lists the default directories; read rather than import.
    spec = importlib.util.find_spec("nltk")
    if spec is None or not spec.submodule_search_locations:
        pytest.skip("nltk is not installed")
    data_py = Path(next(iter(spec.submodule_search_locations))) / "data.py"
    assert f'"{IMAGE_NLTK_DATA}"' in data_py.read_text()


def test_production_images_run_the_parser_check_offline_as_the_service_user():
    import yaml

    workflow = yaml.safe_load(
        (REPO_ROOT / ".github/workflows/docker-tests.yml").read_text()
    )
    job = workflow["jobs"]["ldr-production-smoke-test"]
    checks = [
        step
        for step in job["steps"]
        if step.get("name")
        == "Verify upload parser and NLTK data offline as service user"
    ]
    assert len(checks) == 1
    assert "if" not in checks[0]
    command = checks[0]["run"]
    for setting in (
        "timeout 120s docker run",
        "--network none",
        "--user ldruser",
        "AUTO_DOWNLOAD_NLTK=false",
        "verify_upload_parser.py",
        "--entrypoint /install/.venv/bin/python",
    ):
        assert setting in command
    assert {item["arch"] for item in job["strategy"]["matrix"]["include"]} == {
        "amd64",
        "arm64",
    }


def _zip(members: dict[str, bytes | None], symlink: str | None = None) -> bytes:
    buffer = io.BytesIO()
    with zipfile.ZipFile(buffer, "w") as bundle:
        for name, data in members.items():
            if data is None:
                bundle.writestr(name, b"")
            else:
                bundle.writestr(name, data)
        if symlink:
            info = zipfile.ZipInfo(symlink)
            info.external_attr = (stat.S_IFLNK | 0o777) << 16
            bundle.writestr(info, "/etc/passwd")
    return buffer.getvalue()


@pytest.mark.parametrize(
    ("members", "symlink"),
    [
        ({"punkt_tab/../../escaped": b"x"}, None),
        ({"/tmp/absolute": b"x"}, None),
        ({"other/file": b"x"}, None),
        ({"punkt_tab\\..\\escaped": b"x"}, None),
        ({"punkt_tab/ok": b"x"}, "punkt_tab/link"),
    ],
    ids=["dotdot", "absolute", "other-package", "backslash", "symlink"],
)
def test_script_refuses_unsafe_archive_members(tmp_path, members, symlink):
    script = _load_script()
    package = script.Package("tokenizers", "punkt_tab", 0, "")
    with pytest.raises(ValueError, match="unsafe archive member"):
        script.extract(package, _zip(members, symlink), tmp_path)
    assert list(tmp_path.iterdir()) == []


def test_script_refuses_an_archive_whose_hash_or_size_differs():
    script = _load_script()
    package = script.PACKAGES[0]
    with pytest.raises(ValueError, match="SHA-256"):
        script.verify(package, b"\0" * package.size)
    with pytest.raises(ValueError, match="bytes"):
        script.verify(package, b"\0" * (package.size + 1))


def test_script_installs_verified_archives_where_nltk_looks(tmp_path):
    script = _load_script()
    archives = {
        "punkt_tab": _zip(
            {"punkt_tab/": None, "punkt_tab/english/sent_starters.txt": b"x"}
        ),
        "averaged_perceptron_tagger_eng": _zip(
            {"averaged_perceptron_tagger_eng/weights.json": b"{}"}
        ),
    }
    script.PACKAGES = tuple(
        package._replace(
            size=len(archives[package.name]),
            sha256=hashlib.sha256(archives[package.name]).hexdigest(),
        )
        for package in script.PACKAGES
    )
    requested = []

    def fetcher(url, limit):
        requested.append(url)
        return archives[url.rsplit("/", 1)[1].removesuffix(".zip")]

    script.install(tmp_path, fetcher)
    assert requested == [script.package_url(p) for p in script.PACKAGES]
    data = tmp_path / "tokenizers" / "punkt_tab" / "english"
    tagger = tmp_path / "taggers" / "averaged_perceptron_tagger_eng"
    assert (data / "sent_starters.txt").read_bytes() == b"x"
    assert (tagger / "weights.json").read_bytes() == b"{}"
    # Readable and traversable by every user, writable only by the owner.
    assert stat.S_IMODE((data / "sent_starters.txt").stat().st_mode) == 0o644
    assert stat.S_IMODE(data.stat().st_mode) == 0o755
    assert stat.S_IMODE(tmp_path.stat().st_mode) == 0o755