|
24 | 24 |
|
25 | 25 | from __future__ import annotations |
26 | 26 |
|
| 27 | +import ast |
27 | 28 | import re |
28 | 29 | import sys |
29 | 30 | from collections.abc import Callable, Iterator |
30 | 31 | from dataclasses import dataclass |
31 | 32 |
|
32 | 33 | from skillspector.logging_config import get_logger |
33 | 34 | from skillspector.models import AnalyzerFinding, Location, Severity |
| 35 | +from skillspector.python_ast import parse_python_source |
34 | 36 | from skillspector.security_reconstruction import validated_json_string_spans |
35 | 37 | from skillspector.state import AnalyzerNodeResponse, SkillspectorState |
36 | 38 |
|
|
110 | 112 | re.IGNORECASE, |
111 | 113 | ) |
112 | 114 |
|
| 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 | + |
113 | 126 | # TM1: Tool Parameter Abuse — dangerous parameter values |
114 | 127 | TM1_CODE_PATTERNS = [ |
115 | 128 | # shell=True is a classic command injection vector |
|
118 | 131 | # Preserve the direct-call signal when a Python boolean is assigned to a |
119 | 132 | # local name immediately before the invocation. The bounded newline gap |
120 | 133 | # 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), |
127 | 135 | # Bound command names on both sides so prefixes such as rmm/ (RAPIDS |
128 | 136 | # Memory Manager headers) are not interpreted as destructive commands. |
129 | 137 | # Keep the scan within one bounded shell command. The former ``[^|]*`` |
@@ -2118,6 +2126,171 @@ def _has_unsupported_brace_expansion(tokens: tuple[_ShellToken, ...]) -> bool: |
2118 | 2126 | ) |
2119 | 2127 |
|
2120 | 2128 |
|
| 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 | + |
2121 | 2294 | def _tm1_candidates( |
2122 | 2295 | content: str, |
2123 | 2296 | ) -> Iterator[tuple[int, int, str, float]]: |
@@ -2586,7 +2759,18 @@ def ctx(start: int) -> str: |
2586 | 2759 | tag = [PatternCategory.TOOL_MISUSE.value] |
2587 | 2760 | tm1_findings_by_key: dict[tuple[int, str], AnalyzerFinding] = {} |
2588 | 2761 |
|
| 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 | + |
2589 | 2771 | for match_start, match_end, matched_text, confidence in _tm1_candidates(content): |
| 2772 | + if match_start in cross_scope_starts: |
| 2773 | + continue |
2590 | 2774 | line_num = get_line_number(content, match_start) |
2591 | 2775 | context_text = ctx(match_start) |
2592 | 2776 | matched = matched_text[:200] |
|
0 commit comments