You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2026/10/20 21:06:25 UTC

(tomcat) 20/22: Report a resource link which omits the global attribute as an IllegalArgumentException

This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 8947d8fb5251982d8a61423b36b050241531eb2c
Author: opencode <re...@apache.org>
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 1cec1a6131..d7e7d9ef9f 100644
--- a/java/org/apache/naming/factory/LocalStrings.properties
+++ b/java/org/apache/naming/factory/LocalStrings.properties
@@ -35,6 +35,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 5c8f35c48a..725a062cf0 100644
--- a/java/org/apache/naming/factory/ResourceLinkFactory.java
+++ b/java/org/apache/naming/factory/ResourceLinkFactory.java
@@ -73,9 +73,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 64524ec44d..5620d3dc2d 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: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org