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

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

commit b72f3bc742afbd3f683b515f1c0608bf5f0edabe
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 f4466a8f40..5f4bf61490 100644
--- a/java/org/apache/jasper/compiler/Validator.java
+++ b/java/org/apache/jasper/compiler/Validator.java
@@ -903,6 +903,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;
 
@@ -910,7 +921,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 b7a4b65fb4..d97a6dd522 100644
--- a/java/org/apache/jasper/resources/LocalStrings.properties
+++ b/java/org/apache/jasper/resources/LocalStrings.properties
@@ -98,6 +98,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 a7d060d05a..21bdfea3b5 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