This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-4.0.x by this push:
new 45c1665333 fix: filter SPI providers by classloader visibility to
prevent reactor leaks (#13853)
45c1665333 is described below
commit 45c1665333dac700d5cf3ef73854ed7427bac95c
Author: Guillaume Nodet <[email protected]>
AuthorDate: Thu Nov 5 23:03:55 2026 +0100
fix: filter SPI providers by classloader visibility to prevent reactor
leaks (#13853)
`DefaultExtensibleEnumRegistry.lookup()` feeds on `Lookup.lookupList()`
which
returns all SISU-registered providers globally. In a parallel reactor build,
an extension loaded for one module leaks its PackagingProvider (and other
SPI
registries) into sibling modules, causing "Unknown packaging: app-client" on
projects that don't declare that extension.
- Add `isVisibleFrom(provider, tccl)` in ExtensibleEnumRegistries: walks
the classloader hierarchy and checks Plexus ClassRealm.getImportRealms()
via ClassValue-cached reflection to determine whether a provider's realm
is imported into the current project's realm
- Override lookup() to filter dynamic providers through isVisibleFrom()
before returning a match
- DefaultExtensibleEnumRegistryTest covers the visibility filter,
hidden-class
providers, and null-classloader edge cases
- MavenITgh13306PackagingProviderTest verifies isolation in a reactor with
-T 2
Cherry-pick of f6d1bca52717b46ee84183f746adf75b4cfca321 with conflict
resolution.
---
.../maven/impl/ExtensibleEnumRegistries.java | 69 ++++++++++++++++++++
.../impl/DefaultExtensibleEnumRegistryTest.java | 73 ++++++++++++++++++++++
.../it/MavenITgh13306PackagingProviderTest.java | 2 +
3 files changed, 144 insertions(+)
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
index bfbd307bf5..c4b8f6ba44 100644
---
a/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
+++
b/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
@@ -18,6 +18,8 @@
*/
package org.apache.maven.impl;
+import java.lang.reflect.Method;
+import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -47,6 +49,66 @@
public class ExtensibleEnumRegistries {
+ private static final ClassValue<Optional<Method>> GET_IMPORT_REALMS = new
ClassValue<>() {
+ @Override
+ protected Optional<Method> computeValue(Class<?> type) {
+ try {
+ return Optional.of(type.getMethod("getImportRealms"));
+ } catch (NoSuchMethodException | SecurityException e) {
+ return Optional.empty();
+ }
+ }
+ };
+
+ /**
+ * Checks whether a provider is visible from the given classloader.
+ * Providers from project extensions are loaded by extension-specific
classloaders
+ * (realms) that are only imported into the project realm that declared
the extension.
+ * This prevents extension-scoped providers from leaking across reactor
modules
+ * during parallel builds.
+ *
+ * @param provider the SPI provider to check
+ * @param classLoader the classloader to check visibility from (typically
the TCCL)
+ * @return {@code true} if the provider is visible, {@code false} otherwise
+ */
+ public static boolean isVisibleFrom(Object provider, ClassLoader
classLoader) {
+ if (classLoader == null) {
+ return true;
+ }
+ Class<?> providerClass = provider.getClass();
+ ClassLoader providerCl = providerClass.getClassLoader();
+ if (providerCl == null) {
+ return true;
+ }
+ try {
+ if (classLoader.loadClass(providerClass.getName()) ==
providerClass) {
+ return true;
+ }
+ } catch (ClassNotFoundException | LinkageError e) {
+ // Not loadable by name (hidden class, realm-ID import, or broken
link) — check further
+ }
+ for (ClassLoader cl = classLoader; cl != null; cl = cl.getParent()) {
+ if (cl == providerCl) {
+ return true;
+ }
+ }
+ return isImportedRealm(classLoader, providerCl);
+ }
+
+ private static boolean isImportedRealm(ClassLoader classLoader,
ClassLoader providerCl) {
+ return GET_IMPORT_REALMS
+ .get(classLoader.getClass())
+ .map(method -> {
+ try {
+ Collection<?> importedRealms = (Collection<?>)
method.invoke(classLoader);
+ return importedRealms.contains(providerCl);
+ } catch (ReflectiveOperationException | ClassCastException
e) {
+ return false;
+ }
+ })
+ .orElse(false);
+ }
+
@Named
@SessionScoped
public static class DefaultPathScopeRegistry extends
DefaultExtensibleEnumRegistry<PathScope, PathScopeProvider>
@@ -118,6 +180,11 @@ public DefaultExtensibleEnumRegistry(
* thousands; callers that need to look up the same value repeatedly
in a tight loop should
* cache the result themselves.</p>
*
+ * <p><b>Classloader visibility:</b> providers contributed by project
extensions are only
+ * visible when the current thread's context classloader can load the
provider's class.
+ * This prevents extension-scoped providers from leaking across
reactor modules during
+ * parallel builds.</p>
+ *
* @param id the enum value id, must not be {@code null}
* @return the matching value, or {@code Optional.empty()} if not found
*/
@@ -125,7 +192,9 @@ public DefaultExtensibleEnumRegistry(
public Optional<T> lookup(String id) {
String key = requireNonNull(id, "id").toLowerCase(Locale.ROOT);
if (lookup != null && providerType != null) {
+ ClassLoader tccl =
Thread.currentThread().getContextClassLoader();
Optional<T> result = lookup.lookupList(providerType).stream()
+ .filter(p -> isVisibleFrom(p, tccl))
.flatMap(p -> p.provides().stream())
.filter(t ->
key.equals(t.id().toLowerCase(Locale.ROOT)))
.findFirst();
diff --git
a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultExtensibleEnumRegistryTest.java
b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultExtensibleEnumRegistryTest.java
index 984e70bd8f..4e2bd23bfc 100644
---
a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultExtensibleEnumRegistryTest.java
+++
b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultExtensibleEnumRegistryTest.java
@@ -18,7 +18,11 @@
*/
package org.apache.maven.impl;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Collection;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.maven.api.ExtensibleEnum;
@@ -26,6 +30,7 @@
import org.apache.maven.api.spi.ExtensibleEnumProvider;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@@ -99,6 +104,56 @@ void fallsBackToBuiltinWhenDynamicLookupFindsNothing() {
assertSame(foo, registry.lookup("foo").orElseThrow());
}
+ // --- isVisibleFrom tests ---
+
+ @Test
+ void visibleFromNullClassLoaderAlwaysReturnsTrue() {
+ TestProvider provider = () -> List.of(CUSTOM);
+ assertTrue(ExtensibleEnumRegistries.isVisibleFrom(provider, null));
+ }
+
+ @Test
+ void visibleFromSameClassLoaderReturnsTrue() {
+ TestProvider provider = () -> List.of(CUSTOM);
+ ClassLoader cl = provider.getClass().getClassLoader();
+ assertTrue(ExtensibleEnumRegistries.isVisibleFrom(provider, cl));
+ }
+
+ @Test
+ void visibleFromChildOfParentClassLoaderReturnsTrue() throws Exception {
+ TestProvider provider = () -> List.of(CUSTOM);
+ ClassLoader parent = provider.getClass().getClassLoader();
+ try (URLClassLoader child = new URLClassLoader(new URL[0], parent)) {
+ assertTrue(ExtensibleEnumRegistries.isVisibleFrom(provider,
child));
+ }
+ }
+
+ @Test
+ void notVisibleFromUnrelatedClassLoader() throws Exception {
+ TestProvider provider = () -> List.of(CUSTOM);
+ try (URLClassLoader unrelated = new URLClassLoader(new URL[0], null)) {
+ assertFalse(ExtensibleEnumRegistries.isVisibleFrom(provider,
unrelated));
+ }
+ }
+
+ @Test
+ void visibleFromClassRealmThatImportsProviderRealm() throws Exception {
+ TestProvider provider = () -> List.of(CUSTOM);
+ ClassLoader providerCl = provider.getClass().getClassLoader();
+ try (FakeClassRealm realm = new FakeClassRealm(Set.of(providerCl))) {
+ assertTrue(ExtensibleEnumRegistries.isVisibleFrom(provider,
realm));
+ }
+ }
+
+ @Test
+ void notVisibleFromClassRealmWithoutMatchingImport() throws Exception {
+ TestProvider provider = () -> List.of(CUSTOM);
+ try (URLClassLoader otherCl = new URLClassLoader(new URL[0], null);
+ FakeClassRealm realm = new FakeClassRealm(Set.of(otherCl))) {
+ assertFalse(ExtensibleEnumRegistries.isVisibleFrom(provider,
realm));
+ }
+ }
+
record TestValue(String id) implements ExtensibleEnum {}
interface TestProvider extends ExtensibleEnumProvider<TestValue> {}
@@ -115,4 +170,22 @@ static class TestRegistry extends
ExtensibleEnumRegistries.DefaultExtensibleEnum
super(lookup, TestProvider.class, providers, builtins);
}
}
+
+ /**
+ * Simulates a ClassRealm with {@code getImportRealms()} so the
reflection-based
+ * import check can be exercised without depending on plexus-classworlds.
+ */
+ static class FakeClassRealm extends URLClassLoader {
+ private final Collection<ClassLoader> importedRealms;
+
+ FakeClassRealm(Collection<ClassLoader> importedRealms) {
+ super(new URL[0], null);
+ this.importedRealms = importedRealms;
+ }
+
+ @SuppressWarnings("unused") // called via reflection by isImportedRealm
+ public Collection<ClassLoader> getImportRealms() {
+ return importedRealms;
+ }
+ }
}
diff --git
a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13306PackagingProviderTest.java
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13306PackagingProviderTest.java
index d2bc777e1a..99d96aab7e 100644
---
a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13306PackagingProviderTest.java
+++
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13306PackagingProviderTest.java
@@ -49,6 +49,8 @@ void providerFromProjectExtensionIsVisibleOnlyToItsProject()
throws Exception {
verifier = newVerifier(new File(testDir, "reactor").getPath());
verifier.setAutoclean(false);
+ verifier.addCliArgument("-T");
+ verifier.addCliArgument("2");
verifier.addCliArgument("validate");
try {
verifier.execute();