The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): download an SSH workspace file from the SSH machine in the browser
emozilla17f9295caea9
diff --git a/apps/desktop/src/lib/browser-desktop-bridge.test.ts b/apps/desktop/src/lib/browser-desktop-bridge.test.tsindex 963985d560b..7c86a3f56e9 100644--- a/apps/desktop/src/lib/browser-desktop-bridge.test.ts+++ b/apps/desktop/src/lib/browser-desktop-bridge.test.ts@@ -607,7 +607,9 @@ describe('browser-hosted Desktop bridge', () => {await expect(pending).resolves.toMatchObject({ saved: true })const [requestUrl, init] = fetchMock.mock.calls[0] as [URL, RequestInit]- expect(requestUrl.pathname).toBe('/hermes/api/files/download')+ // The workspace download route native Desktop uses: it reads an SSH-backed profile's files on+ // that machine, like the file tree that listed them, not a same-named file on the gateway host.+ expect(requestUrl.pathname).toBe('/hermes/api/fs/download')expect(requestUrl.searchParams.get('path')).toBe('file:///srv/reports/a%20b.pdf')expect(requestUrl.searchParams.get('profile')).toBe('research')expect(requestUrl.searchParams.get('session_id')).toBe('origin-session')diff --git a/apps/desktop/src/lib/browser-files.ts b/apps/desktop/src/lib/browser-files.tsindex abc83dda61c..0bf0a44f67d 100644--- a/apps/desktop/src/lib/browser-files.ts+++ b/apps/desktop/src/lib/browser-files.ts@@ -4,7 +4,7 @@ import type { HermesApiRequest } from '@/global'import { translateNow } from '@/i18n'import { requireBrowserConnection } from '@/lib/browser-connection'import { fetchBrowserImage } from '@/lib/browser-image-download'-import { type BrowserBootstrap, fileEndpointUrl } from '@/lib/browser-transport'+import { type BrowserBootstrap, FILE_ENDPOINTS, fileEndpointUrl } from '@/lib/browser-transport'import { clickDownloadLink, downloadBlob, downloadFilename } from '@/lib/download'import { notifyError } from '@/store/notifications'@@ -92,7 +92,7 @@ export function createBrowserFilesBridge({// large downloads out of renderer memory.await api({method: 'HEAD',- path: queryPath('/api/files/download', { path: query.path, session_id: query.session_id }),+ path: queryPath(FILE_ENDPOINTS.download, { path: query.path, session_id: query.session_id }),profile: query.profile})diff --git a/apps/desktop/src/lib/browser-transport.ts b/apps/desktop/src/lib/browser-transport.tsindex 7733f221669..9aef9bf7105 100644--- a/apps/desktop/src/lib/browser-transport.ts+++ b/apps/desktop/src/lib/browser-transport.ts@@ -225,6 +225,13 @@ export interface FileEndpointQuery {session_id?: string}+/**+ * Where each ticketed file URL reads. A download uses the workspace route native Desktop downloads+ * with, which reads an SSH-backed profile's files on that machine, like the file tree that lists+ * them; media plays through the managed stream route.+ */+export const FILE_ENDPOINTS = { download: '/api/fs/download', stream: '/api/files/stream' } as const+/*** URL for a download link or media element, which cannot send the session* header. Gated hosts authenticate it with the same-origin cookie. A token@@ -238,7 +245,7 @@ export async function fileEndpointUrl(query: FileEndpointQuery): Promise<URL> {const params = Object.fromEntries(Object.entries(query).filter(([, value]) => value)) as Record<string, string>- const url = endpointUrl(`/api/files/${route}`, bootstrap.basePath)+ const url = endpointUrl(FILE_ENDPOINTS[route], bootstrap.basePath)Object.entries(params).forEach(([key, value]) => url.searchParams.set(key, value))diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.pyindex 52a6fa19130..9879c1ca94e 100644--- a/hermes_cli/web_routers/files.py+++ b/hermes_cli/web_routers/files.py@@ -19,11 +19,11 @@ import sysimport tempfileimport urllib.parsefrom datetime import datetime-from pathlib import Path+from pathlib import Path, PurePosixPathfrom typing import Any, Dict, Optionalfrom fastapi import APIRouter, File, Form, HTTPException, Request, UploadFile-from fastapi.responses import FileResponse, JSONResponse, StreamingResponse+from fastapi.responses import FileResponse, JSONResponse, Response, StreamingResponsefrom hermes_cli import web_server_file_tickets as file_ticketsfrom hermes_cli._subprocess_compat import windows_hide_flags@@ -690,22 +690,32 @@ async def _managed_file_response(def _open_ticket_file(request: Request, authority: file_tickets.FileAuthority, *, media_only: bool):"""Check the ticket's profile homes are still live and pin the file the ticket names."""- try:- authority.check_live()+ def checked():_policy, target, _display, max_bytes, _mime = _managed_readable_file(request, str(authority.target), create_root=False,)- if target != authority.target:- raise HTTPException(status_code=403, detail="File ticket target changed")if media_only and target.suffix.lower() not in _STREAMABLE_MEDIA_EXTENSIONS:raise HTTPException(status_code=415, detail="Unsupported media type")+ return target, max_bytes++ return _pin_ticket_file(authority, checked)+++def _pin_ticket_file(authority: file_tickets.FileAuthority, checked):+ """Open the host file *authority* names once its profile homes are still live and ``checked()``+ (the route's own read rules) returns that same file and its size cap (None: no cap)."""+ try:+ authority.check_live()+ target, max_bytes = checked()+ if target != authority.target:+ raise HTTPException(status_code=403, detail="File ticket target changed")_refuse_live_database(target)handle = target.open("rb")try:opened = os.fstat(handle.fileno())if not stat.S_ISREG(opened.st_mode):raise HTTPException(status_code=400, detail="Only regular files can be read")- if opened.st_size > max_bytes:+ if max_bytes is not None and opened.st_size > max_bytes:raise HTTPException(status_code=413, detail="File is too large")return handleexcept BaseException:@@ -740,10 +750,15 @@ async def issue_file_ticket(payload: FileTicketRequest, request: Request):# Capture the session/profile owner BEFORE looking up its cwd. The file# may be outside that home, but the ticket must still die with its owner.owner = await asyncio.to_thread(owner_home)- path = payload.path- if payload.session_id is not None or path.lower().startswith("file:"):- path = str(await _fs_download_path(path, payload.profile, payload.session_id))- authority = await asyncio.to_thread(capture)+ if payload.route == "download":+ authority = await _fs_download_authority(+ request, payload.path, payload.profile, payload.session_id, owner,+ )+ else:+ path = payload.path+ if payload.session_id is not None or path.lower().startswith("file:"):+ path = str(await _fs_download_path(path, payload.profile, payload.session_id))+ authority = await asyncio.to_thread(capture)except FileNotFoundError as exc:raise HTTPException(status_code=404, detail="File ticket profile is unavailable") from excquery = {"path": payload.path, "profile": payload.profile, "session_id": payload.session_id}@@ -755,26 +770,15 @@ async def issue_file_ticket(payload: FileTicketRequest, request: Request):@router.get("/api/files/download")[email protected]("/api/files/download")-async def download_managed_file(- request: Request, path: str, profile: Optional[str] = None, session_id: Optional[str] = None,-):+async def download_managed_file(request: Request, path: str):"""Stream a managed file as an attachment download.- ``auth_middleware`` also accepts a path-bound ``?ticket=`` (browsers) or the- session token as ``?token=`` (Electron's remote external link) here so a- download without the session header still authenticates.+ ``auth_middleware`` also accepts the session token as ``?token=`` here so a+ shell/browser-opened download (no session header) still authenticates.Chromium marks ``<audio>``/``<video>`` subresource requests via``Sec-Fetch-Dest``; those are served inline for Desktop builds that stilluse this route as their player source, attachment semantics otherwise."""- # Match Desktop's session/host-OS path resolution, then retain the managed- # root, sensitive-file and size checks. Unscoped managed-relative paths- # still resolve under the configured files root, not the server cwd.- if getattr(request.state, "file_ticket_authority", None) is None and (- session_id is not None or path.lower().startswith("file:")- ):- path = str(await _fs_download_path(path, profile, session_id))fetch_destination = request.headers.get("sec-fetch-dest", "").lower()is_media_subresource = fetch_destination in {"audio", "video"}return await _managed_file_response(@@ -1114,25 +1118,103 @@ async def fs_read_data_url(return {"dataUrl": f"data:{_fs_mime_type(target)};base64,{encoded}"}+def _remote_download_target(backend, path: str) -> tuple[str, int]:+ """*path* resolved on the SSH workspace to a regular file a download may read, and its size."""+ from hermes_cli.ssh_workspace_fs import SshWorkspaceFsError++ try:+ target, size = backend.inspect_file(path)+ except SshWorkspaceFsError as exc:+ _raise_fs_backend_error(exc)+ if _is_sensitive_path(Path(target)):+ raise HTTPException(status_code=403, detail="Access to sensitive files is not allowed")+ return target, size+++def _remote_download_response(request: Optional[Request], backend, target: str, size: int) -> Response:+ """*target* streamed from the SSH workspace as an attachment; a HEAD reads none of it."""+ target_path = Path(target)+ filename = urllib.parse.quote(target_path.name)+ headers = {"Content-Disposition": f"attachment; filename*=UTF-8''{filename}"}+ media_type = _fs_mime_type(target_path)+ if request is not None and request.method == "HEAD":+ return Response(media_type=media_type, headers={**headers, "Content-Length": str(size)})+ return StreamingResponse(backend.stream_file(target), media_type=media_type, headers=headers)+++async def _fs_download_authority(+ request: Request, path: str, profile: Optional[str], session_id: Optional[str], owner: Path,+) -> file_tickets.FileAuthority:+ """The file ``/api/fs/download`` serves for this query, captured for a download ticket: on the+ profile's SSH workspace when it has one, else on this host under the same read rules."""+ from hermes_constants import named_profile_home++ backend = await asyncio.to_thread(_remote_fs_backend, path, profile)+ if backend is not None:+ target, _size = await asyncio.to_thread(_remote_download_target, backend, path)+ authority = file_tickets.FileAuthority(PurePosixPath(target), (owner,), workspace=backend)+ await asyncio.to_thread(authority.check_live)+ return authority+ local = await _fs_download_path(path, profile, session_id)++ def capture():+ _hosted_fs_read_guard(local, request)+ target, _st = _fs_regular_file(local, _fs_profile_home(profile))+ homes = [owner]+ target_home = named_profile_home(target)+ if target_home is not None and target_home not in homes:+ homes.append(target_home)+ authority = file_tickets.FileAuthority(target, tuple(homes))+ authority.check_live()+ return authority++ return await asyncio.to_thread(capture)+++async def _ticketed_fs_download(+ request: Request, authority: file_tickets.FileAuthority, profile: Optional[str],+) -> Response:+ """Serve the file a download ticket captured while its profile homes are still live."""+ if authority.workspace is not None:+ try:+ await asyncio.to_thread(authority.check_live)+ except FileNotFoundError as exc:+ raise HTTPException(status_code=404, detail="File ticket profile or resource is unavailable") from exc+ target, size = await asyncio.to_thread(_remote_download_target, authority.workspace, str(authority.target))+ if target != str(authority.target):+ raise HTTPException(status_code=403, detail="File ticket target changed")+ return _remote_download_response(request, authority.workspace, target, size)++ from hermes_cli.web_server_file_response import OpenedFileResponse++ def checked():+ _hosted_fs_read_guard(authority.target, request)+ target, _st = _fs_regular_file(authority.target, _fs_profile_home(profile))+ return target, None++ return OpenedFileResponse(+ path=str(authority.target),+ opener=lambda: _pin_ticket_file(authority, checked),+ media_type=_fs_mime_type(authority.target),+ filename=authority.target.name,+ content_disposition_type="attachment",+ )++@router.get("/api/fs/download")[email protected]("/api/fs/download")async def fs_download(path: str, request: Request = None, profile: Optional[str] = None, session_id: Optional[str] = None,):+ """A workspace file as an attachment: from the profile's SSH workspace when it has one, else+ from this host. A browser download link authenticates with a path-bound ``?ticket=``."""+ authority = getattr(getattr(request, "state", None), "file_ticket_authority", None)+ if authority is not None:+ return await _ticketed_fs_download(request, authority, profile)backend = await asyncio.to_thread(_remote_fs_backend, path, profile)if backend is not None:- try:- target, _size = await asyncio.to_thread(backend.inspect_file, path)- except Exception as exc:- _raise_fs_backend_error(exc)- target_path = Path(target)- if _is_sensitive_path(target_path):- raise HTTPException(status_code=403, detail="Access to sensitive files is not allowed")- filename = urllib.parse.quote(target_path.name)- return StreamingResponse(- backend.stream_file(target),- media_type=_fs_mime_type(target_path),- headers={"Content-Disposition": f"attachment; filename*=UTF-8''{filename}"},- )+ target, size = await asyncio.to_thread(_remote_download_target, backend, path)+ return _remote_download_response(request, backend, target, size)target = await _fs_download_path(path, profile, session_id)_hosted_fs_read_guard(target, request)target, _st = _fs_regular_file(target, _fs_profile_home(profile))diff --git a/hermes_cli/web_server_file_tickets.py b/hermes_cli/web_server_file_tickets.pyindex 874ab71ccb6..ad6183f5c6b 100644--- a/hermes_cli/web_server_file_tickets.py+++ b/hermes_cli/web_server_file_tickets.py@@ -1,4 +1,4 @@-"""Path-bound tickets for browser file URLs (``/api/files/download|stream``).+"""Path-bound tickets for browser file URLs (``/api/fs/download``, ``/api/files/stream``).A download link or an ``<audio>``/``<video>`` source cannot send the sessionheader, and the session token grants host shells and file access, so it must@@ -7,7 +7,8 @@ address"). An authenticated ``POST /api/files/ticket`` mints a ticket that letsthe loopback token gate admit GET/HEAD of exactly one route + query and nothingelse. A download is one request, so its ticket is single-use; a media elementissues many Range requests for one source, so a stream ticket is reusable until-it expires. Each ticket captures the resolved file and the named profile homes it+it expires. Each ticket captures the resolved file (on the profile's SSH workspace+when it has one, as for native Desktop's download) and the named profile homes itbelongs to; the resource opener refuses it once one of those homes is missing ordeleted (``hermes_constants.assert_named_profile_home_live``). A ticket minted fora request that presented the browser session token redeems only while that token@@ -22,8 +23,8 @@ import secretsimport threadingimport timefrom dataclasses import dataclass-from pathlib import Path-from typing import TYPE_CHECKING, Iterable, Mapping+from pathlib import Path, PurePath+from typing import TYPE_CHECKING, Any, Iterable, Mappingif TYPE_CHECKING:from starlette.requests import Request@@ -34,7 +35,9 @@ DOWNLOAD_TTL_SECONDS = 60STREAM_TTL_SECONDS = 60 * 60_MAX_TICKETS = 1024-ROUTES: Mapping[str, str] = {"download": "/api/files/download", "stream": "/api/files/stream"}+#: A download reads the file where the profile's workspace is, like native Desktop's download;+#: media plays through the managed stream route.+ROUTES: Mapping[str, str] = {"download": "/api/fs/download", "stream": "/api/files/stream"}_Scope = tuple[str, tuple[tuple[str, str], ...]]@@ -43,8 +46,11 @@ _Scope = tuple[str, tuple[tuple[str, str], ...]]class FileAuthority:"""Private, resolved resource identity; none of this is supplied by a ticket holder."""- target: Path+ target: PurePathhomes: tuple[Path, ...]+ #: The SSH workspace reader the file is read through (``target`` is then a path on that+ #: machine), or None for a file on this host.+ workspace: Any = Nonedef check_live(self) -> None:"""``FileNotFoundError`` once a named profile home this file belongs to is missing or deleted."""diff --git a/tests/hermes_cli/test_web_server_file_ticket_generations.py b/tests/hermes_cli/test_web_server_file_ticket_generations.pyindex b6bdc3e808b..dce4e1bb054 100644--- a/tests/hermes_cli/test_web_server_file_ticket_generations.py+++ b/tests/hermes_cli/test_web_server_file_ticket_generations.py@@ -19,7 +19,7 @@ import pytestimport uvicornfrom starlette.responses import FileResponse-from hermes_cli import web_server+from hermes_cli import web_server, web_server_file_ticketsfrom hermes_cli.web_routers import filesfrom hermes_constants import mark_named_profile_deleted@@ -115,8 +115,11 @@ def test_http_tickets_die_with_their_profile_or_session_owner(file_server, scoperoute, query = _TICKET_SCOPES[scope](home, profile)params = _ticket(client, route, query)- unused_download = _ticket(client, "download", query)- endpoint = f"/api/files/{route}"+ # A download reads through the workspace route, which names a file by its host path, not by a+ # path under the managed files root.+ download_query = {**query, "path": str(profile / "clip.mp4")} if scope == "managed-relative" else query+ unused_download = _ticket(client, "download", download_query)+ endpoint = web_server_file_tickets.ROUTES[route]for _ in range(2 if route == "stream" else 1):response = client.get(endpoint, params=params, headers={"Range": "bytes=0-2"})assert (response.status_code, response.content) == (206, b"old")@@ -133,7 +136,8 @@ def test_http_tickets_die_with_their_profile_or_session_owner(file_server, scope_delete(profile, remove=remove)stale = client.get(endpoint, params=params, headers={"Range": "bytes=0-2"})assert stale.status_code in (401, 403, 404), stale.content- assert client.get("/api/files/download", params=unused_download).status_code in (401, 403, 404)+ assert client.get(web_server_file_tickets.ROUTES["download"], params=unused_download).status_code in (+ 401, 403, 404)refused = client.post("/api/files/ticket", json={"route": route, **query}, headers={web_server._SESSION_HEADER_NAME: web_server._SESSION_TOKEN,})diff --git a/tests/hermes_cli/test_web_server_files.py b/tests/hermes_cli/test_web_server_files.pyindex e41ead93184..6f27120ba35 100644--- a/tests/hermes_cli/test_web_server_files.py+++ b/tests/hermes_cli/test_web_server_files.py@@ -154,7 +154,7 @@ def test_download_authenticates_via_query_token(forced_files_client):).status_code == 401[email protected]("route", ["/api/fs/download", "/api/fs/read-data-url", "/api/files/download"])[email protected]("route", ["/api/fs/download", "/api/fs/read-data-url"])def test_download_resolves_paths_in_the_originating_profile_session(local_files_client, monkeypatch, route,):@@ -199,29 +199,6 @@ def test_download_resolves_paths_in_the_originating_profile_session(assert response.status_code == 404, response.text[email protected]("method", ["get", "head"])-def test_managed_download_accepts_file_uris_without_bypassing_policy(- forced_files_client, monkeypatch, method,-):- client, root = forced_files_client- artifact = _seed_file(client, root, name="out/a b.txt")- request = getattr(client, method)- response = request("/api/files/download", params={"path": artifact.as_uri()})- assert response.status_code == 200- assert response.headers["content-length"] == "5"- assert response.content == (b"hello" if method == "get" else b"")-- outside = root.parent / "outside.txt"- outside.write_text("outside")- assert request("/api/files/download", params={"path": outside.as_uri()}).status_code == 403- sensitive = root / ".env"- sensitive.write_text("fixture-only")- assert request("/api/files/download", params={"path": sensitive.as_uri()}).status_code == 403-- monkeypatch.setattr(web_server, "_MANAGED_FILE_MAX_BYTES", 1)- assert request("/api/files/download", params={"path": artifact.as_uri()}).status_code == 413--def test_stream_requires_header_auth_and_supports_ranges(forced_files_client):client, root = forced_files_clientfile_path = _seed_file(client, root, name="out/demo.mp4")@@ -299,13 +276,14 @@ def test_browser_file_urls_authenticate_only_with_route_and_path_bound_tickets({**params, "path": [str(video), str(other)]},):assert client.get("/api/files/stream", params=foreign).status_code == 401, foreign- assert client.get("/api/files/download", params=params).status_code == 401+ download_route = web_server_file_tickets.ROUTES["download"]+ assert client.get(download_route, params=params).status_code == 401assert client.get("/api/files/stream", params={"path": str(video), "ticket": expired}).status_code == 401once = {"path": str(video), "ticket": download}assert client.get("/api/files/stream", params=once).status_code == 401- assert client.get("/api/files/download", params=once).content == b"hello"- assert client.get("/api/files/download", params=once).status_code == 401+ assert client.get(download_route, params=once).content == b"hello"+ assert client.get(download_route, params=once).status_code == 401def test_stream_rejects_non_media_active_content(forced_files_client):diff --git a/tests/hermes_cli/test_web_server_fs.py b/tests/hermes_cli/test_web_server_fs.pyindex 3328b72cb4b..cf139be2204 100644--- a/tests/hermes_cli/test_web_server_fs.py+++ b/tests/hermes_cli/test_web_server_fs.py@@ -254,6 +254,65 @@ def test_fs_download_streams_ssh_files_without_preview_cap(client, monkeypatch):assert response.content == b"123456"+def test_a_browser_download_reads_the_ssh_workspace_file_it_listed(client, monkeypatch, tmp_path):+ """The browser lists an SSH workspace through ``/api/fs/list``. Its Download checks the file+ with a HEAD, then hands the browser the URL a download ticket authorizes (an anchor cannot send+ the session header). Both read the file on the SSH machine, as native Desktop's download does,+ never a file at the same path on the gateway's own disk. The ticket is single-use and dies+ with its profile."""+ from hermes_cli import web_server_file_tickets as file_tickets+ from hermes_constants import mark_named_profile_deleted++ home = tmp_path / ".hermes"+ profile = home / "profiles" / "remote-dev"+ profile.mkdir(parents=True)+ (profile / "config.yaml").write_text("{}", encoding="utf-8")+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.delenv("HERMES_DASHBOARD_FILES_ROOT", raising=False)+ same_path = tmp_path / "srv" / "report.txt"+ same_path.parent.mkdir()+ same_path.write_bytes(b"gateway copy")+ remote_only = str(tmp_path / "srv" / "only-remote.txt")+ streamed = []++ class FakeSshFs:+ def inspect_file(self, path):+ return path, len(b"remote copy")++ def stream_file(self, path):+ streamed.append(path)+ yield b"remote copy"++ monkeypatch.setattr(file_routes, "_fs_backend", lambda profile=None: FakeSshFs())+ route = file_tickets.ROUTES["download"]+ anchor = TestClient(web_server.app) # a download link sends no session header++ def ticketed(query):+ minted = client.post("/api/files/ticket", json={"route": "download", **query})+ assert minted.status_code == 200, (query, minted.text)+ return {**query, "ticket": minted.json()["ticket"]}++ try:+ for path in (str(same_path), remote_only):+ query = {"path": path, "profile": "remote-dev"}+ checked = client.head(route, params=query)+ assert (checked.status_code, checked.content) == (200, b""), (path, checked.text)+ params = ticketed(query)+ saved = anchor.get(route, params=params)+ assert (saved.status_code, saved.content) == (200, b"remote copy"), path+ assert saved.headers["content-disposition"].startswith("attachment;")+ assert anchor.get(route, params=params).status_code == 401+ assert streamed == [str(same_path), remote_only] # the HEAD checks read nothing++ params = ticketed({"path": remote_only, "profile": "remote-dev"})+ mark_named_profile_deleted(profile)+ assert anchor.get(route, params=params).status_code in (401, 403, 404)+ assert streamed == [str(same_path), remote_only]+ finally:+ anchor.close()++def test_fs_read_data_url_rejects_over_cap(client, tmp_path, monkeypatch):monkeypatch.setattr(web_server, "_FS_DATA_URL_MAX_BYTES", 3)target = tmp_path / "image.png"