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.

7.3 KB
Raw Blame
"""Tests for the check-no-flask pre-commit hook.

PR #3299's core invariant -- Flask stays gone, werkzeug stays confined
to its two security modules -- must fail CI when violated. The guardian
tests here cover the live tree, synthetic violations, fail-closed
behaviour, and the .pre-commit-config.yaml wiring.
"""

from __future__ import annotations

import importlib.util
import os
import re
import sys
from pathlib import Path
from types import ModuleType

import pytest
import yaml

ROOT = Path(__file__).resolve().parents[2]
HOOK_PATH = ROOT / ".pre-commit-hooks" / "check-no-flask.py"
MODULE = "check_no_flask"

ALLOWED_SANITISER = "src/local_deep_research/security/filename_sanitizer.py"
ALLOWED_VALIDATOR = "src/local_deep_research/security/path_validator.py"


@pytest.fixture
def hook() -> ModuleType:
    assert HOOK_PATH.is_file(), f"hook is missing: {HOOK_PATH}"
    # `language: script` executes the entry directly: without the
    # executable bit pre-commit fails with "Executable ... is not
    # executable" (CI run 33543178672).
    assert os.access(HOOK_PATH, os.X_OK), (
        f"{HOOK_PATH} must be committed with the executable bit"
    )
    spec = importlib.util.spec_from_file_location(MODULE, HOOK_PATH)
    assert spec is not None and spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


def make_repo(tmp_path: Path, files: dict[str, str]) -> Path:
    for rel, content in files.items():
        path = tmp_path / rel
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(content, encoding="utf-8")
    return tmp_path


def test_live_tree_is_flask_free(hook):
    """The enforcement test: the real checkout must scan clean."""
    assert hook.find_violations(ROOT) == []


@pytest.mark.parametrize(
    "statement",
    [
        "import flask",
        "import flask.something",
        "from flask import jsonify",
        "from flask_login import login_required",
        "from flask_wtf import CSRFProtect",
        "import flask_socketio",
        # Conditional/nested imports count too.
        "if auth_needed:\n    from flask import session",
    ],
)
def test_flask_imports_are_flagged(hook, tmp_path, statement):
    root = make_repo(
        tmp_path,
        {"src/local_deep_research/web/routers/x.py": statement},
    )
    violations = hook.find_violations(root)
    assert len(violations) == 1
    assert "flask" in violations[0]
    assert "x.py" in violations[0]


@pytest.mark.parametrize(
    "content",
    [
        "# Historical: from flask import session (removed in #3299)",
        '"""The flask migration removed flask; docstrings may narrate."""',
        "from flaskite import something",
        "import flaskq",
    ],
)
def test_comments_docstrings_and_prefix_lookalikes_pass(
    hook, tmp_path, content
):
    root = make_repo(tmp_path, {"src/x.py": content})
    assert hook.find_violations(root) == []


def test_werkzeug_outside_allowlist_is_flagged(hook, tmp_path):
    root = make_repo(
        tmp_path,
        {
            "src/local_deep_research/web/routers/upload.py": (
                "from werkzeug.utils import secure_filename\n"
            )
        },
    )
    violations = hook.find_violations(root)
    assert len(violations) == 1
    assert "werkzeug" in violations[0]
    assert "upload.py" in violations[0]


@pytest.mark.parametrize("allowed", [ALLOWED_SANITISER, ALLOWED_VALIDATOR])
def test_werkzeug_allowlisted_security_modules_pass(hook, tmp_path, allowed):
    root = make_repo(
        tmp_path,
        {allowed: "from werkzeug.utils import secure_filename\n"},
    )
    assert hook.find_violations(root) == []


def test_tests_directory_is_out_of_scope(hook, tmp_path):
    """Tests may reference flask deliberately (guards, harnesses)."""
    root = make_repo(
        tmp_path,
        {"tests/test_flask_compat.py": "from flask import jsonify\n"},
    )
    assert hook.find_violations(root) == []


def test_flask_dependency_is_flagged(hook, tmp_path):
    root = make_repo(
        tmp_path,
        {"pyproject.toml": '[project]\ndependencies = ["flask~=3.1"]\n'},
    )
    assert hook.find_violations(root) == [
        "pyproject.toml: dependencies entry 'flask~=3.1' re-introduces Flask"
    ]


def test_flask_ecosystem_optional_dependency_is_flagged(hook, tmp_path):
    root = make_repo(
        tmp_path,
        {
            "pyproject.toml": (
                "[project]\n"
                'dependencies = ["fastapi"]\n'
                "[project.optional-dependencies]\n"
                'legacy = ["flask-cors"]\n'
            )
        },
    )
    violations = hook.find_violations(root)
    assert len(violations) == 1
    assert "flask-cors" in violations[0]
    assert "legacy" in violations[0]


def test_werkzeug_dependency_and_prose_pass(hook, tmp_path):
    """Comments are invisible to tomllib; werkzeug itself stays allowed."""
    root = make_repo(
        tmp_path,
        {
            "pyproject.toml": (
                "# Retained as a security-utility library, NOT\n"
                "# Flask/framework residue.\n"
                "[project]\n"
                'dependencies = ["werkzeug~=3.1.6"]\n'
            )
        },
    )
    assert hook.find_violations(root) == []


def test_unparseable_candidate_fails_closed(hook, tmp_path):
    root = make_repo(tmp_path, {"src/broken.py": "from flask import\n"})
    violations = hook.find_violations(root)
    assert len(violations) == 1
    assert "unparseable" in violations[0]


def test_unreadable_candidate_fails_closed(hook, tmp_path):
    locked_dir = tmp_path / "src"
    locked_dir.mkdir()
    locked = locked_dir / "locked.py"
    locked.write_text("from flask import jsonify\n", encoding="utf-8")
    locked.chmod(0)
    if os.access(locked, os.R_OK):  # root ignores mode bits
        pytest.skip("running as root; chmod 0 is still readable")
    violations = hook.find_violations(tmp_path)
    assert len(violations) == 1
    assert "unreadable" in violations[0]


def test_main_exit_codes(hook, tmp_path, monkeypatch, capsys):
    clean = make_repo(tmp_path / "clean", {"src/app.py": "import fastapi\n"})
    monkeypatch.setattr(hook, "ROOT", clean)
    assert hook.main() == 0

    dirty = make_repo(tmp_path / "dirty", {"src/app.py": "import flask\n"})
    monkeypatch.setattr(hook, "ROOT", dirty)
    assert hook.main() == 1
    assert "Flask regression" in capsys.readouterr().out


def test_registered_hook_files_filter_targets_only_src_and_pyproject():
    config = yaml.safe_load((ROOT / ".pre-commit-config.yaml").read_text())
    hooks = [
        hook
        for repository in config["repos"]
        for hook in repository["hooks"]
        if hook["id"] == "check-no-flask"
    ]
    assert len(hooks) == 1
    (hook,) = hooks
    assert hook["entry"] == ".pre-commit-hooks/check-no-flask.py"
    assert hook["language"] == "script" and hook["pass_filenames"] is False
    matcher = re.compile(hook["files"])
    targets = [
        "src/local_deep_research/web/app.py",
        "src/local_deep_research/security/x.py",
        "pyproject.toml",
    ]
    assert all(matcher.search(path) for path in targets)
    leaks = [
        "tests/hooks/test_check_no_flask.py",
        "cookiecutter-docker/src/app.py",
        "src/local_deep_research/web/app.py.bak",
        "docs/flask.md",
    ]
    assert not any(matcher.search(path) for path in leaks)