mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(router): ignore shell operators inside quoted arguments in read detection
_segment_is_read treated any `>` as a redirect and split the pipeline at the first `|`, and _command_segments split on `;`, `&&` and `||`, all without regard to quoting. A quiet sed whose script contains one of these characters (sed -n '/if x > 0/,/return/p' a.py, or sed -n '/def \|class /p' a.py) was therefore not detected as a file read and lost read protection. Mask quoted and backslash-escaped characters before those three scans, and map the match positions back onto the original command. Refs #4478 (item 3, the quoted-pattern case left open by #4521).
This commit is contained in:
@@ -880,9 +880,44 @@ def _command_segments(command: str) -> list[str]:
|
||||
|
||||
Splits on ``;``, ``&&`` and ``||`` — never on a single ``|``. Pipelines are kept
|
||||
whole so the segment's own writes (``cat a.py | tee b.py``) stay visible; the
|
||||
reduction to the first stage happens in :func:`_segment_is_read`.
|
||||
reduction to the first stage happens in :func:`_segment_is_read`. Separators inside
|
||||
quotes (``sed -n '/a;b/p' foo.py``) are part of an argument, not a new command.
|
||||
"""
|
||||
return [seg.strip() for seg in _CMD_SEPARATOR_RE.split(command) if seg.strip()]
|
||||
masked = _mask_quoted(command)
|
||||
segments: list[str] = []
|
||||
start = 0
|
||||
for m in _CMD_SEPARATOR_RE.finditer(masked):
|
||||
segments.append(command[start : m.start()])
|
||||
start = m.end()
|
||||
segments.append(command[start:])
|
||||
return [seg.strip() for seg in segments if seg.strip()]
|
||||
|
||||
|
||||
def _mask_quoted(command: str) -> str:
|
||||
"""Return ``command`` with quoted and backslash-escaped 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.
|
||||
"""
|
||||
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:
|
||||
if char == quote:
|
||||
quote = ""
|
||||
else:
|
||||
out[i] = "_"
|
||||
elif char in {"'", '"'}:
|
||||
quote = char
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def _segment_is_read(c: str) -> bool:
|
||||
@@ -890,9 +925,15 @@ def _segment_is_read(c: str) -> bool:
|
||||
# A redirect or `tee` in THIS segment means the segment writes a file rather than
|
||||
# reading one (`cat a.py > copy.py`, `cat a.py | tee copy.py`). Checked on the whole
|
||||
# segment — pipeline included — before reducing to the stage that touches the file.
|
||||
if re.search(r"(^|\s)(>>?|tee\b)", c):
|
||||
# Both checks run on the quote-masked segment: a `>` or `|` inside a quoted sed
|
||||
# script (`sed -n '/x >/p' a.py`) is part of the argument, not shell syntax.
|
||||
masked = _mask_quoted(c)
|
||||
if re.search(r"(^|\s)(>>?|tee\b)", masked):
|
||||
return False
|
||||
c = c.split("|", 1)[0].strip()
|
||||
pipe = masked.find("|")
|
||||
if pipe != -1:
|
||||
c = c[:pipe]
|
||||
c = c.strip()
|
||||
if not c:
|
||||
return False
|
||||
# Parse the real program with the SAME structural parser the search-fold uses
|
||||
|
||||
@@ -123,6 +123,36 @@ def test_non_reads_are_not_detected(command: str) -> None:
|
||||
assert _is_read_command(command) is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("command", "expected"),
|
||||
[
|
||||
# Shell operators inside a quoted sed script are part of the argument (#4478).
|
||||
("sed -n '/x >/p' foo.py", True),
|
||||
("sed -n '/x >> y/p' foo.py", True),
|
||||
('sed -n "/a > b/p" foo.py', True),
|
||||
("sed -n '/a|b/p' foo.py", True),
|
||||
("sed -n '/a;b/p' foo.py", True),
|
||||
("sed -n '/a && b/p' foo.py", True),
|
||||
("sed -n '/ tee /p' foo.py", True),
|
||||
("cat 'a > b.py'", True),
|
||||
# An escaped operator is a literal argument too.
|
||||
(r"cat foo.py \> out", True),
|
||||
# Real operators next to a quoted script still count.
|
||||
("sed -n '/x >/p' foo.py > out.txt", False),
|
||||
("sed -n '/a|b/p' foo.py | tee out.txt", False),
|
||||
("sed -n '/a;b/p' foo.py; echo done", True),
|
||||
("echo '>' && sed 's/a/b/' foo.py", False),
|
||||
# The -c argument of a shell is quoted at the outer level; the inner
|
||||
# command is checked on its own.
|
||||
('bash -lc "cat foo.py > copy.py"', False),
|
||||
('bash -lc "cat foo.py | tee copy.py"', False),
|
||||
('bash -lc "cat foo.py"', True),
|
||||
],
|
||||
)
|
||||
def test_quoted_shell_operators_are_arguments(command: str, expected: bool) -> None:
|
||||
assert _is_read_command(command) is expected
|
||||
|
||||
|
||||
def test_heredoc_body_is_not_split_into_a_bogus_read() -> None:
|
||||
"""A `;` inside a heredoc body must not produce a segment that looks like a
|
||||
read — the command as a whole writes a file."""
|
||||
|
||||
Reference in New Issue
Block a user