The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): use Chromium's namespace sandbox when userns is available on Linux
4dlt3a7f2234a615
diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex 3e0c866b746..c5da3b99ee8 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -8174,6 +8174,37 @@ def _desktop_linux_needs_no_sandbox() -> bool:return False+def _desktop_linux_userns_sandbox_available() -> bool:+ """Return True when Chromium's unprivileged user-namespace sandbox works.++ When an unprivileged process can create a user namespace, Chromium uses the+ namespace sandbox and never consults the setuid ``chrome-sandbox`` helper,+ so requiring the helper to be root-owned 4755 (and prompting for sudo) is+ unnecessary. Probe the real capability with ``unshare`` instead of reading+ distro-specific sysctls: the probe fails closed on hosts where user+ namespaces are disabled or AppArmor-restricted, which then follow the+ existing setuid-helper path.+ """+ if sys.platform != "linux":+ return False+ unshare = shutil.which("unshare")+ if not unshare:+ return False+ try:+ return (+ subprocess.run(+ [unshare, "--user", "--map-root-user", "true"],+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ timeout=5,+ check=False,+ ).returncode+ == 0+ )+ except (OSError, subprocess.TimeoutExpired):+ return False++def _desktop_linux_sandbox_helper_is_regular_file(packaged_executable: Path) -> bool:"""Return True when ``chrome-sandbox`` exists as a regular file."""if sys.platform != "linux":@@ -8212,6 +8243,10 @@ def _desktop_linux_sandbox_fixup(packaged_executable: Path) -> bool:if sandbox_lstat.st_uid == 0 and stat.S_IMODE(sandbox_lstat.st_mode) == 0o4755:return True+ if _desktop_linux_userns_sandbox_available():+ print("✓ Using Chromium's user-namespace sandbox (setuid helper not needed).")+ return True+sudo = shutil.which("sudo")if not sudo:print("✗ Hermes Desktop requires sudo to configure Electron's Linux sandbox helper.")diff --git a/scripts/desktop-update/posix.sh b/scripts/desktop-update/posix.shindex ad13b859843..1bcd5bc716c 100755--- a/scripts/desktop-update/posix.sh+++ b/scripts/desktop-update/posix.sh@@ -316,6 +316,10 @@ linux_gate() {sb="$unpacked/chrome-sandbox"if [ ! -e "$sb" ]; then GATE=relaunch; return; fiif [ -u "$sb" ] && [ "$(stat -c %u "$sb" 2>/dev/null)" = "0" ]; then GATE=relaunch; return; fi+ # Namespace sandbox usable => Electron never consults the setuid helper,+ # so a non-root chrome-sandbox does not block relaunch (mirrors the+ # _desktop_linux_userns_sandbox_available() probe in hermes_cli/main.py).+ if unshare --user --map-root-user true 2>/dev/null; then GATE=relaunch; return; ficase "${ELECTRON_DISABLE_SANDBOX:-}" in 1|true|TRUE|True) GATE=relaunch; return ;; esac[ "$SANDBOX_FALLBACK" -eq 1 ] && { GATE=relaunch; return; }diff --git a/tests/hermes_cli/test_linux_sandbox_fixup.py b/tests/hermes_cli/test_linux_sandbox_fixup.pynew file mode 100644index 00000000000..9f7e6d1815a--- /dev/null+++ b/tests/hermes_cli/test_linux_sandbox_fixup.py@@ -0,0 +1,116 @@+"""Tests for the Linux desktop sandbox-helper fixup and the userns probe.++``_desktop_linux_sandbox_fixup`` historically demanded a root-owned 4755+``chrome-sandbox`` on every Linux host and shelled out to ``sudo`` to get it+— which fails silently when the desktop entry launches ``hermes desktop``+without a TTY (#88032, #51327), and blocked the updater's relaunch gate+(#58593). On hosts where unprivileged user namespaces work, Chromium uses+its namespace sandbox and never consults the setuid helper, so the fixup now+probes for that capability first and skips the sudo path entirely.+"""++from __future__ import annotations++import stat+import subprocess+import sys+from unittest.mock import patch++from hermes_cli import main as cli_main+++class TestDesktopLinuxUsernsSandboxAvailable:+ def test_false_on_non_linux(self, monkeypatch):+ monkeypatch.setattr(sys, "platform", "darwin")+ assert cli_main._desktop_linux_userns_sandbox_available() is False++ def test_false_when_unshare_is_missing(self, monkeypatch):+ monkeypatch.setattr(sys, "platform", "linux")+ with patch.object(cli_main.shutil, "which", return_value=None):+ assert cli_main._desktop_linux_userns_sandbox_available() is False++ def test_true_when_probe_succeeds(self, monkeypatch):+ monkeypatch.setattr(sys, "platform", "linux")+ with patch.object(cli_main.shutil, "which", return_value="/usr/bin/unshare"), \+ patch.object(cli_main.subprocess, "run") as run:+ run.return_value.returncode = 0+ assert cli_main._desktop_linux_userns_sandbox_available() is True+ probe = run.call_args.args[0]+ assert probe[0] == "/usr/bin/unshare"+ assert "--user" in probe++ def test_false_when_probe_fails(self, monkeypatch):+ """EPERM from the kernel (userns disabled or AppArmor-restricted)."""+ monkeypatch.setattr(sys, "platform", "linux")+ with patch.object(cli_main.shutil, "which", return_value="/usr/bin/unshare"), \+ patch.object(cli_main.subprocess, "run") as run:+ run.return_value.returncode = 1+ assert cli_main._desktop_linux_userns_sandbox_available() is False++ def test_false_when_probe_raises(self, monkeypatch):+ monkeypatch.setattr(sys, "platform", "linux")+ with patch.object(cli_main.shutil, "which", return_value="/usr/bin/unshare"), \+ patch.object(+ cli_main.subprocess,+ "run",+ side_effect=subprocess.TimeoutExpired(cmd="unshare", timeout=5),+ ):+ assert cli_main._desktop_linux_userns_sandbox_available() is False+++class TestDesktopLinuxSandboxFixup:+ def _fake_packaged_app(self, tmp_path):+ """Unpacked-app layout with a non-root, non-setuid chrome-sandbox."""+ unpacked = tmp_path / "linux-unpacked"+ unpacked.mkdir()+ exe = unpacked / "Hermes"+ exe.write_text("", encoding="utf-8")+ sandbox = unpacked / "chrome-sandbox"+ sandbox.write_text("", encoding="utf-8")+ sandbox.chmod(0o755)+ return exe++ def test_userns_host_skips_sudo_and_succeeds(self, monkeypatch, tmp_path):+ """A user-owned helper must not trigger sudo when userns works.++ This is the .desktop-launch regression: no TTY means sudo cannot+ prompt, so reaching the sudo path at all kills the launch.+ """+ monkeypatch.setattr(sys, "platform", "linux")+ exe = self._fake_packaged_app(tmp_path)+ with patch.object(+ cli_main, "_desktop_linux_userns_sandbox_available", return_value=True+ ), \+ patch.object(cli_main.subprocess, "run") as run:+ assert cli_main._desktop_linux_sandbox_fixup(exe) is True+ run.assert_not_called()++ def test_restricted_host_without_sudo_still_fails(self, monkeypatch, tmp_path):+ """The pre-existing strict path is preserved when userns is unusable."""+ monkeypatch.setattr(sys, "platform", "linux")+ exe = self._fake_packaged_app(tmp_path)+ with patch.object(+ cli_main, "_desktop_linux_userns_sandbox_available", return_value=False+ ), \+ patch.object(cli_main.shutil, "which", return_value=None):+ assert cli_main._desktop_linux_sandbox_fixup(exe) is False++ def test_root_owned_setuid_helper_short_circuits(self, monkeypatch, tmp_path):+ """A correctly configured helper wins before the userns probe runs."""+ monkeypatch.setattr(sys, "platform", "linux")+ exe = self._fake_packaged_app(tmp_path)+ real_lstat = (exe.parent / "chrome-sandbox").lstat()++ class _RootSetuidStat:+ st_mode = stat.S_IFREG | 0o4755+ st_uid = 0++ def __getattr__(self, name):+ return getattr(real_lstat, name)++ with patch.object(cli_main.Path, "lstat", return_value=_RootSetuidStat()), \+ patch.object(+ cli_main, "_desktop_linux_userns_sandbox_available"+ ) as probe:+ assert cli_main._desktop_linux_sandbox_fixup(exe) is True+ probe.assert_not_called()