Prevent winstone cleanup from removing files outside webroot - #541
Open
arpan-pramanik wants to merge 1 commit into
Open
Prevent winstone cleanup from removing files outside webroot#541arpan-pramanik wants to merge 1 commit into
arpan-pramanik wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes an unsafe webroot cleanup behavior in HostConfiguration where recursive deletion could follow symlinks and delete files outside the configured webroot (notably observed via /proc/self/task/<pid>/cwd during Jenkins restarts).
Changes:
- Updates
deleteRecursiveto avoid descending into symlink directories (deleting the link itself instead). - Adds defensive exception handling around the symlink check to avoid aborting cleanup due to path-related failures.
Comments suppressed due to low confidence (1)
src/main/java/winstone/HostConfiguration.java:375
- The broad
catch (Exception)around the symlink check/listing hides unexpected failures and also repeatsdir.toPath()(which can throwInvalidPathException) in multiple places. Consider resolving thePathonce, narrowing the exceptions, and only logging/returning on truly unrecoverable path issues; this keeps the control flow clearer and avoids swallowing unrelated runtime exceptions.
private void deleteRecursive(File dir) {
try {
if (!Files.isSymbolicLink(dir.toPath())) {
File[] children = dir.listFiles();
if (children != null) {
for (File child : children) {
deleteRecursive(child);
}
}
}
} catch (Exception ex) {
// Ignore path exceptions here; they will be handled by deleteIfExists below
}
try {
Files.deleteIfExists(dir.toPath());
} catch (Exception ex) {
Logger.logDirectMessage(Level.WARNING, null, "Failed to delete dirs " + dir.getAbsolutePath(), ex);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+359
to
+366
| try { | ||
| if (!Files.isSymbolicLink(dir.toPath())) { | ||
| File[] children = dir.listFiles(); | ||
| if (children != null) { | ||
| for (File child : children) { | ||
| deleteRecursive(child); | ||
| } | ||
| } |
Comment on lines
+360
to
+365
| if (!Files.isSymbolicLink(dir.toPath())) { | ||
| File[] children = dir.listFiles(); | ||
| if (children != null) { | ||
| for (File child : children) { | ||
| deleteRecursive(child); | ||
| } |
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.
This fixes an issue where winstone's webroot cleanup accidentally follows symlinks and wipes files outside the configured webroot. specifically, during a jenkins upgrade restart, the cleanup was recursing into
/proc/self/task/<pid>/cwdwhich is a symlink to the working directory.to fix this, i updated
deleteRecursiveto check if the directory is a symbolic link before listing its children. if it is a symlink, it safely deletes the link itself instead of following it and causing out-of-bounds deletions.resolves jenkinsci/jenkins#26936
Testing done
mvn clean test) to ensure no existing tests or cleanup logic broke.--webrootand a dummy war.deleteRecursivecleanup.Submitter checklist