From e8e677e4c9d4b0af34dabedb45634987119a2a9a Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sat, 7 Nov 2026 08:33:09 +0100 Subject: [PATCH] Remove connection-specific header fields from HTTP/2 responses RFC 9113, section 8.2.2 forbids an endpoint from generating HTTP/2 messages that contain Connection, Proxy-Connection, Keep-Alive, Transfer-Encoding or Upgrade, and clients such as curl reject a response that does. Requests were already checked, but an application that set one of these header fields on the response had it sent unchanged. Remove them in StreamProcessor.prepareHeaders() and share the list with the request check by moving it to Constants. --- java/org/apache/coyote/http2/Constants.java | 9 ++ java/org/apache/coyote/http2/Stream.java | 12 +-- .../apache/coyote/http2/StreamProcessor.java | 5 + .../coyote/http2/TestHttp2Section_8_2_2.java | 99 +++++++++++++++++++ 4 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 test/org/apache/coyote/http2/TestHttp2Section_8_2_2.java diff --git a/java/org/apache/coyote/http2/Constants.java b/java/org/apache/coyote/http2/Constants.java index ce10dd0e400c..2a9bfcd21525 100644 --- a/java/org/apache/coyote/http2/Constants.java +++ b/java/org/apache/coyote/http2/Constants.java @@ -16,6 +16,8 @@ */ package org.apache.coyote.http2; +import java.util.Set; + /** * HTTP/2 protocol constants. */ @@ -61,6 +63,13 @@ public class Constants { */ static final int DEFAULT_MAX_TRAILER_SIZE = 8 * 1024; + // Header fields + /** + * Connection-specific header fields. RFC 9113, section 8.2.2 forbids them in HTTP/2 messages. + */ + static final Set CONNECTION_SPECIFIC_HEADERS = + Set.of("connection", "proxy-connection", "keep-alive", "transfer-encoding", "upgrade"); + /** * Private constructor to prevent instantiation. */ diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java index 0d19bc7bf74a..335939516ba9 100644 --- a/java/org/apache/coyote/http2/Stream.java +++ b/java/org/apache/coyote/http2/Stream.java @@ -21,9 +21,7 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Collections; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -70,19 +68,11 @@ class Stream extends AbstractNonZeroStream implements HeaderEmitter { private static final Integer HTTP_UPGRADE_STREAM = Integer.valueOf(1); - private static final Set HTTP_CONNECTION_SPECIFIC_HEADERS = new HashSet<>(); - static { Response response = new Response(); response.setStatus(100); StreamProcessor.prepareHeaders(null, response, true, null, null); ACK_HEADERS = response.getMimeHeaders(); - - HTTP_CONNECTION_SPECIFIC_HEADERS.add("connection"); - HTTP_CONNECTION_SPECIFIC_HEADERS.add("proxy-connection"); - HTTP_CONNECTION_SPECIFIC_HEADERS.add("keep-alive"); - HTTP_CONNECTION_SPECIFIC_HEADERS.add("transfer-encoding"); - HTTP_CONNECTION_SPECIFIC_HEADERS.add("upgrade"); } private volatile long contentLengthReceived = 0; @@ -385,7 +375,7 @@ public final void emitHeader(String name, String value) throws HpackException { } - if (HTTP_CONNECTION_SPECIFIC_HEADERS.contains(name)) { + if (Constants.CONNECTION_SPECIFIC_HEADERS.contains(name)) { headerException = new StreamException( sm.getString("stream.header.connection", getConnectionId(), getIdAsString(), name), Http2Error.PROTOCOL_ERROR, getIdAsInt()); diff --git a/java/org/apache/coyote/http2/StreamProcessor.java b/java/org/apache/coyote/http2/StreamProcessor.java index 0283f98b1dbf..8c34233017b2 100644 --- a/java/org/apache/coyote/http2/StreamProcessor.java +++ b/java/org/apache/coyote/http2/StreamProcessor.java @@ -227,6 +227,11 @@ static void prepareHeaders(Request coyoteRequest, Response coyoteResponse, boole // Add the pseudo header for status headers.addValue(":status").setString(Integer.toString(statusCode)); + // RFC 9113, section 8.2.2: HTTP/2 messages must not contain connection-specific header fields. The application + // may have set them, or copied them from an HTTP/1.x response. + for (String name : Constants.CONNECTION_SPECIFIC_HEADERS) { + headers.removeHeader(name); + } // Compression can't be used with sendfile // Need to check for compression (and set headers appropriately) before diff --git a/test/org/apache/coyote/http2/TestHttp2Section_8_2_2.java b/test/org/apache/coyote/http2/TestHttp2Section_8_2_2.java new file mode 100644 index 000000000000..c7caba3895be --- /dev/null +++ b/test/org/apache/coyote/http2/TestHttp2Section_8_2_2.java @@ -0,0 +1,99 @@ +/* + * 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.http2; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; + +/** + * Unit tests for Section 8.2.2 of RFC 9113.
+ * The order of the tests in this class is determined by the order of the requirements in the RFC. + */ +public class TestHttp2Section_8_2_2 extends Http2TestBase { + + /* + * An endpoint MUST NOT generate an HTTP/2 message containing connection-specific header fields. Applications that + * set them (or proxy them from an HTTP/1.x origin) must not cause the response to be malformed. + */ + @Test + public void testConnectionSpecificResponseHeadersRemoved() throws Exception { + enableHttp2(); + + Tomcat tomcat = getTomcatInstance(); + + Context ctxt = getProgrammaticRootContext(); + Tomcat.addServlet(ctxt, "simple", new SimpleServlet()); + ctxt.addServletMapping("/simple", "simple"); + Tomcat.addServlet(ctxt, "connection", new ConnectionHeadersServlet()); + ctxt.addServletMapping("/connection", "connection"); + + tomcat.start(); + + openClientConnection(); + doHttpUpgrade(); + sendClientPreface(); + validateHttp2InitialResponse(); + + byte[] frameHeader = new byte[9]; + ByteBuffer headersPayload = ByteBuffer.allocate(128); + buildGetRequest(frameHeader, headersPayload, null, 3, "/connection"); + writeFrame(frameHeader, headersPayload); + + // Headers + parser.readFrame(); + // Body + parser.readFrame(); + + String trace = output.getTrace(); + Assert.assertTrue(trace, trace.contains("3-Header-[x-other]-[value]\n")); + Assert.assertFalse(trace, trace.contains("3-Header-[connection]-")); + Assert.assertFalse(trace, trace.contains("3-Header-[proxy-connection]-")); + Assert.assertFalse(trace, trace.contains("3-Header-[keep-alive]-")); + Assert.assertFalse(trace, trace.contains("3-Header-[transfer-encoding]-")); + Assert.assertFalse(trace, trace.contains("3-Header-[upgrade]-")); + } + + + private static class ConnectionHeadersServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/plain"); + resp.setCharacterEncoding("UTF-8"); + resp.setHeader("Connection", "close"); + resp.setHeader("Proxy-Connection", "keep-alive"); + resp.setHeader("Keep-Alive", "timeout=5"); + resp.setHeader("Transfer-Encoding", "chunked"); + resp.setHeader("Upgrade", "websocket"); + resp.setHeader("X-Other", "value"); + resp.getWriter().print("OK"); + } + } +}