Skip to content

Commit ce0e7ba

Browse files
committed
Delete root cache directory for pre-SECURITY-2586 caches
1 parent 3de6758 commit ce0e7ba

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryCachingCleanup.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,30 @@ public LibraryCachingCleanup() {
2727
@Override protected void execute(TaskListener listener) throws IOException, InterruptedException {
2828
FilePath globalCacheDir = LibraryCachingConfiguration.getGlobalLibrariesCacheDir();
2929
for (FilePath library : globalCacheDir.list()) {
30-
if (!removeIfOldCacheDirectory(library, TimeUnit.DAYS.toMillis(EXPIRE_AFTER_READ_DAYS))) {
31-
// Prior to SECURITY-2586 security fixes, library caches had a two-level directory structure.
30+
if (!removeIfExpiredCacheDirectory(library)) {
31+
// Prior to the SECURITY-2586 fix, library caches had a two-level directory structure.
3232
// These caches will never be used again, so we delete any that we find.
3333
for (FilePath version: library.list()) {
34-
removeIfOldCacheDirectory(version, 0);
34+
if (version.child(LibraryCachingConfiguration.LAST_READ_FILE).exists()) {
35+
library.deleteRecursive();
36+
break;
37+
}
3538
}
3639
}
3740
}
3841
}
3942

4043
/**
41-
* Delete cache directories for the given library if they are outdated.
44+
* Delete the specified cache directory if it is outdated.
4245
* @return true if specified directory is a cache directory, regardless of whether it was outdated. Used to detect
4346
* whether the cache was created before or after the fix for SECURITY-2586.
4447
*/
45-
private boolean removeIfOldCacheDirectory(FilePath library, long maxDurationSinceLastReadMillis) throws IOException, InterruptedException {
48+
private boolean removeIfExpiredCacheDirectory(FilePath library) throws IOException, InterruptedException {
4649
final FilePath lastReadFile = new FilePath(library, LibraryCachingConfiguration.LAST_READ_FILE);
4750
if (lastReadFile.exists()) {
48-
if ((System.currentTimeMillis() - lastReadFile.lastModified()) > maxDurationSinceLastReadMillis) {
51+
if (System.currentTimeMillis() - lastReadFile.lastModified() > TimeUnit.DAYS.toMillis(EXPIRE_AFTER_READ_DAYS)) {
4952
library.deleteRecursive();
50-
library.withSuffix("-name.txt").delete(); // Harmless if this is a pre-SECURITY-2586 cache directory.
53+
library.withSuffix("-name.txt").delete();
5154
}
5255
return true;
5356
}

src/test/java/org/jenkinsci/plugins/workflow/libs/LibraryCachingCleanupTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void preSecurity2586() throws Throwable {
8888
cache.child(LibraryCachingConfiguration.LAST_READ_FILE).touch(System.currentTimeMillis());
8989
ExtensionList.lookupSingleton(LibraryCachingCleanup.class).execute(StreamTaskListener.fromStderr());
9090
assertThat(new File(cache.getRemote()), not(anExistingDirectory()));
91+
assertThat(new File(cache.getParent().getRemote()), not(anExistingDirectory()));
9192
}
9293

9394
}

0 commit comments

Comments
 (0)