From 98095c3185a767896b2b617ac23a12f994bd65f3 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 16 Nov 2026 14:06:41 +0100 Subject: [PATCH] Do not send more data than the content-length of an HTTP/2 response 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, and clients such as curl and browsers reset the stream. An application that wrote more than the content length it had set, for example because it counted characters rather than bytes, had all of the data sent. HTTP/1.1 discards the data beyond the content length. Discard the data beyond the content length in the HTTP/2 stream output buffer as well. --- java/org/apache/coyote/http2/Stream.java | 13 +++++ .../coyote/http2/TestStreamProcessor.java | 55 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java index 0d19bc7bf74a..2a87ead26d6a 100644 --- a/java/org/apache/coyote/http2/Stream.java +++ b/java/org/apache/coyote/http2/Stream.java @@ -1078,6 +1078,19 @@ public final int doWrite(ByteBuffer chunk) throws IOException { if (closed) { throw new IOException(sm.getString("stream.closed", getConnectionId(), getIdAsString())); } + // Do not send more data than the content-length header field of the response states. As for HTTP/1.1, + // the data that is beyond it is discarded. Otherwise the response is malformed (RFC 9113, section + // 8.1.1). + long contentLength = coyoteResponse.getContentLengthLong(); + if (contentLength > -1) { + long remaining = contentLength - written; + if (remaining <= 0) { + chunk.position(chunk.limit()); + return 0; + } else if (chunk.remaining() > remaining) { + chunk.limit(chunk.position() + (int) remaining); + } + } // chunk is always fully written int result = chunk.remaining(); if (writeBuffer.isEmpty()) { diff --git a/test/org/apache/coyote/http2/TestStreamProcessor.java b/test/org/apache/coyote/http2/TestStreamProcessor.java index 47eb991a9ad4..d258518bf580 100644 --- a/test/org/apache/coyote/http2/TestStreamProcessor.java +++ b/test/org/apache/coyote/http2/TestStreamProcessor.java @@ -90,6 +90,48 @@ 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. As for HTTP/1.1, data written beyond the content length is discarded. + */ + @Test + public void testResponseLongerThanContentLength() 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, "excess", new ExcessContent()); + ctxt.addServletMapping("/excess", "excess"); + + tomcat.start(); + + openClientConnection(); + doHttpUpgrade(); + sendClientPreface(); + validateHttp2InitialResponse(); + + byte[] frameHeader = new byte[9]; + ByteBuffer headersPayload = ByteBuffer.allocate(128); + buildGetRequest(frameHeader, headersPayload, null, 3, "/excess"); + writeFrame(frameHeader, headersPayload); + + // Headers and then the body, which ends the stream + parser.readFrame(); + while (!output.getTrace().contains("3-EndOfStream")) { + parser.readFrame(); + } + + Assert.assertEquals("3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + + "3-Header-[content-type]-[text/plain]\n" + "3-Header-[content-length]-[5]\n" + + "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n" + "3-Body-5\n" + + "3-EndOfStream\n", output.getTrace()); + } + + @Test public void testAsyncDispatch() throws Exception { enableHttp2(); @@ -967,4 +1009,17 @@ private void waitForRequestBody(HttpServletRequest req) throws IOException { } } } + + private static final class ExcessContent extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/plain"); + response.setContentLength(5); + // More than the content length + response.getOutputStream().write("0123456789".getBytes(StandardCharsets.ISO_8859_1)); + } + } }