Skip to content

Commit d830a71

Browse files
committed
fix(static): require same-scope shell flag assignment
The variable shell flag regex matched across Python scopes, treating use_shell = True in one function and shell=use_shell in another as one data-flow fact. Resolve the matched assignment and use through the Python AST and drop cross-scope candidates, while keeping closure reads and global/nonlocal declarations. Adds the unrelated-scope negative regression. Signed-off-by: Deepak Jain <[email protected]>
1 parent 426b0ed commit d830a71

2 files changed

Lines changed: 221 additions & 6 deletions

File tree

‎src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py‎

Lines changed: 190 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
from __future__ import annotations
2626

27+
import ast
2728
import re
2829
import sys
2930
from collections.abc import Callable, Iterator
3031
from dataclasses import dataclass
3132

3233
from skillspector.logging_config import get_logger
3334
from skillspector.models import AnalyzerFinding, Location, Severity
35+
from skillspector.python_ast import parse_python_source
3436
from skillspector.security_reconstruction import validated_json_string_spans
3537
from skillspector.state import AnalyzerNodeResponse, SkillspectorState
3638

@@ -110,6 +112,17 @@
110112
re.IGNORECASE,
111113
)
112114

115+
# Match a literal True assigned to a local name shortly before it is passed as
116+
# shell=<name> to a subprocess invocation. The bounded newline gap and the
117+
# intervening-write guard keep this a local data-flow fact; Python scope
118+
# visibility is enforced separately in analyze() via _VARIABLE_SHELL_FLAG_RE.
119+
_VARIABLE_SHELL_FLAG_PATTERN = (
120+
r"(?m)^\s*([A-Za-z_]\w*)\s*=\s*True\s*$\n"
121+
r"(?:(?![^\n]*\b\1\s*=)[^\n]{0,240}\n){0,4}?[^\n]{0,240}"
122+
r"(?:subprocess\.\w+|Popen)\s*\([^)]*\bshell\s*=\s*\1\b"
123+
)
124+
_VARIABLE_SHELL_FLAG_RE = re.compile(_VARIABLE_SHELL_FLAG_PATTERN, re.IGNORECASE | re.MULTILINE)
125+
113126
# TM1: Tool Parameter Abuse — dangerous parameter values
114127
TM1_CODE_PATTERNS = [
115128
# shell=True is a classic command injection vector
@@ -118,12 +131,7 @@
118131
# Preserve the direct-call signal when a Python boolean is assigned to a
119132
# local name immediately before the invocation. The bounded newline gap
120133
# avoids treating an arbitrary distant assignment as a data-flow fact.
121-
(
122-
r"(?m)^\s*([A-Za-z_]\w*)\s*=\s*True\s*$\n"
123-
r"(?:(?![^\n]*\b\1\s*=)[^\n]{0,240}\n){0,4}?[^\n]{0,240}"
124-
r"(?:subprocess\.\w+|Popen)\s*\([^)]*\bshell\s*=\s*\1\b",
125-
0.8,
126-
),
134+
(_VARIABLE_SHELL_FLAG_PATTERN, 0.8),
127135
# Bound command names on both sides so prefixes such as rmm/ (RAPIDS
128136
# Memory Manager headers) are not interpreted as destructive commands.
129137
# Keep the scan within one bounded shell command. The former ``[^|]*``
@@ -2118,6 +2126,171 @@ def _has_unsupported_brace_expansion(tokens: tuple[_ShellToken, ...]) -> bool:
21182126
)
21192127

21202128

2129+
_SCOPE_NODE_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Lambda)
2130+
2131+
2132+
def _scope_chain(tree: ast.Module, target: ast.AST) -> tuple[ast.AST, ...] | None:
2133+
"""Return the chain of enclosing scopes for *target*, outermost first.
2134+
2135+
The module itself is the outermost scope. Returns None when *target* is
2136+
not part of *tree*.
2137+
"""
2138+
parents: dict[ast.AST, ast.AST] = {}
2139+
stack: list[ast.AST] = [tree]
2140+
found = False
2141+
while stack:
2142+
node = stack.pop()
2143+
if node is target:
2144+
found = True
2145+
break
2146+
for child in ast.iter_child_nodes(node):
2147+
parents[child] = node
2148+
stack.append(child)
2149+
if not found:
2150+
return None
2151+
chain: list[ast.AST] = []
2152+
current: ast.AST | None = target
2153+
while current is not None:
2154+
if isinstance(current, _SCOPE_NODE_TYPES) or current is tree:
2155+
chain.append(current)
2156+
current = parents.get(current)
2157+
chain.reverse()
2158+
return tuple(chain)
2159+
2160+
2161+
def _scope_binds_name(scope_node: ast.AST, name: str) -> bool:
2162+
"""Return whether *name* is bound directly in *scope_node*.
2163+
2164+
Nested function/class/lambda bodies are not descended into: their bindings
2165+
belong to those scopes, not this one.
2166+
"""
2167+
if isinstance(scope_node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)):
2168+
args = scope_node.args
2169+
named = [arg.arg for arg in (*args.posonlyargs, *args.args, *args.kwonlyargs)]
2170+
if args.vararg is not None:
2171+
named.append(args.vararg.arg)
2172+
if args.kwarg is not None:
2173+
named.append(args.kwarg.arg)
2174+
if name in named:
2175+
return True
2176+
bodies: list[ast.AST] = (
2177+
[scope_node.body] if isinstance(scope_node, ast.Lambda) else list(scope_node.body)
2178+
)
2179+
elif isinstance(scope_node, (ast.ClassDef, ast.Module)):
2180+
bodies = list(scope_node.body)
2181+
else:
2182+
return False
2183+
stack = list(bodies)
2184+
while stack:
2185+
node = stack.pop()
2186+
if isinstance(node, _SCOPE_NODE_TYPES):
2187+
continue
2188+
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store) and node.id == name:
2189+
return True
2190+
stack.extend(ast.iter_child_nodes(node))
2191+
return False
2192+
2193+
2194+
def _direct_global_nonlocal(scope_node: ast.AST, name: str) -> str | None:
2195+
"""Return 'global'/'nonlocal' when *scope_node* declares *name* as such.
2196+
2197+
Only declarations directly in the scope are considered; nested scopes are
2198+
not descended into.
2199+
"""
2200+
if isinstance(scope_node, ast.Lambda):
2201+
return None
2202+
bodies: list[ast.stmt] | None = None
2203+
if isinstance(scope_node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Module)):
2204+
bodies = scope_node.body
2205+
if bodies is None:
2206+
return None
2207+
stack: list[ast.AST] = list(bodies)
2208+
while stack:
2209+
node = stack.pop()
2210+
if isinstance(node, _SCOPE_NODE_TYPES):
2211+
continue
2212+
if isinstance(node, ast.Global) and name in node.names:
2213+
return "global"
2214+
if isinstance(node, ast.Nonlocal) and name in node.names:
2215+
return "nonlocal"
2216+
stack.extend(ast.iter_child_nodes(node))
2217+
return None
2218+
2219+
2220+
def _variable_shell_flag_same_scope(content: str, file_path: str, match: re.Match[str]) -> bool:
2221+
"""Return whether a variable-shell-flag match is a same-scope data flow.
2222+
2223+
The regex cannot see Python scopes, so ``use_shell = True`` in one
2224+
function followed by ``shell=use_shell`` in another still matches. Resolve
2225+
the matched assignment and the ``shell=`` use through the Python AST and
2226+
require the assignment to be visible from the use: identical scope chains,
2227+
a closure read from an enclosing scope, or a matching global/nonlocal
2228+
declaration. Unparseable content keeps the candidate so a syntax error
2229+
cannot silence the signal.
2230+
"""
2231+
var_name = match.group(1)
2232+
assign_line = content.count("\n", 0, match.start()) + 1
2233+
use_line = content.count("\n", 0, match.end()) + 1
2234+
tree = parse_python_source(content, file_path).tree
2235+
if tree is None:
2236+
return True
2237+
assign_node: ast.Assign | None = None
2238+
call_node: ast.Call | None = None
2239+
for node in ast.walk(tree):
2240+
if (
2241+
assign_node is None
2242+
and isinstance(node, ast.Assign)
2243+
and node.lineno == assign_line
2244+
and isinstance(node.value, ast.Constant)
2245+
and node.value.value is True
2246+
and any(
2247+
isinstance(target, ast.Name) and target.id == var_name for target in node.targets
2248+
)
2249+
):
2250+
assign_node = node
2251+
if (
2252+
call_node is None
2253+
and isinstance(node, ast.Call)
2254+
and any(
2255+
keyword.arg == "shell"
2256+
and isinstance(keyword.value, ast.Name)
2257+
and keyword.value.id == var_name
2258+
and assign_line <= keyword.value.lineno <= use_line
2259+
for keyword in node.keywords
2260+
)
2261+
):
2262+
call_node = node
2263+
if assign_node is None or call_node is None:
2264+
return True
2265+
assign_chain = _scope_chain(tree, assign_node)
2266+
use_chain = _scope_chain(tree, call_node)
2267+
if assign_chain is None or use_chain is None:
2268+
return True
2269+
if assign_chain == use_chain:
2270+
return True
2271+
if len(assign_chain) < len(use_chain) and use_chain[: len(assign_chain)] == assign_chain:
2272+
# Closure read: the use sits in a scope nested inside the assignment's
2273+
# scope, so the name resolves to the assigned value.
2274+
return True
2275+
use_scope = use_chain[-1]
2276+
if use_scope is not tree:
2277+
declaration = _direct_global_nonlocal(use_scope, var_name)
2278+
if declaration == "global":
2279+
return assign_chain == (tree,)
2280+
if declaration == "nonlocal":
2281+
binding = next(
2282+
(
2283+
scope
2284+
for scope in use_chain[-2::-1]
2285+
if isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda))
2286+
and _scope_binds_name(scope, var_name)
2287+
),
2288+
None,
2289+
)
2290+
return binding is not None and assign_chain == _scope_chain(tree, binding)
2291+
return False
2292+
2293+
21212294
def _tm1_candidates(
21222295
content: str,
21232296
) -> Iterator[tuple[int, int, str, float]]:
@@ -2586,7 +2759,18 @@ def ctx(start: int) -> str:
25862759
tag = [PatternCategory.TOOL_MISUSE.value]
25872760
tm1_findings_by_key: dict[tuple[int, str], AnalyzerFinding] = {}
25882761

2762+
# The variable-shell-flag regex cannot see Python scopes, so an assignment
2763+
# in one function and a shell= use in another still match. Drop those
2764+
# cross-scope candidates for Python files.
2765+
cross_scope_starts: set[int] = set()
2766+
if file_type == "python":
2767+
for variable_match in _VARIABLE_SHELL_FLAG_RE.finditer(content):
2768+
if not _variable_shell_flag_same_scope(content, file_path, variable_match):
2769+
cross_scope_starts.add(variable_match.start())
2770+
25892771
for match_start, match_end, matched_text, confidence in _tm1_candidates(content):
2772+
if match_start in cross_scope_starts:
2773+
continue
25902774
line_num = get_line_number(content, match_start)
25912775
context_text = ctx(match_start)
25922776
matched = matched_text[:200]

‎tests/unit/test_patterns_new.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,37 @@ def test_tm1_ignores_reassigned_shell_variable(self) -> None:
14811481
)
14821482
assert not any(finding.rule_id == "TM1" for finding in findings)
14831483

1484+
def test_tm1_ignores_shell_variable_from_unrelated_scope(self) -> None:
1485+
content = (
1486+
"def helper():\n"
1487+
" use_shell = True\n"
1488+
"\n"
1489+
"def main():\n"
1490+
" subprocess.run(cmd, shell=use_shell)\n"
1491+
)
1492+
findings = tm_mod.analyze(content, "runner.py", "python")
1493+
assert not any(finding.rule_id == "TM1" for finding in findings)
1494+
1495+
def test_tm1_keeps_shell_variable_read_through_closure(self) -> None:
1496+
content = (
1497+
"def outer():\n"
1498+
" use_shell = True\n"
1499+
" def inner():\n"
1500+
" subprocess.run(cmd, shell=use_shell)\n"
1501+
)
1502+
findings = tm_mod.analyze(content, "runner.py", "python")
1503+
assert any(finding.rule_id == "TM1" for finding in findings)
1504+
1505+
def test_tm1_keeps_global_shell_variable(self) -> None:
1506+
content = (
1507+
"use_shell = True\n"
1508+
"def main():\n"
1509+
" global use_shell\n"
1510+
" subprocess.run(cmd, shell=use_shell)\n"
1511+
)
1512+
findings = tm_mod.analyze(content, "runner.py", "python")
1513+
assert any(finding.rule_id == "TM1" for finding in findings)
1514+
14841515
def test_application_specific_no_verify_flag_is_not_tool_misuse(self) -> None:
14851516
content = """\
14861517
print("verification: skipped (--no-verify)")

0 commit comments

Comments
 (0)