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): desktop stream fails to start since the VNC stack runs as root (#14670)
Krzysztof Czerwinskif5a030e58590
diff --git a/autogpt_platform/backend/backend/blocks/desktop/_api.py b/autogpt_platform/backend/backend/blocks/desktop/_api.pyindex 43046c8879..73ed085d98 100644--- a/autogpt_platform/backend/backend/blocks/desktop/_api.py+++ b/autogpt_platform/backend/backend/blocks/desktop/_api.py@@ -38,6 +38,15 @@ STREAM_PORT = 6080# box (see ``backend.copilot.computer``).VNC_USER = "root"VNC_PASSWORD_PATH = "/root/.vnc/stream_password"+# Root's logs live in root's directory, not /tmp: there the box's user could+# plant a file of the same name, and with fs.protected_regular set the kernel+# refuses even root an O_CREAT open of another user's file in a sticky+# directory, which is what a box that ran its stream as the user has.+_VNC_DIR = VNC_PASSWORD_PATH.rsplit("/", 1)[0]+_X11VNC_LOG = f"{_VNC_DIR}/x11vnc.log"+_X11VNC_ERROR_LOG = f"{_VNC_DIR}/x11vnc_stderr.log"+_NOVNC_LOG = f"{_VNC_DIR}/novnc.log"+_STOP_STREAM = "pkill -f '[n]ovnc_proxy' || true; pkill -x x11vnc || true"# Bound on the E2B volumes API (private beta) so a slow create cannot stall# sandbox creation; the by-name mount fallback is the normal path anyway.VOLUME_API_TIMEOUT_SECONDS = 10@@ -147,25 +156,48 @@ class DesktopSession:password = "".join(secrets.choice(string.ascii_letters + string.digits) for _ in range(16))+ await self._vnc_command(_STOP_STREAM)await self._vnc_command(- "pkill -f '[n]ovnc_proxy' || true; pkill -x x11vnc || true"- )- await self._vnc_command(- f"umask 077 && mkdir -p {shlex.quote(VNC_PASSWORD_PATH.rsplit('/', 1)[0])}"+ f"umask 077 && mkdir -p {shlex.quote(_VNC_DIR)}"f" && printf %s {shlex.quote(password)} > {shlex.quote(VNC_PASSWORD_PATH)}")- await self._vnc_command(- f"x11vnc -bg -display {DISPLAY} -forever -wait 50 -shared "- f"-rfbport {VNC_PORT} -passwdfile rm:{VNC_PASSWORD_PATH} "- ">/tmp/x11vnc.log 2>/tmp/x11vnc_stderr.log"- )- await self.sandbox.commands.run(- f"cd /opt/noVNC/utils && ./novnc_proxy --vnc localhost:{VNC_PORT} "- f"--listen {STREAM_PORT} --web /opt/noVNC > /tmp/novnc.log 2>&1",- background=True,- user=VNC_USER,- )- await self._wait_for(f'netstat -tuln | grep ":{STREAM_PORT} "')+ # -noshm: x11vnc runs as root and Xvfb as the box's user, and the X+ # server cannot attach a shared-memory segment that root owns+ # (MIT-SHM BadAccess, x11vnc exits 1). Without it the screen is+ # read over the X socket instead.+ try:+ await self._vnc_command(+ f"x11vnc -bg -noshm -display {DISPLAY} -forever -wait 50 "+ f"-shared -rfbport {VNC_PORT} "+ f"-passwdfile rm:{VNC_PASSWORD_PATH} "+ f">{_X11VNC_LOG} 2>{_X11VNC_ERROR_LOG}"+ )+ # -bg returns once x11vnc has detached; one that dies after+ # that would leave noVNC serving a stream with nothing behind.+ await self._wait_for(f'netstat -tln | grep ":{VNC_PORT} "')+ except Exception as exc:+ # One that detached but never served is still running.+ with contextlib.suppress(Exception):+ await self._vnc_command(_STOP_STREAM)+ # x11vnc's own words are in the box, not in the exception.+ raise RuntimeError(+ f"x11vnc did not start: {await self._tail(_X11VNC_ERROR_LOG)}"+ ) from exc+ try:+ await self.sandbox.commands.run(+ f"cd /opt/noVNC/utils && ./novnc_proxy --vnc localhost:{VNC_PORT} "+ f"--listen {STREAM_PORT} --web /opt/noVNC > {_NOVNC_LOG} 2>&1",+ background=True,+ user=VNC_USER,+ )+ await self._wait_for(f'netstat -tuln | grep ":{STREAM_PORT} "')+ except Exception as exc:+ # Do not leave an x11vnc serving under a password nobody holds.+ with contextlib.suppress(Exception):+ await self._vnc_command(_STOP_STREAM)+ raise RuntimeError(+ f"noVNC did not start: {await self._tail(_NOVNC_LOG)}"+ ) from exchost = self.sandbox.get_host(STREAM_PORT)url = (f"https://{host}/vnc.html"@@ -173,6 +205,13 @@ class DesktopSession:)return DesktopStream(url=url, sandbox_id=self.sandbox_id), password+ async def _tail(self, path: str) -> str:+ try:+ result = await self._vnc_command(f"tail -n 15 {shlex.quote(path)}")+ return result.stdout.strip() or "(empty log)"+ except Exception:+ return "(log unreadable)"+async def _stream_listening(self) -> bool:"""Whether noVNC is still serving the stream (it survives a pause)."""return await self._check(f'netstat -tuln | grep -q ":{STREAM_PORT} "')diff --git a/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py b/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.pyindex 9b0a200aa6..138fc2e2e0 100644--- a/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py+++ b/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py@@ -1,7 +1,7 @@"""The live stream: its password never rests on the box, and a re-open handsback the URL the user holds only while the box has kept running."""-from unittest.mock import AsyncMock, MagicMock+from unittest.mock import AsyncMock, MagicMock, patchimport pytest@@ -55,6 +55,14 @@ async def test_a_fresh_stream_keeps_its_password_out_of_the_shells_reach():(x11vnc,) = [cmd for cmd, _ in commands if cmd.startswith("x11vnc")]assert f"-passwdfile rm:{VNC_PASSWORD_PATH}" in x11vncassert "-storepasswd" not in x11vnc and password not in x11vnc+ # Root's x11vnc and the user's Xvfb cannot share memory: with MIT-SHM on,+ # x11vnc exits 1 (BadAccess on ShmAttach) and no desktop ever opens.+ assert " -noshm " in x11vnc+ # Root's logs stay out of /tmp, where the box's user could own a file of+ # the same name (a box that once ran its stream as the user does) and the+ # kernel would refuse root the open.+ (novnc,) = [cmd for cmd, _ in commands if "novnc_proxy --vnc" in cmd]+ assert "/tmp/" not in x11vnc and "/tmp/" not in novnc@pytest.mark.asyncio@@ -89,3 +97,110 @@ async def test_a_remembered_password_is_dropped_once_the_proxy_is_gone():assert password != "issued-before"assert any(cmd.startswith("x11vnc") for cmd, _ in _commands(run))+++async def test_a_failed_x11vnc_says_why_in_its_own_words():+ """Its stderr goes to a file in the box, so the bare exception is empty."""+ run = AsyncMock()++ async def fake_run(command: str, **kwargs):+ if command.startswith("netstat"):+ raise RuntimeError("nothing on the stream port")+ if command.startswith("x11vnc"):+ raise RuntimeError("Command exited with code 1 and error:")+ if command.startswith("tail"):+ return MagicMock(stdout="X Error of failed request: BadAccess\n")+ return MagicMock()++ run.side_effect = fake_run+ sandbox = MagicMock()+ sandbox.commands.run = run+ with pytest.raises(RuntimeError, match="x11vnc did not start: X Error.*BadAccess"):+ await DesktopSession(sandbox).start_stream(None)+ (tail,) = [(c, u) for c, u in _commands(run) if c.startswith("tail")]+ assert tail[1] == VNC_USER+++ "tail, said",+ [+ (MagicMock(stdout=" \n"), "(empty log)"),+ (RuntimeError("gone"), "(log unreadable)"),+ ],+ ids=["empty", "unreadable"],+)+async def test_a_failed_x11vnc_with_no_log_to_show_still_raises(tail, said):+ run = AsyncMock()++ async def fake_run(command: str, **kwargs):+ if command.startswith(("netstat", "x11vnc")):+ raise RuntimeError("Command exited with code 1 and error:")+ if command.startswith("tail"):+ if isinstance(tail, Exception):+ raise tail+ return tail+ return MagicMock()++ run.side_effect = fake_run+ sandbox = MagicMock()+ sandbox.commands.run = run+ with pytest.raises(RuntimeError, match="x11vnc did not start") as raised:+ await DesktopSession(sandbox).start_stream(None)+ assert said in str(raised.value)+++async def test_an_x11vnc_that_dies_after_detaching_is_caught_before_novnc_starts():+ """``-bg`` reports success once it has forked; the port never opening is+ the only sign, and noVNC's own port would come up regardless."""+ run = AsyncMock()++ async def fake_run(command: str, **kwargs):+ if command.startswith("netstat"):+ raise RuntimeError("not listening")+ if command.startswith("tail"):+ return MagicMock(stdout="caught X11 error")+ return MagicMock()++ run.side_effect = fake_run+ sandbox = MagicMock()+ sandbox.commands.run = run+ with (+ patch("backend.blocks.desktop._api._READY_POLL_ATTEMPTS", 2),+ patch("backend.blocks.desktop._api._READY_POLL_SECONDS", 0),+ pytest.raises(RuntimeError, match="x11vnc did not start: caught X11 error"),+ ):+ await DesktopSession(sandbox).start_stream(None)+ commands = [cmd for cmd, _ in _commands(run)]+ assert not any("novnc_proxy --vnc" in cmd for cmd in commands)+ # Detached but never serving, it is still running: stopped, not left.+ started = next(i for i, cmd in enumerate(commands) if cmd.startswith("x11vnc"))+ assert any(cmd.startswith("pkill") for cmd in commands[started + 1 :])+++async def test_a_novnc_that_never_serves_takes_x11vnc_down_with_it():+ """Otherwise x11vnc keeps serving under a password nobody was handed."""+ run = AsyncMock()++ async def fake_run(command: str, **kwargs):+ if command.startswith("netstat") and "5900" not in command:+ raise RuntimeError("not listening")+ if command.startswith("tail"):+ return MagicMock(stdout="websockify: address in use")+ return MagicMock()++ run.side_effect = fake_run+ sandbox = MagicMock()+ sandbox.commands.run = run+ with (+ patch("backend.blocks.desktop._api._READY_POLL_ATTEMPTS", 2),+ patch("backend.blocks.desktop._api._READY_POLL_SECONDS", 0),+ pytest.raises(RuntimeError, match="noVNC did not start: websockify"),+ ):+ await DesktopSession(sandbox).start_stream(None)+ commands = [cmd for cmd, _ in _commands(run)]+ started = next(i for i, cmd in enumerate(commands) if cmd.startswith("x11vnc"))+ assert any(cmd.startswith("pkill") for cmd in commands[started + 1 :])