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 7e9be99b129fa45ec85f5e32339c69066931332b
Author: opencode <[email protected]>
AuthorDate: Wed Oct 14 20:01:42 2026 +0200

    Omit invalid parameter values when serializing a MediaType without charset
    
    toString() skipped parameters with a null or empty value but
    toStringNoCharset() did not, so a media type such as
    "text/html;charset=utf-8;foo=" re-serialised by Response with the
    literal string "foo=null" (null values occur for unterminated quoted
    strings and mid-string empty values) or with an empty "foo=". The
    corrupted value was then written to the Content-Type response header.
    
    toStringNoCharset() now applies the same null/empty value guard as
    toString(). Callers (coyote Response.setContentType and
    MediaTypeCache used by catalina connector Response) only use the
    no-charset serialisation when a charset is present, so this simply
    removes the corruption; programmatic access to the parameters via
    getParameterValue() is unchanged.
---
 java/org/apache/tomcat/util/http/parser/MediaType.java    |  6 +++++-
 .../org/apache/tomcat/util/http/parser/TestMediaType.java | 15 +++++++++++++++
 2 files changed, 20 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 d0a54682af..473d0402d9 100644
--- a/java/org/apache/tomcat/util/http/parser/MediaType.java
+++ b/java/org/apache/tomcat/util/http/parser/MediaType.java
@@ -155,10 +155,14 @@ public class MediaType {
                         if (entry.getKey().equalsIgnoreCase("charset")) {
                             continue;
                         }
+                        String value = entry.getValue();
+                        if (value == null || value.isEmpty()) {
+                            continue;
+                        }
                         result.append(';');
                         result.append(entry.getKey());
                         result.append('=');
-                        result.append(entry.getValue());
+                        result.append(value);
                     }
 
                     noCharset = result.toString();
diff --git a/test/org/apache/tomcat/util/http/parser/TestMediaType.java 
b/test/org/apache/tomcat/util/http/parser/TestMediaType.java
index 85904cda58..10e896bd26 100644
--- a/test/org/apache/tomcat/util/http/parser/TestMediaType.java
+++ b/test/org/apache/tomcat/util/http/parser/TestMediaType.java
@@ -210,10 +210,25 @@ public class TestMediaType {
 
         // Note: Invalid input is filtered out
         Assert.assertEquals("text/html;charset=UTF-8", m.toString());
+        Assert.assertEquals("text/html", m.toStringNoCharset());
         Assert.assertEquals("UTF-8", m.getCharset());
     }
 
 
+    @Test
+    public void testToStringWithInvalidParameterValues() throws IOException {
+        // Parameters with an empty value or a null value (unterminated quoted
+        // string) must be omitted from the string representations.
+        String input = "type/sub-type;charset=UTF-8;a=;b=\"unterminated;c=";
+
+        StringReader sr = new StringReader(input);
+        MediaType m = MediaType.parseMediaType(sr);
+
+        Assert.assertEquals("type/sub-type;charset=UTF-8", m.toString());
+        Assert.assertEquals("type/sub-type", m.toStringNoCharset());
+    }
+
+
     @Test
     public void testBug55454() throws IOException {
         String input = "text/html;;charset=UTF-8";


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

Reply via email to