Skip to content

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): restart the desktop stream when x11vnc died behind a live noVNC (#14698)

Krzysztof Czerwinski90da73f2259e
diff --git a/autogpt_platform/backend/backend/blocks/desktop/_api.py b/autogpt_platform/backend/backend/blocks/desktop/_api.py
index 1eac84a264..cf42b631c8 100644
--- a/autogpt_platform/backend/backend/blocks/desktop/_api.py
+++ b/autogpt_platform/backend/backend/blocks/desktop/_api.py
@@ -46,7 +46,12 @@ _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"
+# The password file goes too: x11vnc deletes it only once it has read it, and
+# one that failed before that would leave the credential resting on the box.
+_STOP_STREAM = (
+ "pkill -f '[n]ovnc_proxy' || true; pkill -x x11vnc || true; "
+ f"rm -f {shlex.quote(VNC_PASSWORD_PATH)}"
+)
# 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
@@ -144,12 +149,12 @@ class DesktopSession:
"""Return the live stream URL and its password, starting the VNC stack
only if needed.
- *password* is the one this caller issued last time. While noVNC is
- still serving it (E2B's pause/resume restores processes) the same URL
- comes back: restarting x11vnc and noVNC would sever the stream the
- user is watching. Without it, or once the proxy is gone, the stack is
- (re)started under a fresh password, and whoever held the old URL is
- locked out. The caller decides when to forget the password (after a
+ *password* is the one this caller issued last time. While x11vnc and
+ noVNC are both still serving it (E2B's pause/resume restores
+ processes) the same URL comes back: restarting them would sever the
+ stream the user is watching. Without it, or once either is gone, the
+ stack is (re)started under a fresh password, and whoever held the old
+ URL is locked out. The caller decides when to forget the password (after a
pause, say) and so when a URL that may have leaked stops working.
"""
if password is None or not await self._stream_listening():
@@ -213,8 +218,23 @@ class DesktopSession:
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} "')
+ """Whether the whole stream is still up (it survives a pause).
+
+ Both halves, not just the proxy: noVNC keeps listening after x11vnc
+ has died, and a URL handed back then opens onto nothing. A listener
+ on the VNC port is not proof of x11vnc either: once it is gone the
+ box's user can bind that port itself, so root's own process is asked
+ for as well, which that user cannot forge.
+ """
+ try:
+ await self._vnc_command(
+ f"pgrep -x -u {VNC_USER} x11vnc >/dev/null"
+ f' && netstat -tln | grep -q ":{VNC_PORT} "'
+ f' && netstat -tln | grep -q ":{STREAM_PORT} "'
+ )
+ except Exception:
+ return False
+ return True
async def _vnc_command(self, command: str):
return await self.run_command(command, user=VNC_USER)
diff --git a/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py b/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py
index 924beffb69..2978c8c4ae 100644
--- a/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py
+++ b/autogpt_platform/backend/backend/blocks/desktop/desktop_stream_test.py
@@ -7,21 +7,39 @@ import pytest
from backend.blocks.desktop._api import (
HOME_PATH,
+ STREAM_PORT,
VNC_PASSWORD_PATH,
+ VNC_PORT,
VNC_USER,
DesktopSession,
)
-def _session(listening: bool) -> tuple[DesktopSession, AsyncMock]:
- """A box where noVNC is (not) serving; ``run`` records every command."""
+def _session(
+ listening: bool,
+ vnc_listening: bool | None = None,
+ x11vnc_running: bool | None = None,
+) -> tuple[DesktopSession, AsyncMock]:
+ """A box where noVNC (and, unless told otherwise, x11vnc with it) is (not)
+ serving; ``run`` records every command. *x11vnc_running* is root's own
+ process, which by default is what holds the VNC port."""
+ vnc_listening = listening if vnc_listening is None else vnc_listening
+ up = {
+ f":{STREAM_PORT} ": listening,
+ f":{VNC_PORT} ": vnc_listening,
+ f"pgrep -x -u {VNC_USER} x11vnc": (
+ vnc_listening if x11vnc_running is None else x11vnc_running
+ ),
+ }
run = AsyncMock()
async def fake_run(command: str, **kwargs):
- if command.startswith("netstat") and "grep -q" in command:
- if listening:
+ if "grep -q" in command:
+ # As the shell would: the check passes only if everything it
+ # asks about is there.
+ if all(is_up for asked, is_up in up.items() if asked in command):
return MagicMock()
- raise RuntimeError("nothing on the stream port")
+ raise RuntimeError("Command exited with code 1 and error:")
return MagicMock()
run.side_effect = fake_run
@@ -73,7 +91,40 @@ async def test_reopen_reuses_the_issued_password_while_novnc_still_serves():
assert password == "issued-before" and "password=issued-before" in stream.url
# Restarting the stack would sever the stream the user is watching.
- assert not any("x11vnc" in cmd for cmd, _ in _commands(run))
+ assert not any(cmd.startswith(("pkill", "x11vnc")) for cmd, _ in _commands(run))
+
+
+async def test_reopen_restarts_a_stream_whose_x11vnc_died_behind_a_live_novnc():
+ """noVNC's port alone says nothing: it keeps listening with no x11vnc
+ behind it, and the same URL would open onto a broken stream."""
+ session, run = _session(listening=True, vnc_listening=False)
+
+ stream, password = await session.start_stream("issued-before")
+
+ assert password != "issued-before" and len(password) == 16
+ assert f"password={password}" in stream.url
+ commands = [cmd for cmd, _ in _commands(run)]
+ # The whole stack, so the surviving proxy does not keep the stream port.
+ stopped = next(i for i, cmd in enumerate(commands) if cmd.startswith("pkill"))
+ assert "ovnc_proxy" in commands[stopped] and "x11vnc" in commands[stopped]
+ assert any(cmd.startswith("x11vnc") for cmd in commands[stopped + 1 :])
+ assert any("novnc_proxy --vnc" in cmd for cmd in commands[stopped + 1 :])
+
+
+async def test_a_listener_on_the_vnc_port_does_not_pass_for_x11vnc():
+ """With x11vnc gone the box's user can bind its port with a server of its
+ own; only root's process, asked for as root, cannot be forged."""
+ session, run = _session(listening=True, vnc_listening=True, x11vnc_running=False)
+
+ _, password = await session.start_stream("issued-before")
+
+ assert password != "issued-before"
+ commands = _commands(run)
+ assert any(cmd.startswith("x11vnc") for cmd, _ in commands)
+ (check,) = [(cmd, user) for cmd, user in commands if "grep -q" in cmd]
+ assert check[1] == VNC_USER
@pytest.mark.asyncio
@@ -217,3 +268,32 @@ async def test_a_novnc_that_never_serves_takes_x11vnc_down_with_it():
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 :])
+
+
+ "failing, error",
+ [("x11vnc", "x11vnc did not start"), ("cd /opt/noVNC", "noVNC did not start")],
+ ids=["x11vnc", "novnc"],
+)
+async def test_a_failed_start_does_not_leave_the_password_on_the_box(failing, error):
+ """x11vnc deletes the file only once it has read it; one that fails before
+ that would leave the credential resting in root's directory."""
+ run = AsyncMock()
+
+ async def fake_run(command: str, **kwargs):
+ if command.startswith(failing):
+ raise RuntimeError("Command exited with code 1 and error:")
+ return MagicMock(stdout="why")
+
+ run.side_effect = fake_run
+ sandbox = MagicMock()
+ sandbox.commands.run = run
+ with pytest.raises(RuntimeError, match=error):
+ await DesktopSession(sandbox).start_stream(None)
+ commands = _commands(run)
+ failed = next(i for i, (cmd, _) in enumerate(commands) if cmd.startswith(failing))
+ assert any(
+ f"rm -f {VNC_PASSWORD_PATH}" in cmd and user == VNC_USER
+ for cmd, user in commands[failed + 1 :]
+ )