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): ignore documented placeholder credentials (#3179)
abhi26931f62790a26
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.pyindex 3592ba489..d2222dd7f 100644--- a/packages/core/src/repowise/core/analysis/security_scan.py+++ b/packages/core/src/repowise/core/analysis/security_scan.py@@ -41,7 +41,7 @@ logger = logging.getLogger(__name__)# the same way ``HEALTH_ANALYZER_VERSION`` forces a full health re-score# (#3072). Without this, a scanner fix only ever reaches a file a user# happens to edit.-SECURITY_SCANNER_VERSION = 1+SECURITY_SCANNER_VERSION = 2_CREDENTIAL_EXACT_PLACEHOLDERS: frozenset[str] = frozenset({"password", "changeit"})@@ -93,6 +93,8 @@ def _is_valid_credential_value(val: str) -> bool:v = val.lower().strip()if v.startswith("<") or _ANGLE_SLOT.search(v) or v in _CREDENTIAL_EXACT_PLACEHOLDERS:return False+ if len(set(v)) == 1 and not v.isalnum():+ return Falsereturn not any(p in v for p in _CREDENTIAL_SUBSTRING_PLACEHOLDERS)@@ -367,21 +369,47 @@ _KEYWORD_KINDS: frozenset[str] = frozenset({"hardcoded_password", "hardcoded_sec# is a key's or marker's name, a constant holding its own name, and a template# placeholder or shell substitution is filled in when it runs; none of them is a# credential. Letters only: a digit or capital makes it look like a key.-_KEY_NAME_VALUE = re.compile(r"[a-z]+(?:[_-][a-z]+)+|[a-z_]*_[a-z_]*")_TEMPLATE_VALUE = re.compile(r"\{\{.*\}\}|\$\{[^}]*\}|\$\(.*\)")+_SCHEME_TEMPLATE_VALUE = re.compile(+ r"(?i:(?:bearer|basic|token))\s+(?:\{\{.*\}\}|\$\{[^}]*\}|\$\(.*\))"+)+_OWN_NAME_VALUE = re.compile(r"[A-Za-z_-]+")+_KEY_NAME_VALUE = re.compile(r"[a-z]+(?:[_-][a-z]+)+|[a-z_]*_[a-z_]*")+++def _is_own_name_value(value: str, name: str) -> bool:+ """Whether a letter-only constant holds its own name or a suffix of it."""+ if not _OWN_NAME_VALUE.fullmatch(value):+ return False+ normalized_name = re.sub(r"[_-]", "", name).lower()+ normalized_value = re.sub(r"[_-]", "", value).lower()+ return bool(normalized_name) and normalized_name.endswith(normalized_value)-def _is_secret_value(kind: str, val: str) -> bool:+def _assignment_name(prefix: str) -> str:+ """Name assigned to the literal immediately following *prefix*."""+ assignment = re.search(r"([A-Za-z_][A-Za-z0-9_-]*)\s*=\s*['\"]?$", prefix)+ return assignment.group(1) if assignment else ""+++def _is_secret_value(kind: str, val: str, name: str = "") -> bool:"""True when *val*, captured by a *kind* pattern, looks like a real credential."""if not _is_valid_credential_value(val):return Falseif kind not in _KEYWORD_KINDS:return True- if kind == "hardcoded_secret" and _KEY_NAME_VALUE.fullmatch(val):- return Falsevalue = val.strip()+ if kind == "hardcoded_secret" and _KEY_NAME_VALUE.fullmatch(value):+ return False+ if _is_own_name_value(value, name):+ return False# A leading ``--`` is a CSS custom property or a CLI flag, not a key.- return not (_TEMPLATE_VALUE.fullmatch(value) or _is_plain_word(value) or value.startswith("--"))+ return not (+ _TEMPLATE_VALUE.fullmatch(value)+ or _SCHEME_TEMPLATE_VALUE.fullmatch(value)+ or _is_plain_word(value)+ or value.startswith("--")+ )def _is_plain_word(value: str) -> bool:@@ -390,8 +418,14 @@ def _is_plain_word(value: str) -> bool:Bounded, because a long run of letters in mixed or single case is as randomas any key."""- single_case = value.islower() or value.isupper() or value.istitle()- return value.isalpha() and single_case and len(value) <= 20+ words = value.split(" ")+ single_case = all(word.islower() or word.isupper() or word.istitle() for word in words)+ return (+ 1 <= len(words) <= 4+ and all(word.isalpha() for word in words)+ and single_case+ and len(value) <= 20+ )def _redaction(val: str) -> str:@@ -792,9 +826,11 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis# Vendor key shapes are real wherever they sit, comments included.match = pattern.search(line)if match:- value = line[slice(*_value_span(kind, match, line))] if match.groups() else ""+ value_span = _value_span(kind, match, line) if match.groups() else (0, 0)+ value = line[slice(*value_span)] if match.groups() else ""if kind in SECRET_KINDS:- if not _is_secret_value(kind, value):+ name = _assignment_name(line[: value_span[0]])+ if not _is_secret_value(kind, value, name):continueif is_low_sev_file:severity = "low"diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.pyindex f613ebdd0..6407da2a4 100644--- a/tests/unit/analysis/test_security_scan.py+++ b/tests/unit/analysis/test_security_scan.py@@ -1073,6 +1073,34 @@ class TestSecretPrecision:def test_dummy_values_are_not_secrets(self, source: str) -> None:assert self._kinds(source, "src/client.tsx") == set()+ @pytest.mark.parametrize(+ ("path", "source"),+ [+ ("src/Conn.kt", 'const val DISCOVERY_TOKEN = "discoveryToken"\n'),+ ("src/Conn.kt", 'const val KEY_STORE_PASSWORD = "keystorePassword"\n'),+ ("src/tokens.ts", "export const LANGUAGE_SERVER_WS_ADDRESS_TOKEN = 'LANGUAGE_SERVER_WS_ADDRESS_TOKEN';\n"),+ ("src/AuthScheme.kt", 'const val MASKED_PASSWORD = "**************"\n'),+ ("src/Auth.kt", 'val bearerToken = "Bearer ${auth.token.tokenValue}"\n'),+ ("src/auth.ts", 'const token = "Basic ${btoa(user)}";\n'),+ ("README.md", '--spring.datasource.password="DB PASSWORD"\n'),+ ("README.md", 'DB_PASSWORD="YOUR DB PASSWORD"\n'),+ ],+ )+ def test_additional_placeholders_are_not_secrets(self, path: str, source: str) -> None:+ assert self._kinds(source, path) == set()++ @pytest.mark.parametrize(+ ("source", "kind"),+ [+ ('TOKEN = "xQzRmWpLkVnBtYcH"\n', "hardcoded_secret"),+ ('TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9abc"\n', "hardcoded_secret"),+ ('const val API_TOKEN = "productionAccessKey"\n', "hardcoded_secret"),+ ('password="Tr0ub4dor 3xyz"\n', "hardcoded_password"),+ ],+ )+ def test_credential_like_values_still_fire(self, source: str, kind: str) -> None:+ assert self._kinds(source) == {kind}+@pytest.mark.parametrize("source",[