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-scan): flag multi-line subprocess.*(... shell=True) calls (#1522)

Felix-Ayushf2e2139be255
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.py
index cd79856fb..6d16f7696 100644
--- a/packages/core/src/repowise/core/analysis/security_scan.py
+++ b/packages/core/src/repowise/core/analysis/security_scan.py
@@ -50,6 +50,25 @@ _PATTERNS: list[tuple[re.Pattern, str, str]] = [
# _PATTERNS matches, so findings are unchanged.
_ANY_PATTERN = re.compile("|".join(f"(?:{p.pattern})" for p, _, _ in _PATTERNS))
+# Patterns whose calls legitimately span multiple physical lines: the opening
+# ``subprocess.<call>(`` lands on one line and ``shell=True`` on another. The
+# per-line loop can never see such a call (``.*`` stops at the newline), so it
+# gets an extra whole-source pass.
+#
+# Continuation is restricted to the same physical line or a newline that is
+# followed by indentation (``(?:[^\n]|\n(?=[ \t]))``), so a closed
+# ``subprocess.run(...)`` cannot jump to a later ``os.popen(..., shell=True)``
+# on a column-0 line. The span is also capped (~200 chars) as a second bound.
+_SPANNING_PATTERNS: list[tuple[re.Pattern, str, str]] = [
+ (
+ re.compile(
+ r"subprocess\.[A-Za-z]+\((?:[^\n]|\n(?=[ \t])){0,200}?shell\s*=\s*True"
+ ),
+ "subprocess_shell_true",
+ "high",
+ ),
+]
+
# Symbol names that are informational security hotspots
_SYMBOL_KEYWORDS = re.compile(
r"\b(auth|token|password|jwt|session|crypto)\b", re.IGNORECASE
@@ -115,6 +134,31 @@ class SecurityScanner:
}
)
+ # Whole-source pass for patterns that span physical lines
+ # (``subprocess.run(\n ...,\n shell=True,\n)``). The per-line
+ # loop above can never see the sink when the call opens on one line
+ # and ``shell=`` lands on another, so scan the full source once. The
+ # finding is reported on the line where the call starts, and a match
+ # the per-line pass already caught on that line is not duplicated.
+ for pattern, kind, severity in _SPANNING_PATTERNS:
+ for match in pattern.finditer(source):
+ start_line = source.count("\n", 0, match.start()) + 1
+ if any(f["kind"] == kind and f["line"] == start_line for f in findings):
+ continue
+ line_start = source.rfind("\n", 0, match.start()) + 1
+ line_end = source.find("\n", match.start())
+ if line_end == -1:
+ line_end = len(source)
+ snippet = source[line_start:line_end].strip()[:120]
+ findings.append(
+ {
+ "kind": kind,
+ "severity": severity,
+ "snippet": snippet,
+ "line": start_line,
+ }
+ )
+
# 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.py
index ae7d99692..84e758329 100644
--- a/tests/unit/analysis/test_security_scan.py
+++ b/tests/unit/analysis/test_security_scan.py
@@ -88,6 +88,49 @@ class TestScanFile:
findings = asyncio.run(scanner.scan_file("b.py", CLEAN.decode(), symbols=[]))
assert findings == []
+ 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'
+ findings = asyncio.run(scanner.scan_file("a.py", source, symbols=[]))
+ hits = [f for f in findings if f["kind"] == "subprocess_shell_true"]
+ assert len(hits) == 1
+ assert hits[0]["line"] == 1
+
+ def test_multiline_subprocess_shell_true_is_flagged(self) -> None:
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ source = 'subprocess.run(\n "rm -rf " + path,\n shell=True,\n)\n'
+ findings = asyncio.run(scanner.scan_file("a.py", source, symbols=[]))
+ hits = [f for f in findings if f["kind"] == "subprocess_shell_true"]
+ assert len(hits) == 1
+ assert hits[0]["line"] == 1
+
+ def test_multiline_popen_shell_true_is_flagged(self) -> None:
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ source = 'subprocess.Popen(\n ["/bin/sh"],\n shell=True,\n)\n'
+ findings = asyncio.run(scanner.scan_file("a.py", source, symbols=[]))
+ hits = [f for f in findings if f["kind"] == "subprocess_shell_true"]
+ assert len(hits) == 1
+ assert hits[0]["line"] == 1
+
+ def test_subprocess_without_shell_does_not_fire(self) -> None:
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ source = 'subprocess.run(\n ["git", "rev-parse", "HEAD"],\n capture_output=True,\n)\n'
+ findings = asyncio.run(scanner.scan_file("a.py", source, symbols=[]))
+ hits = [f for f in findings if f["kind"] == "subprocess_shell_true"]
+ assert hits == []
+
+ def test_spanning_pass_does_not_jump_to_later_shell_true(self) -> None:
+ """A closed subprocess call must not claim a later column-0 shell=True."""
+ scanner = SecurityScanner(session=None, repo_id="r1") # type: ignore[arg-type]
+ source = (
+ 'subprocess.run(["ls"], capture_output=True)\n'
+ "\n"
+ "os.popen(cmd, shell=True)\n"
+ )
+ findings = asyncio.run(scanner.scan_file("a.py", source, symbols=[]))
+ hits = [f for f in findings if f["kind"] == "subprocess_shell_true"]
+ assert hits == []
+
class TestPersistSecurityFindings:
"""The persist.py wiring: source_map in, idempotent rows out."""