|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""A command wrapper followed directly by a control operator runs no command (#694).""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from skillspector.inspection_ledger import LedgerOutcome, LedgerReason |
| 11 | +from skillspector.nodes.analyzers import static_patterns_tool_misuse as tm_module |
| 12 | +from skillspector.nodes.analyzers import static_runner |
| 13 | + |
| 14 | +_PADDING = "\n# ordinary padding\n" * 400 |
| 15 | + |
| 16 | + |
| 17 | +def _exhausted(content: str, file_type: str, *, complete_context: bool = True) -> bool: |
| 18 | + return tm_module.has_bounded_parse_exhaustion( |
| 19 | + content, lambda: None, file_type=file_type, complete_context=complete_context |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("wrapper", ["timeout", "sudo", "nice", "xargs"]) |
| 24 | +def test_markdown_table_cell_naming_a_wrapper_is_complete(wrapper: str) -> None: |
| 25 | + content = f"| Name | Description |\n|---|---|\n| {wrapper} | Max wait in seconds |\n" |
| 26 | + |
| 27 | + assert not _exhausted(content, "markdown") |
| 28 | + assert not _exhausted(content + _PADDING, "markdown") |
| 29 | + |
| 30 | + |
| 31 | +def test_markdown_api_parameter_table_is_complete() -> None: |
| 32 | + content = ( |
| 33 | + "| Name | Type | Default | Description |\n" |
| 34 | + "|---|---|---|---|\n" |
| 35 | + "| timeout | float | 150 | Seconds to wait |\n" |
| 36 | + "| retries | int | 3 | Attempts |\n" |
| 37 | + ) |
| 38 | + |
| 39 | + assert not _exhausted(content, "markdown") |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + ("content", "file_type"), |
| 44 | + [ |
| 45 | + ("sudo | cat\n", "shell"), |
| 46 | + ("timeout; echo ok\n", "shell"), |
| 47 | + ("xargs || true\n", "shell"), |
| 48 | + ("nice & wait\n", "shell"), |
| 49 | + ("(nice)\n", "shell"), |
| 50 | + ("sudo -u | cat\n", "shell"), |
| 51 | + ("sudo |& cat\n", "shell"), |
| 52 | + ("timeout && echo ok\n", "shell"), |
| 53 | + ("case $a in x) nice ;; esac\n", "shell"), |
| 54 | + ("signal.alarm(timeout)\nvalue = 1\n", "python"), |
| 55 | + ("if (timeout) { start(); }\n", "javascript"), |
| 56 | + ], |
| 57 | + ids=[ |
| 58 | + "pipe", |
| 59 | + "semicolon", |
| 60 | + "or-list", |
| 61 | + "background", |
| 62 | + "subshell", |
| 63 | + "option-without-value", |
| 64 | + "pipe-with-stderr", |
| 65 | + "and-list", |
| 66 | + "case-item-end", |
| 67 | + "python-call", |
| 68 | + "javascript-condition", |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_wrapper_before_a_clause_end_is_complete(content: str, file_type: str) -> None: |
| 72 | + assert not _exhausted(content + _PADDING, file_type) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "content", |
| 77 | + [ |
| 78 | + "sudo >log $CMD -rf /\n", |
| 79 | + "sudo <in $CMD -rf /\n", |
| 80 | + "sudo &>log $CMD -rf /\n", |
| 81 | + "timeout ($CMD) -rf /\n", |
| 82 | + 'x | sudo sh -c "$CMD"\n', |
| 83 | + 'sudo |& sh -c "$CMD"\n', |
| 84 | + 'sudo && eval "$CMD"\n', |
| 85 | + "case $a in sudo) $CMD -rf / ;; esac\n", |
| 86 | + 'sudo -s; eval "$CMD"\n', |
| 87 | + ], |
| 88 | + ids=[ |
| 89 | + "stdout-redirection", |
| 90 | + "stdin-redirection", |
| 91 | + "bash-and-redirection", |
| 92 | + "parenthesis", |
| 93 | + "command-string", |
| 94 | + "command-string-after-pipe", |
| 95 | + "eval-after-and-list", |
| 96 | + "runtime-command-in-case-item", |
| 97 | + "eval-after-shell-option", |
| 98 | + ], |
| 99 | +) |
| 100 | +def test_wrapper_with_an_unresolved_command_stays_partial(content: str) -> None: |
| 101 | + assert _exhausted(content, "shell") |
| 102 | + |
| 103 | + |
| 104 | +def test_wrapper_at_the_end_of_a_fragment_stays_partial() -> None: |
| 105 | + assert _exhausted("x | sudo", "shell") |
| 106 | + assert _exhausted("x | timeout", "shell", complete_context=False) |
| 107 | + |
| 108 | + |
| 109 | +def test_markdown_cell_with_a_real_command_keeps_its_finding() -> None: |
| 110 | + literal = "| Step | Command |\n|---|---|\n| wipe | `sudo rm -rf /` |\n" |
| 111 | + runtime = "| Step | Command |\n|---|---|\n| wipe | `sudo $CMD -rf /` |\n" |
| 112 | + |
| 113 | + findings = tm_module.analyze(literal, "SKILL.md", "markdown") |
| 114 | + |
| 115 | + assert any(finding.rule_id == "TM1" for finding in findings) |
| 116 | + assert _exhausted(runtime, "markdown") |
| 117 | + |
| 118 | + |
| 119 | +def test_scan_level_parameter_table_is_fully_inspected() -> None: |
| 120 | + content = "# Tool\n\n| Name | Type |\n|---|---|\n| timeout | float |\n" + "\nText.\n" * 400 |
| 121 | + |
| 122 | + result = static_runner.run_static_patterns_with_ledger( |
| 123 | + {"components": ["SKILL.md"], "file_cache": {"SKILL.md": content}}, [tm_module] |
| 124 | + ) |
| 125 | + |
| 126 | + event = result["inspection_ledger"][0] |
| 127 | + assert event["outcome"] is LedgerOutcome.COMPLETED |
| 128 | + |
| 129 | + |
| 130 | +def test_scan_level_redirected_wrapper_stays_partial() -> None: |
| 131 | + result = static_runner.run_static_patterns_with_ledger( |
| 132 | + {"components": ["SKILL.md"], "file_cache": {"SKILL.md": "sudo >log $CMD -rf /\n"}}, |
| 133 | + [tm_module], |
| 134 | + ) |
| 135 | + |
| 136 | + event = result["inspection_ledger"][0] |
| 137 | + assert event["outcome"] is LedgerOutcome.PARTIAL |
| 138 | + assert event["reason_code"] is LedgerReason.STATIC_PARSE_LIMIT |
0 commit comments