Repository navigation
Conversation
forceDeleteOnExit recursed through File.isDirectory(), which follows symbolic links, so a link to a directory outside the tree got every file under its target scheduled for deletion on exit. deleteDirectory did the same on its fallback path when the link itself could not be removed. Skip the recursion for symbolic links in both places.
This branch has not been deployed
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.
What does this change?
FileUtils.forceDeleteOnExitwalks the tree withFile.isDirectory(), which follows symbolic links. A link to a directory elsewhere gets every file under its target registered withdeleteOnExitthrough the link path, and the shutdown hook deletes those files when the JVM exits. Only the link should go. This is the usual symlink-following deletion bug (CWE-59), and trees like that are common in the scratch directories this helper gets pointed at, since SCM checkouts and unpacked archives can both contain directory links.deleteDirectorymostly avoids it by callingdelete()first, but if the link cannot be unlinked (for example because its parent directory is read-only) it falls through tocleanDirectoryon the link and empties the target before throwing.Both methods now skip the recursion when the directory is a symbolic link, the same guard Commons IO has in its
deleteDirectoryanddeleteDirectoryOnExit.Repro on macOS:
outside/keep.txtpluswork/dirSymlink -> outside, thenFileUtils.forceDeleteOnExit(work)and exit the JVM. Before the changeoutside/keep.txtis deleted; after it onlyworkis gone. The two new tests inFileUtilsTestfail without the fix and pass with it, andmvn verify javadoc:javadocis green on JDK 21.Related issue
None.
Anything reviewers should look at closely?
deleteDirectoryalready did that whenever the unlink succeeded.forceDeleteOnExitDoesNotFollowDirSymlinkforks a JVM (usingjava.homeandjava.class.path), sincedeleteOnExitonly runs at shutdown.ln -sand POSIX directory permissions. Windows junctions are not reported byFiles.isSymbolicLink, so they are not covered here.