fix(router): keep line continuations and comments as shell syntax in the read mask

Review of the first round: _mask_quoted treated a backslash-newline as an
escaped character, which removed the word boundary before a redirect or tee on
the next line (cat foo.py \<newline>> copy.py was classified as a read), and it
let a quote mark inside a comment open a quote that hid the next line's
separators (echo ok # it's<newline>true; cat foo.py lost its read).

A backslash-newline is now masked as two spaces (the shell removes both), and an
unquoted # at the start of a word masks the rest of its line without changing
quote state. Regression cases for continued >, >>, tee and multiline comments.
This commit is contained in:
HetCreep
2026-11-05 13:47:15 +07:00
parent 639bfa9e5b
commit 50798e5ad7
2 changed files with 54 additions and 12 deletions
+40 -12
View File
@@ -894,29 +894,57 @@ def _command_segments(command: str) -> list[str]:
def _mask_quoted(command: str) -> str:
"""Return ``command`` with quoted and backslash-escaped characters replaced by ``_``.
"""Return ``command`` with quoted, escaped and commented characters replaced by ``_``.
The result has the same length, so match positions map back onto ``command``.
Shell operators inside quotes (``sed -n '/x >/p' foo.py``) are arguments, and a
search over the masked string does not mistake them for a redirect, pipe or
command separator. The quote characters themselves are kept.
Shell operators inside quotes (``sed -n '/x >/p' foo.py``), after a backslash, or
inside a comment are not shell syntax, and a search over the masked string does
not mistake them for a redirect, pipe or command separator. The quote characters
themselves are kept.
Two shell rules keep word boundaries intact: a backslash-newline is a line
continuation, so both characters become spaces (``cat a.py \\<newline>> b.py``
still writes ``b.py``); and a ``#`` at the start of a word starts a comment that
runs to the end of the line, so quote marks inside it do not open a quote that
would hide the next line's separators.
"""
out = list(command)
quote = ""
escaped = False
for i, char in enumerate(command):
if escaped:
out[i] = "_"
escaped = False
elif char == "\\" and quote != "'":
escaped = True
elif quote:
i = 0
n = len(command)
word_start = True
while i < n:
char = command[i]
if char == "\\" and quote != "'":
if i + 1 < n and command[i + 1] == "\n":
# Line continuation: the shell removes both characters.
out[i] = out[i + 1] = " "
word_start = True
else:
out[i] = "_"
if i + 1 < n:
out[i + 1] = "_"
word_start = False
i += 2
continue
if quote:
if char == quote:
quote = ""
else:
out[i] = "_"
elif char in {"'", '"'}:
quote = char
word_start = False
elif char == "#" and word_start:
# Comment to end of line; the newline itself stays visible.
while i < n and command[i] != "\n":
out[i] = "_"
i += 1
word_start = True
continue
else:
word_start = char.isspace() or char in ";&|()"
i += 1
return "".join(out)
@@ -147,6 +147,20 @@ def test_non_reads_are_not_detected(command: str) -> None:
('bash -lc "cat foo.py > copy.py"', False),
('bash -lc "cat foo.py | tee copy.py"', False),
('bash -lc "cat foo.py"', True),
# A backslash-newline is a line continuation: the write on the next line
# still counts (review of #4646).
("cat foo.py \\\n> copy.py", False),
("cat foo.py \\\n>> copy.py", False),
("cat foo.py | \\\ntee copy.py", False),
("sed -n '/x >/p' foo.py \\\n> out.txt", False),
("sed -n '1,60p' \\\nfoo.py", True),
# A comment ends at the newline; its quote marks do not open a quote that
# would hide the next line's separators (review of #4646).
('echo start # " unclosed in comment\nls; cat foo.py', True),
("echo ok # author's note\ntrue; cat foo.py", True),
("cat foo.py # > not a redirect", True),
# A hash inside a word is not a comment.
("cat foo#bar.py > out.txt", False),
],
)
def test_quoted_shell_operators_are_arguments(command: str, expected: bool) -> None: