GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make 'chunked must be last encoding' check version-independent · netty/netty@f79947b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpObjectDecoder.java
    • test/java/io/netty/handler/codec/http
      • HttpRequestDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java
+29-19Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -849,24 +849,29 @@ private State readHeaders(ByteBuf buffer) {
849849
}
850850
if (HttpUtil.isTransferEncodingChunked(message)) {
851851
this.chunked = true;
852+
// The "chunked must be the last encoding" rule (RFC 9112 6.1) is not specific to
853+
// HTTP/1.1 -- it applies to any message that carries a Transfer-Encoding header at
854+
// all. This validation must not be gated on protocolVersion(), since a decoder
855+
// configured with useRfc9112TransferEncoding(false) (see HttpDecoderConfig) can
856+
// still reach this point for HTTP/1.0 and other non-1.1 messages, and the ordering
857+
// requirement applies to those just as much as it does to HTTP/1.1.
858+
// See https://datatracker.ietf.org/doc/html/rfc9112#name-message-body-length
859+
Iterator<? extends CharSequence> encodingIt =
860+
message.headers().valueCharSequenceIterator(HttpHeaderNames.TRANSFER_ENCODING);
861+
CharSequence v = null;
862+
while (encodingIt.hasNext()) {
863+
v = encodingIt.next();
864+
}
865+
final int vLen = v.length();
866+
final int chunkedValueLength = HttpHeaderValues.CHUNKED.length();
867+
// We only need to validate if we have more then the chunked value length contained as otherwise
868+
// we know it is only chunked.
869+
if (vLen > chunkedValueLength && !AsciiString.regionMatches(v, true, vLen - chunkedValueLength,
870+
HttpHeaderValues.CHUNKED, 0, chunkedValueLength)) {
871+
throw new IllegalArgumentException(
872+
"chunked must be the last encoding present in the Transfer-Encoding header");
873+
}
852874
if (message.protocolVersion() == HttpVersion.HTTP_1_1) {
853-
Iterator<? extends CharSequence> encodingIt =
854-
message.headers().valueCharSequenceIterator(HttpHeaderNames.TRANSFER_ENCODING);
855-
// Validate that chunked is the last encoding.
856-
// See https://datatracker.ietf.org/doc/html/rfc9112#name-message-body-length
857-
CharSequence v = null;
858-
while (encodingIt.hasNext()) {
859-
v = encodingIt.next();
860-
}
861-
final int vLen = v.length();
862-
final int chunkedValueLength = HttpHeaderValues.CHUNKED.length();
863-
// We only need to validate if we have more then the chunked value length contained as otherwise
864-
// we know it is only chunked.
865-
if (vLen > chunkedValueLength && !AsciiString.regionMatches(v, true, vLen - chunkedValueLength,
866-
HttpHeaderValues.CHUNKED, 0, chunkedValueLength)) {
867-
throw new IllegalArgumentException(
868-
"chunked must be the last encoding present in the Transfer-Encoding header");
869-
}
870875
if (!contentLengthFields.isEmpty()) {
871876
handleTransferEncodingChunkedWithContentLength(message);
872877
}
@@ -926,8 +931,13 @@ private static boolean isLengthEqual(String lengthValue, long contentLength) {
926931
* </li>
927932
* </ul>
928933
* <p>
929-
* <strong>Note:</strong> This method is only called for {@code HTTP/1.1} requests. Earlier HTTP protocol versions
930-
* do not support the {@code Transfer-Encoding} header, and will reject requests that include it.
934+
* <strong>Note:</strong> This method is only called for {@code HTTP/1.1} requests. Under the
935+
* default configuration, earlier HTTP protocol versions carrying a {@code Transfer-Encoding}
936+
* header are rejected before reaching this point. However, if
937+
* {@link HttpDecoderConfig#setUseRfc9112TransferEncoding(boolean)} is set to {@code false}
938+
* (see above), that rejection does not happen — a non-{@code HTTP/1.1} message can then
939+
* legitimately carry {@code Transfer-Encoding} without being rejected for it, though this
940+
* method specifically remains {@code HTTP/1.1}-only and is not invoked for such messages.
931941
*/
932942
@SuppressWarnings("unused")
933943
protected void handleTransferEncodingChunkedWithContentLength(HttpMessage message) {
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,49 @@ public void testChunkedNotLastInTransferEncoding() {
712712
testInvalidHeaders0(requestStr);
713713
}
714714

715+
// Regression: the chunked-must-be-last check was nested inside a protocolVersion() ==
716+
// HTTP_1_1 condition, so it was silently skipped for HTTP/1.0 when
717+
// useRfc9112TransferEncoding is disabled. See HttpObjectDecoder#readHeaders.
718+
@Test
719+
public void testChunkedNotLastInTransferEncodingHttp10WithRfc9112Disabled() {
720+
String requestStr = "GET /some/path HTTP/1.0\r\n" +
721+
"Transfer-Encoding: chunked, identity\r\n" +
722+
"Content-Length: 1\r\n" +
723+
"Host: netty.io\r\n\r\n" +
724+
"a";
725+
HttpDecoderConfig config = new HttpDecoderConfig().setUseRfc9112TransferEncoding(false);
726+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder(config));
727+
assertTrue(channel.writeInbound(
728+
Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
729+
HttpRequest request = channel.readInbound();
730+
assertInstanceOf(IllegalArgumentException.class, request.decoderResult().cause());
731+
assertTrue(request.decoderResult().isFailure(),
732+
"expected 'chunked must be last' to be enforced regardless of HTTP version "
733+
+ "when Transfer-Encoding is legitimately present (useRfc9112TransferEncoding=false)");
734+
channel.finishAndReleaseAll();
735+
}
736+
737+
// Companion to the test above: confirms the fix isn't tied to identity being a no-op
738+
// by using a real trailing encoding (gzip) instead.
739+
@Test
740+
public void testChunkedNotLastInTransferEncodingHttp10WithRfc9112DisabledNonNoOpEncoding() {
741+
String requestStr = "GET /some/path HTTP/1.0\r\n" +
742+
"Transfer-Encoding: chunked, gzip\r\n" +
743+
"Content-Length: 1\r\n" +
744+
"Host: netty.io\r\n\r\n" +
745+
"a";
746+
HttpDecoderConfig config = new HttpDecoderConfig().setUseRfc9112TransferEncoding(false);
747+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder(config));
748+
assertTrue(channel.writeInbound(
749+
Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
750+
HttpRequest request = channel.readInbound();
751+
assertInstanceOf(IllegalArgumentException.class, request.decoderResult().cause());
752+
assertTrue(request.decoderResult().isFailure(),
753+
"expected 'chunked must be last' to be enforced regardless of the specific "
754+
+ "trailing encoding value, matching the identity-trailing case above");
755+
channel.finishAndReleaseAll();
756+
}
757+
715758
@Test
716759
public void testContentLengthAndTransferEncodingHeadersWithVerticalTab() {
717760
testContentLengthAndTransferEncodingHeadersWithInvalidSeparator((char) 0x0b, false);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.