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"); + } + } +}