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

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

commit 3d82fe760aba983a5d668732d0ef2330925de82f
Author: opencode <[email protected]>
AuthorDate: Wed Oct 14 20:34:50 2026 +0200

    Ignore parameters with no name when parsing a MediaType
    
    A dangling ';' at the end of a media type is invalid but tolerated by
    the parser. Previously it resulted in a phantom parameter with an
    empty name and an empty value being added to the parsed MediaType,
    inflating getParameterCount() (e.g. "text/html;" reported one
    parameter). Trailing ";;" had the same effect, inconsistent with the
    handling of a ";;" sequence in the middle of the value where the null
    attribute is skipped and no parameter is stored.
    
    readToken() only returns the empty string at end of input, so an empty
    attribute can never carry a real name; attributes with an empty name
    are now ignored in the same way as null attributes.
---
 java/org/apache/tomcat/util/http/parser/MediaType.java   |  2 +-
 .../apache/tomcat/util/http/parser/TestMediaType.java    | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/tomcat/util/http/parser/MediaType.java 
b/java/org/apache/tomcat/util/http/parser/MediaType.java
index 473d0402d9..2f2ccdcd5b 100644
--- a/java/org/apache/tomcat/util/http/parser/MediaType.java
+++ b/java/org/apache/tomcat/util/http/parser/MediaType.java
@@ -217,7 +217,7 @@ public class MediaType {
                 value = "";
             }
 
-            if (attribute != null) {
+            if (attribute != null && !attribute.isEmpty()) {
                 parameters.put(attribute.toLowerCase(Locale.ENGLISH), value);
             }
 
diff --git a/test/org/apache/tomcat/util/http/parser/TestMediaType.java 
b/test/org/apache/tomcat/util/http/parser/TestMediaType.java
index 10e896bd26..13b8dffadd 100644
--- a/test/org/apache/tomcat/util/http/parser/TestMediaType.java
+++ b/test/org/apache/tomcat/util/http/parser/TestMediaType.java
@@ -334,7 +334,23 @@ public class TestMediaType {
         StringReader sr = new StringReader("type/sub-type;;a=1;;b=2;;");
         MediaType m = MediaType.parseMediaType(sr);
 
+        Assert.assertEquals(2, m.getParameterCount());
         Assert.assertEquals("1", m.getParameterValue("a"));
         Assert.assertEquals("2", m.getParameterValue("b"));
     }
+
+    @Test
+    public void testTrailingSemicolon() throws Exception {
+        // A dangling ';' is invalid but tolerated. It must not result in a
+        // phantom parameter with an empty name.
+        StringReader sr = new StringReader("type/sub-type;charset=UTF-8;");
+        MediaType m = MediaType.parseMediaType(sr);
+
+        Assert.assertNotNull(m);
+        Assert.assertEquals(1, m.getParameterCount());
+        Assert.assertNull(m.getParameterValue(""));
+        Assert.assertEquals("UTF-8", m.getParameterValue("charset"));
+        Assert.assertEquals("type/sub-type;charset=UTF-8", m.toString());
+        Assert.assertEquals("type/sub-type", m.toStringNoCharset());
+    }
 }


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

Reply via email to