Skip to content

The agent that grows with you

fix(desktop): AppArmor fix-it lines only when AppArmor is the reason; Linux-only profile install

Tekniumae7cbe59521b
diff --git a/hermes_cli/main_desktop.py b/hermes_cli/main_desktop.py
index c5d862664b0..0c35d63756f 100644
--- a/hermes_cli/main_desktop.py
+++ b/hermes_cli/main_desktop.py
@@ -1625,13 +1625,12 @@ def _check_desktop_skip_build(
def _packaged_desktop_launch_command(packaged_executable: Path) -> list[str]:
"""``[exe, *sandbox flags]`` after the Linux sandbox fixup; exits when the sandbox can't be configured."""
- from hermes_cli.main_desktop_apparmor import print_blocked_remedies
+ from hermes_cli.main_desktop_apparmor import print_no_sandbox_fallback
launch_command = [str(packaged_executable)]
if not _desktop_linux_sandbox_fixup(packaged_executable):
if _desktop_linux_needs_no_sandbox() and _desktop_linux_sandbox_helper_is_regular_file(packaged_executable):
- print("⚠ Falling back to --no-sandbox because this Linux host restricts unprivileged user namespaces and the Electron sandbox helper could not be configured.")
- print_blocked_remedies(packaged_executable)
+ print_no_sandbox_fallback(packaged_executable)
launch_command.append("--no-sandbox")
else:
sys.exit(1)
diff --git a/hermes_cli/main_desktop_apparmor.py b/hermes_cli/main_desktop_apparmor.py
index 23216a83490..772dcc7a033 100644
--- a/hermes_cli/main_desktop_apparmor.py
+++ b/hermes_cli/main_desktop_apparmor.py
@@ -174,6 +174,9 @@ def _write_and_load(sudo: str, parser: str, target: Path, text: str) -> bool:
def install_profile(packaged_executable: Path) -> bool:
"""Write and load the profile through sudo (prompting on a terminal). Prints the outcome."""
+ if sys.platform != "linux":
+ print("✗ --install-apparmor-profile is Linux only: AppArmor profiles do not exist on this OS.")
+ return False
target = profile_path(packaged_executable)
unsafe = unsafe_path_characters(packaged_executable)
if unsafe:
@@ -209,8 +212,15 @@ def install_profile(packaged_executable: Path) -> bool:
return True
-def print_blocked_remedies(packaged_executable: Path) -> None:
- """The actionable lines for a restricted host where the setuid helper could not be set up."""
+def print_no_sandbox_fallback(packaged_executable: Path) -> None:
+ """Why the launch falls back to ``--no-sandbox``; the AppArmor fix-it lines only on a host whose
+ userns restriction is the reason (``ELECTRON_DISABLE_SANDBOX=1`` alone is the user's choice)."""
+ if not apparmor_restricts_userns():
+ print("⚠ Falling back to --no-sandbox because ELECTRON_DISABLE_SANDBOX=1 is set and the Electron"
+ " sandbox helper could not be configured.")
+ return
+ print("⚠ Falling back to --no-sandbox because this Linux host restricts unprivileged user namespaces"
+ " and the Electron sandbox helper could not be configured.")
helper = shlex.quote(str(packaged_executable.parent / "chrome-sandbox"))
print(" Ubuntu's AppArmor policy (kernel.apparmor_restrict_unprivileged_userns=1) blocks Chromium's")
print(" user-namespace sandbox for Hermes Desktop. To keep the sandbox on, run ONE of these:")
diff --git a/tests/hermes_cli/test_desktop_apparmor_install_non_linux.py b/tests/hermes_cli/test_desktop_apparmor_install_non_linux.py
new file mode 100644
index 00000000000..347b38a7863
--- /dev/null
+++ b/tests/hermes_cli/test_desktop_apparmor_install_non_linux.py
@@ -0,0 +1,22 @@
+"""``hermes desktop --install-apparmor-profile`` outside Linux (#130177 review)."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from hermes_cli import main_desktop_apparmor
+
+pytestmark = pytest.mark.platforms("not linux")
+
+
+def test_install_apparmor_profile_refuses_before_any_warning_or_sudo(tmp_path, monkeypatch, capsys):
+ """No security warning and no raw "apparmor_parser not found": one Linux-only line."""
+ calls: list[list[str]] = []
+ monkeypatch.setattr(main_desktop_apparmor, "_run", lambda command, stdin_text=None: calls.append(command))
+
+ assert main_desktop_apparmor.install_profile(Path(tmp_path / "Hermes")) is False
+ assert calls == []
+ assert capsys.readouterr().out.strip() == (
+ "✗ --install-apparmor-profile is Linux only: AppArmor profiles do not exist on this OS.")
diff --git a/tests/hermes_cli/test_desktop_apparmor_sandbox.py b/tests/hermes_cli/test_desktop_apparmor_sandbox.py
index 2ca9fecbad5..9b9b54307a7 100644
--- a/tests/hermes_cli/test_desktop_apparmor_sandbox.py
+++ b/tests/hermes_cli/test_desktop_apparmor_sandbox.py
@@ -8,6 +8,7 @@ real Electron build does: exit 0 when its sandbox starts, Chromium's FATAL when
from __future__ import annotations
+import os
from pathlib import Path
import pytest
@@ -51,6 +52,21 @@ def restricted_host(tmp_path, monkeypatch):
monkeypatch.setattr(main_desktop.shutil, "which", lambda name: None)
[email protected]("restricted", [False, True])
+def test_no_sandbox_fallback_names_apparmor_only_when_apparmor_is_the_reason(
+ tmp_path, monkeypatch, capsys, restricted_host, restricted):
+ """``ELECTRON_DISABLE_SANDBOX=1`` on an unrestricted host gets no AppArmor fix-it lines."""
+ main_desktop_apparmor.RESTRICT_SYSCTL.write_text("1\n" if restricted else "0\n", encoding="utf-8")
+ monkeypatch.setenv("ELECTRON_DISABLE_SANDBOX", "1")
+ monkeypatch.setattr(main_desktop, "_desktop_linux_userns_sandbox_available", lambda: False)
+ exe = _packaged_app(tmp_path, _BLOCKED)
+
+ assert main_desktop._packaged_desktop_launch_command(exe) == [str(exe), "--no-sandbox"]
+ out = capsys.readouterr().out
+ assert ("--install-apparmor-profile" in out) is restricted
+ assert ("AppArmor" in out) is restricted
+
+
def test_generic_unshare_success_does_not_override_a_blocked_binary(tmp_path, monkeypatch, restricted_host):
"""The reporter's host: unshare passes, Chromium cannot sandbox -> never the userns path."""
exe = _packaged_app(tmp_path, _BLOCKED)
@@ -78,7 +94,7 @@ def _fake_root_tools(tmp_path, monkeypatch, *, parser_fails_on: str = ""):
f'#!/bin/sh\nfor a; do [ "$a" = "{parser_fails_on}" ] && exit 1; done\nexit 0\n', encoding="utf-8")
for tool in bin_dir.iterdir():
tool.chmod(0o755)
- monkeypatch.setenv("PATH", f"{bin_dir}:/usr/bin:/bin")
+ monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}")
profile_dir = tmp_path / "apparmor.d"
profile_dir.mkdir()
monkeypatch.setattr(main_desktop_apparmor, "_PROFILE_DIR", profile_dir)
@@ -124,3 +140,24 @@ def test_a_failed_profile_write_keeps_the_installed_profile(tmp_path, monkeypatc
assert main_desktop_apparmor.install_profile(exe) is False
assert installed.read_bytes() == before
assert list(profile_dir.iterdir()) == [installed]
+
+
+def test_install_stages_loads_then_replaces_the_old_profile(tmp_path, monkeypatch):
+ """The privileged steps run write-staged, load-staged, move-into-place; the old profile is
+ replaced, the staged file is gone, and the binary's own probe confirms the sandbox."""
+ profile_dir, sudo_log = _fake_root_tools(tmp_path, monkeypatch)
+ sysctl = tmp_path / "apparmor_restrict_unprivileged_userns"
+ sysctl.write_text("1\n", encoding="utf-8")
+ monkeypatch.setattr(main_desktop_apparmor, "RESTRICT_SYSCTL", sysctl)
+ exe = _packaged_app(tmp_path, _ALLOWED)
+ target = main_desktop_apparmor.profile_path(exe)
+ target.write_text("# an older Hermes profile\n", encoding="utf-8")
+
+ assert main_desktop_apparmor.install_profile(exe) is True
+ calls = [call.split() for call in sudo_log.read_text(encoding="utf-8").splitlines()]
+ staged = calls[0][1]
+ assert [Path(call[0]).name for call in calls] == ["tee", "apparmor_parser", "mv"]
+ assert calls[1][1:] == ["-r", staged]
+ assert calls[2][1:] == ["-f", staged, str(target)]
+ assert list(profile_dir.iterdir()) == [target]
+ assert target.read_text(encoding="utf-8") == main_desktop_apparmor.profile_text(exe)