From c066ed4dcd41bb7137643dd00bfffc4f50bf0cce Mon Sep 17 00:00:00 2001 From: Maciej Lisiewski Date: Sun, 15 Nov 2026 15:42:56 +0100 Subject: [PATCH] fix(approval): split >&/>| redirects and fd-input operators between osascript -e args `>& file`, `2>& 1` and `>| file` tokenize as three pieces; the walk skipped two and read the target as the program's first operand, so a later -e program was never read. `_shell_command_segment` also ended the command at the `|` of `>|`. An fd-prefixed input redirect (`0 Co-authored-by: foma <308445027+foma-agent@users.noreply.github.com> --- .../tools/test_approval_redirect_segments.py | 28 +++++++++++++++++-- tools/approval_applescript.py | 10 +++++-- tools/approval_detection.py | 6 ++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/tests/tools/test_approval_redirect_segments.py b/tests/tools/test_approval_redirect_segments.py index 94e933a62f..ec562e5309 100644 --- a/tests/tools/test_approval_redirect_segments.py +++ b/tests/tools/test_approval_redirect_segments.py @@ -7,7 +7,7 @@ from tools.approval_applescript import OSASCRIPT_RISKY_DESCRIPTION from tools.approval_detection import _shell_command_segment, _shell_segment_tokens -@pytest.mark.parametrize("redirect", ["2>&1", ">&2", "&>/dev/null"]) +@pytest.mark.parametrize("redirect", ["2>&1", ">&2", "&>/dev/null", ">& both", "2>& 1", ">| out"]) def test_redirect_between_applescript_arguments(redirect): command = (f"osascript -e 'get name of me' {redirect} " "-e 'tell app \"Keynote\" to close front document saving no'") @@ -20,7 +20,7 @@ def test_redirect_before_powershell_payload(): assert "delete" in description.lower() -@pytest.mark.parametrize("redirect", ["2>&1", ">&2", "&>/dev/null"]) +@pytest.mark.parametrize("redirect", ["2>&1", ">&2", "&>/dev/null", ">& both", "2>& 1", ">| out"]) def test_read_only_trailing_redirect(redirect): assert detect_dangerous_command(f"osascript -e 'get name of me' {redirect}") == (False, None, None) @@ -29,6 +29,7 @@ def test_read_only_trailing_redirect(redirect): " & echo quit", " && echo quit", "; echo quit", " | echo quit", "\necho quit", " # quit", # A quoted or escaped `>` is a literal, so the `&` after it is a real separator. r" \>& echo quit", r" &\> echo quit", " '>'& echo quit", + r" \>| echo quit", " '>'| echo quit", ]) def test_real_separators_still_bound_command(suffix): command = "osascript -e 'get name of me'" + suffix @@ -42,3 +43,26 @@ def test_real_separators_still_bound_command(suffix): ]) def test_redirect_tokenization(redirect, expected): assert _shell_segment_tokens(redirect, 0) == expected + + +@pytest.mark.parametrize("redirect", ["0| out | echo quit", " >& both ; echo quit", +]) +def test_input_literals_and_separated_commands_stay_read_only(suffix): + assert detect_dangerous_command("osascript -e 'get name of me'" + suffix) == (False, None, None) diff --git a/tools/approval_applescript.py b/tools/approval_applescript.py index 6198ae0d8d..c4c5eea1f5 100644 --- a/tools/approval_applescript.py +++ b/tools/approval_applescript.py @@ -217,14 +217,18 @@ def _osascript_program(args: str, value_options: tuple[str, ...]) -> tuple[str, redirect, ``-i`` (interactive), a heredoc whose delimiter cannot be read, or unparseable quoting. ``jxa`` is True for any ``-l`` language other than AppleScript. ``heredoc`` is ``(delimiter, quoted, strip_tabs)``.""" - from tools.approval_detection import _shell_segment_tokens + from tools.approval_detection import _scan_shell, _shell_segment_tokens tokens = _shell_segment_tokens(args, 0) if tokens is None: return "", True, False, None pieces: list[str] = [] heredoc = None - jxa = opaque = False + jxa = False + # shlex loses quoting and splits an fd prefix into an operand; inspect real input operators. + opaque = any(kind == "char" and quote is None and args[i] == "<" + and not args.startswith("<<", i) and args[i - 1:i] != "<" + for kind, i, _, quote in _scan_shell(args)) i = 0 while i < len(tokens): token = tokens[i] @@ -235,7 +239,7 @@ def _osascript_program(args: str, value_options: tuple[str, ...]) -> tuple[str, i += 1 continue if token in (">", ">>", ">|", ">&") or ((token.isdigit() or token == "&") and nxt.startswith(">")): - i += 2 if token.startswith(">") else 1 # `2>x` / `&>x` reach here as `2` / `&`, then `>` + i += (3 if nxt in ("&", "|") else 2) if token.startswith(">") else 1 # split operator + target continue if token == "<<<": pieces.append(nxt) diff --git a/tools/approval_detection.py b/tools/approval_detection.py index 11c25d69ef..6d49e9644a 100644 --- a/tools/approval_detection.py +++ b/tools/approval_detection.py @@ -1625,12 +1625,12 @@ def _iter_shell_command_word_spans(command: str): def _shell_command_segment(command: str, start: int) -> str: - """Bound a candidate to its command, preserving quoted argument bytes. The ``&`` of a ``2>&1``, - ``<&3`` or ``&>`` redirection is part of the command, not a separator.""" + """Bound a candidate to its command, preserving quoted argument bytes. The ``&``/``|`` of a ``2>&1``, + ``<&3``, ``>|`` or ``&>`` redirection is part of the command, not a separator.""" end = len(command) redirect_end = -1 # end of an unquoted, unescaped `<`/`>` (a quoted or escaped one is a literal) for kind, i, j, quote in _scan_shell(command, start, subst="uq", brace=True, comments=True): - if kind == "char" and quote is None and command[i] == "&" and (redirect_end == i or command.startswith(">", j)): + if kind == "char" and quote is None and ((command[i] == "&" and (redirect_end == i or command.startswith(">", j))) or (command[i] == "|" and redirect_end == i and command[i - 1] == ">")): continue redirect_end = j if kind == "char" and quote is None and command[i] in "<>" else -1 if kind == "comment" or (kind == "char" and quote is None and command[i] in ";&|\n)`"):