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): add value gate, placeholder filter, and path awareness for credential findings (#2118) (#2121)

Rishabh Mishra2ae1128a038a
diff --git a/packages/core/src/repowise/core/analysis/history_scan.py b/packages/core/src/repowise/core/analysis/history_scan.py
index 32d5f342a..bddf8273b 100644
--- a/packages/core/src/repowise/core/analysis/history_scan.py
+++ b/packages/core/src/repowise/core/analysis/history_scan.py
@@ -44,7 +44,6 @@ from repowise.core.analysis.security_scan import (
SecurityScanner,
)
from repowise.core.ingestion.models import EXTENSION_TO_LANGUAGE
-from repowise.core.test_paths import is_test_related_path
def _run_git(repo_path: Path, args: list[str], *, timeout: float = 30.0) -> str:
@@ -348,17 +347,12 @@ class HistorySecurityScanner:
summary.blobs_scanned = len(blobs)
blob_introduced_at, commit_dates = self._blob_introductions(repo_path, since, to)
- # Test material is excluded from history mode, not merely down-ranked.
- # A committed secret matters because it leaked a live credential, and a
- # fixture is not one: on this repo 730 of 832 history hits (88%) were
- # `api_key="sk-test"` in provider unit tests. That ratio makes the whole
- # report untrustworthy, which is worse than reporting nothing.
- # `--all-patterns` lifts this along with the kind gate.
+ # 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.
source_items = [
(blob_sha, path)
for blob_sha, path in blobs.items()
- if (not path or self._is_source(path))
- and not (secrets_only and path and is_test_related_path(path))
+ if not path or self._is_source(path)
]
contents_map = self._read_blobs_batch(
repo_path, [blob_sha for blob_sha, _ in source_items]
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.py
index 4b1fcfe8d..e70c95a21 100644
--- a/packages/core/src/repowise/core/analysis/security_scan.py
+++ b/packages/core/src/repowise/core/analysis/security_scan.py
@@ -22,13 +22,68 @@ import ast
import logging
import re
from datetime import UTC, datetime
+from pathlib import PurePosixPath
from typing import Any
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
+from repowise.core.test_paths import is_test_related_path
+
logger = logging.getLogger(__name__)
+_CREDENTIAL_EXACT_PLACEHOLDERS: frozenset[str] = frozenset({"password", "changeit"})
+
+_CREDENTIAL_SUBSTRING_PLACEHOLDERS: tuple[str, ...] = (
+ "example",
+ "changeme",
+ "placeholder",
+ "dummy",
+ "sample",
+ "fake",
+ "xxx",
+ "your_",
+ "your-",
+ "...",
+ "fixture",
+)
+
+_LOW_SEVERITY_PATH_TOKENS: frozenset[str] = frozenset(
+ {
+ "test",
+ "tests",
+ "__tests__",
+ "__test__",
+ "fixtures",
+ "__fixtures__",
+ "spec",
+ "specs",
+ "mock",
+ "mocks",
+ "__mocks__",
+ "example",
+ "examples",
+ }
+)
+
+
+def _is_valid_credential_value(val: str) -> bool:
+ """True when *val* is at least 8 chars and not a known placeholder."""
+ if len(val) < 8:
+ return False
+ v = val.lower().strip()
+ if v.startswith("<") or v in _CREDENTIAL_EXACT_PLACEHOLDERS:
+ return False
+ return not any(p in v for p in _CREDENTIAL_SUBSTRING_PLACEHOLDERS)
+
+
+def _is_low_severity_path(file_path: str) -> bool:
+ """True when *file_path* is test material or under fixture, spec, mock, or example directories."""
+ posix_path = file_path.replace("\\", "/")
+ parts = [p.lower() for p in PurePosixPath(posix_path).parts]
+ return any(p in _LOW_SEVERITY_PATH_TOKENS for p in parts) or is_test_related_path(posix_path)
+
+
# ---------------------------------------------------------------------------
# Pattern registry: (compiled_pattern, kind_label, severity)
# ---------------------------------------------------------------------------
@@ -78,8 +133,8 @@ _PATTERNS: list[tuple[re.Pattern, str, str]] = [
# concatenating these patterns' *source text*, which drops per-pattern
# flags. A flag here would leave the prefilter case-sensitive and it would
# reject the line before the pattern ever ran.
- (re.compile(r"(?i:password)\s*=\s*['\"]"), "hardcoded_password", "high"),
- (re.compile(r"(?i:api_?key|secret)\s*=\s*['\"]"), "hardcoded_secret", "high"),
+ (re.compile(r"(?i:password)\s*=\s*['\"]([^'\"]*)"), "hardcoded_password", "high"),
+ (re.compile(r"(?i:api_?key|secret)\s*=\s*['\"]([^'\"]*)"), "hardcoded_secret", "high"),
(re.compile(r'f[\'"].*SELECT.*\{.*\}'), "fstring_sql", "med"),
(re.compile(r"\.execute\(\s*[\'\"]\s*SELECT.*\+"), "concat_sql", "med"),
(re.compile(r"verify\s*=\s*False"), "tls_verify_false", "med"),
@@ -373,13 +428,21 @@ class SecurityScanner:
findings.extend(_call_findings(file_path, source))
# Line-by-line pattern scan
+ is_low_sev_file = _is_low_severity_path(file_path)
for lineno, line in enumerate(lines, start=1):
if not _ANY_PATTERN.search(line):
continue
for pattern, kind, severity in _PATTERNS:
if kind in _CALL_KINDS:
continue
- if pattern.search(line):
+ match = pattern.search(line)
+ if match:
+ if kind in SECRET_KINDS:
+ val = match.group(1) if match.groups() else ""
+ if not _is_valid_credential_value(val):
+ continue
+ if is_low_sev_file:
+ severity = "low"
# Trim snippet to keep it concise
snippet = line.strip()[:120]
findings.append(
diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.py
index 69467f0ce..6817e2df5 100644
--- a/tests/unit/analysis/test_security_scan.py
+++ b/tests/unit/analysis/test_security_scan.py
@@ -21,7 +21,7 @@ from repowise.core.analysis.security_scan import SecurityScanner
SNIPPY = b"""import pickle
-password = 'hunter2'
+password = 'super_secret_password_123'
data = pickle.loads(blob)
x = eval(user_input)
"""
@@ -160,10 +160,10 @@ class TestScanFile:
@pytest.mark.parametrize(
"source",
[
- 'const API_KEY = "abc123";\n',
- "const N8N_API_KEY = 'abc123';\n",
- 'SECRET = "abc123"\n',
- 'PASSWORD = "abc123"\n',
+ 'const API_KEY = "abc123456789";\n',
+ "const N8N_API_KEY = 'abc123456789';\n",
+ 'SECRET = "abc123456789"\n',
+ 'PASSWORD = "abc123456789"\n',
],
)
def test_secret_patterns_are_case_insensitive(self, source: str) -> None:
@@ -180,6 +180,62 @@ class TestScanFile:
findings = asyncio.run(scanner.scan_file("config.ts", source, symbols=[]))
assert {row["kind"] for row in findings} & {"hardcoded_secret", "hardcoded_password"}
+ @pytest.mark.parametrize(
+ "source",
+ [
+ 'password = ""\n',
+ "password = 'short'\n",
+ 'password = "hunter2"\n',
+ 'api_key = "1234567"\n',
+ ],
+ )
+ def test_credential_value_length_gate(self, source: str) -> None:
+ """Credentials shorter than 8 characters yield no findings."""
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ findings = asyncio.run(scanner.scan_file("config.py", source, symbols=[]))
+ assert not any(f["kind"] in {"hardcoded_password", "hardcoded_secret"} for f in findings)
+
+ @pytest.mark.parametrize(
+ "source",
+ [
+ 'password = "dummy_secret_value_123"\n',
+ 'password = "changeme_secret_123"\n',
+ 'password = "changeit"\n',
+ 'password = "password"\n',
+ 'password = "placeholder_secret"\n',
+ 'api_key = "sk-..."\n',
+ 'api_key = "your-api-key-here"\n',
+ 'api_key = "<YOUR_API_KEY>"\n',
+ 'password = "fixture-value"\n',
+ ],
+ )
+ def test_credential_placeholder_filtering(self, source: str) -> None:
+ """Values containing placeholder tokens yield no findings."""
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ findings = asyncio.run(scanner.scan_file("config.py", source, symbols=[]))
+ assert not any(f["kind"] in {"hardcoded_password", "hardcoded_secret"} for f in findings)
+
+ @pytest.mark.parametrize(
+ "file_path",
+ [
+ "tests/unit/test_auth.py",
+ "tests\\unit\\test_auth.py",
+ "test_main.py",
+ "fixtures/keys.py",
+ "spec/helpers.rb",
+ "mocks/auth_mock.ts",
+ "examples/demo.py",
+ ],
+ )
+ def test_credential_path_severity_downgrade(self, file_path: str) -> None:
+ """Valid credentials in test/fixture paths are downgraded to low severity."""
+ 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(file_path, source, symbols=[]))
+ hits = [f for f in findings if f["kind"] in {"hardcoded_password", "hardcoded_secret"}]
+ assert len(hits) == 1
+ assert hits[0]["severity"] == "low"
+
def test_single_line_subprocess_shell_true_is_flagged(self) -> None:
scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
source = 'subprocess.run("rm -rf " + path, shell=True)\n'
diff --git a/tests/unit/analysis/test_security_scan_history.py b/tests/unit/analysis/test_security_scan_history.py
index 86465c773..8ffb3d602 100644
--- a/tests/unit/analysis/test_security_scan_history.py
+++ b/tests/unit/analysis/test_security_scan_history.py
@@ -174,7 +174,7 @@ async def test_history_gate_excludes_code_smells_by_default(session: AsyncSessio
async def test_history_secrets_only_scan_skips_non_secret_patterns(session: AsyncSession) -> None:
"""scan_history with defaults only persists secret findings."""
- content = "password = 'hunter2'\neval(open('x'))\nos.system('ls')\n"
+ content = "password = 'super_secret_password_123'\neval(open('x'))\nos.system('ls')\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": "src/secret.py"} # type: ignore[assignment]
@@ -197,7 +197,7 @@ async def test_history_secrets_only_scan_skips_non_secret_patterns(session: Asyn
async def test_history_unique_blob_scanned_once(session: AsyncSession) -> None:
"""Identical content across two commits is scanned once, attributed to first."""
- content = "api_key = 'LEAKED'\n"
+ content = "api_key = 'LEAKED_SECRET_KEY_12345'\n"
scanner = HistorySecurityScanner(session, "repo-1")
reads = {"n": 0}
@@ -347,7 +347,9 @@ async def test_history_default_skips_test_fixtures_and_placeholders(
async def test_all_patterns_lifts_the_noise_gate(session: AsyncSession, secret_repo: Path) -> None:
"""The filtering is the default's promise, not a hard exclusion."""
(secret_repo / "tests").mkdir(exist_ok=True)
- (secret_repo / "tests" / "test_client.py").write_text('password = "fixture-value"\n')
+ (secret_repo / "tests" / "test_client.py").write_text(
+ 'password = "super_secret_real_password_99"\n'
+ )
_git(secret_repo, "add", ".")
_git(secret_repo, "commit", "-m", "add fixture")
diff --git a/tests/unit/test_no_bare_type_name_copies.py b/tests/unit/test_no_bare_type_name_copies.py
index 2fc6b3d29..bc4a81fa5 100644
--- a/tests/unit/test_no_bare_type_name_copies.py
+++ b/tests/unit/test_no_bare_type_name_copies.py
@@ -98,6 +98,7 @@ _KNOWN: dict[str, int] = {
# prefix to resolve the package it came from. Neither wants a type.
_PREFIX + "framework_edges/rust.py": 1,
_PREFIX + "framework_edges/go.py": 1,
+ _PREFIX + "framework_edges/flask.py": 1,
# Takes the qualifier HEAD of `OrderHandlers.GetOrder` to reach the
# declaring type. It does want a type, but the shared helper returns the
# trailing segment, which is the member — the opposite end, the same reason