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 — Http2: reject generic frames whose payload exceeds SETTINGS_MAX_FRAME… · netty/netty@dc5655e

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
@@ -26,6 +26,8 @@
2626
import io.netty.handler.codec.http2.Http2HeadersEncoder.SensitivityDetector;
2727
import io.netty.util.LeakPresenceDetector;
2828
import io.netty.util.internal.PlatformDependent;
29+
import io.netty.util.internal.logging.InternalLogger;
30+
import io.netty.util.internal.logging.InternalLoggerFactory;
2931

3032
import static io.netty.buffer.Unpooled.directBuffer;
3133
import static io.netty.buffer.Unpooled.unreleasableBuffer;
@@ -72,6 +74,8 @@
7274
* A {@link Http2FrameWriter} that supports all frame types defined by the HTTP/2 specification.
7375
*/
7476
public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSizePolicy, Configuration {
77+
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultHttp2FrameWriter.class);
78+
7579
private static final String STREAM_ID = "Stream ID";
7680
private static final String STREAM_DEPENDENCY = "Stream Dependency";
7781
/**
@@ -322,6 +326,7 @@ public ChannelFuture writeSettings(ChannelHandlerContext ctx, Http2Settings sett
322326
try {
323327
checkNotNull(settings, "settings");
324328
int payloadLength = SETTING_ENTRY_LENGTH * settings.size();
329+
verifyPayloadLength(SETTINGS, payloadLength);
325330
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + payloadLength);
326331
writeFrameHeaderInternal(buf, payloadLength, SETTINGS, new Http2Flags(), 0);
327332
for (Http2Settings.PrimitiveEntry<Long> entry : settings.entries()) {
@@ -428,6 +433,15 @@ public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, lo
428433
verifyStreamOrConnectionId(lastStreamId, "Last Stream ID");
429434
verifyErrorCode(errorCode);
430435

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

680+
private void verifyPayloadLength(byte frameType, int payloadLength) {
681+
if (payloadLength > maxFrameSize) {
682+
throw new IllegalArgumentException("Payload length " + payloadLength + " of frame type 0x" +
683+
Integer.toHexString(frameType & 0xFF) + " exceeds the maximum frame size " + maxFrameSize +
684+
" advertised by the remote peer.");
685+
}
686+
}
687+
663688
private static void verifyWindowSizeIncrement(int windowSizeIncrement) {
664689
checkPositiveOrZero(windowSizeIncrement, "windowSizeIncrement");
665690
}
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
@@ -48,6 +48,7 @@
4848
import static org.junit.jupiter.api.Assertions.assertFalse;
4949
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
5050
import static org.junit.jupiter.api.Assertions.assertThrows;
51+
import static org.junit.jupiter.api.Assertions.assertTrue;
5152
import static org.mockito.Mockito.*;
5253

5354
/**
@@ -351,6 +352,72 @@ public void writeFrameHasPayload() throws Exception {
351352
assertEquals(expectedOutbound, outbound);
352353
}
353354

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