The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(backend): a file: prefix instead of a second token variable
ethernet20f265442957
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex 0afabccda22..55965a3ff58 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -222,26 +222,28 @@ app.include_router(_memory_oauth_router)# the process and is injected into the SPA HTML so only the web UI can use it.def _resolve_session_token() -> str:env_token = os.environ.get("HERMES_DASHBOARD_SESSION_TOKEN")- if env_token:- return env_token- # Deployment managers hand the token over as a file path (agenix,- # sops-nix, systemd credentials) so the secret never enters a store- # path or a process listing. The Nix modules set this from- # backend.sessionTokenFile instead of exporting the token itself.- token_file = os.environ.get("HERMES_DASHBOARD_SESSION_TOKEN_FILE")- if token_file:+ if not env_token:+ return secrets.token_urlsafe(32)+ # A "file:" prefix means the value is a token-file path, not a token:+ # deployment managers (agenix, sops-nix, systemd credentials) hand a+ # secret over as a file, and a path -- unlike the token -- can sit in a+ # systemd Environment= or a launchd plist without putting anything+ # readable into a store path. The Nix modules set this form from+ # backend.sessionTokenFile.+ if env_token.startswith("file:"):+ token_file = env_token[len("file:") :]try:token = Path(token_file).read_text(encoding="utf-8-sig").strip()except (OSError, UnicodeError) as exc:raise SystemExit(- f"HERMES_DASHBOARD_SESSION_TOKEN_FILE names an unreadable file ({token_file}): {exc}"+ f"HERMES_DASHBOARD_SESSION_TOKEN names an unreadable token file ({token_file}): {exc}") from excif not token:raise SystemExit(- f"HERMES_DASHBOARD_SESSION_TOKEN_FILE names an empty file ({token_file})"+ f"HERMES_DASHBOARD_SESSION_TOKEN names an empty token file ({token_file})")return token- return secrets.token_urlsafe(32)+ return env_token_SESSION_TOKEN = _resolve_session_token()diff --git a/tests/hermes_cli/test_dashboard_session_token_file.py b/tests/hermes_cli/test_dashboard_session_token_file.pyindex 7450ccd35c7..1c5d0ff390e 100644--- a/tests/hermes_cli/test_dashboard_session_token_file.py+++ b/tests/hermes_cli/test_dashboard_session_token_file.py@@ -1,35 +1,32 @@-"""The dashboard session token resolves from the environment or a secret file."""+"""The dashboard session token resolves from the environment, or a file it names."""import pytest-def test_env_token_wins_over_file(monkeypatch, tmp_path):+def test_plain_env_token_is_used_verbatim(monkeypatch):from hermes_cli import web_server- token_file = tmp_path / "token"- token_file.write_text("file-token\n", encoding="utf-8")- monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", "env-token")- monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN_FILE", str(token_file))+ monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", "raw-token")- assert web_server._resolve_session_token() == "env-token"+ assert web_server._resolve_session_token() == "raw-token"-def test_file_token_is_read_and_stripped(monkeypatch, tmp_path):+def test_file_prefix_reads_and_strips(monkeypatch, tmp_path):from hermes_cli import web_servertoken_file = tmp_path / "token"- token_file.write_text(" file-token \r\n", encoding="utf-8")- monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)- monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN_FILE", str(token_file))+ token_file.write_text(" file-token " + chr(13) + chr(10), encoding="utf-8")+ monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", f"file:{token_file}")assert web_server._resolve_session_token() == "file-token"-def test_missing_token_file_fails_loudly(monkeypatch, tmp_path):+def test_unreadable_token_file_fails_loudly(monkeypatch, tmp_path):from hermes_cli import web_server- monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)- monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN_FILE", str(tmp_path / "absent"))+ monkeypatch.setenv(+ "HERMES_DASHBOARD_SESSION_TOKEN", f"file:{tmp_path / 'absent'}"+ )with pytest.raises(SystemExit, match="unreadable"):web_server._resolve_session_token()@@ -39,9 +36,8 @@ def test_empty_token_file_fails_loudly(monkeypatch, tmp_path):from hermes_cli import web_servertoken_file = tmp_path / "token"- token_file.write_text("\n", encoding="utf-8")- monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)- monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN_FILE", str(token_file))+ token_file.write_text(chr(10), encoding="utf-8")+ monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", f"file:{token_file}")with pytest.raises(SystemExit, match="empty"):web_server._resolve_session_token()@@ -52,8 +48,7 @@ def test_malformed_token_file_fails_loudly(monkeypatch, tmp_path):token_file = tmp_path / "token"token_file.write_bytes(b"\xff\xfe\x00not-utf8")- monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)- monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN_FILE", str(token_file))+ monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", f"file:{token_file}")with pytest.raises(SystemExit, match="unreadable"):web_server._resolve_session_token()@@ -63,6 +58,5 @@ def test_no_source_mints_a_token(monkeypatch):from hermes_cli import web_servermonkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)- monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN_FILE", raising=False)assert len(web_server._resolve_session_token()) > 0