This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit a884d3d52b34817351cb4f5e904019bc12a76ccd Author: opencode <[email protected]> AuthorDate: Tue Oct 20 13:48:54 2026 +0200 Report a resource link which omits the global attribute as an IllegalArgumentException The global attribute of a resource link is documented as required but nothing checked it at runtime. A web application configured with a resource link which omitted the global attribute made the registration of the link pass a null value to ConcurrentHashMap.put which rejected it with a NullPointerException. The exception escaped the creation of the naming context which only handles a NamingException, and the failure which reached the operator was an unchecked exception naming neither the link nor the missing attribute, while the deployment of the whole web application failed. The registration of the access to a global resource now checks both names up front and reports the misconfiguration as an IllegalArgumentException naming the link, consistent with the other validation failures of the factory. The deployment fails as before but with a message which identifies the faulty resource link. Add a test which checks the rejection of a registration missing either name. --- .../apache/naming/factory/LocalStrings.properties | 1 + .../apache/naming/factory/ResourceLinkFactory.java | 6 +++++ test/org/apache/naming/TestNamingContext.java | 27 ++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/java/org/apache/naming/factory/LocalStrings.properties b/java/org/apache/naming/factory/LocalStrings.properties index 04843fa857..a9c5074c0e 100644 --- a/java/org/apache/naming/factory/LocalStrings.properties +++ b/java/org/apache/naming/factory/LocalStrings.properties @@ -38,6 +38,7 @@ lookupFactory.typeMismatch=The JNDI reference [{0}] was expected to be of type [ resourceFactory.factoryCreationError=Could not create resource factory instance resourceLinkFactory.invalidGlobalContext=Caller provided invalid global context +resourceLinkFactory.nullName=The local resource link [{0}] that refers to global resource [{1}] does not specify the required name or global attribute resourceLinkFactory.nullType=The local resource link [{0}] that refers to global resource [{1}] does not specify the required attribute type resourceLinkFactory.unknownType=The local resource link [{0}] that refers to global resource [{1}] specified the unknown type [{2}] resourceLinkFactory.wrongType=The local resource link [{0}] that refers to global resource [{1}] was expected to return an instance of [{2}] but returned an instance of [{3}] diff --git a/java/org/apache/naming/factory/ResourceLinkFactory.java b/java/org/apache/naming/factory/ResourceLinkFactory.java index eb993c08d6..40f8ad4e57 100644 --- a/java/org/apache/naming/factory/ResourceLinkFactory.java +++ b/java/org/apache/naming/factory/ResourceLinkFactory.java @@ -77,9 +77,15 @@ public class ResourceLinkFactory implements ObjectFactory { * @param globalContext The global naming context * @param localName The local name of the resource * @param globalName The global name of the resource + * + * @throws IllegalArgumentException if the local or global name is not specified */ public static void registerGlobalResourceAccess(Context globalContext, String localName, String globalName) { validateGlobalContext(globalContext); + if (localName == null || globalName == null) { + throw new IllegalArgumentException( + sm.getString("resourceLinkFactory.nullName", localName, globalName)); + } ClassLoader cl = Thread.currentThread().getContextClassLoader(); globalResourceRegistrations.computeIfAbsent(cl, k -> new ConcurrentHashMap<>()).put(localName, globalName); } diff --git a/test/org/apache/naming/TestNamingContext.java b/test/org/apache/naming/TestNamingContext.java index 576f59fbd9..c3aae8dc30 100644 --- a/test/org/apache/naming/TestNamingContext.java +++ b/test/org/apache/naming/TestNamingContext.java @@ -16,6 +16,8 @@ */ package org.apache.naming; +import java.util.Hashtable; + import javax.naming.Context; import javax.naming.NamingException; @@ -95,6 +97,31 @@ public class TestNamingContext extends TomcatBaseTest { } + @Test + public void testResourceLinkWithoutGlobalName() { + // Use a private context to keep the global context validation out of + // the way, whatever test order is used within the JVM + Context context = new NamingContext(new Hashtable<String,Object>(), "test"); + ResourceLinkFactory.setGlobalContext(context); + + // A resource link without a global name is a configuration error that + // must be reported as such, not as a NullPointerException + try { + ResourceLinkFactory.registerGlobalResourceAccess(context, LOCAL_NAME, null); + Assert.fail("Expected an IllegalArgumentException for the missing global name"); + } catch (IllegalArgumentException iae) { + // Expected + } + + try { + ResourceLinkFactory.registerGlobalResourceAccess(context, null, GLOBAL_NAME); + Assert.fail("Expected an IllegalArgumentException for the missing local name"); + } catch (IllegalArgumentException iae) { + // Expected + } + } + + @Test public void testModuleEquivalentToComp() throws Exception { Tomcat tomcat = getTomcatInstance(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
