From 837767f9a8ac9f827a5cf084a7c29166a3add245 Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Fri, 13 Nov 2026 10:50:22 +0000 Subject: [PATCH 1/4] fix(analyzer): treat quoted $(printf ... | ...) as runtime data, not a parse limit A POSIX shell printf whose argument is a double-quoted command substitution piping printf '%s' "$1" into another command made static_patterns_tool_misuse record static_parse_limit (printf_format / unsupported_syntax), so the file counted as partially inspected. The inner substitution's output comes from the downstream command, not from printf, so it is runtime-dependent data in an argument position of the outer printf, as for the echo and assignment variants. Add _printf_invocation_has_following_operator, which walks the substitution with the shell word reader and reports whether a pipeline, command list, or redirection follows the printf words. The quoted-result branch of _static_printf_substitution returns unresolved without a diagnostic in that case, and _is_printf_substitution no longer treats it as a bounded printf substitution. A bare printf with a runtime argument stays fail-closed, a pipe quoted inside a printf argument still does not count as a pipeline, and literal printf handling (#694) is unchanged. Fixes #1107 Signed-off-by: Varshith Puli --- .../analyzers/static_patterns_tool_misuse.py | 42 ++++++ .../test_static_parse_limit_issue_1107.py | 134 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 tests/nodes/analyzers/test_static_parse_limit_issue_1107.py diff --git a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py index 098d1c1e9..07ea5c98f 100644 --- a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py +++ b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py @@ -2227,6 +2227,26 @@ def _has_literal_printf_arguments(inner: str) -> bool: ) +def _printf_invocation_has_following_operator(inner: str) -> bool: + """Return whether a leading printf invocation is followed by shell operators. + + A pipeline, command list, or redirection after the printf words means + the substitution's output is produced by another command (or discarded), + so the value is runtime-dependent data rather than a bounded printf + result. The word reader consumes quoted words whole, so an operator + inside the printf arguments (``'a|b'``) does not count, and an + unresolved boundary stays fail-closed. A comment start is not an + operator: the printf invocation is complete before it. + """ + cursor = 0 + while True: + word, cursor, limited = _next_shell_invocation_word(inner, cursor, lambda: None) + if word is None: + if limited or cursor >= len(inner): + return False + return inner[cursor] in ";|&()<>" + + def _static_printf_substitution( content: str, start: int, @@ -2290,6 +2310,17 @@ def failed( inner_start + high, ParserUncheckedScope.EXPRESSION, ) + literal_result = quoted_result and not backtick and _has_literal_printf_arguments(inner) + if quoted_result and not backtick and not literal_result: + command, _, _ = _next_shell_invocation_word(inner, 0, lambda: None) + if command == "printf": + if _printf_invocation_has_following_operator(inner): + # The printf output is piped (or listed/redirected) into + # another command, so the substitution's value is that + # command's runtime-dependent output, not a bounded printf + # result. Return unresolved without a parse-limit diagnostic. + return None + failed() return None if "|" in inner and (pipeline := _SYMBOLIC_PRINTF_PIPELINE_RE.fullmatch(inner)): # A variable grep pattern can supply flags as well as change matching. @@ -2327,6 +2358,10 @@ def failed( else ParserCause.UNSUPPORTED_SYNTAX ) return None + if recognized and not literal_result and _printf_invocation_has_following_operator(inner): + # Same treatment for allowlisted wrappers (``command``/``builtin``/ + # ``env``) whose first word is not literally ``printf``. + return None if not recognized or (not literal_result and re.search(r"[;|&<>\r\n]", inner) is not None): if diagnostics is not None and invocation_diagnostics: diagnostics.extend(item.shifted(inner_start) for item in invocation_diagnostics[:1]) @@ -2577,6 +2612,13 @@ def _is_printf_substitution( recognized, _ = _printf_invocation_arguments( inner, check_runtime=runtime_check, diagnostics=invocation_diagnostics ) + if recognized and _printf_invocation_has_following_operator(inner): + # A pipeline, command list, or redirection after the printf words + # means the substitution's value is produced by another command, + # so this is not a bounded printf substitution: the value is + # runtime-dependent data, as for an ``echo`` pipeline, and its + # failure to resolve statically is not a coverage gap. + return False if recognized or not check_command_context: if recognized and diagnostics is not None: diagnostics.extend(item.shifted(inner_start) for item in invocation_diagnostics[:1]) diff --git a/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py b/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py new file mode 100644 index 000000000..272ad9583 --- /dev/null +++ b/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py @@ -0,0 +1,134 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""A quoted ``$(printf ... | ...)`` argument must not look partially inspected. + +Issue #1107: a POSIX shell ``printf`` whose argument is a double-quoted +command substitution piping ``printf '%s' "$1"`` into another command made +``static_patterns_tool_misuse`` record ``static_parse_limit`` +(``printf_format`` / ``unsupported_syntax``), so the file counted as +partially inspected. The inner substitution's output comes from the +downstream command, not from ``printf``, so it is runtime-dependent data in +an argument position of the outer ``printf``, exactly as for the ``echo`` +and assignment variants. The quoted-result branch of +``_static_printf_substitution`` failed whenever a quoted substitution +started with ``printf`` and its arguments were not literal, even when the +substitution was a pipeline whose output another command produces. +""" + +from __future__ import annotations + +import pytest + +from skillspector.inspection_ledger import LedgerOutcome +from skillspector.nodes.analyzers import static_patterns_tool_misuse as tm +from skillspector.nodes.analyzers import static_runner + +# Verbatim "quote a value for sh/cron" idiom from the issue report. +_CRONQ = ( + "#!/bin/sh\n" + "cronq() { printf \"'%s'\" \"$(printf '%s' \"$1\" | sed -e \"s/'/'\\\\''/g\" -e 's/%/\\\\%/g')\"; }\n" +) + +# The five line variants from the issue table: the two printf-inner cases +# must complete, and the three controls must stay complete. +_ISSUE_VARIANTS = [ + pytest.param( + """printf "'%s'" "$(printf '%s' "$1" | sed 's/a/b/')" """, + id="printf-inner-single-quoted-format", + ), + pytest.param( + """printf '%s\\n' "$(printf '%s' "$1" | sed 's/a/b/')" """, + id="printf-inner-newline-format", + ), + pytest.param( + """printf "'%s'" "$(echo "$1" | sed 's/a/b/')" """, + id="echo-inner-control", + ), + pytest.param( + """x="$(printf '%s' "$1" | sed 's/a/b/')" """, + id="assignment-control", + ), + pytest.param( + """printf "'%s'" "$1" """, + id="plain-parameter-control", + ), +] + + +@pytest.mark.parametrize("line", _ISSUE_VARIANTS) +def test_issue_variants_complete_static_analysis(line: str) -> None: + source = "#!/bin/sh\n" + line.strip() + "\n" + assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is False + + +@pytest.mark.parametrize( + "line", + [ + pytest.param( + """printf "'%s'" "$(printf '%s' "$1" | sed 's/a/b/')" """, + id="printf-inner-single-quoted-format", + ), + pytest.param( + """printf '%s\\n' "$(printf '%s' "$1" | sed 's/a/b/')" """, + id="printf-inner-newline-format", + ), + ], +) +def test_issue_reproducer_reaches_completed_ledger_outcome(line: str) -> None: + path = "scripts/cron.sh" + source = "#!/bin/sh\n" + line.strip() + "\n" + result = static_runner.run_static_patterns_with_ledger( + {"components": [path], "file_cache": {path: source}}, [tm] + ) + row = result["inspection_ledger"][0] + assert row["path"] == path + assert row["analyzer_id"] == "static_patterns_tool_misuse" + assert row["outcome"] is LedgerOutcome.COMPLETED + assert "reason_code" not in row + + +def test_cronq_idiom_reaches_completed_ledger_outcome() -> None: + path = "scripts/cron.sh" + result = static_runner.run_static_patterns_with_ledger( + {"components": [path], "file_cache": {path: _CRONQ}}, [tm] + ) + row = result["inspection_ledger"][0] + assert row["outcome"] is LedgerOutcome.COMPLETED + assert "reason_code" not in row + + +def test_literal_elf_probe_behavior_is_unchanged() -> None: + """Issue #694: a literal ``$(printf '\\177ELF\\001')`` still resolves.""" + probe = "$(printf '\\177ELF\\001')" + assert tm._static_printf_substitution(probe, 0, len(probe)) == "\x7fELF\x01" + source = ( + "#!/bin/sh\n" + 'if [ "$_current_exe_head" = "$(printf \'\\177ELF\\001\')" ]; then\n' + " echo elf\n" + "fi\n" + ) + assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is False + + +@pytest.mark.parametrize( + "line", + [ + pytest.param( + """printf "'%s'" "$(printf '%s' "$1")" """, + id="bare-printf-with-runtime-argument", + ), + pytest.param( + """printf "'%s'" "$(printf '%s' "$1" 'a|b')" """, + id="quoted-pipe-inside-printf-argument", + ), + ], +) +def test_non_pipeline_printf_stays_partial(line: str) -> None: + """Scope guard: only a real operator after the printf words exempts it. + + A bare ``printf`` with a runtime argument is still fail-closed, and a + pipe character quoted inside a printf argument is not a pipeline. + """ + source = "#!/bin/sh\n" + line.strip() + "\n" + assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is True From 470052c6654a59cc27213ba48f800c1ee17c1255 Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Fri, 13 Nov 2026 16:21:55 +0000 Subject: [PATCH 2/4] fix(analyzer): scope printf pipeline treatment to $(...) substitutions Backtick substitutions keep their runtime-command gap: a backtick printf piped into another command is the runtime command itself, so its failure to resolve statically is still a coverage gap. The $(...) form is runtime-dependent data, as before. Fixes the two test-unit failures on #1109 in test_table_column_lookahead_preserves_runtime_command_gaps (backtick printf 'x' | cat in a markdown table cell). Signed-off-by: Varshith Puli --- .../analyzers/static_patterns_tool_misuse.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py index 07ea5c98f..decd7cbb4 100644 --- a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py +++ b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py @@ -2358,9 +2358,16 @@ def failed( else ParserCause.UNSUPPORTED_SYNTAX ) return None - if recognized and not literal_result and _printf_invocation_has_following_operator(inner): + if ( + recognized + and not backtick + and not literal_result + and _printf_invocation_has_following_operator(inner) + ): # Same treatment for allowlisted wrappers (``command``/``builtin``/ - # ``env``) whose first word is not literally ``printf``. + # ``env``) whose first word is not literally ``printf``. Scoped to + # ``$(...)`` only: a backtick substitution keeps its runtime-command + # gap, as the quoted-result branch above does. return None if not recognized or (not literal_result and re.search(r"[;|&<>\r\n]", inner) is not None): if diagnostics is not None and invocation_diagnostics: @@ -2612,12 +2619,15 @@ def _is_printf_substitution( recognized, _ = _printf_invocation_arguments( inner, check_runtime=runtime_check, diagnostics=invocation_diagnostics ) - if recognized and _printf_invocation_has_following_operator(inner): + if recognized and not backtick and _printf_invocation_has_following_operator(inner): # A pipeline, command list, or redirection after the printf words # means the substitution's value is produced by another command, # so this is not a bounded printf substitution: the value is # runtime-dependent data, as for an ``echo`` pipeline, and its - # failure to resolve statically is not a coverage gap. + # failure to resolve statically is not a coverage gap. Scoped to + # ``$(...)`` only: a backtick substitution is the runtime command + # itself, so its gap is preserved instead of being treated as + # resolved data. return False if recognized or not check_command_context: if recognized and diagnostics is not None: From bda1fea6549127fdc48b6aaa7f43243100670244 Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Mon, 16 Nov 2026 10:26:32 +0000 Subject: [PATCH 3/4] fix(analyzer): scope printf pipeline exemption to quoted data arguments The #1107 exemption treated any $(printf ... | ...) substitution as runtime data, including one in command position: $(printf '%s%s' r 'm -rf /' | cat) supplies the command and its arguments through word splitting, and the early return dropped the reconstruction diagnostic while the outer-operand check had nothing left to inspect. Scope the exemption to proven quoted data arguments: _is_printf_substitution and _static_printf_substitution now take it only when the caller proves the substitution sits inside a double-quoted span, via the quoted_result flag and the quote-limits map for candidates the word reader starts on directly. A bare substitution keeps its coverage warning, and backtick substitutions keep their runtime-command gap. Thread the caller's runtime check through the operator scan as well: _printf_invocation_has_following_operator took lambda: None, so the added full pass ignored cancellation and the analyzer deadline while reading a long quoted argument, which has no _SHELL_COMMAND_WORD_CHARS cap. It now receives the caller's check from every call site. Addresses review feedback on #1109. Signed-off-by: Varshith Puli --- .../analyzers/static_patterns_tool_misuse.py | 101 +++++++++++++++--- .../test_static_parse_limit_issue_1107.py | 49 +++++++++ 2 files changed, 134 insertions(+), 16 deletions(-) diff --git a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py index decd7cbb4..f26f2ebb1 100644 --- a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py +++ b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py @@ -32,7 +32,7 @@ import sys import time from bisect import bisect_left, bisect_right -from collections.abc import Callable, Iterator, Mapping +from collections.abc import Callable, Container, Iterator, Mapping from collections.abc import Set as AbstractSet from dataclasses import dataclass, field, replace from hashlib import sha256 @@ -2227,7 +2227,10 @@ def _has_literal_printf_arguments(inner: str) -> bool: ) -def _printf_invocation_has_following_operator(inner: str) -> bool: +def _printf_invocation_has_following_operator( + inner: str, + check_runtime: Callable[[], None] | None = None, +) -> bool: """Return whether a leading printf invocation is followed by shell operators. A pipeline, command list, or redirection after the printf words means @@ -2236,11 +2239,15 @@ def _printf_invocation_has_following_operator(inner: str) -> bool: result. The word reader consumes quoted words whole, so an operator inside the printf arguments (``'a|b'``) does not count, and an unresolved boundary stays fail-closed. A comment start is not an - operator: the printf invocation is complete before it. + operator: the printf invocation is complete before it. The caller's + runtime check is threaded through: quoted words have no + ``_SHELL_COMMAND_WORD_CHARS`` cap, so this added full pass must still + honor cancellation and the analyzer deadline on a long argument. """ + runtime_check = check_runtime or (lambda: None) cursor = 0 while True: - word, cursor, limited = _next_shell_invocation_word(inner, cursor, lambda: None) + word, cursor, limited = _next_shell_invocation_word(inner, cursor, runtime_check) if word is None: if limited or cursor >= len(inner): return False @@ -2254,9 +2261,11 @@ def _static_printf_substitution( *, backtick: bool = False, quoted_result: bool = False, + check_runtime: Callable[[], None] | None = None, diagnostics: list[StaticParserDiagnostic] | None = None, ) -> str | None: """Evaluate bounded literal ``printf`` output, preserving its quoting.""" + runtime_check = check_runtime or (lambda: None) inner_start = start + (1 if backtick else 2) inner_end = end - 1 inner = content[inner_start:inner_end] @@ -2312,13 +2321,17 @@ def failed( ) literal_result = quoted_result and not backtick and _has_literal_printf_arguments(inner) if quoted_result and not backtick and not literal_result: - command, _, _ = _next_shell_invocation_word(inner, 0, lambda: None) + command, _, _ = _next_shell_invocation_word(inner, 0, runtime_check) if command == "printf": - if _printf_invocation_has_following_operator(inner): + if _printf_invocation_has_following_operator(inner, runtime_check): # The printf output is piped (or listed/redirected) into # another command, so the substitution's value is that # command's runtime-dependent output, not a bounded printf # result. Return unresolved without a parse-limit diagnostic. + # Scoped to proven quoted data arguments: an unquoted + # substitution in command position supplies the command + # itself through word splitting, so it keeps its + # reconstruction diagnostic instead. return None failed() return None @@ -2362,12 +2375,15 @@ def failed( recognized and not backtick and not literal_result - and _printf_invocation_has_following_operator(inner) + and quoted_result + and _printf_invocation_has_following_operator(inner, runtime_check) ): # Same treatment for allowlisted wrappers (``command``/``builtin``/ # ``env``) whose first word is not literally ``printf``. Scoped to - # ``$(...)`` only: a backtick substitution keeps its runtime-command - # gap, as the quoted-result branch above does. + # proven quoted data arguments in ``$(...)`` only: a backtick + # substitution keeps its runtime-command gap, as the quoted-result + # branch above does, and an unquoted substitution keeps its + # reconstruction diagnostic. return None if not recognized or (not literal_result and re.search(r"[;|&<>\r\n]", inner) is not None): if diagnostics is not None and invocation_diagnostics: @@ -2604,6 +2620,7 @@ def _is_printf_substitution( end: int, *, backtick: bool = False, + quoted_result: bool = False, check_command_context: bool = True, check_runtime: Callable[[], None] | None = None, diagnostics: list[StaticParserDiagnostic] | None = None, @@ -2619,15 +2636,23 @@ def _is_printf_substitution( recognized, _ = _printf_invocation_arguments( inner, check_runtime=runtime_check, diagnostics=invocation_diagnostics ) - if recognized and not backtick and _printf_invocation_has_following_operator(inner): + if ( + recognized + and not backtick + and quoted_result + and _printf_invocation_has_following_operator(inner, runtime_check) + ): # A pipeline, command list, or redirection after the printf words # means the substitution's value is produced by another command, # so this is not a bounded printf substitution: the value is # runtime-dependent data, as for an ``echo`` pipeline, and its # failure to resolve statically is not a coverage gap. Scoped to - # ``$(...)`` only: a backtick substitution is the runtime command - # itself, so its gap is preserved instead of being treated as - # resolved data. + # proven quoted data arguments in ``$(...)`` only: an unquoted + # substitution in command position supplies the command itself + # through word splitting, so the outer-operand check has nothing + # left to inspect and the reconstruction diagnostic is kept. A + # backtick substitution is the runtime command itself, so its gap + # is preserved instead of being treated as resolved data. return False if recognized or not check_command_context: if recognized and diagnostics is not None: @@ -2713,6 +2738,7 @@ def _printf_substitution_result( end, backtick=backtick, quoted_result=quoted_result, + check_runtime=check_runtime, diagnostics=value_diagnostics, ) invocation_diagnostics: list[StaticParserDiagnostic] = [] @@ -2724,6 +2750,7 @@ def _printf_substitution_result( start, end, backtick=backtick, + quoted_result=quoted_result, check_command_context=check_command_context, check_runtime=check_runtime, diagnostics=invocation_diagnostics, @@ -2830,6 +2857,7 @@ def _parse_shell_command_word( native_argument: bool = False, backtick_closures: set[tuple[int, int]] | None = None, source_end: int | None = None, + quoted_substitution_starts: Container[int] | None = None, ) -> _ShellCommandWord | None: runtime_check = check_runtime or (lambda: None) # The shell recognizes ``NAME=value`` before expansion and never runs the @@ -3019,6 +3047,13 @@ def _parse_shell_command_word( cursor, substitution_end, runtime_check, + # The reader started on this substitution, so it has no + # quote state of its own: the caller supplies the starts + # proven to sit inside a double-quoted span, and only + # those keep the quoted-data exemption. A bare + # substitution keeps its reconstruction diagnostic. + quoted_result=quoted_substitution_starts is not None + and cursor in quoted_substitution_starts, recognize_limit=not limited and not assignment, **({"check_command_context": False} if native_argument else {}), ) @@ -3205,6 +3240,7 @@ def _destructive_command_words( content, start, substitution_end, + check_runtime=check_runtime, ) is None ): @@ -3391,7 +3427,11 @@ def walk(begin: int, end: int, depth: int = 0) -> bool: and content.startswith("$(", position) and substitution_cache.get(position) == cursor and _static_printf_substitution( - content, position, cursor, quoted_result=True + content, + position, + cursor, + quoted_result=True, + check_runtime=check_runtime, ) is not None ): @@ -4287,12 +4327,24 @@ def _quoted_candidate_is_complete( quoted_printf_allowed and content.startswith("$(", start) and _skip_command_substitution(content, start, end, check_runtime) == end - and _static_printf_substitution(content, start, end, quoted_result=True) is not None + and _static_printf_substitution( + content, start, end, quoted_result=True, check_runtime=check_runtime + ) + is not None ): return True fragment = content[start:end] parsed = _parse_shell_command_word( - fragment, 0, check_runtime=check_runtime, quoted_printf_allowed=quoted_printf_allowed + fragment, + 0, + check_runtime=check_runtime, + quoted_printf_allowed=quoted_printf_allowed, + # The fragment starts on the substitution itself, so the reader has + # no quote state: only a start proven to sit inside a double-quoted + # span keeps the quoted-data exemption here. + quoted_substitution_starts={0} + if quote_limits is not None and start in quote_limits and content.startswith("$(", start) + else None, ) if parsed is None or parsed.limited: return False @@ -4588,6 +4640,15 @@ def retain_operand_bounds( complete_comments: set[tuple[int, int]] = set() word_diagnostics: list[StaticParserDiagnostic] = [] + # Starts proven to sit inside a double-quoted span: the word reader + # starts on such a substitution with no quote state of its own, so the + # enclosing quoted word's exemption would otherwise be lost when the + # substitution is revisited as an independent candidate. Only these + # starts keep the quoted-data exemption; a bare substitution keeps its + # reconstruction diagnostic. + quoted_substitution_starts = frozenset( + position for position in (quote_limits or {}) if content.startswith("$(", position) + ) fence_index = 0 native_argument_index = 0 native_argument_through = 0 @@ -4751,6 +4812,7 @@ def retain_operand_bounds( *word_caches, check_runtime=check_runtime, owned_word_positions=assignment_positions, + quoted_substitution_starts=quoted_substitution_starts, **reader_options, ) retain_operand_bounds(value_span, start, parsed, native_backticks) @@ -4792,6 +4854,7 @@ def retain_operand_bounds( ), quoted_printf_allowed=quoted_printf_allowed, diagnostics=word_diagnostics, + quoted_substitution_starts=quoted_substitution_starts, **reader_options, ) retain_operand_bounds(value_span, start, parsed, native_backticks) @@ -4822,6 +4885,12 @@ def retain_operand_bounds( content, substitution_start, substitution_end, + # The exemption is for proven quoted data arguments + # only: a double quote immediately before the + # substitution puts it in a quoted argument, while a + # bare substitution in command position keeps its + # reconstruction diagnostic. + quoted_result=substitution_start == start + 1 and content[start] == '"', check_runtime=check_runtime, ): if not _quoted_candidate_is_complete( diff --git a/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py b/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py index 272ad9583..726705a30 100644 --- a/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py +++ b/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py @@ -98,6 +98,55 @@ def test_cronq_idiom_reaches_completed_ledger_outcome() -> None: assert "reason_code" not in row +@pytest.mark.parametrize( + "line", + [ + pytest.param( + """$(printf '%s%s' r 'm -rf /' | cat) """, + id="executable-printf-pipeline", + ), + pytest.param( + """$(command printf '%s' x | cat) """, + id="executable-wrapper-printf-pipeline", + ), + ], +) +def test_executable_printf_pipeline_keeps_coverage_warning(line: str) -> None: + """Review #1109: the exemption is for proven quoted data arguments only. + + An unquoted printf pipeline in command position supplies the command + and its arguments through word splitting, so the reconstruction + diagnostic must stay: the outer-operand check has nothing left to + inspect once the substitution is treated as resolved data. + """ + source = "#!/bin/sh\n" + line.strip() + "\n" + assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is True + + +def test_quoted_wrapper_printf_pipeline_still_completes() -> None: + """Review #1109: the quoted wrapper case keeps the #1107 exemption.""" + line = """printf "'%s'" "$(command printf '%s' "$1" | sed 's/a/b/')" """ + source = "#!/bin/sh\n" + line.strip() + "\n" + assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is False + + +def test_operator_scan_honors_runtime_check() -> None: + """Review #1109: the operator scan must not bypass cancellation. + + A long quoted argument has no _SHELL_COMMAND_WORD_CHARS cap, so the + added full pass over the substitution must invoke the caller's runtime + check (cancellation and the analyzer deadline) instead of lambda: None. + """ + long_argument = "'" + "a" * 9000 + "'" + inner = "printf '%s' " + long_argument + " | cat" + + def cancelled() -> None: + raise RuntimeError("cancelled") + + with pytest.raises(RuntimeError, match="cancelled"): + tm._printf_invocation_has_following_operator(inner, check_runtime=cancelled) + + def test_literal_elf_probe_behavior_is_unchanged() -> None: """Issue #694: a literal ``$(printf '\\177ELF\\001')`` still resolves.""" probe = "$(printf '\\177ELF\\001')" From 5f60df5399bbcc56634af1ecaff68fd687ebf36a Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Thu, 19 Nov 2026 16:31:50 +0000 Subject: [PATCH 4/4] fix(analyzer): require a non-command position for the quoted printf exemption A shell executes a double-quoted substitution in command position as the command name, so quoting alone never proves a data argument: "$(printf '%s' "$tool_name" | cat)" on its own line lost its coverage warning, with no outer operand left to trigger the fallback diagnostic. The pipeline exemption now also needs a proven non-command data position, via the file's own _is_shell_command_position test: the word reader computes it from the enclosing word's start, the outer-operand fallback checks the candidate word, and the quote-limits fallback receives it from the full source since a fragment alone proves nothing. A native argument is proven data by its caller, and the argument tokenizer only scans argument words. The position travels as a separate quoted_data_position flag so a literal printf in command position still resolves to its value and keeps its destructive-head finding. The original outer-printf fixture still completes, and the quoted-command case is pinned by a new regression test alongside the unquoted one. Addresses rng1995's review on #1109. Signed-off-by: Varshith Puli --- .../analyzers/static_patterns_tool_misuse.py | 105 +++++++++++++----- .../test_static_parse_limit_issue_1107.py | 37 ++++++ 2 files changed, 115 insertions(+), 27 deletions(-) diff --git a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py index f26f2ebb1..bd32f14b8 100644 --- a/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py +++ b/src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py @@ -2261,6 +2261,7 @@ def _static_printf_substitution( *, backtick: bool = False, quoted_result: bool = False, + quoted_data_position: bool = False, check_runtime: Callable[[], None] | None = None, diagnostics: list[StaticParserDiagnostic] | None = None, ) -> str | None: @@ -2324,17 +2325,24 @@ def failed( command, _, _ = _next_shell_invocation_word(inner, 0, runtime_check) if command == "printf": if _printf_invocation_has_following_operator(inner, runtime_check): - # The printf output is piped (or listed/redirected) into - # another command, so the substitution's value is that - # command's runtime-dependent output, not a bounded printf - # result. Return unresolved without a parse-limit diagnostic. - # Scoped to proven quoted data arguments: an unquoted - # substitution in command position supplies the command - # itself through word splitting, so it keeps its - # reconstruction diagnostic instead. + if quoted_data_position: + # The printf output is piped (or listed/redirected) into + # another command, so the substitution's value is that + # command's runtime-dependent output, not a bounded printf + # result. Return unresolved without a parse-limit + # diagnostic. Scoped to proven quoted data arguments: a + # quoted substitution in command position names the + # executable, and an unquoted substitution in command + # position supplies the command itself through word + # splitting, so both keep their reconstruction + # diagnostic instead. + return None + # A quoted substitution in command position may name the + # executable: fall through so the operator in the inner + # text keeps its reconstruction diagnostic below. + else: + failed() return None - failed() - return None if "|" in inner and (pipeline := _SYMBOLIC_PRINTF_PIPELINE_RE.fullmatch(inner)): # A variable grep pattern can supply flags as well as change matching. # Preserve that uncertainty at the argument that controls the filter. @@ -2376,14 +2384,15 @@ def failed( and not backtick and not literal_result and quoted_result + and quoted_data_position and _printf_invocation_has_following_operator(inner, runtime_check) ): # Same treatment for allowlisted wrappers (``command``/``builtin``/ # ``env``) whose first word is not literally ``printf``. Scoped to - # proven quoted data arguments in ``$(...)`` only: a backtick - # substitution keeps its runtime-command gap, as the quoted-result - # branch above does, and an unquoted substitution keeps its - # reconstruction diagnostic. + # proven quoted data arguments in ``$(...)`` only, in a non-command + # position: a backtick substitution keeps its runtime-command gap, + # as the quoted-result branch above does, and an unquoted + # substitution keeps its reconstruction diagnostic. return None if not recognized or (not literal_result and re.search(r"[;|&<>\r\n]", inner) is not None): if diagnostics is not None and invocation_diagnostics: @@ -2621,6 +2630,7 @@ def _is_printf_substitution( *, backtick: bool = False, quoted_result: bool = False, + quoted_data_position: bool = False, check_command_context: bool = True, check_runtime: Callable[[], None] | None = None, diagnostics: list[StaticParserDiagnostic] | None = None, @@ -2640,6 +2650,7 @@ def _is_printf_substitution( recognized and not backtick and quoted_result + and quoted_data_position and _printf_invocation_has_following_operator(inner, runtime_check) ): # A pipeline, command list, or redirection after the printf words @@ -2647,12 +2658,14 @@ def _is_printf_substitution( # so this is not a bounded printf substitution: the value is # runtime-dependent data, as for an ``echo`` pipeline, and its # failure to resolve statically is not a coverage gap. Scoped to - # proven quoted data arguments in ``$(...)`` only: an unquoted - # substitution in command position supplies the command itself - # through word splitting, so the outer-operand check has nothing - # left to inspect and the reconstruction diagnostic is kept. A - # backtick substitution is the runtime command itself, so its gap - # is preserved instead of being treated as resolved data. + # proven quoted data arguments in ``$(...)`` only, in a + # non-command position: a double-quoted substitution in command + # position names the executable, and an unquoted substitution in + # command position supplies the command itself through word + # splitting, so the outer-operand check has nothing left to + # inspect and the reconstruction diagnostic is kept. A backtick + # substitution is the runtime command itself, so its gap is + # preserved instead of being treated as resolved data. return False if recognized or not check_command_context: if recognized and diagnostics is not None: @@ -2727,6 +2740,7 @@ def _printf_substitution_result( *, backtick: bool = False, quoted_result: bool = False, + quoted_data_position: bool = False, recognize_limit: bool = True, check_command_context: bool = True, ) -> tuple[str | None, bool, StaticParserDiagnostic | None]: @@ -2738,6 +2752,7 @@ def _printf_substitution_result( end, backtick=backtick, quoted_result=quoted_result, + quoted_data_position=quoted_data_position, check_runtime=check_runtime, diagnostics=value_diagnostics, ) @@ -2751,6 +2766,7 @@ def _printf_substitution_result( end, backtick=backtick, quoted_result=quoted_result, + quoted_data_position=quoted_data_position, check_command_context=check_command_context, check_runtime=check_runtime, diagnostics=invocation_diagnostics, @@ -2858,6 +2874,7 @@ def _parse_shell_command_word( backtick_closures: set[tuple[int, int]] | None = None, source_end: int | None = None, quoted_substitution_starts: Container[int] | None = None, + quoted_data_position: bool | None = None, ) -> _ShellCommandWord | None: runtime_check = check_runtime or (lambda: None) # The shell recognizes ``NAME=value`` before expansion and never runs the @@ -2865,6 +2882,15 @@ def _parse_shell_command_word( # positions, and a later ``$NAME`` command word is still checked, so only # this word's printf reconstruction check is skipped. assignment = _is_assignment_word(content, start) + if quoted_data_position is None: + # Quoting alone never proves a data position: a shell executes a + # double-quoted substitution in command position as the command + # name, so the quoted printf exemption also needs the enclosing + # word outside command position. A native argument is proven data + # by its caller; any other word is data only when it does not open + # a line or follow a command separator. A fragment parsed on its + # own (start 0) proves nothing, so it stays fail-closed. + quoted_data_position = native_argument or not _is_shell_command_position(content, start) output: list[str] = [] wrapper_quote = enclosing_delimiter or ( @@ -2930,7 +2956,15 @@ def _parse_shell_command_word( cursor, substitution_end, runtime_check, + # The exemption needs a proven quoted data + # argument: quoting here, and a non-command + # position for the enclosing word, which travels + # separately so a literal printf in command + # position still resolves to its value. A quoted + # substitution in command position names the + # executable, so it keeps its diagnostic. quoted_result=quoted_printf_allowed, + quoted_data_position=quoted_data_position, recognize_limit=not limited and not assignment, **({"check_command_context": False} if native_argument else {}), ) @@ -3050,10 +3084,14 @@ def _parse_shell_command_word( # The reader started on this substitution, so it has no # quote state of its own: the caller supplies the starts # proven to sit inside a double-quoted span, and only - # those keep the quoted-data exemption. A bare + # those keep the quoted-data exemption, and only in a + # proven non-command position for the enclosing word, + # which travels separately so a literal printf in + # command position still resolves to its value. A bare # substitution keeps its reconstruction diagnostic. quoted_result=quoted_substitution_starts is not None and cursor in quoted_substitution_starts, + quoted_data_position=quoted_data_position, recognize_limit=not limited and not assignment, **({"check_command_context": False} if native_argument else {}), ) @@ -4341,10 +4379,14 @@ def _quoted_candidate_is_complete( quoted_printf_allowed=quoted_printf_allowed, # The fragment starts on the substitution itself, so the reader has # no quote state: only a start proven to sit inside a double-quoted - # span keeps the quoted-data exemption here. + # span keeps the quoted-data exemption here. The fragment cannot + # prove the enclosing word's position, so the caller supplies it + # from the full source: a quoted substitution in command position + # names the executable and keeps its diagnostic. quoted_substitution_starts={0} if quote_limits is not None and start in quote_limits and content.startswith("$(", start) else None, + quoted_data_position=not _is_shell_command_position(content, start), ) if parsed is None or parsed.limited: return False @@ -4644,8 +4686,9 @@ def retain_operand_bounds( # starts on such a substitution with no quote state of its own, so the # enclosing quoted word's exemption would otherwise be lost when the # substitution is revisited as an independent candidate. Only these - # starts keep the quoted-data exemption; a bare substitution keeps its - # reconstruction diagnostic. + # starts keep the quoted-data exemption, and only in a proven + # non-command position for the enclosing word; a bare substitution + # keeps its reconstruction diagnostic. quoted_substitution_starts = frozenset( position for position in (quote_limits or {}) if content.startswith("$(", position) ) @@ -4887,10 +4930,15 @@ def retain_operand_bounds( substitution_end, # The exemption is for proven quoted data arguments # only: a double quote immediately before the - # substitution puts it in a quoted argument, while a - # bare substitution in command position keeps its - # reconstruction diagnostic. + # substitution puts it in a quoted argument, and the + # enclosing word must sit outside command position, + # where a quoted substitution would name the executable. + # The position travels separately from quoting, so a + # literal printf in command position still resolves + # to its value. A bare substitution in command + # position keeps its reconstruction diagnostic. quoted_result=substitution_start == start + 1 and content[start] == '"', + quoted_data_position=not _is_shell_command_position(content, start), check_runtime=check_runtime, ): if not _quoted_candidate_is_complete( @@ -8460,6 +8508,9 @@ def printf_value( runtime_check, backtick=backtick, quoted_result=quoted, + # This tokenizer only scans argument words (the command word + # sits before body_start), so a quoted pipeline here is data. + quoted_data_position=True, recognize_limit=not parse_limited, check_command_context=False, ) diff --git a/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py b/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py index 726705a30..d8ed4a722 100644 --- a/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py +++ b/tests/nodes/analyzers/test_static_parse_limit_issue_1107.py @@ -123,6 +123,43 @@ def test_executable_printf_pipeline_keeps_coverage_warning(line: str) -> None: assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is True +@pytest.mark.parametrize( + "line", + [ + pytest.param( + """\"$(printf '%s' \"$tool_name\" | cat)\" """, + id="quoted-command-printf-pipeline", + ), + pytest.param( + """\"$(printf '%s%s' r 'm -rf /' | cat)\" """, + id="quoted-command-printf-pipeline-rm", + ), + pytest.param( + """\"$(command printf '%s' \"$1\" | sed 's/a/b/')\" """, + id="quoted-command-wrapper-printf-pipeline", + ), + pytest.param( + """echo hi; \"$(printf '%s' \"$tool_name\" | cat)\" """, + id="quoted-command-after-semicolon", + ), + pytest.param( + """echo hi | \"$(printf '%s' \"$tool_name\" | cat)\" """, + id="quoted-command-after-pipe", + ), + ], +) +def test_quoted_command_printf_pipeline_keeps_coverage_warning(line: str) -> None: + """Review #1109 (rng1995): a quoted substitution can name the command. + + A shell executes a double-quoted substitution in command position as + the executable, so quoting alone never proves a data argument: the + reconstruction diagnostic must stay, as for the unquoted case. The + outer-printf fixture still completes (see the #1107 tests above). + """ + source = "#!/bin/sh\n" + line.strip() + "\n" + assert tm.has_bounded_parse_exhaustion(source, lambda: None, file_type="shell") is True + + def test_quoted_wrapper_printf_pipeline_still_completes() -> None: """Review #1109: the quoted wrapper case keeps the #1107 exemption.""" line = """printf "'%s'" "$(command printf '%s' "$1" | sed 's/a/b/')" """