Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix(daemon): restrict unix socket file to owner-only access
Saurav Panda8c41cf795a0b
diff --git a/browser_use/skill_cli/daemon.py b/browser_use/skill_cli/daemon.pyindex 225234d5b..502aa290e 100644--- a/browser_use/skill_cli/daemon.py+++ b/browser_use/skill_cli/daemon.py@@ -398,10 +398,21 @@ class Daemon:else:# Unix: socket serverPath(sock_path).unlink(missing_ok=True)- self._server = await asyncio.start_unix_server(- self.handle_connection,- sock_path,- )+ # Restrict the socket file to owner-only access. The HMAC auth token+ # gates dispatch, but a permissive socket lets co-tenants probe.+ old_umask = os.umask(0o077)+ try:+ self._server = await asyncio.start_unix_server(+ self.handle_connection,+ sock_path,+ )+ finally:+ os.umask(old_umask)+ # umask only floors permissions at creation; enforce 0o600 explicitly.+ try:+ os.chmod(sock_path, 0o600)+ except OSError as e:+ logger.warning(f'Could not chmod socket {sock_path} to 0o600: {e}')logger.info(f'Listening on Unix socket {sock_path}')# Write PID file after server is bounddiff --git a/tests/ci/security/test_daemon_socket_perms.py b/tests/ci/security/test_daemon_socket_perms.pynew file mode 100644index 000000000..e79916c98--- /dev/null+++ b/tests/ci/security/test_daemon_socket_perms.py@@ -0,0 +1,77 @@+"""Tests for daemon Unix socket file permissions.++The HMAC auth token gates command dispatch, but the socket file itself is left+world-accessible by `asyncio.start_unix_server`. On multi-user hosts that lets+a hostile co-tenant `connect()` and probe behavior even before the handshake+fails. The fix is to `chmod 0o600` the socket file right after bind, mirroring+the auth-token file's permissions.+"""++from __future__ import annotations++import asyncio+import os+import shutil+import stat+import sys+import tempfile+from pathlib import Path++import pytest++from browser_use.skill_cli.daemon import Daemon++skip_on_windows = pytest.mark.skipif(+ sys.platform == 'win32',+ reason='Daemon uses TCP, not Unix sockets, on Windows.',+)+++@skip_on_windows+async def test_daemon_unix_socket_file_is_0o600(monkeypatch: pytest.MonkeyPatch) -> None:+ """After bind, the Unix socket file must have permissions 0o600 (owner rw only)."""+ # AF_UNIX paths are capped at ~104 bytes on macOS / ~108 on Linux, so the+ # default pytest tmp_path (deep folders/...) is too long. Use a short /tmp dir.+ short_home = Path(tempfile.mkdtemp(prefix='bu-perm-', dir='/tmp'))+ monkeypatch.setenv('BROWSER_USE_HOME', str(short_home))++ daemon = Daemon(headed=False, profile=None, session='perm_test')++ run_task = asyncio.create_task(daemon.run())++ from browser_use.skill_cli.utils import get_socket_path++ sock_path = Path(get_socket_path('perm_test'))+ # Wait for the daemon to bind. start_unix_server creates the file; the chmod+ # (after the fix) runs immediately after.+ for _ in range(100):+ if sock_path.exists():+ break+ if run_task.done():+ # Surface any exception that killed run() before the bind.+ exc = run_task.exception()+ if exc:+ raise AssertionError(f'daemon.run() exited before binding socket: {exc!r}') from exc+ pytest.fail('daemon.run() returned cleanly before binding socket')+ await asyncio.sleep(0.1)+ else:+ run_task.cancel()+ pytest.fail(f'daemon never created socket at {sock_path}')++ try:+ mode = stat.S_IMODE(os.stat(sock_path).st_mode)+ assert mode == 0o600, (+ f'Socket file at {sock_path} has mode {oct(mode)}, expected 0o600. '+ f'World/group access lets co-tenants probe the daemon before the auth handshake.'+ )+ finally:+ daemon._shutdown_event.set()+ try:+ await asyncio.wait_for(run_task, timeout=5)+ except (TimeoutError, asyncio.CancelledError):+ run_task.cancel()+ try:+ await run_task+ except (asyncio.CancelledError, Exception):+ pass+ shutil.rmtree(short_home, ignore_errors=True)