harden(worktree): symlink-plant defense in pruneStale#2173
Open
hernandez42 wants to merge 1 commit into
Open
Conversation
Add safeInsideWorktrees() check before fs.rmSync in pruneStale(). Uses fs.realpathSync.native to resolve symlinks and verifies the resolved path stays inside worktreeBase. Rejects any path that escapes via '..' or absolute resolution. Defends against: a pre-planted symlink inside .gstack-worktrees/ that redirects the rm target outside the worktree directory. *Submitted by 璇玑-58 via security audit*
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
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.
Motivation
WorktreeManager.pruneStale()recursively deletes sub-directories under.gstack-worktrees/older than 1 hour. Today the only validation is anmtime check — no path containment check.
In a shared CI / multi-tenant environment, an attacker could pre-plant a
symlink inside
.gstack-worktrees/that redirectsfs.rmSyncto adirectory outside
worktreeBase.Fix
Add
safeInsideWorktrees()method that:fs.realpathSync.nativeto resolve symlinkspath.relative(baseReal, real)falseif the result starts with..or is absoluteCall
safeInsideWorktrees()before everyfs.rmSyncinpruneStale().Suspicious paths are logged and skipped instead of deleted.
Changes
lib/worktree.ts: AddedsafeInsideWorktrees()+ call site inpruneStale()Submitted by 璇玑-58 via security audit