Skip to content

The agent that grows with you

fix(desktop): reject SSH backends with replaced runtimes

toprakeker8624c1e8f7e3
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 87bc3930de9..a79ab38f62f 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -9529,9 +9529,7 @@ async function sshProbeReuseProof(baseUrl, token, spawnNonce) {
try {
const proof: any = await fetchJson(`${baseUrl}/api/ssh/ownership`, token)
- return proof?.ok === true && proof.sshOwnerNonce === spawnNonce && proof.protocolVersion === 1
- ? 'authenticated-ok'
- : 'authenticated-stale'
+ return remoteLifecycle.classifySshReuseProof(proof, spawnNonce)
} catch (error: any) {
if (/^(401|403|404):/.test(String(error?.message || ''))) {
return 'authenticated-stale'
diff --git a/apps/desktop/electron/remote-lifecycle.test.ts b/apps/desktop/electron/remote-lifecycle.test.ts
index e2b04567704..bfe7d898913 100644
--- a/apps/desktop/electron/remote-lifecycle.test.ts
+++ b/apps/desktop/electron/remote-lifecycle.test.ts
@@ -10,6 +10,7 @@ import { test } from 'vitest'
import { profileSshOverride } from './connection-config'
import {
buildSpawnCommand,
+ classifySshReuseProof,
cleanupStale,
connect,
disconnect,
@@ -41,6 +42,23 @@ const OWNERSHIP_ID = '0123456789abcdef0123456789abcdef'
const SPAWN_NONCE = '0123456789abcdef'
const exec = promisify(execCallback)
+test('SSH reuse proof rejects a backend whose runtime was replaced', () => {
+ assert.equal(
+ classifySshReuseProof(
+ { ok: true, sshOwnerNonce: SPAWN_NONCE, protocolVersion: 1, runtimeIntact: false },
+ SPAWN_NONCE
+ ),
+ 'authenticated-stale'
+ )
+})
+
+test('SSH reuse proof remains compatible when runtime state is absent', () => {
+ assert.equal(
+ classifySshReuseProof({ ok: true, sshOwnerNonce: SPAWN_NONCE, protocolVersion: 1 }, SPAWN_NONCE),
+ 'authenticated-ok'
+ )
+})
+
function ownedLock(over: any = {}) {
return {
schemaVersion: LOCKFILE_SCHEMA_VERSION,
diff --git a/apps/desktop/electron/remote-lifecycle.ts b/apps/desktop/electron/remote-lifecycle.ts
index 06fbe4711e1..fbc4b5cf25a 100644
--- a/apps/desktop/electron/remote-lifecycle.ts
+++ b/apps/desktop/electron/remote-lifecycle.ts
@@ -46,6 +46,15 @@ const READY_POLL_INTERVAL_MS = 750
// Keep startup portable: restricted hosts retain their existing limit.
const REMOTE_NOFILE_SOFT_LIMIT = 65_536
+function classifySshReuseProof(proof, spawnNonce) {
+ return proof?.ok === true &&
+ proof.sshOwnerNonce === spawnNonce &&
+ proof.protocolVersion === PROTOCOL_VERSION &&
+ proof.runtimeIntact !== false
+ ? 'authenticated-ok'
+ : 'authenticated-stale'
+}
+
function mintToken() {
return crypto.randomBytes(32).toString('hex')
}
@@ -977,6 +986,7 @@ async function connect(deps) {
export {
adoptOwnedServedToken,
buildSpawnCommand,
+ classifySshReuseProof,
cleanupStale,
connect,
DEFAULT_READY_TIMEOUT_MS,
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index a6ddf3306ed..7c3fc553c5f 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -38,6 +38,7 @@ import shutil
import stat
import subprocess
import sys
+import sysconfig
import tempfile
import threading
import time
@@ -543,6 +544,7 @@ def _resolve_session_token() -> str:
_SESSION_TOKEN = _resolve_session_token()
_SESSION_HEADER_NAME = "X-Hermes-Session-Token"
_SSH_OWNER_NONCE: Optional[str] = None
+_SSH_RUNTIME_PURELIB: Optional[Tuple[str, int, int]] = None
def _apply_ssh_session_token(token: str) -> None:
@@ -552,8 +554,27 @@ def _apply_ssh_session_token(token: str) -> None:
def _apply_ssh_owner_nonce(nonce: Optional[str]) -> None:
- global _SSH_OWNER_NONCE
+ global _SSH_OWNER_NONCE, _SSH_RUNTIME_PURELIB
_SSH_OWNER_NONCE = nonce
+ _SSH_RUNTIME_PURELIB = None
+ if nonce:
+ try:
+ purelib = sysconfig.get_paths()["purelib"]
+ stat = os.stat(purelib)
+ _SSH_RUNTIME_PURELIB = (purelib, stat.st_dev, stat.st_ino)
+ except (KeyError, OSError):
+ pass
+
+
+def _ssh_runtime_intact() -> bool:
+ if _SSH_RUNTIME_PURELIB is None:
+ return True
+ purelib, device, inode = _SSH_RUNTIME_PURELIB
+ try:
+ stat = os.stat(purelib)
+ except OSError:
+ return False
+ return (stat.st_dev, stat.st_ino) == (device, inode)
# In-browser Chat tab (/chat, /api/pty, /api/ws, …). Always enabled: the
# desktop app and the dashboard's own Chat tab both drive the agent over the
@@ -3578,7 +3599,12 @@ async def get_ssh_ownership(request: Request):
_require_token(request)
if not _SSH_OWNER_NONCE:
raise HTTPException(status_code=404, detail="SSH ownership is not active")
- return {"ok": True, "sshOwnerNonce": _SSH_OWNER_NONCE, "protocolVersion": 1}
+ return {
+ "ok": True,
+ "sshOwnerNonce": _SSH_OWNER_NONCE,
+ "protocolVersion": 1,
+ "runtimeIntact": _ssh_runtime_intact(),
+ }
@app.get("/api/health")
diff --git a/tests/hermes_cli/test_ssh_ownership_endpoint.py b/tests/hermes_cli/test_ssh_ownership_endpoint.py
index 4d6a460dfb2..a8bb58d5e5e 100644
--- a/tests/hermes_cli/test_ssh_ownership_endpoint.py
+++ b/tests/hermes_cli/test_ssh_ownership_endpoint.py
@@ -21,9 +21,24 @@ def test_ssh_ownership_endpoint_requires_token_and_returns_exact_nonce(monkeypat
"ok": True,
"sshOwnerNonce": nonce,
"protocolVersion": 1,
+ "runtimeIntact": True,
}
+def test_ssh_ownership_reports_replaced_runtime(monkeypatch):
+ token = "t" * 64
+ monkeypatch.setattr(web_server, "_SESSION_TOKEN", token)
+ monkeypatch.setattr(web_server, "_SSH_OWNER_NONCE", "0123456789abcdef")
+ monkeypatch.setattr(web_server, "_SSH_RUNTIME_PURELIB", ("/venv/site-packages", 10, 20))
+ monkeypatch.setattr(web_server.os, "stat", lambda _path: type("Stat", (), {"st_dev": 10, "st_ino": 21})())
+ client = TestClient(web_server.app)
+
+ response = client.get("/api/ssh/ownership", headers={"X-Hermes-Session-Token": token})
+
+ assert response.status_code == 200
+ assert response.json()["runtimeIntact"] is False
+
+
def test_ssh_ownership_endpoint_is_absent_without_owner_nonce(monkeypatch):
token = "t" * 64
monkeypatch.setattr(web_server, "_SESSION_TOKEN", token)