Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions java/org/apache/coyote/http2/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
69 changes: 69 additions & 0 deletions test/org/apache/coyote/http2/TestHttp2Section_8_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Header> 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());
}
}
}
Loading