The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): heredoc-pipe guard sees through fd redirects around the pipe
teknium127ad6f560283
diff --git a/tests/tools/test_self_repo_guard.py b/tests/tools/test_self_repo_guard.pyindex 919760eacb2..d49ffd84a24 100644--- a/tests/tools/test_self_repo_guard.py+++ b/tests/tools/test_self_repo_guard.py@@ -168,6 +168,25 @@ class TestBlocksMutationsInSourceRepo:hit, _ = _detect(command, repo, repo)assert hit is True+ @pytest.mark.parametrize(+ ("command", "expected"),+ [+ # `2>&1` is an fd redirect, not a `&` list operator ending the pipeline before bash.+ ("cat <<'EOF' 2>&1 | bash\ngit checkout main\nEOF\n", True),+ ("cat <<'EOF' &>/dev/null | bash\ngit checkout main\nEOF\n", True),+ # The fd number of a redirect is not a script operand: bash is still bare.+ ("cat <<'EOF' | bash 2>/dev/null\ngit checkout main\nEOF\n", True),+ ("cat <<'EOF' | bash 2>&1\ngit checkout main\nEOF\n", True),+ ("cat <<'EOF' | bash >/dev/null\ngit checkout main\nEOF\n", True),+ # A real script operand keeps the body as data; a plain redirect is a file write.+ ("cat <<'EOF' | bash run.sh 2>/dev/null\ngit checkout main\nEOF\n", False),+ ("cat <<'EOF' > notes.md\ngit checkout main\nEOF\n", False),+ ],+ )+ def test_fd_redirects_around_heredoc_pipe(self, repo, command, expected):+ hit, _ = _detect(command, repo, repo)+ assert hit is expected+def test_tilde_dash_c_path(self, repo, monkeypatch, tmp_path):monkeypatch.setenv("HOME", str(repo.parent))hit, _ = _detect("git -C ~/hermes-agent checkout main", tmp_path, repo)diff --git a/tools/self_repo_guard.py b/tools/self_repo_guard.pyindex 92cf845150b..69132d18d5d 100644--- a/tools/self_repo_guard.py+++ b/tools/self_repo_guard.py@@ -45,7 +45,7 @@ _WRAPPER_OPTIONS_WITH_ARG: dict[str, frozenset[str]] = {"-p", "--prompt", "-R", "--chroot", "-T", "--command-timeout", "-u", "--user"}),"env": frozenset({"-a", "--argv0", "-C", "--chdir", "-S", "--split-string", "-u", "--unset"}),"command": _NO_OPTIONS, "builtin": _NO_OPTIONS, "nohup": _NO_OPTIONS, "setsid": _NO_OPTIONS,- "exec": frozenset({"-a"}),+ "exec": frozenset({"-a"}), "nice": frozenset({"-n", "--adjustment"}),"time": frozenset({"-f", "--format", "-o", "--output"})}_MAX_RECURSION = 4# git global options that consume the next argument (-C/--work-tree/-c are acted on).@@ -95,14 +95,16 @@ def _executable_name(value: str) -> str:def _shell_words_at(command: str, start: int) -> list[str]:- """Deobfuscated words of the simple command at ``start`` (stops at a newline or a trailing- ``# comment``; max 64)."""+ """Deobfuscated words of the simple command at ``start`` (stops at a newline, a redirection+ or a trailing ``# comment``; max 64). The fd prefix of ``2>/dev/null`` / ``2>&1`` belongs to+ the redirection, not to the command's operands."""words: list[str] = []cursor = startfor _ in range(64):word_start, word_end, raw_word = _read_shell_word(command, cursor)if word_start == word_end or (words and "\n" in command[cursor:word_start]) or (- _is_shell_comment_start(command, word_start)):+ _is_shell_comment_start(command, word_start)) or (+ raw_word.isdigit() and command[word_end : word_end + 1] in ("<", ">")):breakwords.append(_deobfuscate_shell_word_for_detection(raw_word))cursor = word_end@@ -249,6 +251,8 @@ def _pipeline_end(masked: str, opener: int) -> int:return indexcontinueif char in ";&|":+ if char == "&" and (masked[index - 1] in "<>" or masked.startswith("&>", index)):+ continue # `2>&1` / `>&2` / `&>file` redirect fds: not a list operatorif char == "|" and not masked.startswith("||", index):after_pipe = Trueelif level == 0 and depth == 0 and masked[index - 1] != "|": # `|&` is a pipe