Skip to content

Commit 62cab16

Browse files
authored
Merge pull request #820 from NVIDIA/feat/christopherk/tm1-assignment-final-state
fix(tm1): use the final repeated assignment target
2 parents 3f90f9a + fadbd61 commit 62cab16

2 files changed

Lines changed: 284 additions & 9 deletions

File tree

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

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3814,6 +3814,78 @@ def _direct_shell_name(call: ast.Call) -> str | None:
38143814
return None
38153815

38163816

3817+
def _passive_assignment_name_snapshots(tree: ast.AST) -> dict[ast.AST, frozenset[str]]:
3818+
"""Prove prior simple-name bindings in supported straight-line scopes."""
3819+
from . import static_python_shell_truthiness
3820+
3821+
snapshots: dict[ast.AST, frozenset[str]] = {}
3822+
for scope in ast.walk(tree):
3823+
if not isinstance(scope, (ast.Module, ast.FunctionDef, ast.AsyncFunctionDef)):
3824+
continue
3825+
bound_names = (
3826+
set(_parameter_names(scope.args)) if not isinstance(scope, ast.Module) else set()
3827+
)
3828+
declarations = static_python_shell_truthiness._DirectBindingCollector(frozenset())
3829+
for statement in scope.body:
3830+
declarations.visit(statement)
3831+
bound_names.update(declarations.nonlocal_names)
3832+
safe_names: set[str] = set()
3833+
trusted_names = set(static_python_shell_truthiness._DIRECT_CALL_NAMES).difference(
3834+
bound_names
3835+
)
3836+
unknown_effects = [False]
3837+
for statement in scope.body:
3838+
if isinstance(statement, (ast.Assign, ast.AnnAssign)) and not unknown_effects[0]:
3839+
targets = (
3840+
statement.targets if isinstance(statement, ast.Assign) else [statement.target]
3841+
)
3842+
value = statement.value
3843+
if value is not None:
3844+
pairs = [
3845+
pair for target in targets for pair in _paired_store_values(target, value)
3846+
]
3847+
if pairs and all(
3848+
isinstance(target, ast.Name)
3849+
and (target.id not in bound_names or target.id in safe_names)
3850+
for target, _ in pairs
3851+
):
3852+
snapshots[statement] = frozenset(
3853+
item.id
3854+
for item in ast.walk(value)
3855+
if isinstance(item, ast.Name) and item.id in safe_names
3856+
)
3857+
if isinstance(statement, (ast.Import, ast.ImportFrom)):
3858+
native_import = (
3859+
isinstance(statement, ast.Import)
3860+
and all(
3861+
alias.name == "subprocess" and alias.asname in (None, "subprocess")
3862+
for alias in statement.names
3863+
)
3864+
or isinstance(statement, ast.ImportFrom)
3865+
and statement.level == 0
3866+
and statement.module == "subprocess"
3867+
and all(
3868+
alias.name == "Popen" and alias.asname in (None, "Popen")
3869+
for alias in statement.names
3870+
)
3871+
)
3872+
if not native_import:
3873+
# Arbitrary module initialization can change existing names.
3874+
unknown_effects[0] = True
3875+
safe_names.clear()
3876+
static_python_shell_truthiness._advance_trusted_names(
3877+
statement, trusted_names, bound_names, safe_names, unknown_effects
3878+
)
3879+
if isinstance(statement, (ast.Import, ast.ImportFrom)) and not unknown_effects[0]:
3880+
# Only a fresh, proven native import adds release-safe names.
3881+
safe_names.update(
3882+
trusted_names.intersection(
3883+
static_python_shell_truthiness._direct_bound_names(statement)
3884+
)
3885+
)
3886+
return snapshots
3887+
3888+
38173889
def _build_variable_shell_ast_index(parsed: ParsedPythonFile) -> _VariableShellAstIndex | None:
38183890
"""Index relevant nodes, bindings, and declarations in one AST traversal."""
38193891
tree = parsed.tree
@@ -3833,6 +3905,7 @@ def _build_variable_shell_ast_index(parsed: ParsedPythonFile) -> _VariableShellA
38333905
# self-stores. They neither replace nor restore a binding.
38343906
identity_name_stores: set[ast.Name] = set()
38353907
native_aliases = _native_receiver_aliases(tree)
3908+
passive_assignment_snapshots = _passive_assignment_name_snapshots(tree)
38363909

38373910
def mark_attribute_identity_stores(target: ast.AST, value: ast.AST) -> None:
38383911
for destination, source in _paired_store_values(target, value):
@@ -3846,16 +3919,49 @@ def mark_attribute_identity_stores(target: ast.AST, value: ast.AST) -> None:
38463919
):
38473920
attribute_identity_stores.add(destination)
38483921

3849-
def record_name_store_values(scope: ast.AST, target: ast.AST, value: ast.AST, end: int) -> None:
3922+
def record_name_store_values(
3923+
scope: ast.AST, targets: list[ast.AST], value: ast.AST, end: int
3924+
) -> None:
3925+
from . import static_python_shell_truthiness
3926+
38503927
# Pair unpacked targets with their RHS elements so a native alias, a
38513928
# truthy flag, or a self-store is not mistaken for a replacement.
3852-
for destination, source in _paired_store_values(target, value):
3853-
if not isinstance(destination, ast.Name):
3854-
continue
3929+
pairs = [
3930+
(destination, source)
3931+
for target in targets
3932+
for destination, source in reversed(list(_paired_store_values(target, value)))
3933+
]
3934+
stores = [
3935+
(destination, source)
3936+
for destination, source in pairs
3937+
if isinstance(destination, ast.Name)
3938+
]
3939+
# The paired iterator is stack-based. Restore Python's left-to-right
3940+
# target order before deciding which occurrence owns the final value.
3941+
final_values = {destination.id: source for destination, source in stores}
3942+
safe_names = passive_assignment_snapshots.get(node)
3943+
safe_stores = safe_names is not None
3944+
safe_value = (
3945+
safe_names is not None
3946+
and static_python_shell_truthiness._is_finalizer_safe_value(value, set(safe_names))
3947+
)
3948+
# Unsupported RHS effects or target protocols cannot prove the final
3949+
# flag. Preserve the existing conservative event policy for those shapes.
3950+
false_values = (
3951+
{
3952+
source
3953+
for source in set(final_values.values())
3954+
if static_python_shell_truthiness._truth_value(source, {}) is False
3955+
}
3956+
if safe_stores and safe_value
3957+
else set()
3958+
)
3959+
definitely_false = {name for name, source in final_values.items() if source in false_values}
3960+
for destination, source in stores:
38553961
keeps_signal = _store_keeps_shell_signal(destination.id, source, native_aliases)
38563962
if keeps_signal is None:
38573963
identity_name_stores.add(destination)
3858-
elif keeps_signal:
3964+
elif keeps_signal and destination.id not in definitely_false:
38593965
record_binding(scope, destination.id, end, True)
38603966

38613967
def record_binding(scope: ast.AST, name: str, start: int, restores: bool = False) -> None:
@@ -4126,16 +4232,15 @@ def bind(scope: ast.AST, name: str, start: int) -> None:
41264232
if isinstance(node, ast.Assign):
41274233
stack.append((node.value, scope_chain, None))
41284234
stack.extend((target, scope_chain, end) for target in node.targets)
4129-
for target in node.targets:
4130-
record_name_store_values(scope, target, node.value, end)
4235+
record_name_store_values(scope, list(node.targets), node.value, end)
41314236
elif isinstance(node, (ast.AnnAssign, ast.AugAssign)):
41324237
if node.value is not None:
41334238
stack.append((node.value, scope_chain, None))
41344239
stack.append((node.target, scope_chain, end))
41354240
if isinstance(node, ast.AnnAssign):
41364241
stack.append((node.annotation, scope_chain, None))
41374242
if node.value is not None:
4138-
record_name_store_values(scope, node.target, node.value, end)
4243+
record_name_store_values(scope, [node.target], node.value, end)
41394244
elif isinstance(node, ast.NamedExpr):
41404245
stack.append((node.value, scope_chain, None))
41414246
# PEP 572 makes a walrus target inside one or more
@@ -4148,7 +4253,7 @@ def bind(scope: ast.AST, name: str, start: int) -> None:
41484253
):
41494254
target_chain = target_chain[:-1]
41504255
stack.append((node.target, target_chain, end))
4151-
record_name_store_values(target_chain[-1], node.target, node.value, end)
4256+
record_name_store_values(target_chain[-1], [node.target], node.value, end)
41524257
elif isinstance(node, (ast.For, ast.AsyncFor)):
41534258
iteration_span = _node_character_span(parsed, node.iter)
41544259
iteration_end = iteration_span[1] if iteration_span is not None else end
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Repeated boolean targets must use their final assigned value."""
5+
6+
from __future__ import annotations
7+
8+
import pytest
9+
10+
from skillspector.nodes.analyzers import static_patterns_tool_misuse as tm
11+
12+
13+
def _scan(statement: str) -> list:
14+
source = (
15+
"import subprocess\n"
16+
"enabled = True\n" + statement + "subprocess.run('echo ok', shell=enabled)\n"
17+
)
18+
result = tm.node({"components": ["runner.py"], "file_cache": {"runner.py": source}})
19+
return [finding for finding in result["findings"] if finding.rule_id == "TM1"]
20+
21+
22+
@pytest.mark.parametrize("false_value", ["False", "0", "''", "None", "()"])
23+
@pytest.mark.parametrize(
24+
"assignment",
25+
[
26+
"enabled, enabled = True, {value}\n",
27+
"[enabled, enabled] = [True, {value}]\n",
28+
"enabled, (enabled, enabled) = False, (True, {value})\n",
29+
"enabled = enabled, enabled = True, {value}\n",
30+
],
31+
)
32+
def test_last_false_store_drops_earlier_truthy_value(assignment: str, false_value: str) -> None:
33+
assert not _scan(assignment.format(value=false_value))
34+
35+
36+
@pytest.mark.parametrize(
37+
"assignment",
38+
[
39+
"enabled, enabled = False, True\n",
40+
"[enabled, enabled] = [False, True]\n",
41+
"enabled, (enabled, enabled) = False, (False, True)\n",
42+
"enabled = enabled, enabled = False, True\n",
43+
],
44+
)
45+
def test_last_true_store_keeps_detection(assignment: str) -> None:
46+
findings = _scan(assignment)
47+
assert len(findings) == 1
48+
assert findings[0].severity == "HIGH"
49+
50+
51+
def test_direct_boolean_controls() -> None:
52+
assert len(_scan("")) == 1
53+
assert not _scan("enabled = False\n")
54+
55+
56+
@pytest.mark.parametrize(
57+
"assignment",
58+
[
59+
"enabled, enabled, enabled = True, False, enabled\n",
60+
"[enabled, enabled, enabled] = [True, False, enabled]\n",
61+
"enabled, (enabled, enabled) = True, (False, enabled)\n",
62+
"enabled = enabled, enabled = True, enabled\n",
63+
"enabled, enabled = True, dynamic\n",
64+
],
65+
)
66+
def test_other_final_values_preserve_existing_event_policy(assignment: str) -> None:
67+
assert len(_scan(assignment)) == 1
68+
69+
70+
@pytest.mark.parametrize(
71+
"assignment",
72+
[
73+
"enabled, enabled, enabled = True, opaque, False\n",
74+
"enabled, enabled, enabled = True, factory(), False\n",
75+
"enabled, enabled, other.value = True, False, 0\n",
76+
],
77+
)
78+
def test_opaque_assignment_effects_preserve_existing_event_policy(assignment: str) -> None:
79+
assert len(_scan(assignment)) == 1
80+
81+
82+
@pytest.mark.parametrize(
83+
"statement",
84+
[
85+
"other = factory()\nenabled, enabled, other = True, False, None\n",
86+
"enabled = factory()\nenabled, enabled = True, False\n",
87+
],
88+
)
89+
def test_unknown_prior_values_preserve_existing_event_policy(statement: str) -> None:
90+
assert len(_scan(statement)) == 1
91+
92+
93+
@pytest.mark.parametrize("parameter", [True, False])
94+
def test_local_target_requires_known_prior_value(parameter: bool) -> None:
95+
source = "import subprocess\ndef execute(" + ("other" if parameter else "") + "):\n"
96+
if not parameter:
97+
source += " other = None\n"
98+
source += (
99+
" enabled = True\n"
100+
" enabled, enabled, other = True, False, None\n"
101+
" subprocess.run('echo ok', shell=enabled)\n"
102+
)
103+
response = tm.node({"components": ["runner.py"], "file_cache": {"runner.py": source}})
104+
hits = [finding for finding in response["findings"] if finding.rule_id == "TM1"]
105+
assert len(hits) == int(parameter)
106+
107+
108+
def test_class_scope_preserves_existing_event_policy() -> None:
109+
source = (
110+
"import subprocess\n"
111+
"class Options:\n"
112+
" enabled = True\n"
113+
" enabled, enabled = True, False\n"
114+
" subprocess.run('echo ok', shell=enabled)\n"
115+
)
116+
response = tm.node({"components": ["runner.py"], "file_cache": {"runner.py": source}})
117+
hits = [finding for finding in response["findings"] if finding.rule_id == "TM1"]
118+
assert len(hits) == 1
119+
120+
121+
def test_other_imports_preserve_existing_event_policy() -> None:
122+
assert len(_scan("import math\nenabled, enabled = True, False\n")) == 1
123+
124+
125+
@pytest.mark.parametrize(
126+
"parameter, native_import",
127+
[("Popen", "import subprocess"), ("subprocess", "from subprocess import Popen")],
128+
)
129+
def test_native_import_does_not_make_other_parameter_safe(
130+
parameter: str, native_import: str
131+
) -> None:
132+
source = (
133+
"import subprocess\n"
134+
f"def execute({parameter}):\n"
135+
f" {native_import}\n"
136+
" enabled = True\n"
137+
f" enabled, enabled, {parameter} = True, False, None\n"
138+
+ (
139+
" subprocess.run('echo ok', shell=enabled)\n"
140+
if parameter == "Popen"
141+
else " Popen('echo ok', shell=enabled)\n"
142+
)
143+
)
144+
response = tm.node({"components": ["runner.py"], "file_cache": {"runner.py": source}})
145+
hits = [finding for finding in response["findings"] if finding.rule_id == "TM1"]
146+
assert len(hits) == 1
147+
148+
149+
@pytest.mark.parametrize(
150+
"target, native_import, receiver",
151+
[
152+
("Popen", "from subprocess import Popen", "subprocess.run"),
153+
("subprocess", "import subprocess", "Popen"),
154+
],
155+
)
156+
def test_fresh_native_import_keeps_its_own_binding_safe(
157+
target: str, native_import: str, receiver: str
158+
) -> None:
159+
source = (
160+
"import subprocess\n"
161+
"from subprocess import Popen\n"
162+
"def execute():\n"
163+
f" {native_import}\n"
164+
" enabled = True\n"
165+
f" enabled, enabled, {target} = True, False, None\n"
166+
f" {receiver}('echo ok', shell=enabled)\n"
167+
)
168+
response = tm.node({"components": ["runner.py"], "file_cache": {"runner.py": source}})
169+
hits = [finding for finding in response["findings"] if finding.rule_id == "TM1"]
170+
assert not hits

0 commit comments

Comments
 (0)