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</dev/null`, `0<&0`) lost its operator to shlex the same way; real unquoted `<` operators now mark the program opaque (manual-only, one_shot) before the lossy walk. Here-strings, heredocs and quoted/escaped `<` stay read-only. Refs #141346 Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com> Co-authored-by: foma <308445027+foma-agent@users.noreply.github.com>
This commit is contained in:
@@ -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</dev/null", "0<&0", "0<&3"])
|
||||
@pytest.mark.parametrize("later", ["", " -e 'get version of me'"])
|
||||
def test_fd_input_redirect_is_opaque_manual_only(redirect, later):
|
||||
from tools.approval import SMART_MANUAL_ONLY_DESCRIPTIONS
|
||||
from tools.approval_applescript import OSASCRIPT_OPAQUE_DESCRIPTION
|
||||
|
||||
found, description, _ = detect_dangerous_command(
|
||||
f"osascript -e 'get name of me' {redirect}{later}"
|
||||
)
|
||||
assert found
|
||||
assert description == OSASCRIPT_OPAQUE_DESCRIPTION
|
||||
assert description in SMART_MANUAL_ONLY_DESCRIPTIONS
|
||||
|
||||
|
||||
@pytest.mark.parametrize("suffix", [
|
||||
" '0' '<' /dev/null", " '0</dev/null'", r" 0\</dev/null",
|
||||
" | echo '0</dev/null'", "; echo '0<&3'", " && echo '0</dev/null'",
|
||||
" >| 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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)`"):
|
||||
|
||||
Reference in New Issue
Block a user