fix(tool): resolve symlinks before the project containment check - #40154
Open
0xSemizzz wants to merge 1 commit into
Open
fix(tool): resolve symlinks before the project containment check#401540xSemizzz wants to merge 1 commit into
0xSemizzz wants to merge 1 commit into
Conversation
`containsPath` is `path.relative` string math, but every caller hands the same unresolved string to an fs call, and fs follows symlinks. A link inside the project pointing outside it passed the check while the read or write landed outside the project, so `external_directory` never fired. The fallback prompt was wrong too, not just absent: `read` and `edit` render `path.relative(worktree, filepath)`, so the user saw the in-project link path while a different file was being touched. Windows already resolved here, via `normalizePath` -> `realpathSync.native`; POSIX passed the target through untouched. Resolve on both so the check, the glob, and the prompt metadata all name the path the filesystem will use. Plain realpath is not sufficient because `write` and `apply_patch` target files that do not exist yet, so walk up to the nearest existing ancestor and re-attach the tail. Errors other than ENOENT keep the lexical path, preserving today's behaviour rather than failing the tool call.
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
containsPathispath.relativestring math, but every caller hands the same unresolved string to an fs call, and fs follows symlinks. A link inside the project pointing outside it passes the check while the read or write lands outside, soexternal_directorynever fires.The fallback prompt is wrong too, not just missing.
readandeditdisplaypath.relative(worktree, filepath), so you see the in-project link path while a different file is being touched.Windows already resolved here via
normalizePathandrealpathSync.native. POSIX passed the target straight through. This resolves on both.Issue for this PR
Closes #40160
Type of change
What does this PR do?
assertExternalDirectoryEffectnow resolves the target beforecontainsPath, so the check, the glob it builds, and thefilepathit puts in the prompt metadata all refer to the file the OS will actually open.A plain
realpathis not enough on its own.writeandapply_patchlegitimately target files that do not exist yet, andrealpathfails with ENOENT on those, which would silently fall back to the lexical path and leave the hole open for exactly the case that writes a new file. Sofollow()walks up to the nearest existing ancestor and re-attaches the tail, which resolves a symlinked parent directory even when the leaf is new.Errors other than ENOENT, such as ELOOP or EACCES, keep the lexical path. Resolution is a guard here, not a hard requirement, and failing a tool call because a path could not be resolved would be worse than the current behaviour.
Note this does not change anything for ordinary in-project paths, since they resolve to themselves, so it should not add prompts to normal use.
How did you verify your code works?
Three tests in
packages/opencode/test/tool/external-directory.test.ts: a symlink to an outside dir with an existing file behind it (asserting both the glob and thatmetadata.filepathnames the real target), the same with a file that does not exist yet, and a regression test that an ordinary in-project path still produces no request.I also built the symlink layout on disk and checked
follow()directly forvendor/existing.txt,vendor/brand-new.txt,vendor/deep/a/b.txt, and a normal in-project file. The first three flip from contained to not contained, the last stays contained.cd packages/opencode && bun test test/tool/external-directory.test.tsScreenshots / recordings
N/A, no UI change.
Checklist