Skip to content

The agent that grows with you

fix(webapp): a browser credential file other users could read is replaced, not reused

emozillaed921174d9a5
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.py
index db7c5096165..e817d9ed5ec 100644
--- a/gateway/webapp_listener.py
+++ b/gateway/webapp_listener.py
@@ -16,6 +16,7 @@ from __future__ import annotations
import ipaddress
import logging
+import os
import re
import secrets
import sys
@@ -175,16 +176,18 @@ def listener_plan(home) -> tuple[str, int, WebappMount | None, str | None]:
def browser_session_token(home, *, rotate: bool = False) -> str:
"""The dashboard session token a Webapp browser holds, kept user-only in *home*.
- Created on first use; *rotate* replaces it, which signs every open tab out."""
+ Created on first use; *rotate* replaces it, which signs every open tab out. A stored token other
+ users could read is replaced the same way, and its folder is closed to them again (POSIX)."""
from utils import atomic_write_text
path = Path(home) / TOKEN_PATH
+ _close_to_other_users(path.parent)
if not rotate:
try:
token = path.read_text(encoding="utf-8-sig").strip()
except OSError:
token = ""
- if _TOKEN_FORMAT.fullmatch(token):
+ if _TOKEN_FORMAT.fullmatch(token) and not _readable_by_other_users(path):
return token
token = secrets.token_urlsafe(32)
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
@@ -192,6 +195,31 @@ def browser_session_token(home, *, rotate: bool = False) -> str:
return token
+def _readable_by_other_users(path: Path) -> bool:
+ """POSIX: whether *path* grants group or other access, or belongs to another user. Windows homes
+ keep their own ACL."""
+ if os.name == "nt":
+ return False
+ try:
+ info = path.stat()
+ except OSError:
+ return False
+ return bool(info.st_mode & 0o077) or info.st_uid != os.geteuid() # windows-footgun: ok — POSIX-only branch
+
+
+def _close_to_other_users(folder: Path) -> None:
+ """POSIX: make an existing credential folder user-only (0700) again."""
+ if os.name == "nt":
+ return
+ try:
+ if folder.stat().st_mode & 0o077:
+ folder.chmod(0o700)
+ except FileNotFoundError:
+ return
+ except OSError as exc: # the token file itself stays 0600
+ logger.warning("Could not make %s user-only: %s", folder, exc)
+
+
def _one_use_links_work() -> bool:
"""Whether a browser can redeem a one-use launch link here. Redemption needs proof that the
browser's connection belongs to the gateway's OS user (``web_routers.webapp._peer_is_server_user``),
diff --git a/tests/gateway/fixtures/webapp_listener_peer.py b/tests/gateway/fixtures/webapp_listener_peer.py
index ab08ecc2d80..f21800b325f 100644
--- a/tests/gateway/fixtures/webapp_listener_peer.py
+++ b/tests/gateway/fixtures/webapp_listener_peer.py
@@ -105,8 +105,10 @@ async def probe(mode, port, dist):
"gateway:\n multiplex_profiles: false\nmodel:\n provider: custom\n default: no-inference\n"
" base_url: http://127.0.0.1:1/v1\nauxiliary:\n title_generation:\n enabled: false\n" + webapp_block)
if mode == "on":
- (home / TOKEN_PATH).parent.mkdir(parents=True, exist_ok=True)
+ # Seeded user-only, as the gateway writes it: a stored token other users can read is replaced.
+ (home / TOKEN_PATH).parent.mkdir(mode=0o700, parents=True, exist_ok=True)
(home / TOKEN_PATH).write_text(SEEDED_TOKEN + "\n", encoding="utf-8")
+ (home / TOKEN_PATH).chmod(0o600)
hermes_cli.webapp.webapp_dist_dir = lambda _root: Path(dist) # the built bundle, without a build
runner = GatewayRunner()
runner.session_control_server = GatewayControlServer(home) # verbs only; not listening
diff --git a/tests/gateway/test_webapp_browser_credential.py b/tests/gateway/test_webapp_browser_credential.py
new file mode 100644
index 00000000000..e652a40e7a3
--- /dev/null
+++ b/tests/gateway/test_webapp_browser_credential.py
@@ -0,0 +1,60 @@
+"""The Webapp's browser credential file stays private to the gateway's OS user.
+
+The gateway keeps the browser credential in ``webapp/session-token`` inside its home so open tabs
+survive a restart. On POSIX the folder is user-only (0700) and the file owner-read/write (0600). A
+folder found open to other users is closed again before the credential is used, and a credential
+file other users could read is treated as exposed: it is replaced, which signs the open tabs out,
+rather than reused. Windows homes keep their own ACL, so this applies to POSIX only.
+"""
+import os
+import stat
+
+import pytest
+
+from gateway import webapp_listener as webapp
+
+posix_only = pytest.mark.platforms("posix")
+STORED = "s" * 43
+
+
+def _store(home, *, folder_mode, file_mode):
+ path = home / webapp.TOKEN_PATH
+ path.parent.mkdir(parents=True)
+ path.write_text(STORED + "\n", encoding="utf-8")
+ os.chmod(path, file_mode)
+ os.chmod(path.parent, folder_mode)
+ return path
+
+
+def _mode(path):
+ return stat.S_IMODE(path.stat().st_mode)
+
+
+@posix_only
[email protected]("file_mode", [0o644, 0o640, 0o604])
+def test_a_credential_other_users_could_read_is_replaced(tmp_path, file_mode):
+ path = _store(tmp_path, folder_mode=0o700, file_mode=file_mode)
+ token = webapp.browser_session_token(tmp_path)
+ assert token != STORED
+ assert path.read_text(encoding="utf-8").strip() == token
+ assert _mode(path) == 0o600
+
+
+@posix_only
+def test_a_private_credential_in_an_open_folder_is_kept_and_the_folder_closed(tmp_path):
+ path = _store(tmp_path, folder_mode=0o755, file_mode=0o600)
+ assert webapp.browser_session_token(tmp_path) == STORED
+ assert _mode(path.parent) == 0o700
+ assert _mode(path) == 0o600
+
+
+@posix_only
+def test_signing_out_closes_an_open_credential_folder_too(tmp_path):
+ path = _store(tmp_path, folder_mode=0o755, file_mode=0o600)
+ assert webapp.browser_session_token(tmp_path, rotate=True) != STORED
+ assert (_mode(path.parent), _mode(path)) == (0o700, 0o600)
+
+
+def test_a_private_credential_is_reused(tmp_path):
+ first = webapp.browser_session_token(tmp_path)
+ assert webapp.browser_session_token(tmp_path) == first