Skip to content

Commit 63ae555

Browse files
fix(analyzer): keep quoted-expansion ownership local to a committed parse
A parse that starts inside a comment can close a double quote on the next line and record the real command there as quote-enclosed. When that parse was later discarded or confined to the comment, the record stayed, and the command's own operand scan stopped at its first quote, so `$CMD"" -rf /` was reported as completely inspected. Collect the positions per candidate and commit them only under the gate that already protects owned word positions. Reported-by: yashrajp22 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: elliottwaves-20 <[email protected]>
1 parent 8c8f43a commit 63ae555

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,7 @@ def _has_shell_command_word_exhaustion(
19971997
if _has_quoted_assignment_prefix(content, start):
19981998
continue
19991999
candidate_word_positions: set[int] = set()
2000+
candidate_quoted_expansions: set[int] = set()
20002001
parsed = _parse_shell_command_word(
20012002
content,
20022003
start,
@@ -2012,7 +2013,7 @@ def _has_shell_command_word_exhaustion(
20122013
if start in quoted_expansion_starts
20132014
else None
20142015
),
2015-
quoted_expansion_starts=quoted_expansion_starts,
2016+
quoted_expansion_starts=candidate_quoted_expansions,
20162017
)
20172018
if parsed is None:
20182019
substitution_start = (
@@ -2069,6 +2070,10 @@ def _has_shell_command_word_exhaustion(
20692070
for position in candidate_word_positions
20702071
if content[position + 1 : position + 2] not in ("$", "`")
20712072
)
2073+
# Quote ownership follows the same rule: a discarded parse, or one
2074+
# confined to a comment or assignment, never decides where a later
2075+
# command's operands end.
2076+
quoted_expansion_starts.update(candidate_quoted_expansions)
20722077
# Only executable nested substitutions retain independent command
20732078
# positions. A plain dynamic data argument still owns its inner bytes;
20742079
# revisiting those as commands would turn quoted printf data into code.

‎tests/nodes/analyzers/test_double_quoted_expansion_word.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,31 @@ def test_scan_level_helper_script_is_fully_inspected() -> None:
132132

133133
event = result["inspection_ledger"][0]
134134
assert event["outcome"] is LedgerOutcome.COMPLETED
135+
136+
137+
@pytest.mark.parametrize(
138+
"content",
139+
[
140+
'# "prefix $(date)\n$CMD"" -rf /\n',
141+
'# "prefix $(date)\n$CMD" x"" -rf /\n',
142+
'#!/bin/sh\n# see "notes $(date)\n$CMD"" -rf /\n',
143+
],
144+
ids=["failed-comment-parse", "completed-comment-parse", "after-shebang"],
145+
)
146+
def test_quote_from_a_comment_parse_grants_no_ownership(content: str) -> None:
147+
# A comment ends at the newline, so the real command on the next line owns
148+
# its quotes. A discarded or comment-confined parse must not mark that
149+
# command as enclosed by a quote and thereby hide ``-rf /``.
150+
assert _exhausted(content)
151+
assert _exhausted(content + _PADDING)
152+
153+
154+
def test_reviewer_script_is_not_fully_inspected() -> None:
155+
script = '# "prefix $(date)\n$CMD"" -rf /\n'
156+
157+
result = static_runner.run_static_patterns_with_ledger(
158+
{"components": ["run.sh"], "file_cache": {"run.sh": script}}, [tm_module]
159+
)
160+
161+
event = result["inspection_ledger"][0]
162+
assert event["outcome"] is not LedgerOutcome.COMPLETED

0 commit comments

Comments
 (0)