With terminal.docker_shared_container_key set, every session reuses one
shared:<key> container, which mounts the cwd of the session that created
it. The previous commit took a cwd-mounted session's /workspace from that
session's own cwd, so a second session at another folder read that folder,
and Save (which own_sandbox_only allowed) overwrote it while the agent's
file in the container's real mount stayed unchanged.
Nothing records which cwd a shared container mounted: shared containers
carry no mount fingerprint label, and a cross-process attach never inspects
the container's mounts. So a session on a shared container reads from the
profile's cwd again, as before the previous commit, and a session Save that
resolves there answers 409 "File is no longer in this session's workspace".
Per-workspace containers still mount the session's own cwd.
With terminal.docker_mount_cwd_to_workspace on, a session bound to its own
workspace runs in a container that mounts that workspace at /workspace: the
gateway turn's TERMINAL_CWD is the session's cwd, which bind_turn_workspace
also registers and hashes into the :ws: key. The file routes still took the
mount root from the profile's terminal.cwd (or the process cwd), so a preview
showed that folder's copy and Save wrote it while the agent's file stayed
unchanged.
A session with a workspace key now mounts the cwd that key hashes (the live
binding's cwd, else the session row's), looked up in the session's profile
scope; a cwd whose digest does not match gives no mount root. Its sandbox
is still tried after the mount. MEDIA delivery and requests without a
session binding keep the profile's cwd.
The Save guard took the session's own sandbox to be the first one whose
directory still existed. When the session's whole sandbox directory was
deleted, the shared profile sandbox became that first one, so a Save the
read had resolved into the shared copy overwrote it. The session's own
sandbox is now the first name in the translator's order whether or not its
directory exists; a Save that resolves into any later sandbox answers the
same 409 as a missing file ("File is no longer in this session's
workspace"). Reads keep the fallback.
Reads translate /workspace through the session's own sandbox and then the
profile's shared ones. A Save took the same route, so when the session's copy
was gone it overwrote the shared copy, and the editor's stale-file check (a
re-read that also falls back) could not notice. A session's write now stays in
the first existing sandbox; a file found only further down answers 409 "File is
no longer in this session's workspace", which the editor shows as a failed
Save. Reads keep the fallback.
/api/fs/write-text resolved its path with _fs_path alone: no session and no
container mapping. A preview of /workspace/note.txt read the session's
sandbox copy, but Save wrote the gateway host's /workspace/note.txt, which
is normally missing (400) and is never the file that was read.
write-text takes an optional session_id and resolves through the same
_fs_download_path read-text uses (#143655), with decode_fallback off so a
write never lands on a decoded twin of its name. writeDesktopFileText takes
the same optional session scope, and the preview Save passes the tab's
session. A session another profile owns is a 404 and nothing is written; a
symlink out of the sandbox is not followed. Native local saves are
unchanged.
Audit: write-text is the only renderer path that saves a file it read.
mkdir, the managed uploads and the image paste write into the managed root
or the upload staging folders, never a session's /workspace.
/api/fs/read-text took no session, so a Docker session's text preview read
the profile's shared /workspace copy. A file: URL naming a container path
(file:///workspace/x) was never translated at all: read-text answered 404
and a file: clip streamed the shared copy even with a session.
read-text takes an optional session_id and resolves through
_fs_download_path (#143655), as download does. _agent_fs_path translates the
path a POSIX file: URL names, so the URL spelling maps like the bare path;
the .. and symlink refusals still apply. readDesktopFileText and
readDesktopFileDataUrl take an optional session scope that a gateway read
sends and a local (native) read ignores; a preview tab passes the session
that owns it, and a chat link passes its transcript's session.
Chat and artifact downloads name a session whose container is default:ws:<digest>, so the host copy is that sandbox rather than the shared default directory.
The gateway reads file: URLs for /api/fs/read-text, read-data-url, download, /api/files/stream and
stream tickets by decoding the URL's path once. A %2f in the URL became a real separator after that
decode, so file:///.../docs/..%2fsecret.md read .../secret.md, a file outside the folder the URL
names; on a Windows gateway %5c did the same. The renderer and Electron already refuse such URLs
through the shared parser in apps/shared/src/file-url.ts, so only their pre-check stood in front of
the gateway. Main reads these URLs the same way.
The gateway's reader now refuses a file: URL whose path encodes a separator: %2f always, and %5c on
a Windows gateway or in a Windows URL (a UNC host or a drive letter), as the shared parser does.
The read answers 400. A %5c in a POSIX URL read on a POSIX gateway stays a backslash in the name.
A `file:` URL holding `..%252f` passed the renderer's encoded-separator check, because only after
one decode does it read `..%2f`. The gateway then decoded the path a second time when the first
reading did not exist, turned `..%2f` into `../`, and served a file from outside the folder the
URL names: the preview or clip on screen was not the file its label showed.
The renderer now refuses a URL whose decoded path still encodes a separator (`%2f`, and `%5c` in a
Windows path) or a dot-segment, as it already refused an encoded separator. The gateway never
decodes a `file:` URL's path twice, and its one-decode rescue for a doubly encoded plain path
(#103425) no longer applies when decoding would add a `.` or `..` segment.
Carried from BearHuddleston's #93508 onto main. The Webapp relay and the
profile-incarnation fence stay out: carried code follows main's
named-profile rule and refuses a missing or deleted profile home.
- Webapp routes: one-use launch tickets, window handoff, and gateway
WebSocket tickets minted for the signed-in browser
- Browser surface policy and the SPA document policy
- A host terminal for the browser that survives reconnects
- Staged browser uploads and file tickets; file.attach takes a staged upload
- hermes sessions adopt
- Webapp bundle build helpers and the renderer freshness check
- Regenerated gateway contracts
Co-authored-by: emozilla <emozilla@nousresearch.com>
Translation ran inside the requested profile's home + terminal scope, but the
Windows 404-or-drive decision and the sandbox hint read the active profile's
backend after that scope reset. With mixed backends a missed docker path could
reach the drive lookup, or a local profile's host read got the sandbox 404.
Both now run under one `_agent_path_scope(profile_home)`.
The same scope also installs a bound home's terminal policy when the caller
already set the override (a native profile ticket) and no scope is bound, so
translation stops reading the launch env's TERMINAL_DOCKER_VOLUMES there.
Review follow-ups on the fs / managed-file container path mapping (#85238):
- Translate only when the docker backend is active, so a local-backend gateway
with leftover docker_volumes keeps serving real host files.
- POSIX: a readable host path wins; translation runs only when the host
lookup misses, so a root-run gateway can still browse its own /root.
- Windows: a container-shaped path that misses translation is a 404 instead
of falling back to <drive>:\workspace\x.
- Bind the requested profile's home and terminal policy before translating,
like MEDIA delivery (#109024), so profile=work resolves in the work sandbox.
- Pass quiet=True from the fs routes so ordinary host reads no longer log
"Docker MEDIA path ... did not resolve" warnings.
- Keep refusing '..' in managed reads: paths with '..' are never translated.
- The "may live inside the agent sandbox" 404 detail only shows for
/workspace, /root or a configured volume target.
Docker agents emit container paths (/workspace/..., /root/...) in transcripts
and MEDIA tags. The dashboard's /api/fs/* routes and the managed /api/files
read routes checked those paths against the gateway HOST filesystem, so the
preview, download and file browser 404'd or 403'd on sandbox files (#85238).
A persistent docker agent's /root/fileIwant.zip lives on the host at
<sandbox>/docker/default/home/fileIwant.zip. Clicking it returned "File is
not readable" because the gateway tried to read its own /root.
- `_agent_fs_path` runs the gateway MEDIA translator on the raw string before
`_fs_path` normalizes it. Otherwise, on a Windows gateway, /workspace/x
became D:\workspace\x and could silently serve an unrelated host file.
When no terminal scope is bound (serve --isolated), it retries once under
the profile's terminal policy and falls back to the plain host path.
- read-text, read-data-url, download (including the session_id cwd path) and
list resolve through it before `_hosted_fs_read_guard`, so the locked-root
check sees the real host target.
- `_resolve_managed_path(for_agent_read=True)` translates before the
locked-root containment check. A translated target outside the root still
gets a 403.
- A container-shaped path that fails translation under docker 404s with
"may live inside the agent sandbox" instead of a bare not-found.
Co-authored-by: Brooklyn Nicholson <brooklyn.bb.nicholson@gmail.com>
Co-authored-by: fangliquanflq <fangliquan@qq.com>
Co-authored-by: jacklhchan <32115938+jacklhchan@users.noreply.github.com>
The dashboard file browser's listing route built entries via a bare
comprehension over os.scandir, calling _managed_file_entry() per entry.
That helper raises HTTP 500 ("Could not stat path") on any OSError from
resolved.stat(), so a single dangling symlink (or an entry removed
between readdir and stat) aborted the whole directory listing with a 500
— e.g. a broken ~/.claude link or Steam's recreated linux32/steam made
the entire Files page unusable (#47154).
Whole-class fix, consolidated from the open contributor PRs:
- _managed_file_entry() classifies a symlink whose target is definitively
missing (FileNotFoundError) before the resolved-target boundary check
and returns a `broken_link: true` placeholder entry instead of 500ing.
The placeholder is only emitted for entries inside the managed root
even when the missing target resolves outside it. Permission and other
OSErrors still surface as 403/500 as before.
- Symlink stat results are captured in one snapshot (no second stat), so
a target disappearing mid-listing cannot flip a result into a 500.
- New skip_missing flag lets the listing scan skip an entry that
vanished between readdir and stat (returns None); the write-result
caller stays strict.
- The listing loop appends placeholders and skips vanished entries, so
one dead entry never loses its siblings. The sibling fs_list route
already tolerated ENOENT; /api/files now agrees.
Reading/downloading a broken link directly still returns 404; 403
boundary and sensitive-path behavior are unchanged.
Fixes#47154
Co-authored-by: gangpangpang <2564186295@qq.com>
Co-authored-by: ous fifty <ousfifty@gmail.com>
Incorporates work by @Lqg97 (PR 47159; co-contributors qinggangli and Shiqian Ma, commit emails not linked to GitHub accounts).
The confinement guard reads only env/home state, so the Request the PR
threaded through fs_read_text/fs_download is unused. Make it optional
(request: Request = None keeps FastAPI injection while direct test
callers stay source-compatible) and drop the mandatory argument from
_hosted_fs_read_guard/_managed_files_policy.
The managed-files API (#57505) confines locked-dashboard reads through
_resolve_managed_path, but the file-preview and git-review routes resolve
through unconfined _fs_path / _git_path. On a locked deployment those
routes still returned auth.json / .env / mcp-tokens and any path outside
the managed root (including via git diff --no-index on an absolute file
argument).
Apply the same locked-root and sensitive-path checks to:
- /api/fs/list, /api/fs/read-text, /api/fs/read-data-url
- /api/git/review/diff and /api/git/file-diff (resolved file target)
- /api/git/review/commit-context (caller-supplied repo cwd; this route
returns git diff --cached / git diff HEAD with no per-file arg, so the
cwd itself is the read target)
Local dashboards with no locked root are unchanged. Regression tests cover
credential and outside-root blocks plus in-root allow paths, including a
locked-mode outside-root case for commit-context.
(cherry picked from commit b6ed59823f22fc537f123407a01915135dd6d5fe)
A remote hop may percent-encode an /api/fs path on top of HTTP's own
query decoding, so a non-ASCII filename reaches the gateway as a literal
'%E5%8D%8A...' string; _fs_path never decodes it, the stat fails and the
dashboard preview returns 404 (#103425).
Resolve verbatim first and only fall back to an unquoted retry when that
path does not exist, so filenames that genuinely contain '%XX' keep
resolving as-is. The write-text endpoint opts out to keep creating
literal-percent filenames instead of silently overwriting a decoded
sibling.