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-scan): ignore call kinds in comments, strings and prose (#2618)
Aman8fdfd84530f4
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.pyindex 74e39b2e5..bd760355c 100644--- a/packages/core/src/repowise/core/analysis/security_scan.py+++ b/packages/core/src/repowise/core/analysis/security_scan.py@@ -29,6 +29,7 @@ from typing import Anyfrom sqlalchemy import textfrom sqlalchemy.ext.asyncio import AsyncSession+from repowise.core.support_paths import DOC_EXTENSIONSfrom repowise.core.test_paths import is_test_related_pathlogger = logging.getLogger(__name__)@@ -258,6 +259,15 @@ _SPANNING_PATTERNS: list[tuple[re.Pattern, str, str]] = [# Symbol names that are informational security hotspots_SYMBOL_KEYWORDS = re.compile(r"\b(auth|token|password|jwt|session|crypto)\b", re.IGNORECASE)+# Kinds that name a dangerous call. They are matched against source with+# comments and string literals blanked, so a docstring, a comment or a regex+# literal that merely mentions the call does not fire.+_MASKED_KINDS = frozenset({"pickle_loads", "subprocess_shell_true", "os_system"})++# Prose is documentation, not executable code. Only secret kinds scan it,+# because a key pasted into a README is still a leak.+_PROSE_EXTENSIONS = tuple(DOC_EXTENSIONS)+# Patterns whose matches are genuine leaked credentials (as opposed to the# broader "code smell" patterns like os.system/eval). Full-history scans# default to this subset: a historical commit that *once* called eval() is@@ -617,21 +627,29 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis"""Scan *source* text and symbol names; return finding dicts. No I/O."""findings: list[dict] = []lines = source_lines(source)+ is_prose = file_path.lower().endswith(_PROSE_EXTENSIONS)- findings.extend(_call_findings(file_path, source))+ if not is_prose:+ findings.extend(_call_findings(file_path, source))++ masked = "" if is_prose else _mask_comments_and_strings(source)+ masked_lines = source_lines(masked)# Line-by-line pattern scanis_low_sev_file = _is_low_severity_path(file_path)for lineno, line in enumerate(lines, start=1):if not _ANY_PATTERN.search(line):continue+ code_line = masked_lines[lineno - 1] if lineno <= len(masked_lines) else linesnippet: str | None = Nonekeyword_hits: list[tuple[dict, str]] = []vendor_values: list[str] = []for pattern, kind, severity in _PATTERNS:if kind in _CALL_KINDS:continue- match = pattern.search(line)+ if is_prose and kind not in SECRET_KINDS:+ continue+ match = pattern.search(code_line if kind in _MASKED_KINDS else line)if match:value = line[slice(*_value_span(kind, match, line))] if match.groups() else ""if kind in SECRET_KINDS:@@ -661,7 +679,9 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis# on the opening line, unless the per-line pass already did.pem_body_lines: set[int] = set()for pattern, kind, severity in _SPANNING_PATTERNS:- for match in pattern.finditer(source):+ if is_prose and kind not in SECRET_KINDS:+ continue+ for match in pattern.finditer(masked if kind in _MASKED_KINDS else source):if kind in SECRET_KINDS:val = match.group(1) if match.groups() else ""if not _is_secret_value(kind, val):@@ -710,6 +730,11 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lisif finding["line"] in pem_body_lines:finding["snippet"] = _redaction(lines[finding["line"] - 1].strip())+ # Test and fixture material never carries a shipped sink, whatever the kind.+ if is_low_sev_file:+ for finding in findings:+ finding["severity"] = "low"+# Symbol-name scan (informational / low)for sym in symbols:name = getattr(sym, "name", "") or getattr(sym, "qualified_name", "") or ""diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.pyindex 1aec438cd..590ca4dbb 100644--- a/tests/unit/analysis/test_security_scan.py+++ b/tests/unit/analysis/test_security_scan.py@@ -13,6 +13,7 @@ from __future__ import annotationsimport asynciofrom pathlib import Pathfrom types import SimpleNamespace+from typing import ClassVarimport pytestfrom sqlalchemy import text@@ -936,3 +937,48 @@ class TestVendorShapeEdges:for integrity in (f"sha512-{run}+Q==", f"sha512-Zm9v/{run}/b2=="):source = f' "integrity": "{integrity}",\n'assert "google_api_key" not in self._kinds(source, "package-lock.json")+++class TestCallKindsIgnoreProseAndText:+ """pickle.loads / os.system / shell=True only count as code, not as text."""++ _KINDS: ClassVar[set[str]] = {"pickle_loads", "os_system", "subprocess_shell_true"}++ def _hits(self, source: str, path: str = "a.py") -> list[dict]:+ return [f for f in scan_source(path, source) if f["kind"] in self._KINDS]++ def test_docstring_mention_does_not_fire_but_the_call_does(self) -> None:+ source = 'def load(x):\n """Never call pickle.loads on input."""\n return pickle.loads(x)\n'+ hits = self._hits(source)+ assert [(f["kind"], f["line"]) for f in hits] == [("pickle_loads", 3)]++ def test_comment_and_string_literal_mentions_do_not_fire(self) -> None:+ source = (+ "# avoid os.system here\n"+ 'NAME = "os.system"\n'+ 'PATTERN = r"subprocess.run(x, shell=True)"\n'+ )+ assert self._hits(source) == []++ def test_markdown_mention_does_not_fire(self) -> None:+ source = "Replaced `pickle.loads` and `os.system` with safer calls.\n"+ assert self._hits(source, "docs/CHANGELOG.md") == []++ def test_secret_in_markdown_is_still_flagged(self) -> None:+ source = 'AWS_ACCESS_KEY_ID = "AKIAQZXNRTVYWMPKLBHG"\n'+ kinds = [f["kind"] for f in scan_source("README.md", source)]+ assert "aws_access_key" in kinds++ def test_real_calls_under_tests_are_low(self) -> None:+ source = "os.system(cmd)\n"+ hits = self._hits(source, "tests/test_x.py")+ assert [f["severity"] for f in hits] == ["low"]+ assert [f["severity"] for f in self._hits(source, "src/x.py")] == ["high"]++ def test_eval_under_tests_is_low(self) -> None:+ findings = scan_source("tests/test_x.py", "eval(expr)\n")+ assert [f["severity"] for f in findings if f["kind"] == "eval_call"] == ["low"]++ def test_multiline_shell_true_in_a_string_does_not_fire(self) -> None:+ source = 'TEMPLATE = """\nsubprocess.run(\n x,\n shell=True,\n)\n"""\n'+ assert self._hits(source) == []