Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(security): honour the repository's .gitleaks.toml path allowlist (#3709)

Swati Ahuja508ee92eddf2
diff --git a/docs/layers/SECURITY.md b/docs/layers/SECURITY.md
index 5e0969966..9619c9cdd 100644
--- a/docs/layers/SECURITY.md
+++ b/docs/layers/SECURITY.md
@@ -213,6 +213,16 @@ the gate; they do not appear in the gate's suppressed count. Non-secret
findings on the same line still report. A marker inside a string does not
silence a finding.
+### A repository's `.gitleaks.toml`
+
+A path the root `.gitleaks.toml` allowlists has no secret findings, in the
+stored scan, the history scan or the gate. The scanner reads the `paths` of
+`[allowlist]` and `[[allowlists]]` as gitleaks matches them, plus gitleaks'
+default allowlist paths under `[extend] useDefault = true`. Entries scoped by
+`targetRules`, or by `condition = "AND"` with `regexes`, `stopwords` or
+`commits`, are not applied. The file is read from the checked-out tree, as
+gitleaks does, so a change's own allowlist entries apply to it.
+
### Baselines
`--write-baseline FILE` records the change's findings in a committed JSON file
diff --git a/packages/core/src/repowise/core/analysis/history_scan.py b/packages/core/src/repowise/core/analysis/history_scan.py
index 478cf56ba..760e0180c 100644
--- a/packages/core/src/repowise/core/analysis/history_scan.py
+++ b/packages/core/src/repowise/core/analysis/history_scan.py
@@ -40,6 +40,7 @@ from datetime import UTC, datetime
from pathlib import Path
from typing import Any
+from repowise.core.analysis.secret_allowlist import secret_path_allowlist
from repowise.core.analysis.security_scan import (
SECRET_KINDS,
SecurityScanner,
@@ -202,12 +203,13 @@ class HistorySecurityScanner:
def _blob_introductions(
self, repo_path: Path, since: str | None, to: str | None
- ) -> tuple[dict[str, str], dict[str, str]]:
- """Return ``({blob_sha: first_commit}, {commit_sha: author_iso})``.
+ ) -> tuple[dict[str, str], dict[str, str], dict[str, list[str]]]:
+ """Return ``({blob_sha: first_commit}, {commit_sha: author_iso}, {blob_sha: paths})``.
One ``git log --reverse --raw`` pass attributes each blob to the oldest
commit that introduced it, avoiding an O(commits x files) ``ls-tree``
- loop.
+ loop. ``paths`` lists every path a commit wrote the blob to, oldest
+ first: ``rev-list --objects`` names one path per blob.
"""
if since and to:
rev_range = f"{since}..{to}"
@@ -228,6 +230,7 @@ class HistorySecurityScanner:
)
blob_introduced_at: dict[str, str] = {}
commit_dates: dict[str, str] = {}
+ blob_paths: dict[str, list[str]] = {}
current_commit: str | None = None
for line in raw.splitlines():
@@ -246,6 +249,8 @@ class HistorySecurityScanner:
if blob_sha.strip("0") == "":
continue # deletion: no post-image blob
blob_introduced_at.setdefault(blob_sha, current_commit)
+ # The path is the last tab field (a rename lists old and new).
+ blob_paths.setdefault(blob_sha, []).append(line.rsplit(" ", 1)[-1])
continue
if "\x1f" not in line:
continue
@@ -256,7 +261,7 @@ class HistorySecurityScanner:
current_commit = sha
commit_dates[sha] = iso.strip()
- return blob_introduced_at, commit_dates
+ return blob_introduced_at, commit_dates, blob_paths
def _read_blobs_batch(self, repo_path: Path, blob_shas: list[str]) -> dict[str, str]:
"""Return ``{blob_sha: content}`` via a single ``git cat-file --batch``."""
@@ -338,7 +343,16 @@ class HistorySecurityScanner:
blobs = self._unique_blobs(repo_path, since, to)
summary.blobs_scanned = len(blobs)
- blob_introduced_at, commit_dates = self._blob_introductions(repo_path, since, to)
+ blob_introduced_at, commit_dates, blob_paths = self._blob_introductions(
+ repo_path, since, to
+ )
+ allowlisted = secret_path_allowlist(repo_path)
+ # A blob is allowlisted only when every path it was written to is;
+ # otherwise it is reported at the first path outside the allowlist.
+ for blob_sha, path in blobs.items():
+ if path and allowlisted(path):
+ outside = (p for p in blob_paths.get(blob_sha, ()) if not allowlisted(p))
+ blobs[blob_sha] = next(outside, path)
# Test material findings are downgraded to low severity by scan_file
# so real keys in test files are still recorded without polluting high-severity reports.
@@ -363,7 +377,13 @@ class HistorySecurityScanner:
# Elided values ("sk-...") never get here: the scan's credential
# check rejects them on the captured value, not the snippet.
- kept = [f for f in findings if self._passes_gate(f["kind"], secrets_only=secrets_only)]
+ dropped = SECRET_KINDS if allowlisted(path or "") else frozenset()
+ kept = [
+ f
+ for f in findings
+ if self._passes_gate(f["kind"], secrets_only=secrets_only)
+ and f["kind"] not in dropped
+ ]
if not kept:
continue
diff --git a/packages/core/src/repowise/core/analysis/secret_allowlist.py b/packages/core/src/repowise/core/analysis/secret_allowlist.py
new file mode 100644
index 000000000..51ffa4663
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/secret_allowlist.py
@@ -0,0 +1,124 @@
+"""Paths a repository's own secret-scanner config says hold no real secrets.
+
+Read from ``.gitleaks.toml`` at the repository root, the file gitleaks itself
+loads: the global ``[allowlist]`` table and each ``[[allowlists]]`` entry. Its
+``paths`` are matched as gitleaks does, an unanchored regex search over the
+repo-relative POSIX path. ``[extend] useDefault = true`` also brings in the
+paths of gitleaks' default allowlist (:data:`_GITLEAKS_DEFAULT_PATHS`).
+
+An entry is honoured only when its path alone allows a finding, as in gitleaks:
+one scoped by ``targetRules`` names gitleaks rule ids this scanner does not
+have, and one with ``condition = "AND"`` beside ``commits``, ``regexes`` or
+``stopwords`` needs a check a path cannot answer. Ceiling: ``regexes`` and
+``stopwords`` test the matched secret value and ``commits`` a history scan's
+commit, so they are not read; ``[extend] path``, ``.gitleaksignore`` (keyed on
+gitleaks rule ids) and other scanners' ignore files (detect-secrets baselines,
+``.gitguardian.yaml``) are not read either.
+
+Go RE2 and Python ``re`` mostly agree on the shapes such paths use; where they
+differ, matching more than gitleaks would silence a real secret. So patterns
+compile with ``re.ASCII`` (RE2's ``\\w``, ``\\d`` and case folding are ASCII),
+and a pattern Python reads differently or cannot compile is skipped, so it
+silences nothing: a POSIX class ``[[:alpha:]]`` (a plain set to Python), a
+mid-pattern ``(?i)``, ``\\z``, ``\\Q...\\E``.
+"""
+
+from __future__ import annotations
+
+import logging
+import re
+import tomllib
+from collections.abc import Callable, Iterator, Mapping
+from pathlib import Path
+from typing import Any
+
+logger = logging.getLogger(__name__)
+
+CONFIG_FILENAME = ".gitleaks.toml"
+
+#: The ``paths`` of the global allowlist in gitleaks' default config
+#: (``config/gitleaks.toml``, v8.30), applied when a repo's config extends it.
+_GITLEAKS_DEFAULT_PATHS = (
+ r"gitleaks\.toml",
+ r"(?i)\.(?:bmp|gif|jpe?g|png|svg|tiff?)$",
+ r"(?i)\.(?:eot|[ot]tf|woff2?)$",
+ r"(?i)\.(?:docx?|xlsx?|pdf|bin|socket|vsidx|v2|suo|wsuo|.dll|pdb|exe|gltf)$",
+ r"go\.(?:mod|sum|work(?:\.sum)?)$",
+ r"(?:^|/)vendor/modules\.txt$",
+ r"(?:^|/)vendor/(?:github\.com|golang\.org/x|google\.golang\.org|gopkg\.in|istio\.io"
+ r"|k8s\.io|sigs\.k8s\.io)(?:/.*)?$",
+ r"(?:^|/)gradlew(?:\.bat)?$",
+ r"(?:^|/)gradle\.lockfile$",
+ r"(?:^|/)mvnw(?:\.cmd)?$",
+ r"(?:^|/)\.mvn/wrapper/MavenWrapperDownloader\.java$",
+ r"(?:^|/)node_modules(?:/.*)?$",
+ r"(?:^|/)(?:deno\.lock|npm-shrinkwrap\.json|package-lock\.json|pnpm-lock\.yaml|yarn\.lock)$",
+ r"(?:^|/)bower_components(?:/.*)?$",
+ r"(?:^|/)(?:angular|bootstrap|jquery(?:-?ui)?|plotly|swagger-?ui)[a-zA-Z0-9.-]*(?:\.min)?\.js(?:\.map)?$",
+ r"(?:^|/)javascript\.json$",
+ r"(?:^|/)(?:Pipfile|poetry)\.lock$",
+ r"(?i)(?:^|/)(?:v?env|virtualenv)/lib(?:64)?(?:/.*)?$",
+ r"(?i)(?:^|/)(?:lib(?:64)?/python[23](?:\.\d{1,2})+|python/[23](?:\.\d{1,2})+/lib(?:64)?)(?:/.*)?$",
+ r"(?i)(?:^|/)[a-z0-9_.]+-[0-9.]+\.dist-info(?:/.+)?$",
+ r"(?:^|/)vendor/(?:bundle|ruby)(?:/.*?)?$",
+ r"\.gem$",
+ r"verification-metadata\.xml",
+ r"Database.refactorlog",
+ r"(?:^|/)\.git$",
+)
+
+_NOT_BY_PATH_ALONE = ("commits", "regexes", "stopwords")
+
+
+def _no_path(path: str) -> bool:
+ return False
+
+
+def secret_path_allowlist(root: str | Path | None) -> Callable[[str], bool]:
+ """Whether a repo-relative path is one *root*'s secret-scanner config allowlists.
+
+ No config, or one that does not parse, allowlists nothing.
+ """
+ if root is None:
+ return _no_path
+ try:
+ config = tomllib.loads(Path(root, CONFIG_FILENAME).read_text(encoding="utf-8"))
+ except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError):
+ return _no_path
+ patterns = [*_compiled(_allowed_paths(config))]
+ if not patterns:
+ return _no_path
+ return lambda path: any(p.search(path) for p in patterns)
+
+
+def _allowed_paths(config: Mapping[str, Any]) -> Iterator[str]:
+ """Every ``paths`` entry that allows a finding on its own."""
+ extend = config.get("extend")
+ if isinstance(extend, Mapping) and extend.get("useDefault") is True:
+ yield from _GITLEAKS_DEFAULT_PATHS
+ entries = config.get("allowlists")
+ entries = [*entries] if isinstance(entries, list) else []
+ entries.append(config.get("allowlist"))
+ for entry in entries:
+ if isinstance(entry, Mapping) and _allows_by_path_alone(entry):
+ paths = entry.get("paths")
+ if isinstance(paths, list):
+ yield from (p for p in paths if isinstance(p, str))
+
+
+def _allows_by_path_alone(entry: Mapping[str, Any]) -> bool:
+ if entry.get("targetRules"):
+ return False
+ condition = str(entry.get("condition", "")).upper()
+ return condition not in ("AND", "&&") or not any(entry.get(k) for k in _NOT_BY_PATH_ALONE)
+
+
+def _compiled(paths: Iterator[str]) -> Iterator[re.Pattern[str]]:
+ for raw in paths:
+ if "[[:" in raw:
+ logger.debug("gitleaks allowlist path %r skipped: POSIX class", raw)
+ continue
+ try:
+ yield re.compile(raw, re.ASCII)
+ except re.error as exc:
+ logger.debug("gitleaks allowlist path %r skipped: %s", raw, exc)
diff --git a/packages/core/src/repowise/core/analysis/security_gate.py b/packages/core/src/repowise/core/analysis/security_gate.py
index b5a8a1e74..f41da8cfe 100644
--- a/packages/core/src/repowise/core/analysis/security_gate.py
+++ b/packages/core/src/repowise/core/analysis/security_gate.py
@@ -15,12 +15,14 @@ Only the masked snippet (:func:`~.security_scan._snippet`) leaves this module,
and the fingerprint is taken over it too, so neither a CI log nor the committed
baseline holds a raw value.
-Two repository-owned inputs shape a scan. ``security.patterns`` in
+Three repository-owned inputs shape a scan. ``security.patterns`` in
``.repowise/config.yaml`` adds secret kinds (``custom:<name>``, see
:func:`custom_patterns`), treated like the built-in ones. A
``repowise-security-ignore`` marker on a finding's own line silences it (all
kinds, or only the ``: kind, kind`` it lists); a silenced finding never fails
the gate or enters a baseline, but every output still counts and lists it.
+A path the repo's ``.gitleaks.toml`` allowlists has no secret findings at all
+(:mod:`~.secret_allowlist`).
"""
from __future__ import annotations
@@ -29,7 +31,7 @@ import hashlib
import re
import string
import subprocess
-from collections.abc import Iterable, Iterator, Mapping, Sequence
+from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from dataclasses import dataclass, field
from pathlib import Path, PurePosixPath
from typing import Any
@@ -41,6 +43,7 @@ from repowise.core.support_paths import DOC_EXTENSIONS
from .change_risk.features import GIT_TIMEOUT_SECONDS, _git, revspec_head, split_revspec
from .changed_lines import DIFF_ARGS, parse_unified_diff
+from .secret_allowlist import secret_path_allowlist
from .security_scan import (
SECRET_KINDS,
_allowlisted_lines,
@@ -413,6 +416,7 @@ def _scoped(
*,
secrets_only: bool,
patterns: Sequence[CustomPattern] = (),
+ path_allowlisted: bool = False,
) -> tuple[list[dict], set[tuple[str, str]]]:
"""Findings in *source* that span a line in *lines*, as gate rows, and the long lines skipped.
@@ -420,7 +424,8 @@ def _scoped(
call, but a key pasted into a README is still a leak. Custom patterns are
secret kinds, matched on the lines in *lines* only; a custom match is also
masked in a built-in finding's snippet on its line, while that finding keeps
- the fingerprint it has without patterns.
+ the fingerprint it has without patterns. *path_allowlisted* drops every
+ secret kind, as an allow marker on each line would.
"""
secrets_only = secrets_only or PurePosixPath(path).suffix.lower() in DOC_EXTENSIONS
text = _Text(path, source_lines(source), patterns)
@@ -435,7 +440,7 @@ def _scoped(
_row(text, entry)
for entry in [*found, *custom]
if (entry[1] not in SECRET_KINDS and not entry[1].startswith("custom:"))
- or entry[0] not in allowed
+ or not (path_allowlisted or entry[0] in allowed)
], long_lines
@@ -551,7 +556,10 @@ def _log_range(root: str, revspec: str) -> list[str]:
def _history_secrets(
- root: str, revspec: str, patterns: Sequence[CustomPattern]
+ root: str,
+ revspec: str,
+ patterns: Sequence[CustomPattern],
+ allowlisted: Callable[[str], bool],
) -> tuple[list[dict], int, set[tuple[str, str]]]:
"""Secret rows on the lines each commit in the change added, oldest first.
@@ -578,7 +586,11 @@ def _history_secrets(
"clone, so every line it holds would read as added. Fetch the full "
"history (fetch-depth: 0)."
)
- added = {p: d.new_lines for p, d in parse_unified_diff(diff).items() if d.new_lines}
+ added = {
+ p: d.new_lines
+ for p, d in parse_unified_diff(diff).items()
+ if d.new_lines and not allowlisted(p)
+ }
# One read per commit bounds memory to the files a single commit touched.
paths = sorted(added)
texts = _read_blobs(root, [f"{sha}:{p}" for p in paths])
@@ -640,20 +652,26 @@ def scan_change(
rows: list[dict] = []
scanned = 0
skipped: set[tuple[str, str]] = set()
+ allowlisted = secret_path_allowlist(root)
texts = _read_blobs(root, [f"{rev}:{p}" for p in paths])
for path, text in zip(paths, texts, strict=True):
if text is None:
continue
scanned += 1
scoped, long_lines = _scoped(
- path, text, changed[path], secrets_only=False, patterns=patterns
+ path,
+ text,
+ changed[path],
+ secrets_only=False,
+ patterns=patterns,
+ path_allowlisted=allowlisted(path),
)
rows += scoped
skipped |= long_lines
history: list[dict] = []
commits = 0
if not staged:
- history, commits, long_lines = _history_secrets(root, revspec, patterns)
+ history, commits, long_lines = _history_secrets(root, revspec, patterns, allowlisted)
skipped |= long_lines
findings, suppressed = _merge(rows, history)
return ChangeScan(
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.py
index 5853b794c..dfb055c71 100644
--- a/packages/core/src/repowise/core/analysis/security_scan.py
+++ b/packages/core/src/repowise/core/analysis/security_scan.py
@@ -23,12 +23,13 @@ import logging
import re
from collections.abc import Callable, Iterable, Mapping
from datetime import UTC, datetime
-from pathlib import PurePosixPath
+from pathlib import Path, PurePosixPath
from typing import Any
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
+from repowise.core.analysis.secret_allowlist import secret_path_allowlist
from repowise.core.code_origin import code_origin
from repowise.core.support_paths import DOC_EXTENSIONS
from repowise.core.test_paths import is_test_related_path
@@ -1423,18 +1424,34 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
return [f for f in findings if f["kind"] not in SECRET_KINDS or f["line"] not in allowed]
+def _repo_root(file_info: Any) -> str | None:
+ """The directory *file_info*'s repo-relative path is taken from, or ``None``."""
+ rel = PurePosixPath(file_info.path)
+ absolute = Path(getattr(file_info, "abs_path", "") or "")
+ if not (rel.parts and absolute.is_absolute()):
+ return None
+ if absolute.parts[-len(rel.parts) :] != rel.parts:
+ return None
+ return str(absolute.parents[len(rel.parts) - 1])
+
+
def scan_source_map(
parsed_files: Iterable[Any],
source_map: Mapping[str, bytes | str],
) -> tuple[dict[str, list[dict]], list[str]]:
"""Return ``(findings_by_file, scanned_paths)`` for ``replace_findings``.
- A path missing from *source_map* still gets the symbol-name scan.
+ A path missing from *source_map* still gets the symbol-name scan. Secret
+ findings drop in a path the repository's own secret-scanner config
+ allowlists; the root it is read from is the one the parsed files sit under.
"""
findings_by_file: dict[str, list[dict]] = {}
scanned_paths: list[str] = []
+ allowlisted: Callable[[str], bool] | None = None
for pf in parsed_files:
path = pf.file_info.path
+ if allowlisted is None:
+ allowlisted = secret_path_allowlist(_repo_root(pf.file_info))
raw = source_map.get(path, b"")
if isinstance(raw, (bytes, bytearray)):
source_text = raw.decode("utf-8", errors="replace")
@@ -1442,6 +1459,8 @@ def scan_source_map(
source_text = raw or ""
scanned_paths.append(path)
findings = scan_source(path, source_text, pf.symbols)
+ if findings and allowlisted(path):
+ findings = [f for f in findings if f["kind"] not in SECRET_KINDS]
if findings and IGNORE_TOKEN in source_text:
# The gate keeps a silenced finding to report it as suppressed; the
# stored scan is what the dashboard and agents read, so it drops it.
diff --git a/tests/unit/analysis/test_secret_allowlist.py b/tests/unit/analysis/test_secret_allowlist.py
new file mode 100644
index 000000000..a16b06841
--- /dev/null
+++ b/tests/unit/analysis/test_secret_allowlist.py
@@ -0,0 +1,90 @@
+"""``secret_allowlist``: the paths a repo's own ``.gitleaks.toml`` allowlists."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+from repowise.core.analysis.secret_allowlist import secret_path_allowlist
+from repowise.core.analysis.security_scan import scan_source_map
+
+KEY = "AKIA" + "QZXNRTVYWMPKLBHG"
+
+
+def _config(tmp_path, text: str):
+ (tmp_path / ".gitleaks.toml").write_text(text, encoding="utf-8")
+ return secret_path_allowlist(tmp_path)
+
+
+def test_global_allowlist_paths_are_unanchored_searches(tmp_path):
+ allowed = _config(
+ tmp_path,
+ "[allowlist]\npaths = ['''(^|/)cmd/generate/config/rules''', '''testdata''']\n",
+ )
+ assert allowed("cmd/generate/config/rules/aws.go")
+ assert allowed("pkg/testdata/key.txt")
+ assert not allowed("detect/detect.go")
+
+
+def test_allowlists_entries_honour_only_what_a_path_alone_allows(tmp_path):
+ allowed = _config(
+ tmp_path,
+ "[[allowlists]]\npaths = ['''^fixtures/''']\n"
+ "[[allowlists]]\ntargetRules = ['aws-access-token']\npaths = ['''^scoped/''']\n"
+ "[[allowlists]]\ncondition = 'AND'\nregexes = ['''x''']\npaths = ['''^anded/''']\n"
+ "[[allowlists]]\ncondition = 'AND'\npaths = ['''^paths_only/''']\n",
+ )
+ assert allowed("fixtures/a.go")
+ assert allowed("paths_only/a.go")
+ assert not allowed("scoped/a.go")
+ assert not allowed("anded/a.go")
+
+
+def test_the_legacy_table_skips_scoped_and_anded_entries_too(tmp_path):
+ scoped = "[allowlist]\ntargetRules = ['aws-access-token']\npaths = ['''^a/''']\n"
+ anded = "[allowlist]\ncondition = 'AND'\nstopwords = ['x']\npaths = ['''^a/''']\n"
+ assert not _config(tmp_path, scoped)("a/key.go")
+ assert not _config(tmp_path, anded)("a/key.go")
+
+
+def test_patterns_match_ascii_only_as_re2_does(tmp_path):
+ allowed = _config(tmp_path, "[allowlist]\npaths = ['''^\\w+/key\\.go$''']\n")
+ assert allowed("fixtures/key.go")
+ assert not allowed("fixturés/key.go")
+
+
+def test_a_posix_class_pattern_is_skipped(tmp_path):
+ # Python reads ``[[:alpha:]]`` as the set ``[:alph`` then a literal ``]``.
+ allowed = _config(tmp_path, "[allowlist]\npaths = ['''^[[:alpha:]]+/''', '''^ok/''']\n")
+ assert allowed("ok/x.go")
+ assert not allowed("a]/x.go")
+
+
+def test_use_default_brings_gitleaks_default_paths(tmp_path):
+ allowed = _config(tmp_path, "[extend]\nuseDefault = true\n")
+ assert allowed("config/gitleaks.toml")
+ assert not _config(tmp_path, "[extend]\nuseDefault = false\n")("config/gitleaks.toml")
+
+
+def test_no_config_a_broken_one_or_a_bad_pattern_allowlists_nothing_extra(tmp_path):
+ assert not secret_path_allowlist(tmp_path)("a.go")
+ assert not secret_path_allowlist(None)("a.go")
+ assert not _config(tmp_path, "[allowlist\npaths = 1")("a.go")
+ # Python rejects a mid-pattern global flag that RE2 accepts; the rest still apply.
+ allowed = _config(tmp_path, "[allowlist]\npaths = ['''^a/(?i)b''', '''^ok/''']\n")
+ assert allowed("ok/x.go") and not allowed("a/b.go")
+
+
+def _parsed(root, rel: str):
+ info = SimpleNamespace(path=rel, abs_path=str(root.joinpath(*rel.split("/"))))
+ return SimpleNamespace(file_info=info, symbols=[])
+
+
+def test_the_stored_scan_drops_secrets_in_an_allowlisted_path_only(tmp_path):
+ (tmp_path / ".gitleaks.toml").write_text(
+ "[allowlist]\npaths = ['''^rules/''']\n", encoding="utf-8"
+ )
+ source = f'key = "{KEY}"\nos.system(cmd)\n'
+ files = {"rules/sample.py": source, "app/deploy.py": source}
+ by_file, _ = scan_source_map([_parsed(tmp_path, p) for p in files], files)
+ assert [f["kind"] for f in by_file["rules/sample.py"]] == ["os_system"]
+ assert {f["kind"] for f in by_file["app/deploy.py"]} == {"aws_access_key", "os_system"}
diff --git a/tests/unit/analysis/test_security_gate.py b/tests/unit/analysis/test_security_gate.py
index 75dcd6489..88719e389 100644
--- a/tests/unit/analysis/test_security_gate.py
+++ b/tests/unit/analysis/test_security_gate.py
@@ -116,6 +116,22 @@ def test_a_document_reports_secrets_but_not_code_smells(repo):
assert [f["kind"] for f in _scan(repo).findings] == ["aws_access_key"]
+def test_a_gitleaks_allowlisted_path_drops_its_secrets_at_head_and_in_history(repo):
+ _write(repo, ".gitleaks.toml", "[allowlist]\npaths = ['''^rules/''']\n")
+ _write(repo, "rules/sample.py", f"AWS = '{KEY}'\nos.system(cmd)\n")
+ _write(repo, "rules/gone.py", f"AWS = '{KEY}'\n")
+ _write(repo, "deploy.py", f"AWS = '{KEY}'\n")
+ _commit(repo, "samples")
+ (repo / "rules" / "gone.py").unlink()
+ _commit(repo, "drop one")
+ scan = _scan(repo)
+ assert sorted((f["file_path"], f["kind"]) for f in scan.findings) == [
+ ("deploy.py", "aws_access_key"),
+ ("rules/sample.py", "os_system"),
+ ]
+ assert scan.suppressed == []
+
+
# ---------------------------------------------------------------------------
# Secrets inside the change
# ---------------------------------------------------------------------------
diff --git a/tests/unit/analysis/test_security_scan_history.py b/tests/unit/analysis/test_security_scan_history.py
index dac6f53b8..f0d061fd6 100644
--- a/tests/unit/analysis/test_security_scan_history.py
+++ b/tests/unit/analysis/test_security_scan_history.py
@@ -229,6 +229,7 @@ async def test_history_secrets_only_scan_skips_non_secret_patterns(session: Asyn
scanner._blob_introductions = lambda *a, **k: ( # type: ignore[assignment]
{"blob1": "abc123"},
{"abc123": "2026-01-01T00:00:00+00:00"},
+ {},
)
scanner._read_blobs_batch = lambda *a, **k: {"blob1": content} # type: ignore[assignment]
scanner._is_source = staticmethod(lambda p: True) # type: ignore[assignment]
@@ -260,6 +261,7 @@ async def test_history_unique_blob_scanned_once(session: AsyncSession) -> None:
"c1": "2026-01-01T00:00:00+00:00",
"c2": "2026-02-01T00:00:00+00:00",
},
+ {},
)
def _read(*a, **k):
@@ -281,7 +283,7 @@ async def test_history_progress_fires_for_clean_repo(session: AsyncSession) -> N
scanner = HistorySecurityScanner(session, "repo-1")
scanner._list_commits = lambda *a, **k: [("abc123", "2026-01-01T00:00:00+00:00")] # type: ignore[assignment]
scanner._unique_blobs = lambda *a, **k: {"blob1": "README.md"} # type: ignore[assignment]
- scanner._blob_introductions = lambda *a, **k: ({}, {}) # type: ignore[assignment]
+ scanner._blob_introductions = lambda *a, **k: ({}, {}, {}) # type: ignore[assignment]
scanner._read_blobs_batch = lambda *a, **k: {"blob1": "# docs\n"} # type: ignore[assignment]
scanner._is_source = staticmethod(lambda p: True) # type: ignore[assignment]
@@ -382,6 +384,30 @@ async def test_history_scan_on_real_repo_records_commit_provenance(
assert m["commit_at"] is not None, "introducing commit has no date"
+async def test_history_drops_secrets_in_a_gitleaks_allowlisted_path(
+ session: AsyncSession, secret_repo: Path
+) -> None:
+ (secret_repo / ".gitleaks.toml").write_text("[allowlist]\npaths = ['''^leak\\.py$''']\n")
+ await HistorySecurityScanner(session, "repo-1").scan_history(secret_repo, secrets_only=True)
+ assert not (await session.execute(text("SELECT 1 FROM security_findings"))).all()
+
+
+async def test_history_reports_a_blob_at_its_path_outside_the_allowlist(
+ session: AsyncSession, secret_repo: Path
+) -> None:
+ # One blob at two paths; git lists the allowlisted one first.
+ (secret_repo / ".gitleaks.toml").write_text("[allowlist]\npaths = ['''^(a_fixtures/|leak)''']\n")
+ secret = (secret_repo / "leak.py").read_text()
+ for folder in ("a_fixtures", "src"):
+ (secret_repo / folder).mkdir()
+ (secret_repo / folder / "creds.py").write_text(secret)
+ _git(secret_repo, "add", ".")
+ _git(secret_repo, "commit", "-m", "copy the key")
+ await HistorySecurityScanner(session, "repo-1").scan_history(secret_repo, secrets_only=True)
+ rows = (await session.execute(text("SELECT file_path FROM security_findings"))).all()
+ assert {r._mapping["file_path"] for r in rows} == {"src/creds.py"}
+
+
async def test_history_default_skips_test_fixtures_and_placeholders(
session: AsyncSession, secret_repo: Path
) -> None:
@@ -479,7 +505,7 @@ async def test_blob_introductions_resolves_real_blobs(secret_repo: Path) -> None
"""Every source blob rev-list reports must resolve to an introducing commit."""
scanner = HistorySecurityScanner.__new__(HistorySecurityScanner)
blobs = scanner._unique_blobs(secret_repo, None, None)
- intro, dates = scanner._blob_introductions(secret_repo, None, None)
+ intro, dates, _ = scanner._blob_introductions(secret_repo, None, None)
source = [sha for sha, path in blobs.items() if HistorySecurityScanner._is_source(path)]
assert source, "no source blobs found in the fixture repo"