Fix copy/move recursing into source through a symlinked destination ancestor#1073
Merged
RyanZim merged 1 commit intoJun 29, 2026
Conversation
checkParentPaths walks up the destination's ancestors to detect when the destination is inside the source (which would cause copy to recurse into the tree it is creating). When an intermediate parent did not exist yet it returned early on ENOENT, so a deeper ancestor that is a symlink into the source tree was never checked. copy then created the missing path under the symlink target and walked into the subtree until ENAMETOOLONG. Recurse to the next parent on ENOENT instead of returning, so the self-subdirectory check keeps walking up to an existing ancestor.
JPeer264
approved these changes
Jun 15, 2026
JPeer264
left a comment
Collaborator
There was a problem hiding this comment.
Implementation and tests look fine, but I'm not the best to judge.
Collaborator
|
Also thanks a lot for taking your time to fix it |
RyanZim
approved these changes
Jun 29, 2026
Collaborator
|
Published in |
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.
Fixes #1071.
checkParentPathswalks up the destination's ancestors to catch the case where the destination is actually inside the source, which otherwise makescopyrecurse into the very tree it is creating. When an intermediate destination parent doesn't exist yet, it returned early onENOENTand stopped checking. If a deeper ancestor is a symlink into the source tree, that early return bypasses the check entirely.The result is that
copy(src, link/sub/dest)(wherelink->srcandlink/subdoesn't exist yet) keeps copyingsrcinto itself, growingsrc/sub/dest/sub/dest/...until it dies withENAMETOOLONG, instead of raising the usual "Cannot copy to a subdirectory of itself" error.Per @RyanZim's suggestion in the issue, the fix recurses to the next parent on
ENOENTinstead of returning, so the check keeps walking up until it reaches an existing ancestor (or the source parent / root). This affectscopy,copySync,move, andmoveSyncsince they all go throughcheckParentPaths.Testing
copyandcopySynccovering a non-existent dest parent under a symlink-into-src ancestor (fail before this change: async times out from the runaway recursion, sync doesn't throw; both pass after).npm test(lint + 728 unit + ESM).