Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 177 additions & 5 deletions src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2227,16 +2227,46 @@ def _has_literal_printf_arguments(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
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. 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, runtime_check)
if word is None:
if limited or cursor >= len(inner):
return False
return inner[cursor] in ";|&()<>"


def _static_printf_substitution(
content: str,
start: int,
end: int,
*,
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:
"""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]
Expand Down Expand Up @@ -2290,7 +2320,29 @@ def failed(
inner_start + high,
ParserUncheckedScope.EXPRESSION,
)
return None
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, runtime_check)
if command == "printf":
if _printf_invocation_has_following_operator(inner, runtime_check):
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
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.
Expand Down Expand Up @@ -2327,6 +2379,21 @@ def failed(
else ParserCause.UNSUPPORTED_SYNTAX
)
return None
if (
recognized
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, 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:
diagnostics.extend(item.shifted(inner_start) for item in invocation_diagnostics[:1])
Expand Down Expand Up @@ -2562,6 +2629,8 @@ def _is_printf_substitution(
end: int,
*,
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,
Expand All @@ -2577,6 +2646,27 @@ def _is_printf_substitution(
recognized, _ = _printf_invocation_arguments(
inner, check_runtime=runtime_check, diagnostics=invocation_diagnostics
)
if (
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
# 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
# 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:
diagnostics.extend(item.shifted(inner_start) for item in invocation_diagnostics[:1])
Expand Down Expand Up @@ -2650,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]:
Expand All @@ -2661,6 +2752,8 @@ def _printf_substitution_result(
end,
backtick=backtick,
quoted_result=quoted_result,
quoted_data_position=quoted_data_position,
check_runtime=check_runtime,
diagnostics=value_diagnostics,
)
invocation_diagnostics: list[StaticParserDiagnostic] = []
Expand All @@ -2672,6 +2765,8 @@ def _printf_substitution_result(
start,
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,
Expand Down Expand Up @@ -2778,13 +2873,24 @@ 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,
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
# value as the command name. Its substitutions keep their own candidate
# 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 (
Expand Down Expand Up @@ -2850,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 {}),
)
Expand Down Expand Up @@ -2967,6 +3081,17 @@ 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, 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 {}),
)
Expand Down Expand Up @@ -3153,6 +3278,7 @@ def _destructive_command_words(
content,
start,
substitution_end,
check_runtime=check_runtime,
)
is None
):
Expand Down Expand Up @@ -3339,7 +3465,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
):
Expand Down Expand Up @@ -4235,12 +4365,28 @@ 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. 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
Expand Down Expand Up @@ -4536,6 +4682,16 @@ 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, 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)
)
fence_index = 0
native_argument_index = 0
native_argument_through = 0
Expand Down Expand Up @@ -4699,6 +4855,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)
Expand Down Expand Up @@ -4740,6 +4897,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)
Expand Down Expand Up @@ -4770,6 +4928,17 @@ 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, 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(
Expand Down Expand Up @@ -8339,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,
)
Expand Down
Loading
Loading