(tomcat) branch 9.0.x updated: Fix matching of the pattern documented in the javadoc

[email protected] Fri, 31 Jul 2026 09:39:53 +0000
Newsgroups gmane.comp.jakarta.tomcat.devel
Message-ID <178549079315.3725889.3252352668378941113@gitbox3-he-fi.apache.org>
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


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 270f9a2268 Fix matching of the pattern documented in the javadoc
270f9a2268 is described below

commit 270f9a226838d093862c167d03270ab04dda927f
Author: remm <[email protected]>
AuthorDate: Fri Jul 31 11:35:54 2026 +0200

    Fix matching of the pattern documented in the javadoc
    
    Also add a test case for the noCompressionUserAgents feature, coauthored
    with OpenCode.
---
 java/org/apache/coyote/CompressionConfig.java      |  2 +-
 .../coyote/TestCompressionConfigUserAgents.java    | 67 ++++++++++++++++++++++
 webapps/docs/changelog.xml                         |  6 ++
 3 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/coyote/CompressionConfig.java b/java/org/apache/coyote/CompressionConfig.java
index 9b91930717..2beca97827 100644
--- a/java/org/apache/coyote/CompressionConfig.java
+++ b/java/org/apache/coyote/CompressionConfig.java
@@ -409,7 +409,7 @@ public class CompressionConfig {
                 MessageBytes userAgentValueMB = request.getMimeHeaders().getValue("user-agent");
                 if (userAgentValueMB != null) {
                     String userAgentValue = userAgentValueMB.toString();
-                    if (noCompressionUserAgents.matcher(userAgentValue).matches()) {
+                    if (noCompressionUserAgents.matcher(userAgentValue).find()) {
                         return false;
                     }
                 }
diff --git a/test/org/apache/coyote/TestCompressionConfigUserAgents.java b/test/org/apache/coyote/TestCompressionConfigUserAgents.java
new file mode 100644
index 0000000000..43f988533f
--- /dev/null
+++ b/test/org/apache/coyote/TestCompressionConfigUserAgents.java
@@ -0,0 +1,67 @@
+/*
+ *  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.
+ */
+package org.apache.coyote;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestCompressionConfigUserAgents {
+
+    @Test
+    public void testNoCompressionUserAgents() {
+        CompressionConfig config = new CompressionConfig();
+        config.setNoCompressionUserAgents("gorilla|MSIE|tigrus");
+
+        Request request = new Request();
+        request.getMimeHeaders().addValue("accept-encoding").setString("gzip");
+        Response response;
+
+        // Force mode (compressionLevel == 2) skips the user-agent check,
+        // so use "on" mode where the check applies
+        config.setCompression("on");
+
+        // User-agent matching the pattern should not be compressed
+        response = createResponse();
+        request.getMimeHeaders().addValue("user-agent").setString("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
+        Assert.assertFalse(config.useCompression(request, response));
+
+        // No user-agent header should be compressed
+        response = createResponse();
+        request.getMimeHeaders().removeHeader("user-agent");
+        Assert.assertTrue(config.useCompression(request, response));
+
+        // User-agent not matching the pattern should be compressed
+        response = createResponse();
+        request.getMimeHeaders().removeHeader("user-agent");
+        request.getMimeHeaders().addValue("user-agent").setString("Mozilla/5.0 (X11; Linux x86_64)");
+        Assert.assertTrue(config.useCompression(request, response));
+
+        // Force mode skips the user-agent check
+        response = createResponse();
+        config.setCompression("force");
+        request.getMimeHeaders().removeHeader("user-agent");
+        request.getMimeHeaders().addValue("user-agent").setString("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
+        Assert.assertTrue(config.useCompression(request, response));
+    }
+
+    private Response createResponse() {
+        Response response = new Response();
+        response.setContentLength(4096);
+        response.setContentType("text/html");
+        return response;
+    }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 7002dcced3..a70d39f86b 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -190,6 +190,12 @@
         of how early in the HEADERS frame processing an error is detected.
         (markt)
       </fix>
+      <fix>
+        Fix matching the compression config
+        <code>noCompressionUserAgents</code> with patterns of the style
+        of the example <code>gorilla|desesplorer|tigrus</code> pattern
+        documented in the javadoc. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">