Skip to content

Commit aa12263

Browse files
authored
feat(workbot): keep the agent's browser out of the Computer view (#6588)
The agent's Chromium ran on the same X display as native apps, so the Computer view and computer-use screenshots showed the browser window and its taskbar entry, and native windows could cover the Browser view. Workbot computers now start Chromium on a display of its own (:100). The Computer view shows only the native desktop (:99); the Browser view shows only the browser. Native apps never receive the browser display's X authority. Den worker desktops keep one shared display. Signed-off-by: Jalil <reachjalil@gmail.com>
1 parent 36cd61f commit aa12263

4 files changed

Lines changed: 61 additions & 31 deletions

File tree

‎ee/apps/den-worker-runtime/scripts/cloud-desktop.sh‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
set -eu
33
state="$1/cloud-desktop"
44
display="$2"
5-
case "$display" in :[0-9]|:[0-9][0-9]|:[0-9][0-9][0-9]) ;; *) echo "Invalid cloud display" >&2; exit 1 ;; esac
5+
# An optional display of the browser's own keeps it off the desktop where native
6+
# apps run, so watching or driving the computer never shows the agent's browser.
7+
browser_display="${3:-$display}"
8+
for value in "$display" "$browser_display"; do
9+
case "$value" in :[0-9]|:[0-9][0-9]|:[0-9][0-9][0-9]) ;; *) echo "Invalid cloud display" >&2; exit 1 ;; esac
10+
done
611
command -v chromium >/dev/null
712
command -v Xvfb >/dev/null
813
# OCI-to-VM conversion does not preserve default ACLs. Reapply the desktop
@@ -11,9 +16,10 @@ if [ -f /usr/local/share/openwork-desktop/configure-files.sh ]; then
1116
bash /usr/local/share/openwork-desktop/configure-files.sh
1217
fi
1318
runtime="/run/openwork-desktop/${display#:}"
19+
browser_runtime="/run/openwork-desktop/${browser_display#:}"
1420
# Root's authority and logs never live in the browser-writable profile state.
1521
# Hold directory inodes while setting ownership; never recurse through that state.
16-
python3 - "$1" "$runtime" <<'PY'
22+
python3 - "$1" "$runtime" "$browser_runtime" <<'PY'
1723
import os
1824
import pwd
1925
import stat
@@ -67,15 +73,21 @@ def prepare_state(base, runtime, uid, gid):
6773
finally: os.close(folder)
6874
6975
browser = pwd.getpwnam('openwork-browser')
70-
prepare_state(sys.argv[1], sys.argv[2], browser.pw_uid, browser.pw_gid)
76+
for runtime in dict.fromkeys(sys.argv[2:]):
77+
prepare_state(sys.argv[1], runtime, browser.pw_uid, browser.pw_gid)
7178
PY
7279
export DISPLAY="$display" XAUTHORITY="$runtime/.Xauthority"
73-
as_browser() {
80+
# Native apps get a copy of the desktop's authority below, never the browser display's.
81+
browser_authority="$browser_runtime/.Xauthority"
82+
as_browser_on() {
83+
on_display="$1" on_authority="$2"
84+
shift 2
7485
# The desktop must not inherit the worker's host, model or activity tokens.
7586
env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/openwork-browser \
7687
USER=openwork-browser LOGNAME=openwork-browser LANG=C.UTF-8 \
77-
DISPLAY="$display" XAUTHORITY="$XAUTHORITY" LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe runuser -u openwork-browser -- "$@"
88+
DISPLAY="$on_display" XAUTHORITY="$on_authority" LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe runuser -u openwork-browser -- "$@"
7889
}
90+
as_browser() { as_browser_on "$display" "$XAUTHORITY" "$@"; }
7991
as_app() {
8092
env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/openwork-app \
8193
USER=openwork-app LOGNAME=openwork-app LANG=C.UTF-8 \
@@ -163,20 +175,28 @@ guard_desktop_egress() {
163175
command -v iptables >/dev/null || { echo "The desktop network guard needs iptables" >&2; exit 1; }
164176
guard_desktop_egress
165177
umask 077
166-
if [ ! -s "$XAUTHORITY" ]; then
167-
xauth -f "$XAUTHORITY" add "$display" . "$(python3 -c 'import secrets; print(secrets.token_hex(16))')"
168-
fi
169-
chown root:openwork-browser "$XAUTHORITY"
170-
chmod 0640 "$XAUTHORITY"
171-
if ! xdotool getdisplaygeometry >/dev/null 2>&1; then
172-
as_browser setsid -f Xvfb "$display" -screen 0 1280x800x24 -nolisten tcp -auth "$XAUTHORITY" >"$runtime/display.log" 2>&1 < /dev/null
178+
start_display() {
179+
# display, its own authority file, runtime folder
180+
if [ ! -s "$2" ]; then
181+
xauth -f "$2" add "$1" . "$(python3 -c 'import secrets; print(secrets.token_hex(16))')"
182+
fi
183+
chown root:openwork-browser "$2"
184+
chmod 0640 "$2"
185+
if ! DISPLAY="$1" XAUTHORITY="$2" xdotool getdisplaygeometry >/dev/null 2>&1; then
186+
as_browser_on "$1" "$2" setsid -f Xvfb "$1" -screen 0 1280x800x24 -nolisten tcp -auth "$2" >"$3/display.log" 2>&1 < /dev/null
187+
fi
188+
attempt=0
189+
until DISPLAY="$1" XAUTHORITY="$2" xdotool getdisplaygeometry >/dev/null 2>&1; do
190+
attempt=$((attempt + 1))
191+
[ "$attempt" -lt 50 ] || { echo "Cloud display did not start" >&2; exit 1; }
192+
sleep 0.1
193+
done
194+
}
195+
start_display "$display" "$XAUTHORITY" "$runtime"
196+
if [ "$browser_display" != "$display" ]; then
197+
# No window manager here: Chromium fills this display and draws its own frame.
198+
start_display "$browser_display" "$browser_authority" "$browser_runtime"
173199
fi
174-
attempt=0
175-
until xdotool getdisplaygeometry >/dev/null 2>&1; do
176-
attempt=$((attempt + 1))
177-
[ "$attempt" -lt 50 ] || { echo "Cloud display did not start" >&2; exit 1; }
178-
sleep 0.1
179-
done
180200
if id openwork-app >/dev/null 2>&1; then
181201
# Share display access, never the Chromium profile or browser home. The
182202
# window manager launches menu/taskbar apps under the same isolated account.
@@ -201,9 +221,9 @@ if command -v tint2 >/dev/null; then
201221
fi
202222
if ! curl --fail --silent --max-time 2 http://127.0.0.1:9222/json/version >/dev/null; then
203223
# The browser runs as an unprivileged user with Chromium's sandbox enabled.
204-
as_browser setsid -f chromium --remote-debugging-address=127.0.0.1 --remote-debugging-port=9222 \
205-
--user-data-dir="$state/profile" --window-size=1280,800 --force-device-scale-factor=1 --no-first-run \
206-
--no-default-browser-check about:blank >"$runtime/browser.log" 2>&1 < /dev/null
224+
as_browser_on "$browser_display" "$browser_authority" setsid -f chromium --remote-debugging-address=127.0.0.1 --remote-debugging-port=9222 \
225+
--user-data-dir="$state/profile" --window-position=0,0 --window-size=1280,800 --force-device-scale-factor=1 --no-first-run \
226+
--no-default-browser-check about:blank >"$browser_runtime/browser.log" 2>&1 < /dev/null
207227
fi
208228
attempt=0
209229
until curl --fail --silent --max-time 2 http://127.0.0.1:9222/json/version >/dev/null; do
@@ -218,7 +238,7 @@ if [ "${WORKBOT_DESKTOP_VNC:-0}" = 1 ]; then
218238
window=
219239
attempt=0
220240
until [ -n "$window" ]; do
221-
window=$(xdotool search --onlyvisible --class chromium 2>/dev/null | head -n 1) || window=
241+
window=$(DISPLAY="$browser_display" XAUTHORITY="$browser_authority" xdotool search --onlyvisible --class chromium 2>/dev/null | head -n 1) || window=
222242
[ -n "$window" ] && break
223243
attempt=$((attempt + 1))
224244
[ "$attempt" -lt 25 ] || { echo "The browser window is unavailable" >&2; exit 1; }

‎ee/packages/headless-computer/scripts/desktop-bridge.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def start(expected):
8585
pass
8686
except FileNotFoundError:
8787
pass
88-
subprocess.run(['/bin/sh', str(ROOT / 'start.sh'), str(STATE), ':99'],
88+
subprocess.run(['/bin/sh', str(ROOT / 'start.sh'), str(STATE), ':99', ':100'],
8989
env={**ENV, 'WORKBOT_DESKTOP_VNC': '1'}, check=True, timeout=30)
9090
if not (STATE / 'token').exists():
9191
(STATE / 'token').write_text(secrets.token_urlsafe(32))

‎ee/packages/headless-computer/scripts/vnc-stdio.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/python3 -I
22
"""One private RFB transport per viewer; no shared TCP listener to impersonate."""
33
import os
4-
from pathlib import Path
54
import signal
65
import socket
76
import subprocess
@@ -12,13 +11,9 @@
1211
def serve(view, control, input_fd=0, output_fd=1):
1312
if os.geteuid() != 0 or view not in ('browser', 'computer') or control not in ('control', 'view'):
1413
raise PermissionError('The image-owned guest must start this viewer.')
15-
state = Path('/var/lib/openwork-desktop')
14+
# The browser has a display of its own (start.sh), so neither view ever shows the other's windows.
15+
display = ':100' if view == 'browser' else ':99'
1616
extra = []
17-
if view == 'browser':
18-
window = (state / 'browser-window').read_text().strip()
19-
if not window.isdecimal():
20-
raise ValueError('The browser window is unavailable.')
21-
extra += ['-id', window]
2217
if control == 'view':
2318
extra += ['-viewonly']
2419
# x11vnc already sends only changed regions; also poll and send at most ~10 times a second
@@ -33,7 +28,7 @@ def interrupted(_signal, _frame):
3328
signal.signal(signal.SIGINT, interrupted)
3429
try:
3530
process = subprocess.Popen(['/usr/sbin/runuser', '-u', 'openwork-browser', '--', 'x11vnc', '-inetd', '-q',
36-
'-display', ':99', '-auth', '/run/openwork-desktop/99/.Xauthority',
31+
'-display', display, '-auth', f'/run/openwork-desktop/{display[1:]}/.Xauthority',
3732
'-nopw', '-noxdamage', *pacing, *extra], stdin=child, stdout=child,
3833
stderr=subprocess.DEVNULL, start_new_session=True,
3934
env={'PATH': '/usr/local/bin:/usr/bin:/bin', 'HOME': '/home/openwork-browser',

‎ee/packages/headless-computer/scripts/vnc-stdio.test.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ def server(command, **kwargs):
4141
for fd in (incoming, receive, outgoing):
4242
if fd is not None: os.close(fd)
4343

44+
def test_each_view_shows_only_its_own_display(self):
45+
for view, display in [('computer', ':99'), ('browser', ':100')]:
46+
with self.subTest(view=view):
47+
commands = []
48+
def server(command, **kwargs):
49+
commands.append(command)
50+
raise SystemExit(0)
51+
with patch.object(vnc.os, 'geteuid', return_value=0), patch.object(vnc.signal, 'signal'), \
52+
patch.object(vnc.subprocess, 'Popen', side_effect=server), self.assertRaises(SystemExit):
53+
vnc.serve(view, 'view')
54+
command = commands[0]
55+
self.assertEqual(command[command.index('-display') + 1], display)
56+
self.assertEqual(command[command.index('-auth') + 1], f'/run/openwork-desktop/{display[1:]}/.Xauthority')
57+
self.assertNotIn('-id', command)
58+
4459
def test_only_the_privileged_guest_can_start_valid_viewers(self):
4560
for uid, view, control in [(1000, 'computer', 'view'), (0, 'other', 'view'), (0, 'computer', 'other')]:
4661
with patch.object(vnc.os, 'geteuid', return_value=uid), self.assertRaises(PermissionError):

0 commit comments

Comments
 (0)