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.

11.3 KB
Raw Blame
# allow: no-sut-import -- guardian for repository-level shell gates
"""Regression tests for content enforcement behind the pathname whitelist.

The six binary assets allowlisted by .github/scripts/file-whitelist-check.sh
used to be validated by parsing PNG chunk / MP3 frame structure and rejecting
anything that didn't parse "cleanly". That approach was abandoned: a
container-format allowlist restricts chunk/frame *types*, not chunk/frame
*length or content*. PNG's IDAT chunk type is both unavoidable (every real
PNG needs one) and unrestricted in what it may contain, so a complete ZIP
fits inside a well-formed, correctly-CRC'd IDAT chunk placed before a
legitimate IEND -- the old structural check accepted that file outright.
MP3 has the equivalent hole: an ID3v2 header's declared tag size is trusted
and skipped without validation, so a payload can be sized into the tag
region ahead of genuine trailing audio frames. These are six exact, known,
rarely-changing files, so the fix pins their content by SHA-256 digest
instead: any byte difference at all is rejected, which is what the tests
below exercise directly.
"""

from __future__ import annotations

import struct
import subprocess
import zipfile
import zlib
from io import BytesIO
from pathlib import Path


REPO_ROOT = Path(__file__).resolve().parents[2]
CI_CHECKER = REPO_ROOT / ".github" / "scripts" / "file-whitelist-check.sh"
PRECOMMIT_CHECKER = REPO_ROOT / ".pre-commit-hooks" / "file-whitelist-check.sh"
KNOWN_BINARY_ASSETS = (
    "docs/images/Local Search.png",
    "docs/images/local_search_embedding_model_type.png",
    "docs/images/local_search_paths.png",
    "src/local_deep_research/web/static/favicon.png",
    "src/local_deep_research/web/static/sounds/error.mp3",
    "src/local_deep_research/web/static/sounds/success.mp3",
)


def _content_check(*paths: Path | str) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        ["bash", str(CI_CHECKER), "--content-only", *map(str, paths)],
        cwd=REPO_ROOT,
        check=False,
        capture_output=True,
        text=True,
        timeout=30,
    )


def test_utf8_text_and_the_route_snapshot_pass_content_validation(tmp_path):
    text_file = tmp_path / "ordinary.py"
    text_file.write_text(
        "message = 'reviewable UTF-8: café'\n", encoding="utf-8"
    )

    result = _content_check(
        text_file,
        "tests/web/flask_route_table_snapshot.json",
    )

    assert result.returncode == 0, result.stdout + result.stderr
    assert result.stdout == ""


def test_the_six_exact_binary_assets_match_their_pinned_digests():
    result = _content_check(*KNOWN_BINARY_ASSETS)

    assert result.returncode == 0, result.stdout + result.stderr
    assert result.stdout == ""


def test_binary_magic_cannot_hide_behind_an_allowed_source_suffix(tmp_path):
    disguised = tmp_path / "looks_reviewable.py"
    disguised.write_bytes(b"\x89PNG\r\n\x1a\nnot-really-python")

    result = _content_check(disguised)

    assert result.returncode == 1
    assert str(disguised) in result.stdout
    assert "PNG image" in result.stdout


def test_non_utf8_and_binary_control_bytes_fail_closed(tmp_path):
    non_utf8 = tmp_path / "opaque.json"
    non_utf8.write_bytes(b"reviewable-prefix\xff\xfe")
    controlled = tmp_path / "controlled.md"
    controlled.write_bytes(b"ordinary text" + (b"\x1b" * 16))

    result = _content_check(non_utf8, controlled)

    assert result.returncode == 1
    assert f"{non_utf8}\tnon-UTF-8 content" in result.stdout
    assert f"{controlled}\tdense binary control byte(s): 0x1b" in result.stdout


def _init_repo_with_asset(
    tmp_path: Path, repo_relative_path: str, data: bytes
) -> None:
    # The checker's pinned-digest lookup keys off a path resolved relative to
    # its own `git rev-parse --show-toplevel`, so a polyglot has to live at
    # that exact relative path inside a real (if otherwise empty) git
    # repository to exercise the ALLOWED_BINARY_ASSET_PATHS / pinned-digest
    # branch instead of the generic binary-magic branch. A fresh repo like
    # this has no .github/security/binary-asset-hashes.txt of its own, so
    # every asset path in it is "no pinned digest on file" -- still a
    # rejection, which is exactly the property under test.
    subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
    target = tmp_path / repo_relative_path
    target.parent.mkdir(parents=True, exist_ok=True)
    target.write_bytes(data)


def _run_content_check_in(
    tmp_path: Path, asset_path: str
) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        ["bash", str(CI_CHECKER), "--content-only", asset_path],
        cwd=tmp_path,
        check=False,
        capture_output=True,
        text=True,
        timeout=30,
    )


def test_a_png_polyglot_with_an_appended_zip_payload_is_rejected(tmp_path):
    # Classic PNG/ZIP polyglot: a legitimate PNG followed by an appended ZIP
    # local file header. Trivially a different SHA-256 than the pinned one.
    real_png = (
        REPO_ROOT / "src/local_deep_research/web/static/favicon.png"
    ).read_bytes()
    polyglot = (
        real_png + b"PK\x03\x04\x14\x00\x00\x00\x08\x00hidden-zip-payload"
    )
    asset_path = "src/local_deep_research/web/static/favicon.png"
    _init_repo_with_asset(tmp_path, asset_path, polyglot)

    result = _run_content_check_in(tmp_path, asset_path)

    assert result.returncode == 1, result.stdout + result.stderr
    assert "does not match its pinned digest" in result.stdout
    assert "binary-asset-hashes.txt" in result.stdout


def test_a_png_polyglot_payload_inside_an_already_allowlisted_idat_chunk_is_rejected(
    tmp_path,
):
    # This is the bypass that broke the previous (container-structure)
    # approach: unlike an unrecognized/private chunk type, IDAT is a real,
    # unavoidable, legitimately-repeatable PNG chunk type. A complete ZIP
    # fits inside a well-formed, correctly-CRC'd *IDAT* chunk placed before
    # a legitimate terminal IEND -- chunk-type allowlisting alone accepted
    # this file, while `zipfile.ZipFile()` (which scans backward for an
    # End-Of-Central-Directory signature and tolerates a trailing IEND
    # chunk like a ZIP comment) still opened it. Hash pinning has no notion
    # of "recognized chunk type" to fool -- it just compares bytes.
    real_png = (
        REPO_ROOT / "src/local_deep_research/web/static/favicon.png"
    ).read_bytes()

    offset = 8
    while True:
        chunk_length = int.from_bytes(real_png[offset : offset + 4], "big")
        chunk_type = real_png[offset + 4 : offset + 8]
        chunk_end = offset + 8 + chunk_length + 4
        if chunk_type == b"IEND":
            iend_start = offset
            break
        offset = chunk_end

    buf = BytesIO()
    with zipfile.ZipFile(buf, "w") as zf:
        zf.writestr("payload.txt", "malicious embedded payload")
    zip_bytes = buf.getvalue()
    idat_chunk_type = b"IDAT"  # not a private/unrecognized type -- real PNG
    crc = zlib.crc32(idat_chunk_type + zip_bytes) & 0xFFFFFFFF
    idat_chunk = (
        struct.pack(">I", len(zip_bytes))
        + idat_chunk_type
        + zip_bytes
        + struct.pack(">I", crc)
    )
    polyglot = real_png[:iend_start] + idat_chunk + real_png[iend_start:]
    # The embedded ZIP must actually be openable -- otherwise this isn't
    # exercising the real bypass, just an inert extra IDAT chunk.
    assert zipfile.ZipFile(BytesIO(polyglot)).namelist() == ["payload.txt"]

    asset_path = "src/local_deep_research/web/static/favicon.png"
    _init_repo_with_asset(tmp_path, asset_path, polyglot)

    result = _run_content_check_in(tmp_path, asset_path)

    assert result.returncode == 1, result.stdout + result.stderr
    assert "does not match its pinned digest" in result.stdout


def test_an_mp3_polyglot_with_an_appended_zip_payload_is_rejected(tmp_path):
    real_mp3 = (
        REPO_ROOT / "src/local_deep_research/web/static/sounds/success.mp3"
    ).read_bytes()
    polyglot = (
        real_mp3 + b"PK\x03\x04\x14\x00\x00\x00\x08\x00hidden-zip-payload"
    )
    asset_path = "src/local_deep_research/web/static/sounds/success.mp3"
    _init_repo_with_asset(tmp_path, asset_path, polyglot)

    result = _run_content_check_in(tmp_path, asset_path)

    assert result.returncode == 1, result.stdout + result.stderr
    assert "does not match its pinned digest" in result.stdout


def test_an_mp3_polyglot_with_a_payload_sized_into_the_id3_tag_region_is_rejected(
    tmp_path,
):
    # The second bypass this rewrite is aimed at: the previous MP3 check
    # trusted the ID3v2 header's declared `tag_size` and skipped that many
    # bytes WITHOUT validating them, then only walked genuine MPEG frames
    # from there to EOF. An attacker can prepend an ID3v2 header declaring a
    # large tag, fill that declared region with an arbitrary payload, and
    # append genuine audio frames afterward -- the old check walked straight
    # past the payload to the real frames and accepted the file.
    real_mp3 = (
        REPO_ROOT / "src/local_deep_research/web/static/sounds/error.mp3"
    ).read_bytes()
    payload = b"payload-smuggled-inside-the-declared-id3-tag-region--" * 10
    tag_size = len(payload)
    size_bytes = bytes(
        [
            (tag_size >> 21) & 0x7F,
            (tag_size >> 14) & 0x7F,
            (tag_size >> 7) & 0x7F,
            tag_size & 0x7F,
        ]
    )
    id3_header = b"ID3" + bytes([0x03, 0x00, 0x00]) + size_bytes
    polyglot = id3_header + payload + real_mp3

    asset_path = "src/local_deep_research/web/static/sounds/error.mp3"
    _init_repo_with_asset(tmp_path, asset_path, polyglot)

    result = _run_content_check_in(tmp_path, asset_path)

    assert result.returncode == 1, result.stdout + result.stderr
    assert "does not match its pinned digest" in result.stdout


def test_a_single_flipped_byte_in_a_pinned_asset_is_rejected(tmp_path):
    # The point of hash pinning: it does not matter whether a modification
    # is "structurally valid" by any container format's rules. One flipped
    # byte anywhere -- here, inside the final chunk's CRC -- is rejected.
    real_png = bytearray(
        (
            REPO_ROOT / "src/local_deep_research/web/static/favicon.png"
        ).read_bytes()
    )
    real_png[-1] ^= 0xFF
    asset_path = "src/local_deep_research/web/static/favicon.png"
    _init_repo_with_asset(tmp_path, asset_path, bytes(real_png))

    result = _run_content_check_in(tmp_path, asset_path)

    assert result.returncode == 1, result.stdout + result.stderr
    assert "does not match its pinned digest" in result.stdout


def test_local_precommit_hook_treats_binary_content_as_fatal(tmp_path):
    # .py is intentionally a broad *pathname* allowance. This proves content
    # still decides the result after that filename stage has passed.
    disguised = tmp_path / "allowlisted_name.py"
    disguised.write_bytes(b"PK\x03\x04pretend-zip")

    result = subprocess.run(
        ["bash", str(PRECOMMIT_CHECKER), str(disguised)],
        cwd=REPO_ROOT,
        check=False,
        capture_output=True,
        text=True,
        timeout=30,
    )

    assert result.returncode == 1
    assert "UNEXPECTED BINARY CONTENT" in result.stdout
    assert "ZIP/container" in result.stdout
    assert "WHITELIST VIOLATIONS" not in result.stdout


def test_json_syntax_is_a_precommit_invariant():
    config = (REPO_ROOT / ".pre-commit-config.yaml").read_text(encoding="utf-8")
    assert "-   id: check-json\n" in config