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

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

commit 4c26e32c20b44c5e959b193028245a773d5b3aba
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Nov 17 16:20:30 2026 +0000

    Follow-up to PR #1154
    
    Add a change log entry
    
    Stricter pseudo header checks
    
    Share header field value sanitising and avoid allocations
    Move the HTTP/2 header value sanitising to HeaderUtil.sanitizeFieldValue()
    and use it for HTTP/2 response headers and for trailer fields. A trailer
    field with a name that is not a token or a value that contains a character
    above 255 is now dropped rather than having characters replaced, and
    trailer values are trimmed. This is consistent with HTTP/2 headers.
    Avoid allocating when validating valid HTTP/2 response headers by checking
    byte based names and values directly, and make HttpParser.isToken(String)
    use charAt() rather than copying the String to a char[].
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 .../coyote/http11/filters/ChunkedOutputFilter.java |  41 ++-----
 .../apache/coyote/http2/Http2UpgradeHandler.java   | 103 ++++++++++-------
 java/org/apache/tomcat/util/http/HeaderUtil.java   |  51 +++++++++
 .../apache/tomcat/util/http/parser/HttpParser.java |   5 +-
 .../http11/filters/TestChunkedOutputFilter.java    | 123 ---------------------
 .../coyote/http2/TestHttp2Section_8_2_1.java       |  71 ++++++++++++
 .../http/TestHeaderUtilSanitizeFieldValue.java     | 119 ++++++++++++++++++++
 webapps/docs/changelog.xml                         |   4 +
 8 files changed, 322 insertions(+), 195 deletions(-)

diff --git a/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java 
b/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
index 6e20ea9a6b..b56bb3759b 100644
--- a/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
+++ b/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
@@ -29,6 +29,7 @@ import org.apache.coyote.http11.HttpOutputBuffer;
 import org.apache.coyote.http11.OutputFilter;
 import org.apache.tomcat.util.buf.HexUtils;
 import org.apache.tomcat.util.http.HeaderUtil;
+import org.apache.tomcat.util.http.parser.HttpParser;
 
 /**
  * Chunked output filter.
@@ -174,10 +175,19 @@ public class ChunkedOutputFilter implements OutputFilter {
                     if 
(HeaderUtil.isHeaderDisallowedInTrailers(trailerField.getKey())) {
                         continue;
                     }
-                    osw.write(filterForHeaders(trailerField.getKey()));
+                    // Ignore invalid field names
+                    if (!HttpParser.isToken(trailerField.getKey())) {
+                        continue;
+                    }
+                    // Correct the value or ignore the field if the value 
can't be sent
+                    String value = 
HeaderUtil.sanitizeFieldValue(trailerField.getValue());
+                    if (value == null) {
+                        continue;
+                    }
+                    osw.write(trailerField.getKey());
                     osw.write(':');
                     osw.write(' ');
-                    osw.write(filterForHeaders(trailerField.getValue()));
+                    osw.write(value);
                     osw.write("\r\n");
                 }
             }
@@ -194,33 +204,6 @@ public class ChunkedOutputFilter implements OutputFilter {
     }
 
 
-    /*
-     * Filters out CTLs excluding TAB and any code points above 255 (since 
this is meant to be ISO-8859-1).
-     *
-     * This doesn't perform full HTTP validation. For example, it does not 
limit field names to tokens.
-     *
-     * Strictly, correct trailer fields is an application concern. The 
filtering here is a basic attempt to help
-     * mis-behaving applications prevent the worst of the potential 
side-effects of invalid trailer fields.
-     */
-    // package private so it is visible for testing
-    static String filterForHeaders(String input) {
-        char[] chars = input.toCharArray();
-        boolean updated = false;
-        for (int i = 0; i < chars.length; i++) {
-            if (chars[i] < 32 && chars [i] != 9 || chars[i] == 127 || chars[i] 
> 255) {
-                chars[i] = ' ';
-                updated = true;
-            }
-        }
-
-        if (updated) {
-            return new String(chars);
-        } else {
-            return input;
-        }
-    }
-
-
     @Override
     public void recycle() {
         response = null;
diff --git a/java/org/apache/coyote/http2/Http2UpgradeHandler.java 
b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
index 845dbb73e0..c48942684c 100644
--- a/java/org/apache/coyote/http2/Http2UpgradeHandler.java
+++ b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
@@ -48,6 +48,9 @@ import org.apache.coyote.http2.Http2Parser.Output;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.buf.MessageBytes;
+import org.apache.tomcat.util.http.HeaderUtil;
 import org.apache.tomcat.util.http.MimeHeaders;
 import org.apache.tomcat.util.http.parser.HttpParser;
 import org.apache.tomcat.util.http.parser.Priority;
@@ -116,6 +119,7 @@ class Http2UpgradeHandler extends AbstractStream implements 
InternalHttpUpgradeH
     protected static final byte[] GOAWAY = { 0x07, 0x00, 0x00, 0x00, 0x00, 
0x00 };
 
     private static final String HTTP2_SETTINGS_HEADER = "HTTP2-Settings";
+    private static final String STATUS_PSEUDO_HEADER = ":status";
 
     /**
      * Default header sink for HPACK decoding.
@@ -926,26 +930,41 @@ class Http2UpgradeHandler extends AbstractStream 
implements InternalHttpUpgradeH
      * Removes the header fields that cannot be sent in a valid HTTP/2 
message, as HTTP/1.1 does. A header field with a
      * name that is not a token, or with a value that contains a character 
that is not in the range 0 to 255, would
      * otherwise make the encoder fail after it had updated its dynamic table. 
Control characters in a value are
-     * replaced with a space and leading and trailing whitespace is removed 
(RFC 9113, section 8.2.1).
+     * replaced with a space and leading and trailing whitespace is removed 
(RFC 9113, section 8.2.1). The only pseudo
+     * header field permitted is a single :status, which is generated by 
Tomcat. Pseudo header fields set by the
+     * application are removed.
      *
      * @param stream  the stream the headers are written to
      * @param headers the headers to check. Invalid header fields are removed 
and invalid values are corrected.
      */
     private void removeInvalidHeaders(Stream stream, MimeHeaders headers) {
         for (int i = 0; i < headers.size(); i++) {
+            // Fast path for the normal case. Valid header fields are not 
converted to Strings. A pseudo header field
+            // name is not a token so it always takes the slow path.
+            if (isValidHeaderName(headers.getName(i)) && 
isValidHeaderValue(headers.getValue(i))) {
+                continue;
+            }
             String name = headers.getName(i).toString();
+            String value = headers.getValue(i).toString();
             if (name.startsWith(":")) {
-                // Pseudo header fields are always generated by Tomcat
+                // The only pseudo header field in a response is :status and 
Tomcat adds it after any application
+                // provided header fields. Any other pseudo header field, or 
an earlier :status, was set by the
+                // application and is not valid.
+                if (STATUS_PSEUDO_HEADER.equals(name) && 
headers.findHeader(STATUS_PSEUDO_HEADER, i + 1) == -1) {
+                    continue;
+                }
+                log.warn(sm.getString("upgradeHandler.response.invalidHeader", 
connectionId, stream.getIdAsString(),
+                        name, value));
+                headers.removeHeader(i--);
                 continue;
             }
-            String value = headers.getValue(i).toString();
-            String validValue = HttpParser.isToken(name) ? 
sanitizeHeaderValue(value) : null;
+            String validValue = HttpParser.isToken(name) ? 
HeaderUtil.sanitizeFieldValue(value) : null;
             if (validValue == null) {
                 log.warn(sm.getString("upgradeHandler.response.invalidHeader", 
connectionId, stream.getIdAsString(),
                         name, value));
                 headers.removeHeader(i--);
             } else if (validValue != value) {
-                // sanitizeHeaderValue() returns the same instance if the 
value was valid
+                // HeaderUtil.sanitizeFieldValue() returns the same instance 
if the value was valid
                 headers.getValue(i).setString(validValue);
             }
         }
@@ -953,48 +972,50 @@ class Http2UpgradeHandler extends AbstractStream 
implements InternalHttpUpgradeH
 
 
     /**
-     * Corrects a header value so that it is valid in an HTTP/2 message.
-     *
-     * @param value the value to correct
-     *
-     * @return the corrected value or {@code null} if the value contains a 
character that cannot be sent
+     * Checks, without converting byte based data to a String, that a header 
name is a token.
      */
-    static String sanitizeHeaderValue(String value) {
-        int length = value.length();
-        boolean valid = true;
-        if (length > 0 &&
-                (HttpParser.isWhiteSpace(value.charAt(0)) || 
HttpParser.isWhiteSpace(value.charAt(length - 1)))) {
-            valid = false;
-        }
-        for (int i = 0; i < length; i++) {
-            char c = value.charAt(i);
-            if (c > 255) {
-                return null;
-            }
-            // Control characters other than tab are not valid
-            if (!HttpParser.isFieldContent(c)) {
-                valid = false;
+    private static boolean isValidHeaderName(MessageBytes name) {
+        if (name.getType() == MessageBytes.T_BYTES) {
+            ByteChunk bc = name.getByteChunk();
+            if (bc.getLength() == 0) {
+                return false;
+            }
+            byte[] bytes = bc.getBuffer();
+            for (int i = bc.getStart(); i < bc.getEnd(); i++) {
+                if (!HttpParser.isToken(bytes[i] & 0xFF)) {
+                    return false;
+                }
             }
+            return true;
         }
-        if (valid) {
-            return value;
-        }
+        return HttpParser.isToken(name.toString());
+    }
+
 
-        char[] result = value.toCharArray();
-        int first = 0;
-        int last = length;
-        for (int i = 0; i < length; i++) {
-            if (!HttpParser.isFieldContent(result[i])) {
-                result[i] = ' ';
+    /**
+     * Checks, without converting byte based data to a String, that a header 
value needs no correction.
+     */
+    private static boolean isValidHeaderValue(MessageBytes value) {
+        if (value.getType() == MessageBytes.T_BYTES) {
+            ByteChunk bc = value.getByteChunk();
+            int start = bc.getStart();
+            int end = bc.getEnd();
+            if (start == end) {
+                return true;
+            }
+            byte[] bytes = bc.getBuffer();
+            if (HttpParser.isWhiteSpace(bytes[start] & 0xFF) || 
HttpParser.isWhiteSpace(bytes[end - 1] & 0xFF)) {
+                return false;
+            }
+            for (int i = start; i < end; i++) {
+                if (!HttpParser.isFieldContent(bytes[i] & 0xFF)) {
+                    return false;
+                }
             }
+            return true;
         }
-        while (first < last && HttpParser.isWhiteSpace(result[first])) {
-            first++;
-        }
-        while (last > first && HttpParser.isWhiteSpace(result[last - 1])) {
-            last--;
-        }
-        return new String(result, first, last - first);
+        String s = value.toString();
+        return s != null && HeaderUtil.sanitizeFieldValue(s) == s;
     }
 
 
diff --git a/java/org/apache/tomcat/util/http/HeaderUtil.java 
b/java/org/apache/tomcat/util/http/HeaderUtil.java
index 2dfc63a001..08828d8b77 100644
--- a/java/org/apache/tomcat/util/http/HeaderUtil.java
+++ b/java/org/apache/tomcat/util/http/HeaderUtil.java
@@ -20,6 +20,8 @@ import java.util.HashSet;
 import java.util.Locale;
 import java.util.Set;
 
+import org.apache.tomcat.util.http.parser.HttpParser;
+
 /**
  * Utility methods for HTTP header processing.
  */
@@ -63,6 +65,55 @@ public class HeaderUtil {
     }
 
 
+    /**
+     * Corrects an HTTP header field value so that it is valid when sent as 
ISO-8859-1. Control characters other than
+     * tab, and DEL, are replaced with a space and leading and trailing 
whitespace is removed. A value that does not
+     * need to be corrected is returned unchanged without being copied.
+     *
+     * @param value The value to correct
+     *
+     * @return The same instance if the value was valid, the corrected value 
if it was not or {@code null} if the value
+     *             contains a character that is not in the range 0 to 255 and 
so cannot be sent
+     */
+    public static String sanitizeFieldValue(String value) {
+        int length = value.length();
+        boolean valid = true;
+        if (length > 0 &&
+                (HttpParser.isWhiteSpace(value.charAt(0)) || 
HttpParser.isWhiteSpace(value.charAt(length - 1)))) {
+            valid = false;
+        }
+        for (int i = 0; i < length; i++) {
+            char c = value.charAt(i);
+            if (c > 255) {
+                return null;
+            }
+            // Control characters other than tab are not valid
+            if (!HttpParser.isFieldContent(c)) {
+                valid = false;
+            }
+        }
+        if (valid) {
+            return value;
+        }
+
+        char[] result = value.toCharArray();
+        int first = 0;
+        int last = length;
+        for (int i = 0; i < length; i++) {
+            if (!HttpParser.isFieldContent(result[i])) {
+                result[i] = ' ';
+            }
+        }
+        while (first < last && HttpParser.isWhiteSpace(result[first])) {
+            first++;
+        }
+        while (last > first && HttpParser.isWhiteSpace(result[last - 1])) {
+            last--;
+        }
+        return new String(result, first, last - first);
+    }
+
+
     /**
      * Converts an HTTP header line in byte form to a printable String. Bytes 
corresponding to visible ASCII characters
      * will be converted to those characters. All other bytes (0x00 to 0x1F, 
0x7F to 0xFF) will be represented in 0xNN
diff --git a/java/org/apache/tomcat/util/http/parser/HttpParser.java 
b/java/org/apache/tomcat/util/http/parser/HttpParser.java
index b0429a0688..9ee2541181 100644
--- a/java/org/apache/tomcat/util/http/parser/HttpParser.java
+++ b/java/org/apache/tomcat/util/http/parser/HttpParser.java
@@ -303,8 +303,9 @@ public class HttpParser {
         if (s.isEmpty()) {
             return false;
         }
-        for (char c : s.toCharArray()) {
-            if (!isToken(c)) {
+        // Use charAt() rather than toCharArray() to avoid copying the String
+        for (int i = 0; i < s.length(); i++) {
+            if (!isToken(s.charAt(i))) {
                 return false;
             }
         }
diff --git a/test/org/apache/coyote/http11/filters/TestChunkedOutputFilter.java 
b/test/org/apache/coyote/http11/filters/TestChunkedOutputFilter.java
deleted file mode 100644
index 71be41180a..0000000000
--- a/test/org/apache/coyote/http11/filters/TestChunkedOutputFilter.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- *  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.http11.filters;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-
-@RunWith(Parameterized.class)
-public class TestChunkedOutputFilter {
-
-    private final String VALID_STRING = "aaa";
-
-    @Parameterized.Parameters(name = "{index}")
-    public static Collection<Object[]> parameters() {
-        List<Object[]> parameterSets = new ArrayList<>();
-
-        for (int i = 0; i < 500; i++) {
-            Boolean valid;
-            if (i < 32 && i != 9 || i == 127 || i > 255) {
-                valid = Boolean.FALSE;
-            } else {
-                valid = Boolean.TRUE;
-            }
-            parameterSets.add(new Object[] { Character.valueOf((char) i), 
valid});
-        }
-        return parameterSets;
-    }
-
-
-    @Parameter(0)
-    public Character charUnderTest;
-    @Parameter(1)
-    public boolean valid;
-
-
-    @Test
-    public void testAtStart() {
-        StringBuilder sb = new StringBuilder(4);
-        sb.append(charUnderTest);
-        sb.append(VALID_STRING);
-
-        String result = ChunkedOutputFilter.filterForHeaders(sb.toString());
-
-        String expected;
-        if (valid) {
-            expected = sb.toString();
-        } else {
-            StringBuilder esb = new StringBuilder(4);
-            esb.append(' ');
-            esb.append(VALID_STRING);
-            expected = esb.toString();
-        }
-
-        Assert.assertEquals(expected, result);
-    }
-
-
-    @Test
-    public void testInMiddle() {
-        StringBuilder sb = new StringBuilder(4);
-        sb.append(VALID_STRING);
-        sb.append(charUnderTest);
-        sb.append(VALID_STRING);
-
-        String result = ChunkedOutputFilter.filterForHeaders(sb.toString());
-
-        String expected;
-        if (valid) {
-            expected = sb.toString();
-        } else {
-            StringBuilder esb = new StringBuilder(4);
-            esb.append(VALID_STRING);
-            esb.append(' ');
-            esb.append(VALID_STRING);
-            expected = esb.toString();
-        }
-
-        Assert.assertEquals(expected, result);
-    }
-
-
-    @Test
-    public void testAtEnd() {
-        StringBuilder sb = new StringBuilder(4);
-        sb.append(VALID_STRING);
-        sb.append(charUnderTest);
-
-        String result = ChunkedOutputFilter.filterForHeaders(sb.toString());
-
-        String expected;
-        if (valid) {
-            expected = sb.toString();
-        } else {
-            StringBuilder esb = new StringBuilder(4);
-            esb.append(VALID_STRING);
-            esb.append(' ');
-            expected = esb.toString();
-        }
-
-        Assert.assertEquals(expected, result);
-    }
-}
diff --git a/test/org/apache/coyote/http2/TestHttp2Section_8_2_1.java 
b/test/org/apache/coyote/http2/TestHttp2Section_8_2_1.java
index 1f070e1354..3b8d83bd35 100644
--- a/test/org/apache/coyote/http2/TestHttp2Section_8_2_1.java
+++ b/test/org/apache/coyote/http2/TestHttp2Section_8_2_1.java
@@ -90,6 +90,58 @@ public class TestHttp2Section_8_2_1 extends Http2TestBase {
     }
 
 
+    /*
+     * Pseudo header fields set by the application must not be sent. The only 
pseudo header field in a response is the
+     * :status that Tomcat generates and it must appear exactly once.
+     */
+    @Test
+    public void testApplicationSetPseudoHeaders() throws Exception {
+        enableHttp2();
+
+        Tomcat tomcat = getTomcatInstance();
+
+        Context ctxt = getProgrammaticRootContext();
+        Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
+        ctxt.addServletMapping("/simple", "simple");
+        Tomcat.addServlet(ctxt, "pseudo", new PseudoHeadersServlet());
+        ctxt.addServletMapping("/pseudo", "pseudo");
+
+        tomcat.start();
+
+        openClientConnection();
+        doHttpUpgrade();
+        sendClientPreface();
+        validateHttp2InitialResponse();
+
+        // Repeat to check that the HPACK dynamic table is still consistent
+        for (int streamId = 3; streamId < 7; streamId += 2) {
+            byte[] frameHeader = new byte[9];
+            ByteBuffer headersPayload = ByteBuffer.allocate(128);
+            buildGetRequest(frameHeader, headersPayload, null, streamId, 
"/pseudo");
+            writeFrame(frameHeader, headersPayload);
+
+            // Headers
+            parser.readFrame();
+            // Body
+            parser.readFrame();
+
+            String id = Integer.toString(streamId);
+            String trace = output.getTrace();
+            // Exactly one :status and it is the one generated by Tomcat
+            Assert.assertTrue(trace, trace.contains(id + 
"-Header-[:status]-[200]\n"));
+            Assert.assertEquals(trace, 1, trace.split(":status", -1).length - 
1);
+            Assert.assertFalse(trace, trace.contains("-[999]"));
+            Assert.assertFalse(trace, trace.contains("x-pseudo"));
+            Assert.assertFalse(trace, trace.contains(":authority"));
+            Assert.assertFalse(trace, trace.contains(":STATUS"));
+            Assert.assertTrue(trace, trace.contains(id + 
"-Header-[x-last]-[ok]\n"));
+            Assert.assertTrue(trace, trace.contains(id + "-Body-2\n"));
+            Assert.assertTrue(trace, trace.contains(id + "-EndOfStream\n"));
+            output.clearTrace();
+        }
+    }
+
+
     private static class InvalidHeadersServlet extends HttpServlet {
 
         private static final long serialVersionUID = 1L;
@@ -111,4 +163,23 @@ public class TestHttp2Section_8_2_1 extends Http2TestBase {
             resp.getWriter().print("OK");
         }
     }
+
+
+    private static class PseudoHeadersServlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {
+            resp.setContentType("text/plain");
+            resp.setHeader(":X-Pseudo", "value");
+            resp.setHeader(":x-pseudo", "value");
+            resp.setHeader(":authority", "example.com");
+            // Duplicates the :status that Tomcat generates
+            resp.setHeader(":status", "999");
+            resp.setHeader(":STATUS", "999");
+            resp.setHeader("X-Last", "ok");
+            resp.getWriter().print("OK");
+        }
+    }
 }
diff --git 
a/test/org/apache/tomcat/util/http/TestHeaderUtilSanitizeFieldValue.java 
b/test/org/apache/tomcat/util/http/TestHeaderUtilSanitizeFieldValue.java
new file mode 100644
index 0000000000..db1f1b0c61
--- /dev/null
+++ b/test/org/apache/tomcat/util/http/TestHeaderUtilSanitizeFieldValue.java
@@ -0,0 +1,119 @@
+/*
+ *  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.tomcat.util.http;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+
+/**
+ * Tests {@link HeaderUtil#sanitizeFieldValue(String)} for every character 
from 0 to 499.
+ */
+@RunWith(Parameterized.class)
+public class TestHeaderUtilSanitizeFieldValue {
+
+    private static final String VALID_STRING = "aaa";
+
+    @Parameterized.Parameters(name = "{index}")
+    public static Collection<Object[]> parameters() {
+        List<Object[]> parameterSets = new ArrayList<>();
+
+        for (int i = 0; i < 500; i++) {
+            parameterSets.add(new Object[] { Character.valueOf((char) i) });
+        }
+        return parameterSets;
+    }
+
+
+    @Parameter(0)
+    public Character charUnderTest;
+
+
+    private boolean isCtl() {
+        char c = charUnderTest.charValue();
+        return c < 32 && c != 9 || c == 127;
+    }
+
+
+    private boolean isTooBig() {
+        return charUnderTest.charValue() > 255;
+    }
+
+
+    private boolean isWhitespace() {
+        char c = charUnderTest.charValue();
+        return c == 9 || c == 32;
+    }
+
+
+    private void doTest(String input, String corrected, boolean edge) {
+        String result = HeaderUtil.sanitizeFieldValue(input);
+
+        if (isTooBig()) {
+            Assert.assertNull(result);
+        } else if (isCtl() || edge && isWhitespace()) {
+            Assert.assertEquals(corrected, result);
+        } else {
+            // Valid character. The value must be returned as is.
+            Assert.assertSame(input, result);
+        }
+    }
+
+
+    @Test
+    public void testAtStart() {
+        // A CTL is replaced with a space and whitespace is trimmed
+        doTest(charUnderTest + VALID_STRING, VALID_STRING, true);
+    }
+
+
+    @Test
+    public void testInMiddle() {
+        doTest(VALID_STRING + charUnderTest + VALID_STRING, VALID_STRING + ' ' 
+ VALID_STRING, false);
+    }
+
+
+    @Test
+    public void testAtEnd() {
+        // A CTL is replaced with a space and whitespace is trimmed
+        doTest(VALID_STRING + charUnderTest, VALID_STRING, true);
+    }
+
+
+    @Test
+    public void testEmpty() {
+        Assert.assertEquals("", HeaderUtil.sanitizeFieldValue(""));
+    }
+
+
+    @Test
+    public void testWhitespaceOnly() {
+        Assert.assertEquals("", HeaderUtil.sanitizeFieldValue(" \t "));
+    }
+
+
+    @Test
+    public void testTrim() {
+        Assert.assertEquals("a b", HeaderUtil.sanitizeFieldValue(" \ta b\t "));
+    }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 5e71582439..fd023d96f4 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -189,6 +189,10 @@
         Accept an unknown complete length in the <code>Content-Range</code>.
         header. Pull request <pr>1107</pr> provided by Arturo Bernal. (markt)
       </fix>
+      <fix>
+        Add HTTP/2 response header filtering similar to that provided for
+        HTTP/1.1. Pull request <pr>1154</pr> provided by Arturo Bernal. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


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

Reply via email to