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 — Fix UnsupportedOperationException in readTrailingHeaders (#16412) · netty/netty@00eb965

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
      • HttpResponseDecoderTest.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
+28-21Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -867,48 +867,55 @@ private LastHttpContent readTrailingHeaders(ByteBuf buffer) {
867867
return LastHttpContent.EMPTY_LAST_CONTENT;
868868
}
869869

870-
CharSequence lastHeader = null;
871870
if (trailer == null) {
872871
trailer = this.trailer = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailersFactory);
873872
}
874873
while (lineLength > 0) {
875874
final byte[] lineContent = line.array();
876875
final int startLine = line.arrayOffset() + line.readerIndex();
877876
final byte firstChar = lineContent[startLine];
878-
if (lastHeader != null && (firstChar == ' ' || firstChar == '\t')) {
879-
List<String> current = trailer.trailingHeaders().getAll(lastHeader);
880-
if (!current.isEmpty()) {
881-
int lastPos = current.size() - 1;
882-
//please do not make one line from below code
883-
//as it breaks +XX:OptimizeStringConcat optimization
884-
String lineTrimmed = langAsciiString(lineContent, startLine, line.readableBytes()).trim();
885-
String currentLastPos = current.get(lastPos);
886-
current.set(lastPos, currentLastPos + lineTrimmed);
887-
}
877+
if (name != null && (firstChar == ' ' || firstChar == '\t')) {
878+
//please do not make one line from below code
879+
//as it breaks +XX:OptimizeStringConcat optimization
880+
String trimmedLine = langAsciiString(lineContent, startLine, lineLength).trim();
881+
String valueStr = value;
882+
value = valueStr + ' ' + trimmedLine;
888883
} else {
889-
splitHeader(lineContent, startLine, lineLength);
890-
AsciiString headerName = name;
891-
if (!HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(headerName) &&
892-
!HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(headerName) &&
893-
!HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(headerName)) {
894-
trailer.trailingHeaders().add(headerName, value);
884+
if (name != null && isPermittedTrailingHeader(name)) {
885+
trailer.trailingHeaders().add(name, value);
895886
}
896-
lastHeader = name;
897-
// reset name and value fields
898-
name = null;
899-
value = null;
887+
splitHeader(lineContent, startLine, lineLength);
900888
}
889+
901890
line = headerParser.parse(buffer, defaultStrictCRLFCheck);
902891
if (line == null) {
903892
return null;
904893
}
905894
lineLength = line.readableBytes();
906895
}
907896

897+
// Add the last trailer
898+
if (name != null && isPermittedTrailingHeader(name)) {
899+
trailer.trailingHeaders().add(name, value);
900+
}
901+
902+
// reset name and value fields
903+
name = null;
904+
value = null;
905+
908906
this.trailer = null;
909907
return trailer;
910908
}
911909

910+
/**
911+
* Checks whether the given trailer field name is permitted per RFC 9110 section 6.5
912+
*/
913+
private static boolean isPermittedTrailingHeader(final AsciiString name) {
914+
return !HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(name) &&
915+
!HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(name) &&
916+
!HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(name);
917+
}
918+
912919
protected abstract boolean isDecodingRequest();
913920
protected abstract HttpMessage createMessage(String[] initialLine) throws Exception;
914921
protected abstract HttpMessage createInvalidMessage();
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
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,92 @@ public void testEmptyHeaderValue() {
269269
assertEquals("", req.headers().get(of("EmptyHeader")));
270270
}
271271

272+
@Test
273+
public void testSingleTrailingHeader() {
274+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
275+
String request = "POST / HTTP/1.1\r\n" +
276+
"Host: localhost\r\n" +
277+
"Transfer-Encoding: chunked\r\n" +
278+
"\r\n" +
279+
"5\r\n" +
280+
"hello\r\n" +
281+
"0\r\n" +
282+
"X-Checksum: abc123\r\n" +
283+
"\r\n";
284+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
285+
HttpRequest req = channel.readInbound();
286+
assertFalse(req.decoderResult().isFailure());
287+
HttpContent body = channel.readInbound();
288+
body.release();
289+
LastHttpContent last = channel.readInbound();
290+
assertFalse(last.decoderResult().isFailure());
291+
assertEquals("abc123", last.trailingHeaders().get(of("X-Checksum")));
292+
last.release();
293+
assertFalse(channel.finish());
294+
}
295+
296+
@Test
297+
public void testMultiLineTrailingHeader() {
298+
// Regression: folded trailer values previously threw UnsupportedOperationException
299+
// because trailingHeaders().getAll() returns an AbstractList that does not implement set().
300+
// Note: obs-fold in trailers is permitted as trailers are field-lines per
301+
// https://www.rfc-editor.org/rfc/rfc9112#section-5.2
302+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
303+
String request = "POST / HTTP/1.1\r\n" +
304+
"Host: localhost\r\n" +
305+
"Transfer-Encoding: chunked\r\n" +
306+
"\r\n" +
307+
"5\r\n" +
308+
"hello\r\n" +
309+
"0\r\n" +
310+
"X-Long: part1\r\n" +
311+
" part2\r\n" +
312+
"\t\t\t part3\r\n" +
313+
"X-Short: value\r\n" +
314+
"\r\n";
315+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
316+
HttpRequest req = channel.readInbound();
317+
assertFalse(req.decoderResult().isFailure());
318+
HttpContent body = channel.readInbound();
319+
body.release();
320+
LastHttpContent last = channel.readInbound();
321+
assertFalse(last.decoderResult().isFailure());
322+
assertEquals("part1 part2 part3", last.trailingHeaders().get(of("X-Long")));
323+
assertEquals("value", last.trailingHeaders().get(of("X-Short")));
324+
last.release();
325+
assertFalse(channel.finish());
326+
}
327+
328+
@Test
329+
public void testForbiddenTrailingHeadersAreDropped() {
330+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
331+
String request = "POST / HTTP/1.1\r\n" +
332+
"Host: localhost\r\n" +
333+
"Transfer-Encoding: chunked\r\n" +
334+
"\r\n" +
335+
"5\r\n" +
336+
"hello\r\n" +
337+
"0\r\n" +
338+
HttpHeaderNames.CONTENT_LENGTH + ": 5\r\n" +
339+
HttpHeaderNames.TRANSFER_ENCODING + ": chunked\r\n" +
340+
"X-Custom: keep\r\n" +
341+
HttpHeaderNames.TRAILER + ": X-Checksum\r\n" + // covering post-loop flush path
342+
"\r\n";
343+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
344+
HttpRequest req = channel.readInbound();
345+
assertFalse(req.decoderResult().isFailure());
346+
HttpContent body = channel.readInbound();
347+
body.release();
348+
LastHttpContent last = channel.readInbound();
349+
assertFalse(last.decoderResult().isFailure());
350+
assertNull(last.trailingHeaders().get(HttpHeaderNames.CONTENT_LENGTH));
351+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRANSFER_ENCODING));
352+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRAILER));
353+
assertEquals("keep", last.trailingHeaders().get(of("X-Custom")));
354+
last.release();
355+
assertFalse(channel.finish());
356+
}
357+
272358
@Test
273359
public void test100Continue() {
274360
HttpRequestDecoder decoder = new HttpRequestDecoder();
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,64 @@ private static void testLastResponseWithTrailingHeaderFragmented(byte[] content,
665665
assertNull(ch.readInbound());
666666
}
667667

668+
@Test
669+
public void testMultiLineTrailingHeader() {
670+
// Regression: folded trailer values previously threw UnsupportedOperationException
671+
// because trailingHeaders().getAll() returns an AbstractList that does not implement set().
672+
// Note: obs-fold in trailers is permitted as trailers are field-lines per
673+
// https://www.rfc-editor.org/rfc/rfc9112#section-5.2
674+
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
675+
String response = "HTTP/1.1 200 OK\r\n" +
676+
"Transfer-Encoding: chunked\r\n" +
677+
"\r\n" +
678+
"0\r\n" +
679+
"X-Long: part1\r\n" +
680+
" part2\r\n" +
681+
"\t\t\t part3\r\n" +
682+
"X-Short: value\r\n" +
683+
"\r\n";
684+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(response, CharsetUtil.US_ASCII)));
685+
HttpResponse res = ch.readInbound();
686+
assertFalse(res.decoderResult().isFailure());
687+
assertSame(HttpVersion.HTTP_1_1, res.protocolVersion());
688+
assertEquals(HttpResponseStatus.OK, res.status());
689+
690+
LastHttpContent last = ch.readInbound();
691+
assertFalse(last.decoderResult().isFailure());
692+
assertEquals("part1 part2 part3", last.trailingHeaders().get(of("X-Long")));
693+
assertEquals("value", last.trailingHeaders().get(of("X-Short")));
694+
last.release();
695+
assertFalse(ch.finish());
696+
}
697+
698+
@Test
699+
public void testForbiddenTrailingHeadersAreDropped() {
700+
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
701+
String response = "HTTP/1.1 200 OK\r\n" +
702+
"Transfer-Encoding: chunked\r\n" +
703+
"\r\n" +
704+
"0\r\n" +
705+
HttpHeaderNames.CONTENT_LENGTH + ": 5\r\n" +
706+
HttpHeaderNames.TRANSFER_ENCODING + ": chunked\r\n" +
707+
"X-Custom: keep\r\n" +
708+
HttpHeaderNames.TRAILER + ": X-Checksum\r\n" + // covering post-loop flush path
709+
"\r\n";
710+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(response, CharsetUtil.US_ASCII)));
711+
HttpResponse res = ch.readInbound();
712+
assertFalse(res.decoderResult().isFailure());
713+
assertSame(HttpVersion.HTTP_1_1, res.protocolVersion());
714+
assertEquals(HttpResponseStatus.OK, res.status());
715+
716+
LastHttpContent last = ch.readInbound();
717+
assertFalse(last.decoderResult().isFailure());
718+
assertNull(last.trailingHeaders().get(HttpHeaderNames.CONTENT_LENGTH));
719+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRANSFER_ENCODING));
720+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRAILER));
721+
assertEquals("keep", last.trailingHeaders().get(of("X-Custom")));
722+
last.release();
723+
assertFalse(ch.finish());
724+
}
725+
668726
@Test
669727
public void testResponseWithContentLength() {
670728
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());

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.