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 — Auto-port 4.1: Http2: reject generic frames whose payload exceeds SET… · netty/netty@13ae2b2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • DefaultHttp2FrameWriter.java
    • test/java/io/netty/handler/codec/http2
      • DefaultHttp2FrameWriterTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import io.netty.handler.codec.http2.Http2FrameWriter.Configuration;
2626
import io.netty.handler.codec.http2.Http2HeadersEncoder.SensitivityDetector;
2727
import io.netty.util.internal.PlatformDependent;
28+
import io.netty.util.internal.logging.InternalLogger;
29+
import io.netty.util.internal.logging.InternalLoggerFactory;
2830

2931
import static io.netty.buffer.Unpooled.directBuffer;
3032
import static io.netty.buffer.Unpooled.unreleasableBuffer;
@@ -71,6 +73,8 @@
7173
* A {@link Http2FrameWriter} that supports all frame types defined by the HTTP/2 specification.
7274
*/
7375
public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSizePolicy, Configuration {
76+
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultHttp2FrameWriter.class);
77+
7478
private static final String STREAM_ID = "Stream ID";
7579
private static final String STREAM_DEPENDENCY = "Stream Dependency";
7680
/**
@@ -321,6 +325,7 @@ public ChannelFuture writeSettings(ChannelHandlerContext ctx, Http2Settings sett
321325
try {
322326
checkNotNull(settings, "settings");
323327
int payloadLength = SETTING_ENTRY_LENGTH * settings.size();
328+
verifyPayloadLength(SETTINGS, payloadLength);
324329
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + payloadLength);
325330
writeFrameHeaderInternal(buf, payloadLength, SETTINGS, new Http2Flags(), 0);
326331
for (Http2Settings.PrimitiveEntry<Long> entry : settings.entries()) {
@@ -427,6 +432,15 @@ public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, lo
427432
verifyStreamOrConnectionId(lastStreamId, "Last Stream ID");
428433
verifyErrorCode(errorCode);
429434

435+
// GOAWAY must still be sent, so instead of rejecting oversized debug data truncate it to fit into
436+
// SETTINGS_MAX_FRAME_SIZE together with the 8 bytes of last stream id and error code.
437+
int maxDebugDataLength = maxFrameSize - 8;
438+
if (debugData.readableBytes() > maxDebugDataLength) {
439+
logger.warn("GOAWAY debug data of {} bytes exceeds the maximum of {} bytes allowed by " +
440+
"SETTINGS_MAX_FRAME_SIZE, truncating it.", debugData.readableBytes(), maxDebugDataLength);
441+
debugData = debugData.readSlice(maxDebugDataLength);
442+
}
443+
430444
int payloadLength = 8 + debugData.readableBytes();
431445
ByteBuf buf = ctx.alloc().buffer(GO_AWAY_FRAME_HEADER_LENGTH);
432446
// Assume nothing below will throw until buf is written. That way we don't have to take care of ownership
@@ -476,6 +490,9 @@ public ChannelFuture writeFrame(ChannelHandlerContext ctx, byte frameType, int s
476490
new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
477491
try {
478492
verifyStreamOrConnectionId(streamId, STREAM_ID);
493+
// RFC 9113 Section 4.2: SETTINGS_MAX_FRAME_SIZE applies to the payload of every frame. The semantics of
494+
// a generic frame are unknown so it cannot be split, the write must fail instead.
495+
verifyPayloadLength(frameType, payload.readableBytes());
479496
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH);
480497
// Assume nothing below will throw until buf is written. That way we don't have to take care of ownership
481498
// in the catch block.
@@ -659,6 +676,14 @@ private static void verifyErrorCode(long errorCode) {
659676
}
660677
}
661678

679+
private void verifyPayloadLength(byte frameType, int payloadLength) {
680+
if (payloadLength > maxFrameSize) {
681+
throw new IllegalArgumentException("Payload length " + payloadLength + " of frame type 0x" +
682+
Integer.toHexString(frameType & 0xFF) + " exceeds the maximum frame size " + maxFrameSize +
683+
" advertised by the remote peer.");
684+
}
685+
}
686+
662687
private static void verifyWindowSizeIncrement(int windowSizeIncrement) {
663688
checkPositiveOrZero(windowSizeIncrement, "windowSizeIncrement");
664689
}
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriterTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriterTest.java
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import static org.junit.jupiter.api.Assertions.assertFalse;
5050
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
5151
import static org.junit.jupiter.api.Assertions.assertThrows;
52+
import static org.junit.jupiter.api.Assertions.assertTrue;
5253
import static org.mockito.Mockito.*;
5354

5455
/**
@@ -356,6 +357,72 @@ public void writeFrameHasPayload() throws Exception {
356357
assertEquals(expectedOutbound, outbound);
357358
}
358359

360+
@Test
361+
public void writeFrameExceedingMaxFrameSizeFailsPromiseAndReleasesPayload() throws Exception {
362+
frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
363+
ByteBuf payload = Unpooled.buffer().writeZero(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND + 1);
364+
365+
ChannelFuture future = frameWriter.writeFrame(ctx, (byte) 0xf, 0, new Http2Flags(), payload, promise);
366+
367+
assertTrue(future.isDone());
368+
assertFalse(future.isSuccess());
369+
assertInstanceOf(IllegalArgumentException.class, future.cause());
370+
assertTrue(future.cause().getMessage().contains("0xf"));
371+
// Nothing must have been written and the payload must have been released
372+
assertEquals(0, outbound.readableBytes());
373+
assertEquals(0, payload.refCnt());
374+
verify(ctx, never()).write(any(), any(ChannelPromise.class));
375+
}
376+
377+
@Test
378+
public void writeFrameAtMaxFrameSizeSucceeds() throws Exception {
379+
frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
380+
ByteBuf payload = Unpooled.buffer().writeZero(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
381+
382+
ChannelFuture future = frameWriter.writeFrame(ctx, (byte) 0xf, 0, new Http2Flags(), payload, promise);
383+
384+
// The mocked context never completes the writes, so only verify that the write was not rejected
385+
assertFalse(future.isDone() && !future.isSuccess());
386+
assertEquals(Http2CodecUtil.FRAME_HEADER_LENGTH + Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND,
387+
outbound.readableBytes());
388+
// 24 bit payload length field must hold exactly the payload size
389+
assertEquals(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND, outbound.getUnsignedMedium(0));
390+
}
391+
392+
@Test
393+
public void writeSettingsExceedingMaxFrameSizeFailsPromise() throws Exception {
394+
frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
395+
Http2Settings settings = new Http2Settings();
396+
// Each setting takes 6 bytes, so this is one entry more than fits into a single frame
397+
int entries = Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND / Http2CodecUtil.SETTING_ENTRY_LENGTH + 1;
398+
for (int i = 0; i < entries; i++) {
399+
settings.put((char) (0x100 + i), Long.valueOf(0));
400+
}
401+
402+
ChannelFuture future = frameWriter.writeSettings(ctx, settings, promise);
403+
404+
assertTrue(future.isDone());
405+
assertFalse(future.isSuccess());
406+
assertInstanceOf(IllegalArgumentException.class, future.cause());
407+
assertTrue(future.cause().getMessage().contains("0x4"));
408+
assertEquals(0, outbound.readableBytes());
409+
}
410+
411+
@Test
412+
public void writeGoAwayTruncatesDebugDataExceedingMaxFrameSize() throws Exception {
413+
frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
414+
ByteBuf debugData = Unpooled.buffer().writeZero(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
415+
416+
frameWriter.writeGoAway(ctx, 0, Http2Error.NO_ERROR.code(), debugData, promise);
417+
418+
// 8 bytes of last stream id and error code plus the truncated debug data fill exactly one frame
419+
assertEquals(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND, outbound.getUnsignedMedium(0));
420+
assertEquals(Http2CodecUtil.FRAME_HEADER_LENGTH + Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND,
421+
outbound.readableBytes());
422+
// The original debug data buffer must have been released
423+
assertEquals(0, debugData.refCnt());
424+
}
425+
359426
@Test
360427
public void writePriority() {
361428
frameWriter.writePriority(

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.