Skip to content

Commit 5d8f538

Browse files
authored
Merge pull request #779 from elliottwaves-20/fix/694-wrapper-without-command
fix(analyzer): treat a command wrapper with no command as complete
2 parents b07f181 + 623284f commit 5d8f538

2 files changed

Lines changed: 162 additions & 2 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,8 +2390,10 @@ def resolved_command_string(word: str | None, limited: bool) -> str | None:
23902390
wrapper_seen = True
23912391
while True:
23922392
option, limited = next_word()
2393-
if limited or option is None:
2393+
if limited:
23942394
return True, None
2395+
if option is None:
2396+
return not _ends_wrapped_clause(content, cursor), None
23952397
if option == "--":
23962398
pending, limited = next_word()
23972399
if limited:
@@ -2423,8 +2425,10 @@ def resolved_command_string(word: str | None, limited: bool) -> str | None:
24232425
wrapper_seen = True
24242426
while True:
24252427
option, limited = next_word()
2426-
if limited or option is None:
2428+
if limited:
24272429
return True, None
2430+
if option is None:
2431+
return not _ends_wrapped_clause(content, cursor), None
24282432
if option in {"-k", "--kill-after", "-s", "--signal"}:
24292433
_, limited = next_word()
24302434
if limited:
@@ -2441,6 +2445,24 @@ def resolved_command_string(word: str | None, limited: bool) -> str | None:
24412445
return wrapper_seen, None
24422446

24432447

2448+
def _ends_wrapped_clause(content: str, cursor: int) -> bool:
2449+
"""Return whether a wrapper's missing command is a proven end of its clause.
2450+
2451+
When a control operator follows ``sudo``, ``nice``, ``xargs`` or
2452+
``timeout`` directly, the clause names no wrapped command, so there is no
2453+
command string to reconstruct. A shell such a wrapper starts on its own
2454+
(``sudo -s``) reads its input like ``sh`` in a pipeline, which this check
2455+
does not model either. Commands after the operator start their own
2456+
clause and are checked separately. A redirection may still precede the
2457+
wrapped command, and a fragment may continue past its end, so both
2458+
remain unresolved.
2459+
"""
2460+
if cursor >= len(content):
2461+
return False
2462+
character = content[cursor]
2463+
return character in ";|)" or (character == "&" and content[cursor + 1 : cursor + 2] != ">")
2464+
2465+
24442466
def _command_wrapper_quote(content: str, command_start: int) -> str | None:
24452467
if command_start == 0 or content[command_start - 1] not in "'\"`":
24462468
return None
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

Comments
 (0)