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.

test(guardrails): prevent dead dotfile negations above the .gitignore catch-all (#4923)

LearningCircuit4ac5e481f6c2
diff --git a/tests/security/test_repo_guardrails.py b/tests/security/test_repo_guardrails.py
index 7c99bbe2b3..16ef48342b 100644
--- a/tests/security/test_repo_guardrails.py
+++ b/tests/security/test_repo_guardrails.py
@@ -1,18 +1,72 @@
-# allow: no-sut-import — guardian; asserts repo file-whitelist and CODEOWNERS structural invariants
+# 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 and
-CODEOWNERS that protect against accidental re-introduction of broad
-file-type exceptions (especially binary wildcards).
+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",
+)
+
# Binary extensions that MUST be path-anchored (start with ^) in the whitelist.
# Broad binary wildcards like `\.png$` permanently bloat git history.
BINARY_EXTENSIONS = {
@@ -187,3 +241,147 @@ class TestCodeownersGuardrails:
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_dotfiles_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_dotfiles_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 dotfiles fixed in #4909/#4922. 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:
+ # `-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 dotfiles 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 PR #4922.\n"
+ + "\n".join(f" - {p}" for p in ignored)
+ )
+
+ def test_gitignore_exists(self):
+ """The root .gitignore must exist."""
+ assert (REPO_ROOT / ".gitignore").exists()