Skip to content

Commit 2ba149d

Browse files
rng1995claude
andcommitted
fix(analyzer): keep one TM1 owner for true-prefixed names without AST proof
Port the six true-prefixed TM1 regression tests from NVIDIA#577. This branch already carries test_true_direct_calls_on_one_line_keep_distinct_locations unchanged, so only the other five are added. Two of them reported the same call twice on this branch: - A variable window for a name spelled `true` (in any case) duplicated the case-insensitive direct `shell=True` owner at the same call. AST reconciliation removes that window only when the dataflow companion takes the call, so a file that does not parse, or a call after an unknown receiver effect, kept both findings. coalesce_path_findings now drops such a window when a direct lexical owner starts at the same call. A window for a call whose direct candidate was folded into an identical same-line call still reports that call. - A call-anchored window for a longer true-prefixed name (`true_value`) used its raw text as identity, so the raw and normalized security views of one call did not collapse, and reconciliation then moved both to the call. The window now takes its identity from the normalized view text, as the direct owner already does. Also cover both root causes with a test for a nested `true` call after an unknown receiver effect and a normalized `true_value` call in a file that does not parse. Signed-off-by: Narendran Raghavan <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8ed05b6 commit 2ba149d

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5162,6 +5162,9 @@ def ctx(start: int) -> str:
51625162
if call_start is not None:
51635163
if variable_match[0].casefold().startswith("true"):
51645164
evidence[static_runner._VIEW_START_EVIDENCE] = call_start
5165+
# Like a direct owner, a call-anchored window keeps one
5166+
# identity across raw and normalized security views.
5167+
complete_identity_match = normalized_security_view(matched_text).text
51655168
evidence[static_runner._VIEW_ANCHOR_EVIDENCE] = call_start
51665169
finding = AnalyzerFinding(
51675170
rule_id="TM1",
@@ -5277,12 +5280,26 @@ def coalesce_path_findings(content: str, findings: list[Finding]) -> list[Findin
52775280
)
52785281
is int
52795282
}
5283+
# The case-insensitive direct ``shell=True`` pattern also matches a name
5284+
# spelled ``true``, so its lexical owner already reports that exact call
5285+
# even when no Python AST can reconcile the variable window.
5286+
true_name_call_starts = bound_call_starts | {
5287+
coordinate
5288+
for finding in findings
5289+
if finding.evidence.get(LEXICAL_DIRECT_SHELL_EVIDENCE) is True
5290+
for key in (
5291+
static_runner._ABSOLUTE_START_EVIDENCE,
5292+
static_runner._ABSOLUTE_ALTERNATE_START_EVIDENCE,
5293+
)
5294+
if type(coordinate := finding.evidence.get(key)) is int
5295+
}
52805296
findings = [
52815297
finding
52825298
for finding in findings
52835299
if not (
5284-
isinstance(finding.evidence.get(_VARIABLE_SHELL_FLAG_EVIDENCE), str)
5285-
and finding.evidence.get(static_runner._ABSOLUTE_ANCHOR_EVIDENCE) in bound_call_starts
5300+
isinstance(variable_name := finding.evidence.get(_VARIABLE_SHELL_FLAG_EVIDENCE), str)
5301+
and finding.evidence.get(static_runner._ABSOLUTE_ANCHOR_EVIDENCE)
5302+
in (true_name_call_starts if variable_name.casefold() == "true" else bound_call_starts)
52865303
)
52875304
]
52885305
canonical_bound_calls: dict[tuple[str, int, str], set[int]] = {}

‎tests/nodes/analyzers/test_tool_misuse_python_ast.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,3 +1508,84 @@ def test_python_window_surface_enables_ast_companion() -> None:
15081508
)
15091509

15101510
assert len(findings) == 1
1511+
1512+
1513+
@pytest.mark.parametrize("name", ["true", "TRUE", "TrUe"])
1514+
def test_case_variant_true_has_one_direct_owner(name: str) -> None:
1515+
findings = _tm1(f"{name} = True\nsubprocess.run(command, shell={name})\n")
1516+
1517+
assert len(findings) == 1
1518+
assert findings[0].start_line == 2
1519+
assert not findings[0].evidence
1520+
1521+
1522+
def test_normalized_prefix_keeps_lexical_signal_after_unknown_receiver_effect() -> None:
1523+
assert (
1524+
len(
1525+
_tm1(
1526+
"true_value = True\n"
1527+
"def inner():\n"
1528+
" ff; subprocess.run(command, shell=true_value)\n"
1529+
"replace_subprocess()\n"
1530+
"inner()\n"
1531+
)
1532+
)
1533+
== 1
1534+
)
1535+
1536+
1537+
def test_normalized_prefix_does_not_duplicate_true_direct_fallback() -> None:
1538+
findings = _tm1("true = True\nff; subprocess.run(command, shell=true)\n")
1539+
1540+
assert len(findings) == 1
1541+
assert (findings[0].start_line, findings[0].start_column) == (2, 3)
1542+
assert not findings[0].evidence
1543+
1544+
1545+
def test_true_direct_lexical_signal_survives_unknown_receiver_effect() -> None:
1546+
findings = _tm1(
1547+
"true = True\n"
1548+
"subprocess.run(command, shell=true)\n"
1549+
"replace_subprocess()\n"
1550+
"subprocess.run(command, shell=true)\n"
1551+
)
1552+
1553+
assert [finding.start_line for finding in findings] == [2, 4]
1554+
1555+
1556+
@pytest.mark.parametrize(
1557+
("path", "suffix"),
1558+
[
1559+
pytest.param("run.py", "if:\n", id="malformed-python"),
1560+
pytest.param("run.js", "", id="non-python"),
1561+
],
1562+
)
1563+
def test_true_direct_fallback_survives_without_ast_ownership(path: str, suffix: str) -> None:
1564+
findings = _tm1(f"true = True\nsubprocess.run(command, shell=true)\n{suffix}", path)
1565+
1566+
assert len(findings) == 1
1567+
assert not findings[0].evidence
1568+
1569+
1570+
@pytest.mark.parametrize(
1571+
"content",
1572+
[
1573+
pytest.param(
1574+
"true = True\n"
1575+
"def inner():\n"
1576+
" subprocess.run(command, shell=true)\n"
1577+
"replace_subprocess()\n"
1578+
"inner()\n",
1579+
id="direct-true-after-unknown-receiver-effect",
1580+
),
1581+
pytest.param(
1582+
"true_value = True\nff; subprocess.run(command, shell=true_value)\nif:\n",
1583+
id="normalized-prefix-without-ast",
1584+
),
1585+
],
1586+
)
1587+
def test_true_named_shell_window_has_one_owner_per_call(content: str) -> None:
1588+
findings = _tm1(content)
1589+
1590+
assert len(findings) == 1
1591+
assert not findings[0].evidence

0 commit comments

Comments
 (0)