Skip to content

Commit 89b8b1a

Browse files
philwebbsdeleuze
authored andcommitted
Prevent cache pollution by storing only the factories
Update `SpringFactoriesLoader` so that the cache stores only the factories and not the complete loader. Prior to this commit, if a cache entry was added with the thread context classloader, the loader instance would be added and the classloader stored. If the thread context classloader subsequently changes, and a call is made to `forDefaultResourceLocation` with `null` for the classloader, the cached entry would be used which contains the older classloader. Closes gh-34732
1 parent 900dd0c commit 89b8b1a

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class SpringFactoriesLoader {
101101

102102
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
103103

104-
static final Map<ClassLoader, Map<String, SpringFactoriesLoader>> cache = new ConcurrentReferenceHashMap<>();
104+
static final Map<ClassLoader, Map<String, Factories>> cache = new ConcurrentReferenceHashMap<>();
105105

106106

107107
@Nullable
@@ -321,10 +321,11 @@ public static SpringFactoriesLoader forResourceLocation(String resourceLocation,
321321
Assert.hasText(resourceLocation, "'resourceLocation' must not be empty");
322322
ClassLoader resourceClassLoader = (classLoader != null ? classLoader :
323323
SpringFactoriesLoader.class.getClassLoader());
324-
Map<String, SpringFactoriesLoader> loaders = cache.computeIfAbsent(
324+
Map<String, Factories> factoriesCache = cache.computeIfAbsent(
325325
resourceClassLoader, key -> new ConcurrentReferenceHashMap<>());
326-
return loaders.computeIfAbsent(resourceLocation, key ->
327-
new SpringFactoriesLoader(classLoader, loadFactoriesResource(resourceClassLoader, resourceLocation)));
326+
Factories factories = factoriesCache.computeIfAbsent(resourceLocation, key ->
327+
new Factories(loadFactoriesResource(resourceClassLoader, resourceLocation)));
328+
return new SpringFactoriesLoader(classLoader, factories.byType());
328329
}
329330

330331
protected static Map<String, List<String>> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) {
@@ -667,4 +668,8 @@ static FailureHandler handleMessage(BiConsumer<Supplier<String>, Throwable> mess
667668
}
668669
}
669670

671+
672+
private record Factories(Map<String, List<String>> byType) {
673+
674+
}
670675
}

spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626

2727
import org.apache.commons.logging.Log;
28+
import org.assertj.core.extractor.Extractors;
2829
import org.junit.jupiter.api.AfterAll;
2930
import org.junit.jupiter.api.BeforeAll;
3031
import org.junit.jupiter.api.Nested;
@@ -166,9 +167,26 @@ void loadForResourceLocationLoadsFactories() {
166167
void sameCachedResultIsUsedForDefaultClassLoaderAndNullClassLoader() {
167168
SpringFactoriesLoader forNull = SpringFactoriesLoader.forDefaultResourceLocation(null);
168169
SpringFactoriesLoader forDefault = SpringFactoriesLoader.forDefaultResourceLocation(ClassUtils.getDefaultClassLoader());
169-
assertThat(forNull).isSameAs(forDefault);
170+
assertThat(forNull).extracting("factories").isSameAs(Extractors.byName("factories").apply(forDefault));
170171
}
171172

173+
@Test
174+
void staleClassLoaderIsUsedWithCachedResult() {
175+
ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
176+
ClassLoader cl1 = new ClassLoader() {
177+
};
178+
SpringFactoriesLoader factories1 = SpringFactoriesLoader.forDefaultResourceLocation(defaultClassLoader);
179+
assertThat(factories1).extracting("classLoader").isEqualTo(defaultClassLoader);
180+
ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
181+
try {
182+
Thread.currentThread().setContextClassLoader(cl1);
183+
SpringFactoriesLoader factories2 = SpringFactoriesLoader.forDefaultResourceLocation(null);
184+
assertThat(factories2).extracting("classLoader").isNull();
185+
}
186+
finally {
187+
Thread.currentThread().setContextClassLoader(previousClassLoader);
188+
}
189+
}
172190

173191
@Nested
174192
class FailureHandlerTests {

0 commit comments

Comments
 (0)