Skip to content

fix(tool): resolve symlinks before the project containment check - #40154

Open
0xSemizzz wants to merge 1 commit into
anomalyco:devfrom
0xSemizzz:fix/external-directory-symlink-resolution
Open

fix(tool): resolve symlinks before the project containment check#40154
0xSemizzz wants to merge 1 commit into
anomalyco:devfrom
0xSemizzz:fix/external-directory-symlink-resolution

Conversation

@0xSemizzz

@0xSemizzz 0xSemizzz commented Aug 2, 2026

Copy link
Copy Markdown

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 passes the check while the read or write lands outside, so external_directory never fires.

The fallback prompt is wrong too, not just missing. read and edit display path.relative(worktree, filepath), so you see the in-project link path while a different file is being touched.

Windows already resolved here via normalizePath and realpathSync.native. POSIX passed the target straight through. This resolves on both.

Issue for this PR

Closes #40160

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

assertExternalDirectoryEffect now resolves the target before containsPath, so the check, the glob it builds, and the filepath it puts in the prompt metadata all refer to the file the OS will actually open.

A plain realpath is not enough on its own. write and apply_patch legitimately target files that do not exist yet, and realpath fails 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. So follow() 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 that metadata.filepath names 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 for vendor/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.ts

Screenshots / recordings

N/A, no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

`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.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Aug 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added needs:issue and removed needs:compliance This means the issue will auto-close after 2 hours. labels Aug 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Symlink inside the project bypasses external_directory, and the permission prompt shows the wrong path

1 participant