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 allow markers in prose without a comment opener (#4143)

Aman10838d3134b7
diff --git a/packages/core/src/repowise/core/analysis/security/suppression.py b/packages/core/src/repowise/core/analysis/security/suppression.py
index 17ff1835a..e68929117 100644
--- a/packages/core/src/repowise/core/analysis/security/suppression.py
+++ b/packages/core/src/repowise/core/analysis/security/suppression.py
@@ -11,13 +11,20 @@ from .paths import _PROSE_EXTENSIONS
# finding's line: detect-secrets, gitleaks, TruffleHog, Bandit and gosec
# ``nosec``, and Ruff's hard-coded-password codes. detect-secrets also has a
# marker for the line below.
-_ALLOWLIST_SAME_RE = re.compile(
- r"(?:#|//|/\*)[ \t]*(?:pragma:[ \t]*allowlist[ \t]+secret|gitleaks:allow|trufflehog:ignore"
+_SAME_MARKER = (
+ r"(?:pragma:[ \t]*allowlist[ \t]+secret|gitleaks:allow|trufflehog:ignore"
r"|nosec|noqa:[ \t\w,]*\bS10[5-7])\b"
)
-_ALLOWLIST_NEXT_RE = re.compile(
- r"(?:#|//|/\*)[ \t]*pragma:[ \t]*allowlist[ \t]+nextline[ \t]+secret\b"
-)
+_NEXT_MARKER = r"pragma:[ \t]*allowlist[ \t]+nextline[ \t]+secret\b"
+# Code: a marker only counts inside a real comment, so it needs an opener.
+_COMMENT_OPENER = r"(?:#|//|/\*)[ \t]*"
+# Prose (Markdown, reStructuredText, plain text) has no single comment syntax, so a
+# marker counts anywhere on its line, as long as it is not the tail of a longer word.
+_PROSE_LEAD = r"(?<![\w-])"
+_ALLOWLIST_SAME_RE = re.compile(_COMMENT_OPENER + _SAME_MARKER)
+_ALLOWLIST_NEXT_RE = re.compile(_COMMENT_OPENER + _NEXT_MARKER)
+_ALLOWLIST_SAME_PROSE_RE = re.compile(_PROSE_LEAD + _SAME_MARKER)
+_ALLOWLIST_NEXT_PROSE_RE = re.compile(_PROSE_LEAD + _NEXT_MARKER)
_ALLOWLIST_TOKENS = ("allowlist", "gitleaks:allow", "trufflehog:ignore", "nosec", "noqa:")
@@ -33,8 +40,13 @@ def _allowlisted_lines(file_path: str, source: str) -> set[int]:
if file_path.lower().endswith(_PROSE_EXTENSIONS)
else mask_comments_and_strings(source, strings=False, **mask_dialect(file_path))
)
+ patterns = (
+ ((_ALLOWLIST_SAME_RE, 0), (_ALLOWLIST_NEXT_RE, 1))
+ if uncommented is not None
+ else ((_ALLOWLIST_SAME_PROSE_RE, 0), (_ALLOWLIST_NEXT_PROSE_RE, 1))
+ )
allowed: set[int] = set()
- for pattern, below in ((_ALLOWLIST_SAME_RE, 0), (_ALLOWLIST_NEXT_RE, 1)):
+ for pattern, below in patterns:
for m in pattern.finditer(source):
if uncommented is None or uncommented[m.start() : m.end()] != m.group():
allowed.add(source.count("\n", 0, m.start()) + 1 + below)
diff --git a/tests/unit/analysis/golden/security_scan_corpus.json b/tests/unit/analysis/golden/security_scan_corpus.json
index 966c40175..c996f81b6 100644
--- a/tests/unit/analysis/golden/security_scan_corpus.json
+++ b/tests/unit/analysis/golden/security_scan_corpus.json
@@ -602,12 +602,6 @@
"allowlist_prose": {
"path": "docs/keys.md",
"findings": [
- {
- "kind": "aws_access_key",
- "severity": "high",
- "snippet": "key AKIA**** gitleaks:allow",
- "line": 1
- },
{
"kind": "github_token",
"severity": "high",
diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.py
index 6fc01bcd8..65dd26887 100644
--- a/tests/unit/analysis/test_security_scan.py
+++ b/tests/unit/analysis/test_security_scan.py
@@ -1619,6 +1619,27 @@ class TestNoAlarmOnCodeThatDoesNotShip:
source = f'key = "{self.KEY}" {marker}\n'
assert self._found(path, source) == []
+ @pytest.mark.parametrize(
+ ("path", "source"),
+ [
+ ("README.md", "key {key} pragma: allowlist secret\n"),
+ ("README.md", "key {key} <!-- nosec -->\n"),
+ ("README.md", "key {key} # pragma: allowlist secret\n"),
+ ("docs/guide.rst", "key {key} nosec\n"),
+ ("README.md", "pragma: allowlist nextline secret\nkey {key}\n"),
+ ],
+ )
+ def test_prose_allow_markers_need_no_comment_opener(self, path: str, source: str) -> None:
+ assert self._found(path, source.format(key=self.KEY)) == []
+
+ def test_a_word_containing_a_marker_does_not_silence_prose(self) -> None:
+ source = f"key {self.KEY} nosecurity\n"
+ assert self._found("README.md", source) == [("aws_access_key", "high")]
+
+ def test_a_bare_marker_outside_a_comment_still_fires_in_code(self) -> None:
+ source = f'key = "{self.KEY}" nosec\n'
+ assert self._found("src/settings.py", source) == [("aws_access_key", "high")]
+
@pytest.mark.parametrize(
"source",
[