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): stop reporting RegExp.exec, and catch the constant secret form (#1947)
tacopiekfc5446773646
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.pyindex aa95a9758..2748d94ba 100644--- a/packages/core/src/repowise/core/analysis/security_scan.py+++ b/packages/core/src/repowise/core/analysis/security_scan.py@@ -46,13 +46,40 @@ _CALL_PATTERNS: list[tuple[re.Pattern, str, str]] = []_CALL_KINDS = frozenset(kind for _, kind, _ in _CALL_PATTERNS)+# ``exec`` is not a global in JavaScript; the name belongs to+# ``RegExp.prototype.exec``. The receiver-chain prefix above therefore matches+# ``re.exec(str)``, ``/x/.exec(s)`` and ``pattern.exec(xml)`` — idiomatic parsing+# code, reported at ``high``. Measured on a 17-repo TypeScript corpus, every+# ``exec_call`` hit outside Python was a regex match and none was a process+# spawn, so the kind was pure noise on those languages.+#+# The dangerous call in JavaScript comes from ``child_process``, so that is what+# gates it: a file that never names the module cannot spawn one. ``eval`` needs+# no such gate — it is a genuine global there.+_CHILD_PROCESS_IMPORT = re.compile(r"child_process|node:child_process")+_JS_EXEC_CALL = re.compile(r"(?<![\w$])(?:[A-Za-z_$][\w$]*\s*\.\s*)*exec(?:File)?(?:Sync)?\s*\(")+_PATTERNS: list[tuple[re.Pattern, str, str]] = [*_CALL_PATTERNS,(re.compile(r"pickle\.loads"), "pickle_loads", "high"),(re.compile(r"subprocess\..*shell\s*=\s*True"), "subprocess_shell_true", "high"),(re.compile(r"os\.system"), "os_system", "high"),- (re.compile(r"password\s*=\s*['\"]"), "hardcoded_password", "high"),- (re.compile(r"(?:api_?key|secret)\s*=\s*['\"]"), "hardcoded_secret", "high"),+ # Case-insensitive: a credential pinned in source is usually written as a+ # SCREAMING_CASE constant assigned a quoted literal, and the lowercase-only+ # patterns walked straight past that form. Found by scanning a corpus in+ # which a live n8n key sat unreported under exactly that spelling.+ #+ # The wording above avoids spelling the matched form out literally: this+ # loop runs on raw source, comments included, so an example written out here+ # would make the file report itself.+ #+ # Case-insensitivity is written as a scoped inline group rather than the+ # ``re.IGNORECASE`` flag on purpose: ``_ANY_PATTERN`` below is built by+ # 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'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"),@@ -237,13 +264,25 @@ def _call_findings(file_path: str, source: str) -> list[dict]:masked = _mask_comments_and_strings(source)lines = source.splitlines()findings = []++ def add(kind: str, severity: str, offset: int) -> None:+ lineno = source.count("\n", 0, offset) + 1+ snippet = lines[lineno - 1].strip()[:120] if lineno <= len(lines) else ""+ findings.append({"kind": kind, "severity": severity, "snippet": snippet, "line": lineno})++ is_python = file_path.lower().endswith((".py", ".pyi"))for pattern, kind, severity in _CALL_PATTERNS:+ if kind == "exec_call" and not is_python:+ continue # handled below, gated on child_processfor match in pattern.finditer(masked):- lineno = source.count("\n", 0, match.start()) + 1- snippet = lines[lineno - 1].strip()[:120] if lineno <= len(lines) else ""- findings.append(- {"kind": kind, "severity": severity, "snippet": snippet, "line": lineno}- )+ add(kind, severity, match.start())++ # The module name is searched in raw source on purpose: it arrives as a+ # string literal (``require("child_process")``), which masking blanks.+ if not is_python and _CHILD_PROCESS_IMPORT.search(source):+ for match in _JS_EXEC_CALL.finditer(masked):+ add("exec_call", "high", match.start())+return findingsdiff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.pyindex 748da8e55..34b82aac5 100644--- a/tests/unit/analysis/test_security_scan.py+++ b/tests/unit/analysis/test_security_scan.py@@ -107,7 +107,6 @@ class TestScanFile:("source", "expected"),[("eval (payload);\n", {"eval_call"}),- ("exec\n (payload);\n", {"exec_call"}),("window.eval(payload);\n", {"eval_call"}),("const result = `${eval(payload)}`;\n", {"eval_call"}),("retrieval (payload);\n", set()),@@ -115,6 +114,12 @@ class TestScanFile:("// eval(payload)\n", set()),("/* exec(payload) */\n", set()),('const text = "eval(payload)";\n', set()),+ # ``exec`` is not a global in JavaScript, so a bare call is a local+ # function, and a call on a receiver is ``RegExp.prototype.exec``.+ # Neither spawns anything; see the corpus test below.+ ("exec\n (payload);\n", set()),+ ("const m = /^a(b)$/.exec(cell);\n", set()),+ ("while ((m = re.exec(expr)) !== null) {}\n", set()),],)def test_fallback_eval_exec_call_corpus(self, source: str, expected: set[str]) -> None:@@ -122,6 +127,27 @@ class TestScanFile:findings = asyncio.run(scanner.scan_file("calls.js", source, symbols=[]))assert {row["kind"] for row in findings if row["kind"].endswith("_call")} == expected+ @pytest.mark.parametrize(+ ("source", "expected"),+ [+ ('import { exec } from "child_process";\nexec(cmd);\n', {"exec_call"}),+ ('const cp = require("child_process");\ncp.execSync(cmd);\n', {"exec_call"}),+ (+ 'import { execFile } from "node:child_process";\nexecFile(bin, args);\n',+ {"exec_call"},+ ),+ # Regex parsing in a file that also spawns: the gate is per file, so+ # this is the residual false positive the gate cannot remove.+ ('require("child_process");\nconst m = /a/.exec(s);\n', {"exec_call"}),+ ],+ )+ def test_js_exec_is_reported_only_when_child_process_is_present(+ self, source: str, expected: set[str]+ ) -> None:+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]+ findings = asyncio.run(scanner.scan_file("spawn.ts", source, symbols=[]))+ assert {row["kind"] for row in findings if row["kind"].endswith("_call")} == expected+def test_combined_prefilter_uses_the_same_safe_call_boundaries(self) -> None:from repowise.core.analysis.security_scan import _ANY_PATTERN@@ -131,6 +157,29 @@ class TestScanFile:assert not _ANY_PATTERN.search("retrieval (")assert not _ANY_PATTERN.search("my_eval(")+ @pytest.mark.parametrize(+ "source",+ [+ 'const API_KEY = "abc123";\n',+ "const N8N_API_KEY = 'abc123';\n",+ 'SECRET = "abc123"\n',+ 'PASSWORD = "abc123"\n',+ ],+ )+ def test_secret_patterns_are_case_insensitive(self, source: str) -> None:+ """The constant form is how a pinned credential is usually written.++ Also covers the prefilter: ``_ANY_PATTERN`` is built from the patterns'+ source text, so a per-pattern ``re.IGNORECASE`` flag would be dropped+ there and the line would never reach the pattern loop.+ """+ from repowise.core.analysis.security_scan import _ANY_PATTERN++ assert _ANY_PATTERN.search(source)+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]+ findings = asyncio.run(scanner.scan_file("config.ts", source, symbols=[]))+ assert {row["kind"] for row in findings} & {"hardcoded_secret", "hardcoded_password"}+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'