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.

21.4 KB
Raw Blame
# allow: no-sut-import — guardian; asserts repo file-whitelist, CODEOWNERS, and .gitignore structural invariants
"""Tests that repository integrity guardrails remain intact.

These tests verify structural invariants of the file whitelist,
CODEOWNERS, and .gitignore that protect against accidental re-introduction
of broad file-type exceptions (especially binary wildcards) and of dead
dotfile negations placed before the `.*` catch-all.
"""

import re
import shutil
import subprocess
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[2]


def _in_git_work_tree():
    """True if `git` is on PATH and REPO_ROOT is inside a git work tree.

    `git check-ignore` needs a work tree even with `--no-index`, so the
    git-semantics guard below is skipped (not failed) when run from a
    detached source tree (e.g. an unpacked tarball without `.git`) or an
    environment without git installed.
    """
    if not shutil.which("git"):
        return False
    try:
        return (
            subprocess.run(
                ["git", "rev-parse", "--is-inside-work-tree"],
                cwd=REPO_ROOT,
                capture_output=True,
                timeout=10,
            ).returncode
            == 0
        )
    except (OSError, subprocess.SubprocessError):
        # git missing (FileNotFoundError) or hung (TimeoutExpired) — treat as
        # "no git" and skip the git-semantics guard rather than break collection.
        return False


_IN_GIT_WORK_TREE = _in_git_work_tree()

# Representative dotfile / dot-dir paths that MUST stay trackable — the exact
# regression targets from #4909 and #4922. Each was (or would have been)
# silently dropped by `git add` when its `.gitignore` negation was dead. This
# is git-verified (real `git check-ignore` semantics), so it also catches
# breakage the positional check can't: a deleted negation, a later re-ignore,
# or a dir negation missing its `!.dir/` re-allow. These are concrete paths
# known to be intended-trackable, so there is no false-positive risk.
DOTFILES_THAT_MUST_STAY_TRACKABLE = (
    ".dockerignore",
    ".gitkeep",
    "any_new_dir/.gitkeep",  # the whole point of .gitkeep: a fresh empty dir
    ".nvmrc",
    ".pre-commit-config.yaml",
    "any_subdir/.gitignore",  # nested per-directory .gitignore
    ".gitleaks.toml",
    ".semgrepignore",
    ".trivyignore",
    ".safety-policy.yml",
    ".semgrep/rules/some_new_rule.yml",  # loaded by semgrep.yml --config
    "src/local_deep_research/defaults/.env.template",
    ".file-whitelist.txt",  # the security whitelist file itself
    ".hadolint.yaml",
    ".yamllint.yaml",
    ".zap/some_new_rule.tsv",  # ZAP config dir: type-scoped .tsv re-allow
)

# Non-dotfile root config / community-health / recognized guidance files masked
# by the `/*.*` (and `/*.json`) ROOT catch-all — same tracked-but-masked class
# as the dotfiles above, just a different catch-all. Regression targets from
# the follow-up to #4932. Guarded the same way (real `git check-ignore`
# semantics).
ROOT_FILES_THAT_MUST_STAY_TRACKABLE = (
    "SECURITY.md",
    "vite.config.js",  # COPY'd by the Dockerfile — build-breaking if dropped
    "eslint.config.js",
    "playwright.config.js",
    "lighthouserc.json",
    "AGENTS.md",  # recognized agent-guidance file — root-only via `!/AGENTS.md`
    "CLAUDE.md",  # recognized agent-guidance file — root-only via `!/CLAUDE.md`
    "GEMINI.md",  # recognized agent-guidance file — root-only via `!/GEMINI.md`
)

# Binary extensions that MUST be path-anchored (start with ^) in the whitelist.
# Broad binary wildcards like `\.png$` permanently bloat git history.
BINARY_EXTENSIONS = {
    "png",
    "jpg",
    "jpeg",
    "gif",
    "bmp",
    "tiff",
    "webp",
    "svg",
    "ico",
    "mp3",
    "mp4",
    "wav",
    "ogg",
    "m4a",
    "webm",
    "exe",
    "dll",
    "so",
    "dylib",
    "bin",
    "zip",
    "tar",
    "gz",
    "rar",
    "7z",
    "pdf",
    "doc",
    "docx",
    "xls",
    "xlsx",
    "db",
    "sqlite",
    "sqlite3",
    "woff",
    "woff2",
    "ttf",
    "eot",
}

# These guardrail paths must be the LAST CODEOWNERS entries (last-match-wins).
CODEOWNERS_GUARDRAIL_PATHS = {
    "/.gitignore",
    "/.file-whitelist.txt",
    "/.pre-commit-hooks/file-whitelist-check.sh",
    "/.github/scripts/file-whitelist-check.sh",
    "/.github/scripts/agent_guidance_index_filter.py",
    "/AGENTS.md",
    "/CLAUDE.md",
    "/GEMINI.md",
    "/.agents-lint.json",
    "/.github/security/binary-asset-hashes.txt",
    "/.github/CODEOWNERS",
}


class TestFileWhitelistGuardrails:
    """Verify .file-whitelist.txt doesn't contain broad binary wildcards."""

    @pytest.fixture()
    def whitelist_patterns(self):
        """Load non-comment, non-empty lines from .file-whitelist.txt."""
        whitelist = REPO_ROOT / ".file-whitelist.txt"
        assert whitelist.exists(), ".file-whitelist.txt not found at repo root"
        lines = []
        for line in whitelist.read_text().splitlines():
            stripped = line.strip()
            if stripped and not stripped.startswith("#"):
                lines.append(stripped)
        return lines

    def test_binary_patterns_are_path_anchored(self, whitelist_patterns):
        """Binary extension patterns must start with ^ (path-scoped).

        A broad pattern like `\\.png$` would allow PNGs anywhere in the repo,
        defeating the purpose of the whitelist. Binary patterns must be
        anchored to specific paths, e.g. `^docs/images/specific\\.png$`.
        """
        violations = []
        for pattern in whitelist_patterns:
            # Check if this pattern matches a binary extension
            for ext in BINARY_EXTENSIONS:
                # Pattern ends with the extension (escaped dot + ext + $)
                if re.search(rf"\\\.{re.escape(ext)}\)?(\|.*)?$", pattern):
                    if not pattern.startswith("^"):
                        violations.append(
                            f"Unanchored binary wildcard: '{pattern}' — "
                            f"must start with ^ to scope to a specific path"
                        )
                    break
        assert not violations, (
            "Binary file patterns in .file-whitelist.txt must be path-anchored "
            "(start with ^) to prevent repo bloat.\n"
            + "\n".join(f"  - {v}" for v in violations)
        )

    def test_no_broad_binary_wildcards(self, whitelist_patterns):
        """Binary extension patterns must not use .* wildcards.

        A pattern like `^docs/images/.*\\.png$` allows unlimited binary files
        in a directory. Binary files should be listed by explicit path to
        prevent repo bloat. Each new binary file should be a deliberate,
        reviewed addition.
        """
        violations = []
        for pattern in whitelist_patterns:
            for ext in BINARY_EXTENSIONS:
                if re.search(rf"\\\.{re.escape(ext)}\)?(\|.*)?$", pattern):
                    if ".*" in pattern:
                        violations.append(
                            f"Broad binary wildcard: '{pattern}' — "
                            f"uses .* which allows unlimited binary files; "
                            f"list each binary file explicitly instead"
                        )
                    break
        assert not violations, (
            "Binary file patterns must not use .* wildcards. "
            "List each binary file by explicit path to prevent repo bloat.\n"
            + "\n".join(f"  - {v}" for v in violations)
        )

    def test_whitelist_file_exists(self):
        """The shared whitelist file must exist."""
        assert (REPO_ROOT / ".file-whitelist.txt").exists()

    def test_whitelist_has_patterns(self, whitelist_patterns):
        """Whitelist must contain at least some patterns."""
        assert len(whitelist_patterns) > 10, (
            f"Whitelist only has {len(whitelist_patterns)} patterns — "
            "seems too few, file may be corrupted"
        )


class TestCodeownersGuardrails:
    """Verify CODEOWNERS guardrail rules remain at the bottom."""

    @pytest.fixture()
    def codeowners_rules(self):
        """Load non-comment, non-empty lines from CODEOWNERS."""
        codeowners = REPO_ROOT / ".github" / "CODEOWNERS"
        assert codeowners.exists(), ".github/CODEOWNERS not found"
        rules = []
        for line in codeowners.read_text().splitlines():
            stripped = line.strip()
            if stripped and not stripped.startswith("#"):
                rules.append(stripped)
        return rules

    def test_guardrail_rules_are_last(self, codeowners_rules):
        """Guardrail CODEOWNERS entries must be the last rules in the file.

        GitHub CODEOWNERS uses last-match-wins. If someone adds a rule
        after the guardrails (e.g. `* @someone`), it would override the
        maintainer-only restriction on .gitignore and whitelist files.
        """
        last_n = codeowners_rules[-len(CODEOWNERS_GUARDRAIL_PATHS) :]
        actual_paths = {line.split()[0] for line in last_n}
        assert actual_paths == CODEOWNERS_GUARDRAIL_PATHS, (
            f"CODEOWNERS guardrail rules are not the last entries!\n"
            f"Expected last {len(CODEOWNERS_GUARDRAIL_PATHS)} rules to be: "
            f"{CODEOWNERS_GUARDRAIL_PATHS}\n"
            f"Got: {actual_paths}\n"
            "Someone may have added rules after the guardrails, which would "
            "override the maintainer-only restriction (last-match-wins)."
        )

    def test_guardrail_rules_are_maintainer_only(self, codeowners_rules):
        """Guardrail rules must be restricted to @LearningCircuit only."""
        last_n = codeowners_rules[-len(CODEOWNERS_GUARDRAIL_PATHS) :]
        for rule in last_n:
            parts = rule.split()
            path = parts[0]
            owners = parts[1:]
            if path in CODEOWNERS_GUARDRAIL_PATHS:
                assert owners == ["@LearningCircuit"], (
                    f"Guardrail rule '{path}' has owners {owners} — "
                    "must be restricted to @LearningCircuit only"
                )


class TestGitignoreDotfileNegationOrdering:
    """Verify .gitignore dotfile negations sit AFTER the `.*` catch-all.

    .gitignore has a `.*` / `.*/` catch-all that ignores every dotfile and
    dot-directory. A `!` re-include (negation) for a dotfile placed BEFORE
    that catch-all is a DEAD rule: the later catch-all re-ignores it. Such a
    file stays tracked only if it was committed before the catch-all existed
    (git never ignores an already-tracked path), which masks the bug until a
    NEW matching file — a fresh `.gitkeep`, a `.semgrep/rules/*` rule, a
    nested `.gitignore` — is silently dropped by `git add`. See PR #4922.

    Two complementary checks:
      * ``test_dotfile_negations_are_after_catch_all`` — a fast, deterministic,
        false-positive-free *structural* guard for the exact recurring bug: a
        LITERAL dot-component negation placed before the catch-all.
      * ``test_known_paths_remain_trackable`` — a *git-semantics* guard on
        the concrete regression targets, which also catches dead negations the
        structural check can't (deleted negation, later re-ignore, missing
        parent-dir re-allow).

    Scope of the structural check (intentional limits — the git check above
    backstops the known targets): it does NOT flag a glob negation that also
    matches a dotfile (``!*.env`` — git's wildmatch has no FNM_PERIOD, so
    ``*``/``?``/``[`` match a leading dot too), because flagging every
    ``*``-leading negation would wrongly trip legitimate ones like ``!*.js``.
    A general ``git check-ignore``-over-every-negation approach was prototyped
    and rejected: this repo's broad-ignore + per-type/dir "enabler" negations
    (``!*/``, ``!.github/``, ``!*.js`` …) make per-negation and per-tracked-file
    git evaluation produce many false positives.
    """

    @pytest.fixture()
    def gitignore_lines(self):
        """Load (line_index, text) for non-comment, non-empty .gitignore lines.

        line_index is the 0-based position in the full file, so ordering
        against the catch-all is preserved.
        """
        gitignore = REPO_ROOT / ".gitignore"
        assert gitignore.exists(), ".gitignore not found at repo root"
        lines = []
        for idx, line in enumerate(
            gitignore.read_text(encoding="utf-8").splitlines()
        ):
            stripped = line.strip()
            if stripped and not stripped.startswith("#"):
                lines.append((idx, stripped))
        return lines

    @staticmethod
    def _catch_all_index(lines):
        """Return the line index of the last `.*` / `.*/` dotfile catch-all."""
        catch_all = [idx for idx, text in lines if text in (".*", ".*/")]
        assert catch_all, (
            ".gitignore has no `.*` dotfile catch-all — this guardrail "
            "assumes one exists. If the structure changed, update this test."
        )
        return max(catch_all)

    @staticmethod
    def _targets_dotfile(pattern):
        """True if a `!` negation names a LITERAL dotfile / dot-directory.

        A path component that literally starts with `.` (`.gitkeep`,
        `.semgrep/`, `.github/**/*.yml`, or a deep `src/.../.env.template`) is
        a dotfile the `.*` catch-all re-ignores, so its negation must sit after
        the catch-all. Components like `package.json` or `src/**/*.py` have no
        leading-dot literal and are not at risk here.

        Only LITERAL leading dots are matched — see the class docstring for why
        glob-leading negations (`!*.env`) are deliberately out of scope.
        """
        body = pattern[1:]  # strip leading '!'
        return any(comp.startswith(".") for comp in body.split("/") if comp)

    def test_dotfile_negations_are_after_catch_all(self, gitignore_lines):
        """Every dotfile `!` negation must come AFTER the `.*` catch-all.

        A dotfile negation before the catch-all is silently overridden, so a
        newly added matching file is dropped by `git add` without warning.
        Move such negations below the catch-all, next to `!.gitleaksignore` /
        `!.dockerignore`.
        """
        catch_all_idx = self._catch_all_index(gitignore_lines)
        violations = [
            f"line {idx + 1}: '{text}'"
            for idx, text in gitignore_lines
            if text.startswith("!")
            and self._targets_dotfile(text)
            and idx < catch_all_idx
        ]
        assert not violations, (
            "Dotfile negation(s) found BEFORE the `.*` catch-all in .gitignore "
            "— these are DEAD rules (the catch-all re-ignores them; new "
            "matching files are silently dropped by `git add`). Move them below "
            "the catch-all, next to `!.gitleaksignore` / `!.dockerignore`. "
            "See PR #4922.\n" + "\n".join(f"  - {v}" for v in violations)
        )

    @pytest.mark.skipif(
        not _IN_GIT_WORK_TREE,
        reason="git check-ignore requires git and a git work tree",
    )
    def test_known_paths_remain_trackable(self):
        """Regression targets must be trackable per real git-ignore semantics.

        Uses `git check-ignore --no-index` (evaluates the whole rule set the
        way git does) on the concrete files fixed across #4909/#4922/#4932 and
        its follow-up — dotfiles masked by the `.*` catch-all and root configs
        masked by the `/*.*` catch-all. Catches dead negations the positional
        check can't see — a deleted negation, a later re-ignore, or a `.dir/`
        negation missing its parent-dir re-allow. `--no-index` ignores
        tracked-status, so this fails even while the file is still committed
        (that masking is exactly the bug).
        """
        ignored = []
        for path in (
            DOTFILES_THAT_MUST_STAY_TRACKABLE
            + ROOT_FILES_THAT_MUST_STAY_TRACKABLE
        ):
            # `-q` is required: it sets exit status only (rc 0 = ignored,
            # 1 = not ignored). Do NOT switch to `-v` — with `-v` a path
            # matched by a `!` negation also returns rc 0, which would
            # invert this test's meaning.
            result = subprocess.run(
                ["git", "check-ignore", "--no-index", "-q", path],
                cwd=REPO_ROOT,
                capture_output=True,
                timeout=10,
            )
            assert result.returncode in (0, 1), (
                f"git check-ignore failed for '{path}' (rc={result.returncode}): "
                f"{result.stderr.decode(errors='replace')}"
            )
            if result.returncode == 0:
                ignored.append(path)
        assert not ignored, (
            "These files would be silently dropped by `git add` — their "
            ".gitignore negation is dead (missing, moved above the `.*`/`/*.*` "
            "catch-all, missing a parent-dir re-allow, or killed by a later "
            "re-ignore). See PRs #4922 / #4932.\n"
            + "\n".join(f"  - {p}" for p in ignored)
        )

    @pytest.mark.skipif(
        not _IN_GIT_WORK_TREE,
        reason="git check-ignore requires git and a git work tree",
    )
    def test_agents_guidance_is_root_only_trackable(self):
        """Check real `--no-index -q` exit codes and later allowlisted paths.

        Exit code 0 means ignored and 1 means trackable; representative paths
        prove the final rule still wins after each Markdown allowlist. Scoped
        to EXACT-CASE root-only behavior for every recognized agent-guidance
        file — ``AGENTS.md`` (root plural), ``AGENT.md``, ``CLAUDE.md``, and ``GEMINI.md``.
        Lowercase / mixed-case CLAUDE.md and GEMINI.md variants are deliberately NOT
        asserted here: ``.gitignore`` is case-sensitive, so a root lowercase
        ``claude.md`` is already ignored by the ``*`` deny-all with no matching
        re-allow, but the broad Markdown re-allows (``!docs/**/*.md``,
        ``!examples/**/*.md``, ``!changelog.d/**/*.md``) WOULD re-include
        lowercase variants under those dirs — the gitignore layer cannot
        enforce case folding. Case-variant rejection is owned instead by
        ``tests/security/test_agent_guidance_whitelist.py``, which uses
        ``git add -f`` to bypass ``.gitignore`` and asserts the tracked-index
        basename guard in both shell scripts rejects every case variant.
        """
        for path, expected_returncode in (
            ("AGENTS.md", 1),
            ("nested/AGENTS.md", 0),
            (".github/AGENTS.md", 0),
            (".semgrep/rules/AGENTS.md", 0),
            ("docs/AGENTS.md", 0),
            ("docs/sub/AGENTS.md", 0),
            ("examples/AGENTS.md", 0),
            ("cookiecutter-docker/AGENTS.md", 0),
            ("changelog.d/AGENTS.md", 0),
            ("AGENT.md", 0),
            ("docs/AGENT.md", 0),
            # CLAUDE.md mirrors the AGENTS.md coverage at EXACT case only:
            # the final ``.gitignore`` block denies the ``CLAUDE.md`` basename
            # at every depth and re-allows only the root ``!/CLAUDE.md``, so
            # the root file must stay trackable while every nested exact-case
            # sibling stays ignored after the Markdown allowlists. Case
            # variants (``claude.md``, ``CLaude.md``, ``CLAUDE.MD``, ...) are
            # intentionally NOT listed here — see the method docstring.
            ("CLAUDE.md", 1),
            ("nested/CLAUDE.md", 0),
            (".github/CLAUDE.md", 0),
            (".semgrep/rules/CLAUDE.md", 0),
            ("docs/CLAUDE.md", 0),
            ("docs/sub/CLAUDE.md", 0),
            ("examples/CLAUDE.md", 0),
            ("cookiecutter-docker/CLAUDE.md", 0),
            ("changelog.d/CLAUDE.md", 0),
            # GEMINI.md mirrors the AGENTS.md / CLAUDE.md coverage at EXACT case
            # only: the final ``.gitignore`` block denies the ``GEMINI.md`` basename
            # at every depth and re-allows only the root ``!/GEMINI.md``, so the
            # root file must stay trackable while every nested exact-case sibling
            # stays ignored after the Markdown allowlists. Case variants
            # (``gemini.md``, ``Gemini.md``, ``GEMINI.MD``, ...) are intentionally
            # NOT listed here — see the method docstring.
            ("GEMINI.md", 1),
            ("nested/GEMINI.md", 0),
            (".github/GEMINI.md", 0),
            ("docs/GEMINI.md", 0),
            ("changelog.d/GEMINI.md", 0),
        ):
            result = subprocess.run(
                ["git", "check-ignore", "--no-index", "-q", path],
                cwd=REPO_ROOT,
                capture_output=True,
                timeout=10,
            )
            assert result.returncode in (0, 1), (
                f"git check-ignore failed for '{path}' (rc={result.returncode}): "
                f"{result.stderr.decode(errors='replace')}"
            )
            assert result.returncode == expected_returncode, (
                f"'{path}' has the wrong ignore state: expected exit code "
                f"{expected_returncode}, got {result.returncode}. Root AGENTS.md "
                "and CLAUDE.md and GEMINI.md must be trackable; every nested exact-case "
                "AGENTS.md / AGENT.md / CLAUDE.md / GEMINI.md must stay ignored after "
                "later Markdown allowlists. (Case-variant CLAUDE.md / GEMINI.md rejection "
                "is owned by tests/security/test_agent_guidance_whitelist.py — see this "
                "method's docstring for why.)"
            )

    def test_gitignore_exists(self):
        """The root .gitignore must exist."""
        assert (REPO_ROOT / ".gitignore").exists()