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 209fa2e3be0325ad37f468d74a7bf240707e8ab3
Author: opencode <[email protected]>
AuthorDate: Mon Oct 19 09:45:57 2026 +0200

    Report a translation error for an invalid TLD jsp-version
    
    Validator.checkXmlAttributes() parsed the required version of the tag
    library with Double.parseDouble() once per attribute and without any
    exception handling. TLD XML validation is off by default and the parsed
    jsp-version value is only null checked, never format validated, so a
    TLD using a value such as "2.1.1" parsed fine and any page using one
    of the library's tags with attributes failed with a raw
    NumberFormatException rather than a JSP translation error. The same
    value is already handled defensively at the other two places where it
    is parsed: Compiler.generateJava() for tag files and the implicit TLD
    handling in ImplicitTagLibraryInfo.
    
    The required version is constant for the tag so the parse is now
    performed once before the attribute loop instead of for every
    attribute. A value that cannot be parsed is reported as a translation
    error at the tag invocation using a new message.
    
    Note that this also reports the invalid version for a tag invocation
    with no attributes, which previously compiled without any error.
    
    Found by code review.
---
 java/org/apache/jasper/compiler/Validator.java     | 12 ++++++-
 .../jasper/resources/LocalStrings.properties       |  1 +
 test/org/apache/jasper/compiler/TestValidator.java | 18 ++++++++++
 test/webapp/WEB-INF/invalid-version.tld            | 38 ++++++++++++++++++++++
 test/webapp/jsp/parsing/taglib-invalid-version.jsp | 24 ++++++++++++++
 5 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/jasper/compiler/Validator.java 
b/java/org/apache/jasper/compiler/Validator.java
index 4d9e4deb37..965f46b2b4 100644
--- a/java/org/apache/jasper/compiler/Validator.java
+++ b/java/org/apache/jasper/compiler/Validator.java
@@ -966,6 +966,17 @@ class Validator {
             TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
             Attributes attrs = n.getAttributes();
 
+            // The required version of the tag library is constant for the tag,
+            // so parse it once rather than for every attribute
+            double libraryVersion = 0;
+            try {
+                libraryVersion = 
Double.parseDouble(tagInfo.getTagLibrary().getRequiredVersion());
+            } catch (NumberFormatException e) {
+                // Never reached: jspError always throws
+                err.jspError(n, "jsp.error.invalid.taglib.version", 
tagInfo.getTagLibrary().getRequiredVersion(),
+                        n.getQName());
+            }
+
             for (int i = 0; attrs != null && i < attrs.getLength(); i++) {
                 boolean found = false;
 
@@ -973,7 +984,6 @@ class Validator {
                         (!n.getRoot().isXmlSyntax() && 
attrs.getValue(i).startsWith("<%=")));
                 boolean elExpression = false;
                 boolean deferred = false;
-                double libraryVersion = 
Double.parseDouble(tagInfo.getTagLibrary().getRequiredVersion());
                 boolean deferredSyntaxAllowedAsLiteral =
                         pageInfo.isDeferredSyntaxAllowedAsLiteral() || 
libraryVersion < 2.1;
 
diff --git a/java/org/apache/jasper/resources/LocalStrings.properties 
b/java/org/apache/jasper/resources/LocalStrings.properties
index d533c92542..bab1da9ea7 100644
--- a/java/org/apache/jasper/resources/LocalStrings.properties
+++ b/java/org/apache/jasper/resources/LocalStrings.properties
@@ -99,6 +99,7 @@ jsp.error.invalid.includeInTagFileJar=The include [{0}] is 
not valid since it is
 jsp.error.invalid.name=File [{0}] uses name [{1}] in jsp:getProperty for a 
bean that was not previously introduced as per JSP.5.3
 jsp.error.invalid.scope=Illegal value of ''scope'' attribute: [{0}] (must be 
one of "page", "request", "session", or "application")
 jsp.error.invalid.tagdir=Tag file directory [{0}] does not start with 
"/WEB-INF/tags"
+jsp.error.invalid.taglib.version=Invalid JSP version [{0}] defined by the tag 
library used by tag [{1}]
 jsp.error.invalid.varscope=Invalid variable scope [{0}]
 jsp.error.invalid.version=Invalid JSP version defined for tag file at [{0}]
 jsp.error.ise_on_clear=Illegal to clear() when buffer size == 0
diff --git a/test/org/apache/jasper/compiler/TestValidator.java 
b/test/org/apache/jasper/compiler/TestValidator.java
index 64ae9b90e0..2e084ab744 100644
--- a/test/org/apache/jasper/compiler/TestValidator.java
+++ b/test/org/apache/jasper/compiler/TestValidator.java
@@ -41,6 +41,24 @@ public class TestValidator extends TomcatBaseTest {
         Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
     }
 
+    @Test
+    public void testInvalidTldVersion() throws Exception {
+        getTomcatInstanceTestWebapp(false, true);
+
+        ByteChunk res = new ByteChunk();
+
+        int rc = getUrl("http://localhost:"; + getPort() + 
"/test/jsp/parsing/taglib-invalid-version.jsp", res, null);
+
+        Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
+
+        // The invalid jsp-version in the TLD must produce a clean translation
+        // error. It previously triggered a raw NumberFormatException at
+        // translation time since the value was parsed without a catch.
+        String result = res.toString();
+        Assert.assertTrue(result, result.contains("Invalid JSP version 
[2.1.1]"));
+        Assert.assertFalse(result, result.contains("NumberFormatException"));
+    }
+
     @Test
     public void testTldVersions22() throws Exception {
         Tomcat tomcat = getTomcatInstance();
diff --git a/test/webapp/WEB-INF/invalid-version.tld 
b/test/webapp/WEB-INF/invalid-version.tld
new file mode 100644
index 0000000000..9e0fc0c3cf
--- /dev/null
+++ b/test/webapp/WEB-INF/invalid-version.tld
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<taglib xmlns="http://java.sun.com/xml/ns/javaee";
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd";
+    version="2.1">
+    <tlib-version>1.0</tlib-version>
+    <short-name>iv</short-name>
+    <!-- The jsp-version element is not part of the TLD 2.1 schema. It is
+         accepted here because TLD XML validation is off by default. The value
+         is not a valid version number. -->
+    <jsp-version>2.1.1</jsp-version>
+    <tag>
+      <name>echo</name>
+      <tag-class>org.apache.jasper.compiler.TestValidator$Echo</tag-class>
+      <body-content>empty</body-content>
+      <attribute>
+        <name>echo</name>
+        <required>false</required>
+        <rtexprvalue>false</rtexprvalue>
+      </attribute>
+    </tag>
+</taglib>
diff --git a/test/webapp/jsp/parsing/taglib-invalid-version.jsp 
b/test/webapp/jsp/parsing/taglib-invalid-version.jsp
new file mode 100644
index 0000000000..47136a9b72
--- /dev/null
+++ b/test/webapp/jsp/parsing/taglib-invalid-version.jsp
@@ -0,0 +1,24 @@
+<%--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" %>
+<%@ taglib uri="/WEB-INF/invalid-version.tld" prefix="iv" %>
+<html>
+  <head><title>Taglib with an invalid TLD version test case</title></head>
+  <body>
+    <iv:echo echo="Should not appear"/>
+  </body>
+</html>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to