Skip to content

Commit 7822cb1

Browse files
authored
Merge pull request #634 from deepujain/fix/628-shell-assignment-quote-ownership
fix(analyzer): avoid false parse limits on quoted shell assignments
2 parents ec6633e + 11d1629 commit 7822cb1

2 files changed

Lines changed: 214 additions & 2 deletions

File tree

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
_RUNTIME_SHELL_PARAMETER_SENTINEL = "\ue002"
6767
_SIMPLE_BRACED_PARAMETER_RE = re.compile(r"\$\{(?:[A-Za-z_][A-Za-z0-9_]*|[0-9]+|[@*#?$!-])\}")
6868
_SHELL_DELIMITER_WORD_RE = re.compile(r"[^$'\"`\\(){}<>#;|&!\s]++")
69+
# A ``#`` that starts a word begins a shell comment. Quoting is not tracked, so
70+
# ``echo "a # b"`` also matches; callers only use this to restrict ownership.
71+
_SHELL_COMMENT_START_RE = re.compile(r"(?<![^\s;&|()<>])#")
6972
_PERL_LITERAL_PRINT_RE = re.compile(
7073
r"^[ \t]*+print\b(?:[ \t]++(?:STDOUT|STDERR)\b)?[ \t]*+(?P<paren>\()?[ \t]*+"
7174
r"(?P<literal>\"(?:\\[\\\"'nrt]|[^\\\"$@`\r\n])*+\""
@@ -1421,9 +1424,14 @@ def _parse_shell_command_word(
14211424
backtick_end_cache: dict[int, int | None] | None = None,
14221425
*,
14231426
check_runtime: Callable[[], None] | None = None,
1427+
owned_word_positions: set[int] | None = None,
1428+
enclosing_delimiter: str | None = None,
14241429
) -> _ShellCommandWord | None:
14251430
runtime_check = check_runtime or (lambda: None)
14261431
output: list[str] = []
1432+
wrapper_quote = enclosing_delimiter or (
1433+
_command_wrapper_quote(content, start) if content[start] in "$`" else None
1434+
)
14271435
quote: str | None = None
14281436
ansi_c_quote = False
14291437
dynamic = False
@@ -1437,6 +1445,8 @@ def _parse_shell_command_word(
14371445
character = content[cursor]
14381446
if quote is not None:
14391447
if character == quote:
1448+
if owned_word_positions is not None:
1449+
owned_word_positions.add(cursor)
14401450
quote = None
14411451
ansi_c_quote = False
14421452
elif character == "\\" and ansi_c_quote:
@@ -1598,6 +1608,11 @@ def _parse_shell_command_word(
15981608
cursor = parameter_end
15991609
continue
16001610
elif character == "`":
1611+
if enclosing_delimiter == "`":
1612+
# The enclosing substitution ends here; this is not an opener.
1613+
if owned_word_positions is not None:
1614+
owned_word_positions.add(cursor)
1615+
break
16011616
substitution_end = _skip_backtick_substitution(
16021617
content,
16031618
cursor,
@@ -1630,6 +1645,8 @@ def _parse_shell_command_word(
16301645
cursor = substitution_end
16311646
continue
16321647
elif character in "'\"":
1648+
if character == wrapper_quote:
1649+
break
16331650
quote = character
16341651
elif character == "\\" and cursor + 1 < limit:
16351652
if content[cursor + 1] == "\n":
@@ -1726,6 +1743,9 @@ def _has_shell_command_word_exhaustion(
17261743
) -> bool:
17271744
"""Find candidate command words whose deterministic parse hit a safety bound."""
17281745
parsed_through = 0
1746+
# Completed words own closing quotes, never executable expansion starts.
1747+
# Inner commands remain independent candidates; never suppress their bodies.
1748+
owned_word_positions: set[int] = set()
17291749
parameter_end_cache: dict[int, _ParameterExpansionEnd] = {}
17301750
substitution_end_cache: dict[int, int | None] = {}
17311751
backtick_end_cache: dict[int, int | None] = {}
@@ -1734,24 +1754,34 @@ def _has_shell_command_word_exhaustion(
17341754
for candidate in _SHELL_COMMAND_WORD_START_RE.finditer(content):
17351755
check_runtime()
17361756
start = candidate.start()
1757+
if start in owned_word_positions:
1758+
continue
17371759
if structural_quote_closers is not None and start in structural_quote_closers:
17381760
continue
17391761
json_string_start = structural_quote_openers is not None and (
17401762
start in structural_quote_openers or start - 1 in structural_quote_openers
17411763
)
1764+
assignment_quote = start > 0 and content[start - 1] == "=" and content[start] in "'\""
17421765
if start < parsed_through or (
1743-
not json_string_start and not _is_shell_command_word_start(content, start)
1766+
not json_string_start
1767+
and not assignment_quote
1768+
and not _is_shell_command_word_start(content, start)
17441769
):
17451770
continue
17461771
if _has_quoted_assignment_prefix(content, start):
17471772
continue
1773+
candidate_word_positions: set[int] = set()
17481774
parsed = _parse_shell_command_word(
17491775
content,
17501776
start,
17511777
parameter_end_cache,
17521778
substitution_end_cache,
17531779
backtick_end_cache,
17541780
check_runtime=check_runtime,
1781+
owned_word_positions=candidate_word_positions,
1782+
enclosing_delimiter=(
1783+
_assignment_value_wrapper(content, start) if assignment_quote else None
1784+
),
17551785
)
17561786
if parsed is None:
17571787
substitution_start = (
@@ -1789,6 +1819,21 @@ def _has_shell_command_word_exhaustion(
17891819
if unresolved_end - start > _SHELL_COMMAND_WORD_CHARS:
17901820
return True
17911821
continue
1822+
# Comments end at a newline, and a quote after ``=`` in prose or host
1823+
# source may be mis-paired. Such a word cannot own a later line.
1824+
confined_word = "\n" in content[start : parsed.end] and (
1825+
assignment_quote
1826+
or _SHELL_COMMENT_START_RE.search(content, content.rfind("\n", 0, start) + 1, start)
1827+
is not None
1828+
)
1829+
if not confined_word:
1830+
# A quote that opens a runtime-selected word stays an independent
1831+
# candidate, so a mis-paired claim cannot hide its operands.
1832+
owned_word_positions.update(
1833+
position
1834+
for position in candidate_word_positions
1835+
if content[position + 1 : position + 2] not in ("$", "`")
1836+
)
17921837
# Only executable nested substitutions retain independent command
17931838
# positions. A plain dynamic data argument still owns its inner bytes;
17941839
# revisiting those as commands would turn quoted printf data into code.
@@ -1805,8 +1850,12 @@ def _has_shell_command_word_exhaustion(
18051850
or "$" not in raw_word
18061851
or not any(marker in raw_word for marker in ("$(", "`"))
18071852
or simple_backtick_parameter
1808-
):
1853+
) and not (assignment_quote and confined_word):
18091854
parsed_through = max(parsed_through, parsed.end)
1855+
if assignment_quote:
1856+
# An assignment value never names the command, and main never
1857+
# parsed this position, so it only claims ownership.
1858+
continue
18101859
if parsed.limited:
18111860
return True
18121861
if parsed.dynamic and may_have_destructive_outer_operands:
@@ -2089,6 +2138,19 @@ def _command_wrapper_quote(content: str, command_start: int) -> str | None:
20892138
return content[command_start - 1] if backslashes % 2 == 0 else None
20902139

20912140

2141+
def _assignment_value_wrapper(content: str, value_start: int) -> str | None:
2142+
"""Return an unescaped quote or backtick directly enclosing an assignment."""
2143+
name_start = value_start - 1
2144+
if name_start > 0 and content[name_start - 1] == "+":
2145+
name_start -= 1
2146+
while name_start > 0 and (
2147+
content[name_start - 1] == "_"
2148+
or (content[name_start - 1].isascii() and content[name_start - 1].isalnum())
2149+
):
2150+
name_start -= 1
2151+
return _command_wrapper_quote(content, name_start)
2152+
2153+
20922154
def _perl_literal_print_shell_text(
20932155
content: str,
20942156
check_runtime: Callable[[], None],
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import json
5+
from pathlib import Path
6+
7+
import pytest
8+
from typer.testing import CliRunner
9+
10+
from skillspector.cli import app
11+
from skillspector.nodes.analyzers.static_patterns_tool_misuse import has_bounded_parse_exhaustion
12+
13+
_COMMENT_PADDING = "\n" + "#" * 6000 + "\n"
14+
_APOSTROPHE_PADDING = "\n" + "y = 'b'\n" * 800
15+
16+
17+
@pytest.mark.parametrize(
18+
"source",
19+
[
20+
'out="$(date)"',
21+
'v="a (b)"',
22+
'v="|$a|"',
23+
'printf "%s" "$(f "$x")"',
24+
'[ "$(f)" = true ]',
25+
'cat <<< "$(f)"',
26+
'v="a \\" (b)"',
27+
'v="a \\" |$a|"',
28+
],
29+
)
30+
def test_complete_shell_quotes_do_not_consume_following_padding(source: str) -> None:
31+
assert not has_bounded_parse_exhaustion(source + "\n" + "#" * 6000 + "\n", lambda: None)
32+
33+
34+
@pytest.mark.parametrize(
35+
"source", ['x="$(unterminated', 'x="unclosed', '"$(printf "%s" "$x")" -rf /']
36+
)
37+
def test_unresolved_shell_words_still_exhaust(source: str) -> None:
38+
assert has_bounded_parse_exhaustion(source + "\n" + "#" * 6000 + "\n", lambda: None)
39+
40+
41+
def test_assignment_ownership_retains_nested_command_budget() -> None:
42+
nested_word = "r" * 4097
43+
source = f'out="$({nested_word} -rf /)"'
44+
assert has_bounded_parse_exhaustion(source, lambda: None)
45+
46+
47+
def test_failed_word_parse_does_not_claim_following_command() -> None:
48+
source = 'Test-Path "$($_.FullName)\\cli-path"; $($CMD) -rf /'
49+
assert has_bounded_parse_exhaustion(source, lambda: None)
50+
51+
52+
@pytest.mark.parametrize(
53+
"source", ['Run "$(resolve_tool) -rf /"', '# see "notes\n$(resolve_tool) -rf /\n# "']
54+
)
55+
def test_quoted_runtime_commands_keep_fail_closed_coverage(source: str) -> None:
56+
from skillspector.inspection_ledger import LedgerOutcome, LedgerReason
57+
from skillspector.nodes.analyzers import static_patterns_tool_misuse, static_runner
58+
59+
assert has_bounded_parse_exhaustion(source, lambda: None)
60+
path = "script.sh" if source.startswith("#") else "SKILL.md"
61+
result = static_runner.run_static_patterns_with_ledger(
62+
{"components": [path], "local_file_cache": {path: source}, "file_cache": {path: source}},
63+
[static_patterns_tool_misuse],
64+
)
65+
assert any(
66+
e["outcome"] == LedgerOutcome.PARTIAL
67+
and e["reason_code"] == LedgerReason.STATIC_PARSE_LIMIT
68+
for e in result["inspection_ledger"]
69+
)
70+
71+
72+
@pytest.mark.parametrize("file_type", ["shell", "markdown", "python"])
73+
def test_non_shell_unclosed_assignment_remains_conservative(file_type: str) -> None:
74+
source = 'name="value' + "\n" + "text " * 1200
75+
assert has_bounded_parse_exhaustion(source, lambda: None, file_type=file_type)
76+
77+
78+
@pytest.mark.parametrize(
79+
("source", "file_type"),
80+
[
81+
('# use "$(cmd)" here', "shell"),
82+
('```bash\nout="$(date)"\n```', "markdown"),
83+
('x="$(printf \'%s\' "$X")"', "shell"),
84+
("model_count=\"$(printf '%s\\n' \"$j\" | jq -r 'length')\"", "shell"),
85+
],
86+
ids=["issue-comment-form", "markdown-fence", "printf-value", "printf-pipeline-value"],
87+
)
88+
def test_benign_quoted_values_do_not_consume_comment_padding(source: str, file_type: str) -> None:
89+
assert not has_bounded_parse_exhaustion(
90+
source + _COMMENT_PADDING, lambda: None, file_type=file_type
91+
)
92+
93+
94+
@pytest.mark.parametrize(
95+
("source", "file_type"),
96+
[
97+
('# Keep `X="${X}"` so X\'s value wins.', "python"),
98+
("# Keep `X='${X}'`, so X's value wins.", "python"),
99+
('Use `X="${X}"`. That\'s the self-reference.', "shell"),
100+
],
101+
ids=["double-quoted", "single-quoted", "prose"],
102+
)
103+
def test_backtick_enclosed_assignment_stops_at_its_closing_backtick(
104+
source: str, file_type: str
105+
) -> None:
106+
assert not has_bounded_parse_exhaustion(
107+
source + _APOSTROPHE_PADDING, lambda: None, file_type=file_type
108+
)
109+
110+
111+
@pytest.mark.parametrize(
112+
("source", "file_type"),
113+
[
114+
('ls # see "notes\n"$(resolve_tool)" -rf /\n# "', "shell"),
115+
('Set name="value in prose.\n\n```bash\n"$(resolve_tool)" -rf /\n```\n"\n', "markdown"),
116+
('x = f(name="value)\n"$(resolve_tool)" -rf /\n"\n', "python"),
117+
('Set name="value\n$T -rf /\n"\n', "markdown"),
118+
('name="a "$(resolve_tool)" -rf /"', "shell"),
119+
],
120+
ids=[
121+
"trailing-comment",
122+
"prose-assignment-before-fence",
123+
"host-language-assignment",
124+
"prose-assignment-parameter",
125+
"same-line-runtime-opener",
126+
],
127+
)
128+
def test_misaligned_quote_claims_keep_runtime_commands_partial(source: str, file_type: str) -> None:
129+
assert has_bounded_parse_exhaustion(source, lambda: None, file_type=file_type)
130+
131+
132+
def test_issue_628_bundle_reports_complete_coverage(tmp_path: Path) -> None:
133+
(tmp_path / "SKILL.md").write_text(
134+
"---\nname: repro\ndescription: Minimal repro.\n---\nRun `scripts/run.sh`.\n",
135+
encoding="utf-8",
136+
)
137+
padding = "".join(f"# padding line {index} " + "." * 50 + "\n" for index in range(90))
138+
(tmp_path / "scripts").mkdir()
139+
(tmp_path / "scripts" / "run.sh").write_text(
140+
'#!/bin/bash\nout="$(date)"\necho "$out"\n' + padding, encoding="utf-8"
141+
)
142+
143+
result = CliRunner().invoke(app, ["scan", str(tmp_path), "--format", "json", "--no-llm"])
144+
145+
# Exit 1 is the documented high-risk verdict, not a scan execution failure.
146+
assert result.exit_code in {0, 1}, result.output
147+
report = json.loads(result.output)
148+
assert report["analysis_completeness"]["status"] == "complete"
149+
assert report["analysis_completeness"]["ledger_exceptions"] == []
150+
assert not any(issue["id"] == "AE1" for issue in report["issues"])

0 commit comments

Comments
 (0)