The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): refuse a session Save whose file is only in a shared sandbox
unsupportedpastels391d6f4fdc02
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.pyindex 8feda942312..b20ed620230 100644--- a/hermes_cli/web_routers/files.py+++ b/hermes_cli/web_routers/files.py@@ -1087,7 +1087,9 @@ async def fs_write_text(payload: FsWriteText, profile: Optional[str] = None):_raise_fs_backend_error(exc)return {"ok": True, "path": target, "byteSize": byte_size}- target = await _fs_download_path(payload.path, profile, payload.session_id, decode_fallback=False)+ target = await _fs_download_path(+ payload.path, profile, payload.session_id, decode_fallback=False, own_sandbox_only=True,+ )if len(text.encode("utf-8")) > _FS_TEXT_WRITE_MAX_BYTES:raise HTTPException(status_code=413, detail="Content too large")@@ -1122,9 +1124,12 @@ async def fs_write_text(payload: FsWriteText, profile: Optional[str] = None):async def _fs_download_path(path: str, profile: Optional[str], session_id: Optional[str], *, decode_fallback: bool = True,+ own_sandbox_only: bool = False,) -> Path:# Resolve container paths in the requested profile's sandbox, not the active one's.# A write passes decode_fallback=False: it must never land on a decoded twin of its name.+ # A session's write also passes own_sandbox_only: reads fall back to the shared sandbox+ # when the session's own copy is gone, a Save must not (409 instead).profile_home = _fs_profile_home(profile)if session_id is not None:session = await _fs_owned_session(session_id, profile) or {}@@ -1133,6 +1138,7 @@ async def _fs_download_path(return _agent_fs_path(path, cwd=cwd, profile_home=profile_home,workspace_key=_session_workspace_key(session_id, cwd), decode_fallback=decode_fallback,+ own_sandbox_only=own_sandbox_only,)return _agent_fs_path(path, profile_home=profile_home, decode_fallback=decode_fallback)diff --git a/hermes_cli/web_server_files.py b/hermes_cli/web_server_files.pyindex 9c26ba83303..68d5959091d 100644--- a/hermes_cli/web_server_files.py+++ b/hermes_cli/web_server_files.py@@ -133,19 +133,41 @@ def _is_container_cwd(host: str) -> bool:return any(host == root or host.startswith(root + "/") for root in ("/workspace", "/root"))-def _translate_agent_path(candidate: Path, workspace_key: str = "") -> Path | None:+def _translate_agent_path(+ candidate: Path, workspace_key: str = "", own_sandbox_only: bool = False,+) -> Path | None:"""One translation attempt under whatever scope is bound; None off the docker backend.The gate keeps a local-backend gateway with leftover ``docker_volumes`` from servingmount sources in place of real host files. Quiet: fs callers also see ordinary host- paths, which are not MEDIA misses worth a warning."""+ paths, which are not MEDIA misses worth a warning. ``own_sandbox_only`` (a session's+ Save) refuses with 409 a file the translator found only in a fallback sandbox."""from gateway.platforms.base import _docker_env_active, _translate_docker_container_media_pathfrom tools.terminal_tool import _ensure_terminal_env_bridged_ensure_terminal_env_bridged()if not _docker_env_active():return None- return _translate_docker_container_media_path(candidate, quiet=True, workspace_key=workspace_key)+ translated = _translate_docker_container_media_path(candidate, quiet=True, workspace_key=workspace_key)+ if own_sandbox_only and translated is not None and _in_fallback_sandbox(translated, workspace_key):+ raise HTTPException(status_code=409, detail=SANDBOX_COPY_MISSING)+ return translated+++SANDBOX_COPY_MISSING = "File is no longer in this session's workspace"+++def _in_fallback_sandbox(target: Path, workspace_key: str) -> bool:+ """Whether *target* is in a Docker sandbox after the first existing one in the translator's+ order (the session's own, then the profile's shared ones). Reads fall through to those copies+ when the session's own is missing; a Save that did would change another sandbox's file."""+ from gateway.platforms.docker_media_paths import _docker_sandbox_dir_candidates+ from tools.environments.base import get_sandbox_dir++ base = get_sandbox_dir() / "docker"+ sandboxes = [d for d in ((base / name).resolve(strict=False)+ for name in _docker_sandbox_dir_candidates("", workspace_key)) if d.is_dir()]+ return any(_path_is_under(sandbox, target) for sandbox in sandboxes[1:])@contextmanager@@ -184,7 +206,7 @@ def _agent_path_scope(profile_home: Path | None = None) -> Iterator[None]:def _agent_container_path(- raw: str, *, profile_home: Path | None = None, workspace_key: str = "",+ raw: str, *, profile_home: Path | None = None, workspace_key: str = "", own_sandbox_only: bool = False,) -> Path | None:"""Map an agent-visible container path to its host bind mount, or None.@@ -211,14 +233,16 @@ def _agent_container_path(candidate = Path(raw)with _agent_path_scope(profile_home):- translated = _translate_agent_path(candidate, workspace_key)+ translated = _translate_agent_path(candidate, workspace_key, own_sandbox_only)if translated is not None or get_terminal_scope() is not None:return translatedtoken = set_terminal_scope(build_profile_terminal_scope(get_hermes_home()))try:- return _translate_agent_path(candidate, workspace_key)+ return _translate_agent_path(candidate, workspace_key, own_sandbox_only)finally:reset_terminal_scope(token)+ except HTTPException:+ raise # a Save refused in a fallback sandboxexcept Exception: # fail open: translation is additive, the host path still serveslogger.debug("agent container path translation failed for %s", raw, exc_info=True)return None@@ -229,10 +253,11 @@ SANDBOX_NOT_FOUND = "File not found on the gateway host: the path may live insiddef _agent_fs_path(raw_path: str, *, cwd: str | None = None, profile_home: Path | None = None,- workspace_key: str = "", decode_fallback: bool = True,+ workspace_key: str = "", decode_fallback: bool = True, own_sandbox_only: bool = False,) -> Path:"""``_fs_path`` for agent-facing reads; container paths resolve to their host- mount, so callers' boundary guards see the real host target.+ mount, so callers' boundary guards see the real host target. A session's Save passes+ ``own_sandbox_only``: a container path stays in the session's own sandbox (else 409).POSIX: a readable host path wins, and translation only runs when the hostlookup misses, so a root-run gateway can still browse its own ``/root``.@@ -246,9 +271,12 @@ def _agent_fs_path(return hosttranslated = _agent_container_path(_file_url_container_text(raw), profile_home=profile_home, workspace_key=workspace_key,+ own_sandbox_only=own_sandbox_only,)return translated if translated is not None else host- translated = _agent_container_path(raw, profile_home=profile_home, workspace_key=workspace_key)+ translated = _agent_container_path(+ raw, profile_home=profile_home, workspace_key=workspace_key, own_sandbox_only=own_sandbox_only,+ )if translated is not None:return translatedif _is_unmapped_container_path(raw, profile_home=profile_home):diff --git a/tests/hermes_cli/test_web_server_fs_session_workspace.py b/tests/hermes_cli/test_web_server_fs_session_workspace.pyindex 2f2661b1266..8ab0141965d 100644--- a/tests/hermes_cli/test_web_server_fs_session_workspace.py+++ b/tests/hermes_cli/test_web_server_fs_session_workspace.py@@ -200,6 +200,21 @@ def test_save_writes_the_file_the_preview_read(client, sandboxes, path):assert _notes(sandboxes) == {"own": "edited", "foreign": "foreign note", "shared": "shared note"}[email protected]("path", ["/workspace/note.txt", "file:///workspace/note.txt"])+def test_save_never_falls_back_to_the_shared_copy_once_the_sessions_copy_is_gone(client, sandboxes, path):+ (sandboxes["shared"] / "note.txt").write_text("session note", encoding="utf-8") # the same text+ (sandboxes["own"] / "note.txt").unlink()++ # A read still falls back, so the editor's stale-file check sees the text it opened.+ read = client.get("/api/fs/read-text", params={"path": path, "session_id": "sess-ws"})+ saved = client.post("/api/fs/write-text", json={"path": path, "content": "edited", "session_id": "sess-ws"})++ assert (read.status_code, read.json()["text"]) == (200, "session note")+ assert (saved.status_code, saved.json()) == (409, {"detail": "File is no longer in this session's workspace"})+ assert (sandboxes["shared"] / "note.txt").read_text(encoding="utf-8") == "session note"+ assert not (sandboxes["own"] / "note.txt").exists()++def test_save_without_a_session_writes_where_a_bare_read_reads(client, sandboxes):read = client.get("/api/fs/read-text", params={"path": "/workspace/note.txt"})saved = client.post("/api/fs/write-text", json={"path": "/workspace/note.txt", "content": "edited"})