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): mask secret values in security finding snippets (#2375)

Akshat malikf0342511228a
diff --git a/packages/core/alembic/versions/0067_mask_credential_snippets.py b/packages/core/alembic/versions/0067_mask_credential_snippets.py
new file mode 100644
index 000000000..20778d36e
--- /dev/null
+++ b/packages/core/alembic/versions/0067_mask_credential_snippets.py
@@ -0,0 +1,52 @@
+"""Clear plaintext secret values from existing credential findings.
+
+Working-tree scans (``SecurityScanner.scan_file``) now mask the captured secret
+value in the snippet before persisting. Existing rows pre-dating this change
+may contain the raw plaintext value. This migration clears ``snippet`` to an
+empty string for every row whose ``kind`` is a credential kind
+(``hardcoded_password`` or ``hardcoded_secret``). The next working-tree scan
+will repopulate those rows with a masked snippet via ``replace_findings``.
+History rows (``commit_sha`` non-empty) are also cleared: a future
+``repowise security scan --history`` re-run will repopulate them masked too.
+
+No data is truly lost: the snippet is a 120-character excerpt of the matched
+line, and the line is still present in the repository. The clearing is
+irreversible in this migration intentionally — the downgrade path would
+re-expose plaintext secrets, which is exactly the problem being fixed.
+
+Revision ID: 0067
+Revises: 0066
+Create Date: 2026-09-18
+"""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers
+revision: str = "0067"
+down_revision: str | None = "0066"
+branch_labels: str | Sequence[str] | None = None
+depends_on: str | Sequence[str] | None = None
+
+
+def upgrade() -> None:
+ bind = op.get_bind()
+ bind.execute(
+ sa.text(
+ "UPDATE security_findings SET snippet = '' "
+ "WHERE kind IN ('hardcoded_password', 'hardcoded_secret')"
+ )
+ )
+
+
+def downgrade() -> None:
+ """Intentionally a no-op.
+
+ Re-exposing plaintext secret values in the snippet column in order to
+ reverse this migration would defeat its purpose. Downgrade is therefore
+ not supported.
+ """
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.py
index e70c95a21..0ac127e1b 100644
--- a/packages/core/src/repowise/core/analysis/security_scan.py
+++ b/packages/core/src/repowise/core/analysis/security_scan.py
@@ -228,6 +228,46 @@ SECRET_KINDS: frozenset[str] = frozenset({"hardcoded_password", "hardcoded_secre
SYMBOL_NAME_KINDS: frozenset[str] = frozenset({"security_sensitive_symbol"})
+def _mask_secret_snippet(snippet: str, val: str) -> str:
+ """Replace the first occurrence of *val* inside *snippet* with a redacted form.
+
+ Keeps the variable name and surrounding text intact so the finding remains
+ locatable in the source file. The replacement is ``val[:4] + '****'``
+ (first four characters visible, everything else hidden). If the value is
+ shorter than four characters, the whole value is replaced with ``'****'``.
+
+ Examples
+ --------
+ >>> _mask_secret_snippet("password = 'super_secret_pass_99'", "super_secret_pass_99")
+ "password = 'supe****'"
+ """
+ if not val:
+ return snippet
+ redacted = (val[:4] + "****") if len(val) >= 4 else "****"
+ return snippet.replace(val, redacted, 1)
+
+
+def _mask_findings(findings: list[dict]) -> None:
+ """Redact the captured secret value from every credential finding's snippet.
+
+ Operates in-place on *findings*. Only kinds listed in ``SECRET_KINDS`` are
+ touched; every other kind (code-smell patterns, symbol names, spanning
+ patterns not in SECRET_KINDS) is left unchanged.
+
+ This is called as a final post-pass inside ``scan_file`` before returning,
+ so it covers the per-line loop, the spanning-pattern loop, and the
+ ``HistorySecurityScanner`` path in one place.
+ """
+ for finding in findings:
+ if finding.get("kind") not in SECRET_KINDS:
+ continue
+ val: str = finding.get("_secret_val", "")
+ if val:
+ finding["snippet"] = _mask_secret_snippet(finding.get("snippet", ""), val)
+ # Remove the internal key — it must never leave this module.
+ del finding["_secret_val"]
+
+
def _mask_comments_and_strings(source: str) -> str:
"""Blank common comments/strings while preserving offsets and newlines."""
chars = list(source)
@@ -443,6 +483,19 @@ class SecurityScanner:
continue
if is_low_sev_file:
severity = "low"
+ # Trim snippet to keep it concise
+ snippet = line.strip()[:120]
+ findings.append(
+ {
+ "kind": kind,
+ "severity": severity,
+ "snippet": snippet,
+ "line": lineno,
+ # Internal: consumed by _mask_findings, never persisted.
+ "_secret_val": val,
+ }
+ )
+ continue
# Trim snippet to keep it concise
snippet = line.strip()[:120]
findings.append(
@@ -492,6 +545,12 @@ class SecurityScanner:
}
)
+ # Mask secret values before any finding leaves this function.
+ # A single post-pass keyed on SECRET_KINDS covers the per-line loop,
+ # the spanning-pattern loop above, and HistorySecurityScanner (which
+ # calls this same method), so every present and future snippet site is
+ # covered by construction.
+ _mask_findings(findings)
return findings
def _uses_sqlite(self) -> bool:
diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.py
index 6817e2df5..8b76a6ac0 100644
--- a/tests/unit/analysis/test_security_scan.py
+++ b/tests/unit/analysis/test_security_scan.py
@@ -17,7 +17,7 @@ from types import SimpleNamespace
import pytest
from sqlalchemy import text
-from repowise.core.analysis.security_scan import SecurityScanner
+from repowise.core.analysis.security_scan import SecurityScanner, _mask_secret_snippet
SNIPPY = b"""import pickle
@@ -78,6 +78,15 @@ class TestScanFile:
assert "eval_call" in kinds
by_kind = {f["kind"]: f for f in findings}
assert by_kind["hardcoded_password"]["line"] == 3
+ # The raw secret value must NOT appear in the snippet.
+ pw_snippet = by_kind["hardcoded_password"]["snippet"]
+ assert "super_secret_password_123" not in pw_snippet, (
+ f"secret value leaked into snippet: {pw_snippet!r}"
+ )
+ # The masked form must be present.
+ assert "supe****" in pw_snippet, (
+ f"expected masked form 'supe****' in snippet: {pw_snippet!r}"
+ )
def test_clean_source_yields_nothing(self) -> None:
scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
@@ -516,3 +525,113 @@ class TestPersistSecurityFindings:
("a.py", "hardcoded_password", 1),
("a.py", "security_sensitive_symbol", 7),
]
+
+
+class TestSecretMasking:
+ """Credential snippets must be masked; non-credential snippets must not."""
+
+ # ------------------------------------------------------------------ #
+ # _mask_secret_snippet — pure-function unit tests #
+ # ------------------------------------------------------------------ #
+
+ @pytest.mark.parametrize(
+ ("snippet", "val", "expected"),
+ [
+ # Normal case: first 4 chars kept, rest replaced.
+ ("password = 'super_secret_pass_99'", "super_secret_pass_99", "password = 'supe****'"),
+ # Short-ish value: still 4 chars kept.
+ ("api_key = 'abcd1234'", "abcd1234", "api_key = 'abcd****'"),
+ # Value exactly 4 chars: keep all 4, append ****.
+ ("secret = 'abcd'", "abcd", "secret = 'abcd****'"),
+ # Value shorter than 4 chars: replace entirely.
+ ("secret = 'abc'", "abc", "secret = '****'"),
+ # Empty val: snippet unchanged.
+ ("password = 'something'", "", "password = 'something'"),
+ ],
+ )
+ def test_mask_secret_snippet_helper(
+ self, snippet: str, val: str, expected: str
+ ) -> None:
+ assert _mask_secret_snippet(snippet, val) == expected
+
+ # ------------------------------------------------------------------ #
+ # scan_file — credential kinds are masked #
+ # ------------------------------------------------------------------ #
+
+ @pytest.mark.parametrize(
+ ("source", "kind", "raw_val"),
+ [
+ (
+ "password = 'super_secret_password_123'\n",
+ "hardcoded_password",
+ "super_secret_password_123",
+ ),
+ (
+ 'API_KEY = "abc123456789"\n',
+ "hardcoded_secret",
+ "abc123456789",
+ ),
+ (
+ 'SECRET = "abcd1234"\n',
+ "hardcoded_secret",
+ "abcd1234",
+ ),
+ ],
+ )
+ def test_credential_snippet_is_masked(
+ self, source: str, kind: str, raw_val: str
+ ) -> None:
+ """The plaintext secret value must not appear in any snippet."""
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ findings = asyncio.run(scanner.scan_file("config.py", source, symbols=[]))
+ hits = [f for f in findings if f["kind"] == kind]
+ assert hits, f"no finding of kind {kind!r} produced"
+ for hit in hits:
+ assert raw_val not in hit["snippet"], (
+ f"plaintext secret leaked into snippet for {kind!r}: {hit['snippet']!r}"
+ )
+ # Masked form must be present: first 4 chars + ****.
+ expected_mask = raw_val[:4] + "****"
+ assert expected_mask in hit["snippet"], (
+ f"expected masked form {expected_mask!r} not found in {hit['snippet']!r}"
+ )
+
+ def test_credential_finding_has_no_internal_secret_val_key(self) -> None:
+ """The internal ``_secret_val`` key must be consumed by _mask_findings
+ and must never appear in a returned finding."""
+ source = "password = 'super_secret_password_123'\n"
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ findings = asyncio.run(scanner.scan_file("config.py", source, symbols=[]))
+ for f in findings:
+ assert "_secret_val" not in f, (
+ f"internal key '_secret_val' leaked out of scan_file: {f}"
+ )
+
+ # ------------------------------------------------------------------ #
+ # scan_file — non-credential kinds are NOT masked #
+ # ------------------------------------------------------------------ #
+
+ def test_non_credential_snippet_is_not_masked(self) -> None:
+ """eval_call and pickle_loads snippets must be the raw matched line."""
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ findings = asyncio.run(scanner.scan_file("a.py", SNIPPY.decode(), symbols=[]))
+ by_kind = {f["kind"]: f for f in findings}
+ # eval_call snippet should contain the raw text.
+ assert "eval" in by_kind["eval_call"]["snippet"]
+ assert "****" not in by_kind["eval_call"]["snippet"]
+ # pickle_loads snippet should contain the raw text.
+ assert "pickle" in by_kind["pickle_loads"]["snippet"]
+ assert "****" not in by_kind["pickle_loads"]["snippet"]
+
+ def test_severity_downgrade_still_masks_credential(self) -> None:
+ """Even when severity is downgraded (test-path), the snippet is masked."""
+ source = "password = 'super_secret_real_password_99'\n"
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ findings = asyncio.run(
+ scanner.scan_file("tests/unit/test_auth.py", source, symbols=[])
+ )
+ hits = [f for f in findings if f["kind"] == "hardcoded_password"]
+ assert hits, "finding must still fire on test path (just downgraded)"
+ assert hits[0]["severity"] == "low"
+ assert "super_secret_real_password_99" not in hits[0]["snippet"]
+ assert "supe****" in hits[0]["snippet"]