From 2e14b9c3cb3857d1abe240f6c2ef7d617a56369b Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 16 Nov 2026 16:43:35 +0100 Subject: [PATCH] Reset an HTTP/2 stream rather than end it short of the content-length RFC 9113, section 8.1.1 states that a response is malformed if the value of the content-length header field is not the sum of the DATA frame payloads. An application that ended its response having written fewer bytes than the content length it had set had the stream ended normally, so the client received a truncated response that looked complete. Reset the stream with PROTOCOL_ERROR in that case so that the client can tell the response is incomplete. Responses that have no body, such as those to HEAD requests, are not affected. --- .../coyote/http2/LocalStrings.properties | 1 + java/org/apache/coyote/http2/Stream.java | 14 ++++ .../coyote/http2/TestStreamProcessor.java | 75 +++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/java/org/apache/coyote/http2/LocalStrings.properties b/java/org/apache/coyote/http2/LocalStrings.properties index c7deec4db53f..3ad1826924ef 100644 --- a/java/org/apache/coyote/http2/LocalStrings.properties +++ b/java/org/apache/coyote/http2/LocalStrings.properties @@ -119,6 +119,7 @@ stream.inputBuffer.reset=Stream reset stream.inputBuffer.signal=Data added to inBuffer when read thread is waiting. Signalling that thread to continue stream.inputBuffer.swallowUnread=Swallowing [{0}] bytes previously read into input stream buffer stream.notWritable=Connection [{0}], Stream [{1}], This stream is not writable +stream.outputBuffer.contentLength=Connection [{0}], Stream [{1}], The response was ended after [{3}] bytes of body but its content length header value is [{2}] stream.outputBuffer.flush.debug=Connection [{0}], Stream [{1}], flushing output with buffer at position [{2}], writeInProgress [{3}] and closed [{4}] stream.recycle.duplicate=Connection [{0}], Stream [{1}] Duplicate request to recycle the associated request and response has been ignored stream.recycle.first=Connection [{0}], Stream [{1}] The associated request and response have been recycled diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java index 0d19bc7bf74a..07f6dae08f76 100644 --- a/java/org/apache/coyote/http2/Stream.java +++ b/java/org/apache/coyote/http2/Stream.java @@ -1222,6 +1222,20 @@ public final void end() throws IOException { } if (!closed) { closed = true; + /* + * Ending the stream with less data than the content-length header field of the response states + * would present a truncated response as a complete one. Reset the stream instead. A response that is + * defined to have no body has already closed the buffer so it never gets here (RFC 9113, section + * 8.1.1). + */ + long contentLength = coyoteResponse.getContentLengthLong(); + if (contentLength > -1 && written < contentLength) { + reset = new StreamException( + sm.getString("stream.outputBuffer.contentLength", getConnectionId(), getIdAsString(), + Long.valueOf(contentLength), Long.valueOf(written)), + Http2Error.PROTOCOL_ERROR, getIdAsInt()); + throw new CloseNowException(reset); + } flush(true); writeTrailers(); } diff --git a/test/org/apache/coyote/http2/TestStreamProcessor.java b/test/org/apache/coyote/http2/TestStreamProcessor.java index 47eb991a9ad4..28c18b0d0bd4 100644 --- a/test/org/apache/coyote/http2/TestStreamProcessor.java +++ b/test/org/apache/coyote/http2/TestStreamProcessor.java @@ -90,6 +90,59 @@ public void testAsyncComplete() throws Exception { } + /* + * RFC 9113, section 8.1.1 states that a response is malformed if the value of the content-length header field is + * not the sum of the DATA frame payloads. Ending the stream normally would present a truncated response as a + * complete one so the stream is reset. + */ + @Test + public void testResponseShorterThanContentLength() throws Exception { + doTestResponseShorterThanContentLength(false); + } + + + @Test + public void testResponseShorterThanContentLengthFlushed() throws Exception { + doTestResponseShorterThanContentLength(true); + } + + + private void doTestResponseShorterThanContentLength(boolean flush) throws Exception { + enableHttp2(); + + Tomcat tomcat = getTomcatInstance(); + + // Map the servlet to /simple so we can re-use the HTTP/2 handling logic from the super class. + Context ctxt = getProgrammaticRootContext(); + Tomcat.addServlet(ctxt, "simple", new SimpleServlet()); + ctxt.addServletMapping("/simple", "simple"); + Tomcat.addServlet(ctxt, "short", new ShortContent(flush)); + ctxt.addServletMapping("/short", "short"); + + tomcat.start(); + + openClientConnection(); + doHttpUpgrade(); + sendClientPreface(); + validateHttp2InitialResponse(); + + byte[] frameHeader = new byte[9]; + ByteBuffer headersPayload = ByteBuffer.allocate(128); + buildGetRequest(frameHeader, headersPayload, null, 3, "/short"); + writeFrame(frameHeader, headersPayload); + + parser.readFrame(); + while (!output.getTrace().contains("3-RST")) { + parser.readFrame(); + } + + String trace = output.getTrace(); + Assert.assertTrue(trace, trace.contains("3-Header-[content-length]-[10]")); + Assert.assertTrue(trace, trace.contains("3-RST-[" + Http2Error.PROTOCOL_ERROR.getCode() + "]")); + Assert.assertFalse(trace, trace.contains("3-EndOfStream")); + } + + @Test public void testAsyncDispatch() throws Exception { enableHttp2(); @@ -967,4 +1020,26 @@ private void waitForRequestBody(HttpServletRequest req) throws IOException { } } } + + private static final class ShortContent extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private final boolean flush; + + ShortContent(boolean flush) { + this.flush = flush; + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/plain"); + response.setContentLength(10); + // Less than the content length + response.getOutputStream().write("01234".getBytes(StandardCharsets.ISO_8859_1)); + if (flush) { + response.flushBuffer(); + } + } + } }