Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(security): honor detect-secrets allowlist pragmas (#3246)
Emre K.03f03f57fbb0
diff --git a/docs/layers/SECURITY.md b/docs/layers/SECURITY.mdindex 7cb5803a8..3ae3615b4 100644--- a/docs/layers/SECURITY.md+++ b/docs/layers/SECURITY.md@@ -193,6 +193,14 @@ lists. There is no next-line or whole-file form. A silenced finding never failsthe gate and is still reported as suppressed. Exact rules are in the[CLI reference](../reference/CLI_REFERENCE.md#repowise-security-check-revspec).+The scanner also honours detect-secrets comments: `# pragma: allowlist secret`+on the finding's line, or `# pragma: allowlist nextline secret` on the line+above it (`//` and `/*` comments work too). Unlike the Repowise gate-only+marker, allowlisted secret findings are excluded from the stored scan and+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.+### Baselines`--write-baseline FILE` records the change's findings in a committed JSON filediff --git a/packages/core/src/repowise/core/analysis/security_gate.py b/packages/core/src/repowise/core/analysis/security_gate.pyindex f981b2e84..14f28429d 100644--- a/packages/core/src/repowise/core/analysis/security_gate.py+++ b/packages/core/src/repowise/core/analysis/security_gate.py@@ -41,7 +41,14 @@ from repowise.core.support_paths import DOC_EXTENSIONSfrom .change_risk.features import GIT_TIMEOUT_SECONDS, _git, revspec_head, split_revspecfrom .changed_lines import DIFF_ARGS, parse_unified_diff-from .security_scan import SECRET_KINDS, masked_snippet, scan_source, source_lines+from .security_scan import (+ SECRET_KINDS,+ _detect_secrets_allowlisted,+ _mask_comments_and_strings,+ masked_snippet,+ scan_source,+ source_lines,+)#: Severities from least to most severe; ``--fail-on`` names the lowest that fails.SEVERITIES = ("low", "med", "high")@@ -440,7 +447,17 @@ def _scoped(if (entry := _builtin_entry(text, f, lines, secrets_only)) is not None]custom, long_lines = _custom_entries(text, lines)- return [_row(text, entry) for entry in [*found, *custom]], long_lines+ uncommented = (+ source_lines(_mask_comments_and_strings(source, strings=False))+ if "pragma: allowlist" in source+ else text.lines+ )+ return [+ _row(text, entry)+ for entry in [*found, *custom]+ if (entry[1] not in SECRET_KINDS and not entry[1].startswith("custom:"))+ or not _detect_secrets_allowlisted(text.lines, uncommented, entry[0])+ ], long_lines#: ``(line, kind, severity, snippet shown, fingerprint)``diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.pyindex 6d5ddaf7f..44b4b42e3 100644--- a/packages/core/src/repowise/core/analysis/security_scan.py+++ b/packages/core/src/repowise/core/analysis/security_scan.py@@ -43,6 +43,30 @@ logger = logging.getLogger(__name__)# happens to edit.SECURITY_SCANNER_VERSION = 2+# detect-secrets allows a marker on the finding's line or the line above.+_ALLOWLIST_SAME_RE = re.compile(r"(?:#|//|/\*)\s*pragma:\s*allowlist\s+secret\b")+_ALLOWLIST_NEXT_RE = re.compile(+ r"(?:#|//|/\*)\s*pragma:\s*allowlist\s+nextline\s+secret\b"+)+++def _detect_secrets_allowlisted(+ lines: list[str], uncommented: list[str], lineno: int+) -> bool:+ """Whether a pragma in a comment (not a string) allowlists this line."""++ def marked(index: int, pattern: re.Pattern[str]) -> bool:+ return any(+ uncommented[index][m.start() : m.end()] != m.group()+ for m in pattern.finditer(lines[index])+ )++ return 0 < lineno <= len(lines) and (+ marked(lineno - 1, _ALLOWLIST_SAME_RE)+ or (lineno > 1 and marked(lineno - 2, _ALLOWLIST_NEXT_RE))+ )++_CREDENTIAL_EXACT_PLACEHOLDERS: frozenset[str] = frozenset({"password", "changeit"})_CREDENTIAL_SUBSTRING_PLACEHOLDERS: tuple[str, ...] = (@@ -1000,7 +1024,16 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis})- return findings+ uncommented = (+ source_lines(_mask_comments_and_strings(source, strings=False))+ if "pragma: allowlist" in source+ else lines+ )+ return [+ f for f in findings+ if f["kind"] not in SECRET_KINDS+ or not _detect_secrets_allowlisted(lines, uncommented, f["line"])+ ]def scan_source_map(diff --git a/tests/unit/analysis/test_security_gate_extras.py b/tests/unit/analysis/test_security_gate_extras.pyindex 28ca79270..3d15152eb 100644--- a/tests/unit/analysis/test_security_gate_extras.py+++ b/tests/unit/analysis/test_security_gate_extras.py@@ -96,6 +96,24 @@ def test_a_bare_marker_silences_every_kind_on_its_line(repo):(suppressed,) = scan.suppressedassert (suppressed["kind"], suppressed["line_number"]) == ("aws_access_key", 1)+def test_detect_secrets_pragmas_do_not_enter_gate_or_suppressed_report(repo):+ _write(+ repo,+ "cfg.py",+ f"AWS = '{KEY}'; eval(payload) # pragma: allowlist secret\n"+ "# pragma: allowlist nextline secret\n"+ f"AWS = '{KEY}'\n"+ f"AWS = '{KEY}'\n",+ )+ _commit(repo, "add")+ scan = _scan(repo, patterns=(_pattern(regex=r"AKIA[A-Z]{16}"),))+ assert [(f["kind"], f["line_number"]) for f in scan.findings] == [+ ("eval_call", 1),+ ("aws_access_key", 4),+ ("custom:internal_token", 4),+ ]+ assert scan.suppressed == []+def test_a_kind_scoped_marker_silences_only_the_kinds_it_names(repo):_write(diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.pyindex 25def5de4..b7f8c09db 100644--- a/tests/unit/analysis/test_security_scan.py+++ b/tests/unit/analysis/test_security_scan.py@@ -99,6 +99,32 @@ class TestScanFile:findings = asyncio.run(scanner.scan_file("b.py", CLEAN.decode(), symbols=[]))assert findings == []+ @pytest.mark.parametrize(+ "allowed",+ [+ 'password = "actualcredential123" # pragma: allowlist secret\n',+ '# pragma: allowlist nextline secret\npassword = "actualcredential123"\n',+ ],+ )+ def test_detect_secrets_pragmas_exclude_findings_before_storage(self, allowed: str) -> None:+ source = allowed + 'password = "anothercredential456"\n'+ findings = scan_source("config.py", source)+ assert [(f["kind"], f["line"]) for f in findings] == [+ ("hardcoded_password", len(allowed.splitlines()) + 1)+ ]++ def test_detect_secrets_pragma_inside_a_string_does_not_allowlist(self) -> None:+ source = 'password = "actualcredential123 # pragma: allowlist secret"\n'+ assert [(f["kind"], f["line"]) for f in scan_source("config.py", source)] == [+ ("hardcoded_password", 1)+ ]++ def test_detect_secrets_pragma_does_not_hide_non_secret_calls(self) -> None:+ source = 'password = "actualcredential123"; eval(payload) # pragma: allowlist secret\n'+ assert [(f["kind"], f["line"]) for f in scan_source("config.py", source)] == [+ ("eval_call", 1)+ ]+@pytest.mark.parametrize(("source", "expected"),[