diff --git a/tests/tools/test_approval_hermes_home_spellings.py b/tests/tools/test_approval_hermes_home_spellings.py index ea1e1926a0..3c1d6c91c0 100644 --- a/tests/tools/test_approval_hermes_home_spellings.py +++ b/tests/tools/test_approval_hermes_home_spellings.py @@ -113,10 +113,12 @@ class TestShellEscapedSpaceInWindowsHome: assert _is_dangerous(r"sed -i s/a/b/ /c/Users/John\ Smith/AppData/Local/hermes2/config.yaml") is False -class TestEscapedSpaceFollowsShellQuoting: - r"""``\ `` is the same character as a space only where the shell reads it as an escape. Folding it - anywhere else names a directory the command does not touch, and gates a harmless command as an - edit of Hermes's own home.""" +class TestEscapedSpaceFailsClosed: + r"""An unquoted ``\ `` is the same character as a space, so a path spelled through it is the home. + Whether a given ``\ `` is unquoted takes a full shell parse: a quote in a comment, a heredoc body + or a backtick flips every lexical quote model for the rest of the command. The fold therefore + reads every ``\ `` inside a home spelling as a space; a quoted one costs a prompt, a misread one + would lose the gate on the approval policy file.""" @pytest.fixture def spaced_hermes_home(self, monkeypatch): @@ -134,7 +136,9 @@ class TestEscapedSpaceFollowsShellQuoting: def test_an_escaped_space_after_the_home_continues_the_name(self, spaced_hermes_home): # `hermes\ data\ backup` is one directory, a sibling of the home, not the home plus a word. + # Quoted, the backslash is literal and the name is no closer to the home: no quote state matters. assert _is_dangerous(r"mv /home/tester/hermes\ data\ backup /home/tester/backup") is False + assert _is_dangerous(r"mv '/home/tester/hermes\ data\ backup' /home/tester/backup") is False def test_an_escaped_space_after_a_spaceless_home_continues_the_name(self, monkeypatch): monkeypatch.setenv("HOME", "/home/tester") @@ -145,17 +149,40 @@ class TestEscapedSpaceFollowsShellQuoting: assert _is_dangerous(r"sed -i s/a/b/ /home/John\ Smith/hermes/config.yaml") is True assert _is_dangerous(r"sed -i s/a/b/ /home/John\ Smith/hermes2/config.yaml") is False + @pytest.mark.parametrize("prefix", [ + "echo hi # don't\n", + "# it's\n", + "cat </dev/null\nit's\nEOF\n", + "cat <<-EOF >/dev/null\n\tit's\n\tEOF\n", + ': < str: return re.escape(component).replace(r"\ ", r"\\? ") -def _live_space_escapes(command: str, start: int = 0, end: int | None = None, found: set | None = None) -> set: - """Indexes of each backslash in *command* the shell reads as escaping a space or tab: unquoted, or - unquoted inside a ``$(...)`` / backtick body (a fresh quote context). Inside single or double - quotes the backslash is a literal character.""" - found = set() if found is None else found - for kind, i, j, quote in _scan_shell(command, start, end, subst="q"): - if kind == "subst": - _live_space_escapes(command, i + (2 if command.startswith("$(", i) else 1), j - 1, found) - elif kind == "esc" and quote is None and command[i + 1] in " \t": - found.add(i) - return found - - def _fold_matches(pattern, command: str, replace) -> str: - """``pattern.sub(replace, command)``, keeping a match as written when an escaped space in it is - not what the shell reads: + """``pattern.sub(replace, command)``, except a match that ends on the backslash of a ``\\ ``: + unquoted it continues the last name (``hermes\\ data\\ backup`` is a sibling of the home + ``hermes data``, ``config.yaml\\ `` is another file), quoted it is a literal name character. + Either way the command does not name what the fold would, so that rule needs no quote state. - - a quoted ``\\ `` is a literal backslash, so ``'/home/John\\ Smith'`` is another directory than - the ``John Smith`` the optional escape in :func:`_spell_component` stands for; - - an unquoted ``\\ `` right after the match continues the last name instead of separating a child: - ``hermes\\ data\\ backup`` is a sibling of the home ``hermes data``, not inside it. + An escaped space INSIDE the home is always read as a space, quoted or not. Telling the two apart + takes a full shell parse (a quote in a comment, heredoc body or backtick flips every lexical quote + model for the rest of the command), so the fold fails closed: a quoted ``'/home/John\\ Smith/...'`` + costs a prompt, a misread unquoted one would lose the gate on the approval policy file. """ - live = None - def fold(match: re.Match) -> str: - nonlocal live - escapes = [i for i in range(match.start(), match.end()) if command.startswith(("\\ ", "\\\t"), i)] - if escapes: - live = _live_space_escapes(command) if live is None else live - for i in escapes: - # Inside the match the escape spells a component's space; at its end it follows the home. - if (i in live) != (i + 1 < match.end()): - return match.group(0) + if command.startswith(("\\ ", "\\\t"), match.end() - 1): + return match.group(0) return replace(match) return pattern.sub(fold, command)