Repository navigation
fix(core): ask external_directory before session_move leaves the project - #62204
Conversation
There was a problem hiding this comment.
The change looks good to me. The new test fails on the base, where the move happens with no external_directory request, and passes with this PR (3/3 in tool-opencode.test.ts). typecheck:tsc passes for packages/core.
Public API: no config, schema, HTTP or SDK changes. session_move can now trigger an external_directory check (a deny rule blocks the move, an ask rule prompts), and its directory output is the resolved absolute path instead of the raw input. Callers passing absolute paths see the same value; relative or ~ inputs now come back resolved.
No blocking concerns.
There was a problem hiding this comment.
The fix in packages/core/src is the same as at my last review, so the earlier verification still holds. The new commits only remove the test and the docs line saying the session utilities ask for no permission, which is right now that session_move checks external_directory. The remaining tests in tool-opencode.test.ts pass. No new concerns.
Ports upstream 0dffb96 (anomalyco#62204, v2.2.6) to the v2.2.5 base; retires at the next base move. session_move gave the session any existing directory as its new location. Everything under a session's directory counts as inside the project, so after a move the outside-folder rules were skipped for that whole tree. The tool now resolves the destination against the target session's directory (FileAccess.resolvePath: relative paths and ~) and, unless it lies inside the project or one of its worktrees, applies the configured external_directory rules through FileAccess.authorizeExternal before moving. The tool's output and content report the directory moved to. The Code Mode utilities no longer claim that they request no permission action. Scope: these rules guard the agent's session_move tool, as upstream's fix does. The server's session-move route, which the TUI, the app and plugins use, and the plugin host's move path are operator and trusted-code routes and do not apply them. A caller that reaches the service's HTTP API with its credentials is outside what tool permissions cover. Files: packages/core/src/tool/plugin/opencode.ts, packages/core/src/plugin/internal.ts (Git for internal plugins), packages/core/src/git.ts (exact worktree list), services/www/src/docs/content/tools.mdx; tests in packages/core/test/tool-opencode.test.ts and git.test.ts, with Git provided in packages/core/test/plugin/fixture.ts and session-refusal-builtins.test.ts; git-environment.test.ts now expects the -z argument. Adapted from upstream (ACF): - The decision uses the destination's real folder (links, and on Windows junctions, resolved), and the move is given that folder, so a link in the project that leads out of it asks for the folder it reaches. A destination that does not exist is refused before any ask or move, and so is a real folder whose path begins or ends with whitespace, because the move trims what it is given. Just before the move the path is resolved again and must still reach the same folder, so a change while the ask was open refuses the move; a link swapped in after that re-check, before the move takes effect, is not caught. Containment is also checked against the real folders of the session's directory and its project, so a project opened through a link moves within itself without an ask. - A stored worktree row exempts a destination inside it only while both sides agree at the time of the move: the project's repository lists the row's folder among its worktrees (git worktree list in the project's checkout), and the folder is the top of a checkout whose Git common directory is the repository's (git rev-parse). Clones that share a remote share a project ID, a row can outlive its checkout, and a checkout's own Git files can name any common directory. Rows are stored as real paths, so a destination inside none of them costs no Git process. Other worktree strategies get the ordinary rules. - That list is read exactly. Git.worktree.list now runs git worktree list --porcelain -z and takes each path as Git wrote it: it split lines and trimmed each path, so a name ending in whitespace such as a no-break space (legal on Windows) came out as another path, and a path with a newline could split (worktreeEntries). An entry whose path is empty or not valid UTF-8 is left out. With a Git older than 2.36, which has no -z, the other callers (Project's main-worktree lookup and the git worktree strategy's list) get the previous newline parse; session_move asks for an exact list ({ exact: true }), which then fails, so no worktree is exempt. - The ask is made with intent "write". Everything under the destination counts as inside afterwards, so the shared OS temp root's read exemption in ACF's authorizeExternal must not apply to a move; a move there is refused with the same guidance as a write until the identical request is repeated. Remaining limit: containment uses path.relative, as Core's file tools do, which ignores case on Windows, so a case-sensitive folder above the project could be taken for one inside it. Upstream's commit has no test; the tests are ACF's: no ask inside the project or in a linked worktree of its repository, including one whose name begins with a space or ends with a no-break space; an ask for an independent clone that shares the remote, for a row whose checkout has gone, and for a checkout whose Git files name the project's common directory without the repository listing it; the ask and the absolute destination for a relative path outside; a link inside the project that leads out asks for the folder it reaches and the move goes there, while one that stays inside does not ask; a missing destination, and a real path ending in whitespace (a chosen real path stands in for one), are refused before any ask; a destination that changes while the ask is open is refused after it; a rejected ask, with the real move service, leaves no queued move and the session where it was; and the temp-root case. Git tests: the list keeps a leading space and a trailing no-break space and ignores a newline in a lock reason, and the parser leaves out an empty or invalid UTF-8 path while later entries keep their kind.
ACF-CHANGES.md gets one row per fix ported on this branch: UTF-8 refusal (anomalyco#64868, with ACF's byte order mark change), Code Mode fetch redirects (anomalyco#60502), URL credentials and rule matching (anomalyco#61701, anomalyco#62373), site-scoped "Allow always" (anomalyco#66000, with the image_generate redirect limit), service password out of the CLI log (anomalyco#64911), the session_move ask and its scope (anomalyco#62204), and file reads as downloads with cross-site API refusal (anomalyco#62422).
… the base Ports ahead of the base, each with ACF's own fixes from three review rounds: refuse non-UTF-8 rewrites (anomalyco#64868); check Code Mode fetch redirects (anomalyco#60502); reject URL credentials and match rules as written (anomalyco#61701, anomalyco#62373); site-scoped Always for web fetch (anomalyco#66000); keep the service password out of the CLI log (anomalyco#64911); session_move asks external_directory (anomalyco#62204) on the real destination, re-checked before the move, with worktrees exempt only when the project's own Git lists them (read exactly, -z); file reads as downloads and cross-site API refusal (anomalyco#62422). Plus ACF's byte-only BOM sync and the template Ask test. Reviews: 2026-11-29T18-08, 19-01, 19-28 and 20-02 relays (Associate GPT-6.2 Sol). Gates on Bun 1.4.4 at 0c082b0231 (one ledger sentence changed since): core 1398/3 skip/2 fail (project-icon symlink EPERM, same on base), typecheck:tsc 0 in core/cli/server/util, root check only the known custom-elements.d.ts symlink baseline. Private scan: 8 'email' matches, all synthetic URL userinfo examples ([email protected], [email protected]); gitleaks clean.
Issue for this PR
Closes #49825
Closes #61054
Type of change
What does this PR do?
session_movedidn't check any permission. The session's directory decides which paths count as internal, so an agent withexternal_directory: denycould move itself to../outsideand then write there freely.Now the destination goes through the same
external_directorycheck the file tools use before the move is queued:The project's own worktrees are skipped so "create a worktree, then move into it" doesn't prompt every time (worktrees live outside the project folder). A deny rule now blocks the move, an ask rule prompts, and a rejection queues nothing.
I used
external_directoryinstead of a newopencode_session_moveaction (as in #58047) so existing confinement rules apply without users adding a new rule.Small side change: the tool now returns the resolved absolute directory instead of echoing the input.
Out of scope:
session_renamestill has no permission check; it can't move a session's boundary.How did you verify your code works?
bun test test/tool-opencode.test.tsinpackages/corepasses.bun run checkfrom the root: 35 tasks pass.Added by the opencode bot (AI-generated).
Public API: session_move now checks external_directory for destinations outside the project and returns the resolved absolute directory (backwards compatible)