diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java index 0d19bc7bf74a..6a5312895164 100644 --- a/java/org/apache/coyote/http2/Stream.java +++ b/java/org/apache/coyote/http2/Stream.java @@ -403,6 +403,15 @@ public final void emitHeader(String name, String value) throws HpackException { } } + if ("content-length".equals(name) && !isNumeric(value)) { + // RFC 9110, section 8.6 only permits digits. Anything else, such as a sign, makes the message malformed. + headerException = new StreamException( + sm.getString("stream.header.contentLength.invalid", getConnectionId(), getIdAsString()), + Http2Error.PROTOCOL_ERROR, getIdAsInt()); + // No need for further processing. The stream will be reset. + return; + } + if (headerException != null) { // Don't bother processing the header since the stream is going to // be reset anyway @@ -808,6 +817,19 @@ final void receivedEndOfStream() throws Http2Exception { inputBuffer.notifyEof(); } + private static boolean isNumeric(String value) { + if (value.isEmpty()) { + return false; + } + for (int i = 0; i < value.length(); i++) { + if (!HttpParser.isNumeric(value.charAt(i))) { + return false; + } + } + return true; + } + + final long getContentLengthLong() throws Http2Exception { try { return coyoteRequest.getContentLengthLong(); diff --git a/test/org/apache/coyote/http2/TestHttp2Section_8_1.java b/test/org/apache/coyote/http2/TestHttp2Section_8_1.java index 55cda91103ef..7c586f2c34aa 100644 --- a/test/org/apache/coyote/http2/TestHttp2Section_8_1.java +++ b/test/org/apache/coyote/http2/TestHttp2Section_8_1.java @@ -702,4 +702,73 @@ private void doTest1kPayload(int contentLength) throws Exception { actual = filteredTrace.toString(); Assert.assertEquals(actual, getSimpleResponseTrace(5), actual); } + + + @Test + public void testContentLengthValid() throws Exception { + doTestContentLength("0", true); + } + + + @Test + public void testContentLengthLeadingZeros() throws Exception { + doTestContentLength("00", true); + } + + + @Test + public void testContentLengthPlusSign() throws Exception { + doTestContentLength("+0", false); + } + + + @Test + public void testContentLengthMinusZero() throws Exception { + doTestContentLength("-0", false); + } + + + @Test + public void testContentLengthNegative() throws Exception { + doTestContentLength("-1", false); + } + + + @Test + public void testContentLengthEmpty() throws Exception { + doTestContentLength("", false); + } + + + /* + * RFC 9110, section 8.6 defines the value of a content-length header field as one or more digits. RFC 9113, + * section 8.1.1 requires that a request that is malformed is treated as a stream error of type PROTOCOL_ERROR. + */ + private void doTestContentLength(String value, boolean valid) throws Exception { + http2Connect(); + + byte[] frameHeader = new byte[9]; + ByteBuffer headersPayload = ByteBuffer.allocate(128); + + List
headers = new ArrayList<>(5); + headers.add(new Header(":method", "GET")); + headers.add(new Header(":scheme", "http")); + headers.add(new Header(":path", "/simple")); + headers.add(new Header(":authority", "localhost:" + getPort())); + headers.add(new Header("content-length", value)); + + buildGetRequest(frameHeader, headersPayload, null, headers, 3); + writeFrame(frameHeader, headersPayload); + + // Read first response frame + parser.readFrame(); + + if (valid) { + // Normal response. There will be a data frame with the body too. + parser.readFrame(); + Assert.assertEquals(output.getTrace(), getSimpleResponseTrace(3)); + } else { + Assert.assertEquals("3-RST-[1]\n", output.getTrace()); + } + } }