← 输入框|github.com|刷新
Skip to content

[v5] git: worktree, make the filesystem wrapper a symlink-safe boundary - #2277

Merged
pjbgf merged 1 commit into
releases/v5.xfrom
checkout-v5
Jul 29, 2026
Merged

pjbgf merged 1 commit into
releases/v5.xfrom
checkout-v5

Conversation

@pjbgf

@pjbgf pjbgf commented Jul 24, 2026

Copy link
Copy Markdown
Member

worktreeFilesystem validated path strings but did not account for symlinks already present on disk. As a result a worktree operation on an otherwise valid path could resolve through a symlinked component and act outside the worktree.

Enforce the invariant at the boundary. validNoLeadingSymlink rejects paths whose leading directories resolve through an existing symlink, and it runs on every operation. Mutating operations go through validWritePath (Create, OpenFile, MkdirAll, Symlink, Rename, Remove) and reads through validReadPath (Open, Stat, ReadDir, Lstat, Readlink, Chroot). This also covers Worktree.Remove, RemoveGlob and rmFileAndDirsIfEmpty, which delete through the wrapper. Chroot additionally refuses a symlink as the final component so a sub-filesystem such as a submodule worktree cannot be scoped to a redirected target.

The wrapper stops at leading-component traversal. Final-component no-follow during materialisation is enforced at the call site. checkoutFile removes a blocking symlink, whether in a leading component or at the final path, before writing. A forced checkout therefore replaces it with a real entry and succeeds, matching upstream create_directories and its unlink-before-write behaviour. Callers that do not remove it first get a safe error at the boundary.

Mirrors upstream Git's has_symlink_leading_path in symlinks.c and the check_leading_path and unlink-before-write guards in entry.c.

Back-ports #2276.

Copilot AI review requested due to automatic review settings July 24, 2026 22:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens worktree filesystem operations against symlink-based path escapes by enforcing a “no leading symlink traversal” invariant at the worktreeFilesystem boundary, and by explicitly removing blocking symlinks at checkout materialisation time.

Changes:

  • Add leading-component symlink checks for both read and write operations in worktreeFilesystem (plus an extra final-component check for Chroot).
  • Ensure checkout materialisation removes any blocking symlinks before writing tracked entries.
  • Add regression tests covering leading-symlink traversal rejection and forced-checkout symlink replacement behavior.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.

File Description
worktree.go Adds clearBlockingSymlinks and uses it to ensure checkout writes don’t follow/overwrite through planted symlinks.
worktree_fs.go Introduces validWritePath / validNoLeadingSymlink and applies read/write boundary validation; tightens Chroot semantics.
worktree_fs_test.go Adds tests to pin both boundary behavior (reject traversal) and checkout behavior (replace blocking symlinks).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread worktree_fs.go Resolved
Comment thread worktree_fs.go Resolved
Comment thread worktree.go Resolved
Comment thread worktree_fs_test.go Resolved
worktreeFilesystem validated path strings but did not account for
symlinks already present on disk. As a result a worktree operation on an
otherwise valid path could resolve through a symlinked component and act
outside the worktree.

Enforce the invariant at the boundary. validNoLeadingSymlink rejects
paths whose leading directories resolve through an existing symlink, and
it runs on every operation. Mutating operations go through validWritePath
(Create, OpenFile, MkdirAll, Symlink, Rename, Remove) and reads through
validReadPath (Open, Stat, ReadDir, Lstat, Readlink, Chroot). This also
covers Worktree.Remove, RemoveGlob and rmFileAndDirsIfEmpty, which delete
through the wrapper. Chroot additionally refuses a symlink as the final
component so a sub-filesystem such as a submodule worktree cannot be
scoped to a redirected target.

The wrapper stops at leading-component traversal. Final-component
no-follow during materialisation is enforced at the call site.
checkoutFile removes a blocking symlink, whether in a leading component
or at the final path, before writing. A forced checkout therefore
replaces it with a real entry and succeeds, matching upstream
create_directories and its unlink-before-write behaviour. Callers that do
not remove it first get a safe error at the boundary.

Mirrors upstream Git's has_symlink_leading_path in symlinks.c and the
check_leading_path and unlink-before-write guards in entry.c.

Assisted-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Paulo Gomes <paulo@entire.io>
@pjbgf
pjbgf merged commit 3eeb238 into releases/v5.x Jul 29, 2026
11 checks passed
@pjbgf
pjbgf deleted the checkout-v5 branch July 29, 2026 09:01
rjosephwright added a commit to cloudboss/unobin that referenced this pull request Aug 10, 2026
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.

3 participants