AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): judge a heredoc write into the workspace as a file write, not a shell command (#15037)
Reinier van der Leer942771f95385
diff --git a/autogpt_platform/backend/backend/copilot/gate/shell_write.py b/autogpt_platform/backend/backend/copilot/gate/shell_write.pynew file mode 100644index 0000000000..d5b29b33ac--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/gate/shell_write.py@@ -0,0 +1,85 @@+"""A shell command that only writes one file into the workspace, told apart+from every other command so the gate can treat it as the file write it is.++Recognised, with nothing else in the command:++ [cd <dir> &&] cat > <path> << 'TAG' (also >>, <<TAG, <<-TAG, "TAG")+ [cd <dir> &&] tee [-a] <path> << 'TAG'+ [cd <dir> &&] printf|echo '<text>' > <path> (also >>)++The body must end at the first terminator line, and an unquoted terminator+only passes when the body has nothing the shell would expand. Anything else,+a path outside ``WORKSPACE_PATH``/``SHARED_PATH`` or through ``..`` included, is+not a write. Symlinks live on the sandbox's disk, so the caller checks those.+"""++import posixpath+import re++from backend.blocks.desktop._api import SHARED_PATH, WORKSPACE_PATH+from backend.copilot.context import E2B_WORKDIR++# A literal path: quoted with nothing the quotes still expand, or bare with no+# glob, expansion, operator or quote character.+_WORD = r"""(?:'[^'\n]*'|"[^"$`\\\n]*"|[\w./~+,@%:=-]+)"""+# Bash splits words on a space or a tab only; a newline ends the command, and+# ``\s`` would also accept that and the characters bash keeps inside a word.+_GAP = r"[ \t]"+_CD = rf"(?:cd{_GAP}+(?P<dir>{_WORD}){_GAP}*&&{_GAP}*)?"+_HEREDOC = re.compile(+ rf"{_CD}(?:cat{_GAP}*>>?{_GAP}*(?P<cat>{_WORD})"+ rf"|tee{_GAP}+(?:-a{_GAP}+)?(?P<tee>{_WORD}))"+ rf"{_GAP}*<<(?P<strip>-?){_GAP}*(?P<q>['\"]?)(?P<tag>\w+)(?P=q)"+)+_PRINT = re.compile(+ rf"{_CD}(?:printf|echo){_GAP}+'[^']*'{_GAP}*>>?{_GAP}*(?P<path>{_WORD})"+)+_EXPANDS = re.compile(r"[$`\\]")+_ROOTS = (WORKSPACE_PATH + "/", SHARED_PATH + "/")+++def workspace_write_target(command: str) -> str | None:+ """The absolute path ``command`` writes, or None if it does anything else."""+ # Only what bash itself skips: ``str.strip`` also drops word characters,+ # and trailing blanks on a heredoc's last line change its terminator.+ command = command.lstrip(" \t\n").rstrip("\n")+ head, _, body = command.partition("\n")+ heredoc = _HEREDOC.fullmatch(head.rstrip(" \t"))+ if heredoc is not None:+ if not _body_ends_at_terminator(body, heredoc):+ return None+ return _in_workspace(heredoc["dir"], heredoc["cat"] or heredoc["tee"])+ printed = _PRINT.fullmatch(command.rstrip(" \t"))+ if printed is not None:+ return _in_workspace(printed["dir"], printed["path"])+ return None+++def _body_ends_at_terminator(body: str, heredoc: re.Match[str]) -> bool:+ """The shell runs whatever follows the first terminator line as commands."""+ lines = body.split("\n")+ strip = "\t" if heredoc["strip"] else ""+ ends = [i for i, line in enumerate(lines) if line.lstrip(strip) == heredoc["tag"]]+ if not ends or ends[0] != len(lines) - 1:+ return False+ return bool(heredoc["q"]) or not _EXPANDS.search("\n".join(lines[:-1]))+++def _in_workspace(directory: str | None, path: str) -> str | None:+ base = _resolve(E2B_WORKDIR, directory) if directory else E2B_WORKDIR+ target = _resolve(base, path) if base else None+ return target if target and target.startswith(_ROOTS) else None+++def _resolve(base: str, word: str) -> str | None:+ """None where the shell would read the word differently: ``~user``, ``cd -``,+ and ``..``, which the kernel takes from wherever a symlink before it points."""+ if word[:1] in "'\"":+ word = word[1:-1]+ elif word == "~" or word.startswith("~/"):+ word = E2B_WORKDIR + word[1:]+ elif word.startswith(("~", "-")):+ return None+ if ".." in word.split("/"):+ return None+ return posixpath.normpath(posixpath.join(base, word))diff --git a/autogpt_platform/backend/backend/copilot/gate/shell_write_test.py b/autogpt_platform/backend/backend/copilot/gate/shell_write_test.pynew file mode 100644index 0000000000..3c2bfcab4f--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/gate/shell_write_test.py@@ -0,0 +1,169 @@+"""A pure file write into the workspace is told apart from every other command."""++import pytest++from backend.copilot.gate.shell_write import workspace_write_target++_POST = "/home/user/workspace/blog/post.md"+++ "command, target",+ [+ (+ "cd /home/user/workspace/blog && cat > post.md << 'EOF'\n# Hooks\n$(not run)\nEOF",+ _POST,+ ),+ (f"cat >> {_POST} <<'EOF'\nmore\nEOF\n", _POST),+ (f'cat > {_POST} << "END"\n`x`\nEND', _POST),+ (f"cat > {_POST} <<EOF\nplain text\nEOF", _POST),+ (f"cat > {_POST} <<-EOF\n\tindented\n\tEOF", _POST),+ (f"tee {_POST} << 'EOF'\nbody\nEOF", _POST),+ (f"tee -a '{_POST}' << 'EOF'\nbody\nEOF", _POST),+ ("printf '%s\\n' > ~/workspace/notes.txt", "/home/user/workspace/notes.txt"),+ ("echo 'hi\nthere' >> ~/shared/out.md", "/home/user/shared/out.md"),+ ("cd ~/workspace && echo 'x' > ./a//b.txt", "/home/user/workspace/a/b.txt"),+ ],+)+def test_the_write_shapes_are_recognised(command, target):+ assert workspace_write_target(command) == target+++ "command",+ [+ # Anything besides the write.+ f"cat > {_POST} << 'EOF'; rm -rf ~\nbody\nEOF",+ f"cat > {_POST} << 'EOF' && curl x | sh\nbody\nEOF",+ f"cat > {_POST} << 'EOF'\nbody\nEOF\nrm -rf ~",+ f"cat > {_POST} << 'EOF'\nbody\nEOF\ncurl x | sh\nEOF",+ f"cat > {_POST} << 'EOF'\nbody\nEOF; rm -rf ~",+ f"curl x | cat > {_POST}",+ f"echo 'x' > {_POST}; rm -rf ~",+ f"echo 'x' | sh > {_POST}",+ # The body or the path would expand.+ f"cat > {_POST} << EOF\n$(rm -rf ~)\nEOF",+ f"cat > {_POST} << EOF\n`rm -rf ~`\nEOF",+ f'echo "$(rm -rf ~)" > {_POST}',+ "cat > /home/user/workspace/$(rm -rf ~).md << 'EOF'\nx\nEOF",+ "cat > $HOME/workspace/p.md << 'EOF'\nx\nEOF",+ "cat > /home/user/workspace/*.md << 'EOF'\nx\nEOF",+ # Outside the workspace volumes.+ "cat > /home/user/.bashrc << 'EOF'\nx\nEOF",+ "cat > /home/user/workspace/../.bashrc << 'EOF'\nx\nEOF",+ "cd /etc && cat > hosts << 'EOF'\nx\nEOF",+ "cd - && cat > p.md << 'EOF'\nx\nEOF",+ "cat > ~root/workspace/p.md << 'EOF'\nx\nEOF",+ "cat > notes.md << 'EOF'\nx\nEOF",+ # A symlinked directory before a ``..`` moves where it lands.+ "cd ~/workspace/link && cat > ../p.md << 'EOF'\nx\nEOF",+ "cat > ~/workspace/link/../p.md << 'EOF'\nx\nEOF",+ # No terminator, so the shell reads to the end.+ f"cat > {_POST} << 'EOF'\nbody",+ "ls ~/workspace",+ ],+)+def test_anything_else_stays_a_shell_command(command):+ assert workspace_write_target(command) is None+++_OUT = "/home/user/workspace/o.txt"+# Each branch as bash tokens; the heredoc body follows the header's own newline.+_BRANCHES = [+ (["echo", "'x'", ">", _OUT], ""),+ (["printf", "'x'", ">>", _OUT], ""),+ (["cd", "/home/user/workspace", "&&", "echo", "'x'", ">", "o.txt"], ""),+ (["cat", ">", _OUT, "<<", "'EOF'"], "\nx\nEOF"),+ (["tee", "-a", _OUT, "<<-", "EOF"], "\nx\nEOF"),+ (+ ["cd", "/home/user/workspace", "&&", "cat", ">", "o.txt", "<<", "'EOF'"],+ "\nx\nEOF",+ ),+]+++def _every_gap(separator: str) -> list[str]:+ """Each branch with one gap between tokens replaced by ``separator``."""+ return [+ " ".join(tokens[:i]) + separator + " ".join(tokens[i:]) + body+ for tokens, body in _BRANCHES+ for i in range(1, len(tokens))+ ]+++def test_every_branch_is_recognised_with_plain_spaces():+ for tokens, body in _BRANCHES:+ assert workspace_write_target(" ".join(tokens) + body) == _OUT+++# Only a space or a tab separates bash words; a newline ends the command, and+# the rest are word characters, so any of them between tokens changes the parse.+ "command",+ [+ command+ for separator in ["\n", "\r", "\v", "\f", "\xa0", "\x1c"]+ for command in _every_gap(separator)+ ],+)+def test_a_token_gap_bash_does_not_read_as_a_blank_stays_a_shell_command(command):+ assert workspace_write_target(command) is None+++ "command",+ [+ # kcze's two: a newline turns the quoted word into the next command.+ f"echo\n'/home/user/workspace/run.sh' > {_OUT}",+ "cd\n/home/user/workspace/run.sh && echo 'x' > o.txt",+ # A second command, joined any way bash joins them.+ f"echo 'x' > {_OUT}; id",+ f"echo 'x' > {_OUT} & id",+ f"echo 'x' > {_OUT} | id",+ f"echo 'x' > {_OUT} && id",+ f"echo 'x' > {_OUT} || id",+ f"echo 'x' > {_OUT}\nid",+ "cd /home/user/workspace; echo 'x' > o.txt",+ "cd /home/user/workspace & echo 'x' > o.txt",+ "cd /home/user/workspace || echo 'x' > o.txt",+ f"cat > {_OUT} << 'EOF' ; id\nx\nEOF",+ f"cat > {_OUT} << 'EOF' &\nx\nEOF",+ f"cat > {_OUT} << 'EOF' | sh\nx\nEOF",+ # Every expansion, in each place a word can sit.+ "echo 'x' > /home/user/workspace/$(id).txt",+ "echo 'x' > /home/user/workspace/`id`.txt",+ "echo 'x' > /home/user/workspace/$HOME.txt",+ "echo 'x' > /home/user/workspace/${HOME}.txt",+ "echo 'x' > \"/home/user/workspace/$(id).txt\"",+ "echo 'x' > >(sh)",+ f"echo 'x' <(id) > {_OUT}",+ f'echo "$(id)" > {_OUT}',+ "cd /home/user/workspace/$(id) && echo 'x' > o.txt",+ f"cat <(id) > {_OUT} << 'EOF'\nx\nEOF",+ f"cat > {_OUT} << EOF\n$(id)\nEOF",+ f"cat > {_OUT} << EOF\n${{HOME}}\nEOF",+ f"cat > {_OUT} << EOF\n$HOME\nEOF",+ # Anything after the terminator runs as commands.+ f"cat > {_OUT} << 'EOF'\nx\nEOF\nid",+ f"cat > {_OUT} << 'EOF'\nx\nEOF\n id",+ f"cat > {_OUT} << 'EOF'\nx\nEOF\n\n\nid",+ f"cat > {_OUT} <<- 'EOF'\nx\n\tEOF\nid\nEOF",+ # A header that does not end where the regex thinks it does.+ f"cat > {_OUT} << 'EOF'\r\nx\nEOF",+ f"cat > {_OUT} << 'EOF'\\\nx\nEOF",+ # A would-be terminator bash does not read as one leaves a line after it.+ f"cat > {_OUT} << 'EOF'\nx\nEOF ",+ # Python strips these from the ends of a string; bash keeps them in a word.+ f"\x1cecho 'x' > {_OUT}",+ f"echo 'x' > {_OUT}\x1c",+ f"echo 'x' > {_OUT}\x85",+ f"cat > {_OUT} << 'EOF'\nx\nEOF\x1c",+ ],+)+def test_a_second_command_or_an_expansion_stays_a_shell_command(command):+ assert workspace_write_target(command) is None+++def test_a_body_line_that_only_looks_like_the_terminator_is_data():+ # ``<<`` strips no tabs, so bash reads ``\tEOF`` and ``id`` as body lines.+ assert workspace_write_target(f"cat > {_OUT} << 'EOF'\n\tEOF\nid\nEOF") == _OUTdiff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.pyindex 6f136b359a..78d322fe4a 100644--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py@@ -30,12 +30,20 @@ from backend.copilot.context import ()from backend.copilot.credential_selection import selected_credentialsfrom backend.copilot.gate.executed_files import run_targets+from backend.copilot.gate.policy import Effect+from backend.copilot.gate.shell_write import workspace_write_target+from backend.copilot.gate.subject import Subjectfrom backend.copilot.integration_creds import (get_github_user_git_identity,get_integration_env_vars,)from backend.copilot.model import ChatSession-from backend.util.sandbox_login import changed_login_files, judged_text, read_capped+from backend.util.sandbox_login import (+ changed_login_files,+ judged_text,+ read_capped,+ run_internal,+)from .base import BaseToolfrom .connect_integration import requested_scopes@@ -70,6 +78,8 @@ def _build_completion_response(class BashExecTool(BaseTool):"""Execute Bash commands on E2B or in a bubblewrap sandbox."""+ has_gate_subject = True+@propertydef name(self) -> str:return "bash_exec"@@ -111,6 +121,16 @@ class BashExecTool(BaseTool):# users reach the token injection path.return True+ async def gate_subject(+ self, user_id: str, session: ChatSession, args: dict[str, Any]+ ) -> Subject | None:+ """A command that only writes one workspace file is judged as that write."""+ command = args.get("command")+ target = workspace_write_target(command) if isinstance(command, str) else None+ if target is None or not await _lands_on(target):+ return None+ return Subject(key="write_workspace_file", name=target, effect=Effect.WORKSPACE)+async def gate_context(self, args: dict[str, Any]) -> dict[str, str | None] | None:"""What the scripts this command runs contain, and the login files changedsince the sandbox was made; None where one could not be read or told."""@@ -280,3 +300,22 @@ class BashExecTool(BaseTool):error="e2b_execution_error",session_id=session_id,)+++async def _lands_on(path: str) -> bool:+ """A shell write to ``path`` lands there: no symlink on it, and no changed login+ file, which runs first and can redirect it (``CDPATH``, an exported ``cat``).+ Same accepted race as ``_check_sandbox_symlink_escape``."""+ sandbox = get_current_sandbox()+ if sandbox is None:+ return False+ try:+ if await changed_login_files(sandbox):+ return False+ result = await run_internal(+ sandbox, f"readlink -m -- {shlex.quote(path)}", cwd=E2B_WORKDIR, timeout=5+ )+ except Exception:+ logger.warning("Could not resolve a bash_exec write target", exc_info=True)+ return False+ return result.exit_code == 0 and (result.stdout or "").strip() == pathdiff --git a/autogpt_platform/backend/backend/copilot/tools/gate_subject_test.py b/autogpt_platform/backend/backend/copilot/tools/gate_subject_test.pyindex 63075cc72c..fae799dc94 100644--- a/autogpt_platform/backend/backend/copilot/tools/gate_subject_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/gate_subject_test.py@@ -5,6 +5,9 @@ approval handed to the run are the ones the engines call."""import json+import os+import shlex+import subprocessfrom datetime import UTC, datetimefrom types import SimpleNamespacefrom typing import Any@@ -36,7 +39,11 @@ from backend.copilot.gate.review import review_payloadfrom backend.copilot.gate.subject import workflow_subjectfrom backend.copilot.model import AutopilotMode, ChatSession, ChatSessionMetadatafrom backend.copilot.tools.bash_exec import BashExecTool-from backend.copilot.tools.models import BlockOutputResponse, ErrorResponse+from backend.copilot.tools.models import (+ BashExecResponse,+ BlockOutputResponse,+ ErrorResponse,+)from backend.copilot.tools.run_agent import RunAgentToolfrom backend.copilot.tools.run_capability import RunCapabilityToolfrom backend.data.graph import BaseGraph, GraphModel, Link, Node, NodeModel@@ -787,3 +794,163 @@ async def _opened() -> Headline:# The headline the real card stores, so the chat row names what it names.return Headline.model_validate(review_payload(tool_name, args, subject)["headline"])+++# ---- a workspace file written through bash_exec -----------------------------++_LONG_POST = (+ "cd /home/user/workspace/blog && cat > post.md << 'EOF'\n"+ + "word " * 2_000+ + "\nEOF"+)+++class _LocalSandbox:+ """Runs sandbox commands as E2B does, with real ``bash -l`` here and+ ``/home/user`` rooted at ``home``."""++ def __init__(self, home: str):+ self.home = home+ self.sandbox_id = "sbx-test"+ self.commands = self+ self.files: Any = None++ async def run(+ self,+ cmd: str,+ envs: dict[str, str] | None = None,+ cwd: str = "/home/user",+ **_: Any,+ ) -> SimpleNamespace:+ user_home = (envs or {}).get("HOME", "/home/user")+ done = subprocess.run(+ ["bash", "-l", "-c", cmd.replace("/home/user", self.home)],+ cwd=cwd.replace("/home/user", self.home),+ env={**os.environ, "HOME": user_home.replace("/home/user", self.home)},+ capture_output=True,+ text=True,+ timeout=10,+ )+ return SimpleNamespace(+ exit_code=done.returncode,+ stdout=done.stdout.replace(self.home, "/home/user"),+ stderr=done.stderr,+ )+++def home(tmp_path):+ """The sandbox's home, with a real ``~/workspace/blog`` in it."""+ (tmp_path / "workspace" / "blog").mkdir(parents=True)+ with patch(+ "backend.copilot.tools.bash_exec.get_current_sandbox",+ return_value=_LocalSandbox(str(tmp_path)),+ ):+ yield tmp_path+++async def _bash(mode: AutopilotMode, command: str):+ """What ran, whether it was held, and how often the supervisor was asked."""+ shell = AsyncMock(+ return_value=BashExecResponse(+ message="ok", stdout="", stderr="", exit_code=0, timed_out=False+ )+ )+ asks = AsyncMock(return_value=Judgement(allowed=False, reason="too long"))+ with (+ patch.object(BashExecTool, "_execute", shell),+ patch(f"{_GATE}.supervise", asks),+ ):+ result = await BashExecTool().execute(+ "user-1", _session(mode), "call-1", command=command+ )+ return shell.await_count, _is_held(result), asks.await_count++[email protected]("mode", ["ask_first", "auto"])+ "command, runs",+ [(_LONG_POST, True), (_LONG_POST + "\nrm -rf ~", False)],+)+async def test_a_heredoc_write_into_the_workspace_runs_as_a_file_write(+ gate, home, mode, command, runs+):+ """A write never reaches the supervisor, however long; a second command does."""+ ran, held, asked = await _bash(mode, command)+ assert held is not runs+ assert ran == int(runs)+ assert asked == int(not runs)+++ "link, points_to",+ [("workspace/blog/post.md", ".bashrc"), ("workspace/blog", ".config")],+)+async def test_a_write_through_a_symlink_is_judged_as_the_shell_command(+ gate, home, link, points_to+):+ """The shell follows the link, so the file written is not the one named."""+ path = home / link+ if path.is_dir():+ path.rmdir()+ (home / points_to).mkdir()+ path.symlink_to(home / points_to)+ ran, held, asked = await _bash("auto", _LONG_POST)+ assert (ran, held, asked) == (0, True, 1)+++async def test_a_login_file_cannot_answer_the_symlink_check(gate, home):+ """The model can edit ``~/.profile``, which ``bash -l`` runs before a command."""+ (home / "workspace/blog/post.md").symlink_to(home / ".bashrc")+ (home / ".profile").write_text('readlink() { echo "$3"; }\n')+ ran, held, asked = await _bash("auto", _LONG_POST)+ assert (ran, held, asked) == (0, True, 1)++[email protected]_login_chain+ "profile, command, lands",+ [+ # A relative `cd` searches CDPATH first.+ (+ "export CDPATH={home}/other\n",+ _LONG_POST.replace("/home/user/workspace/blog", "workspace/blog"),+ "other/workspace/blog/post.md",+ ),+ # The command's own `bash -c` inherits an exported function.+ (+ "cat() {{ command cat > {home}/elsewhere; }}; export -f cat\n",+ _LONG_POST,+ "elsewhere",+ ),+ ],+)+async def test_a_write_under_a_changed_login_chain_is_judged(+ gate, home, profile, command, lands+):+ """``bash -l`` runs the chain before the command and can move the write."""+ (home / "other/workspace/blog").mkdir(parents=True)+ sandbox, chain = _LocalSandbox(str(home)), FakeSandbox(stock_files())+ sandbox.files = chain.files+ profile = profile.format(home=home)+ with (+ patch.object(+ sandbox_login, "get_redis_async", AsyncMock(return_value=FakeRedis())+ ),+ patch(f"{_BASH}.get_current_sandbox", return_value=sandbox),+ ):+ await sandbox_login.record_baseline(sandbox)+ (home / ".profile").write_text(profile)+ chain.store["/home/user/.profile"] = profile.encode()+ await sandbox.run(f"bash -c {shlex.quote(command)}")+ assert (home / lands).read_text().startswith("word ")+ ran, held, asked = await _bash("auto", command)+ assert (ran, held, asked) == (0, True, 1)+++async def test_a_write_with_no_sandbox_to_resolve_it_in_is_judged(gate):+ with patch(+ "backend.copilot.tools.bash_exec.get_current_sandbox", return_value=None+ ):+ ran, held, asked = await _bash("auto", _LONG_POST)+ assert (ran, held, asked) == (0, True, 1)