GeezerVison is showing github.com — HTTP3: Limit the max payload length of unknown frames · netty/netty@cfcfb97
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/http3 test/java/io/netty/handler/codec/http3
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ClientConnectionHandler.java +33-1Lines changed: 33 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,8 +83,40 @@ public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStrea | 83 | 83 | @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable, | 84 | 84 | @Nullable Http3Settings.NonStandardHttp3SettingsValidator | 85 | 85 | nonStandardSettingsValidator) { | | 86 | + this(inboundControlStreamHandler, pushStreamHandlerFactory, unknownInboundStreamHandlerFactory, localSettings, | | 87 | + disableQpackDynamicTable, nonStandardSettingsValidator, | | 88 | + Http3CodecUtils.DEFAULT_MAX_UNKNOWN_FRAME_PAYLOAD_LENGTH); | | 89 | + } | | 90 | + | | 91 | + /** | | 92 | + * Create a new instance. | | 93 | + * | | 94 | + * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about | | 95 | + * {@link Http3RequestStreamFrame}s or {@code null} if the user is not | | 96 | + * interested in these. | | 97 | + * @param pushStreamHandlerFactory the {@link LongFunction} that will provide a custom | | 98 | + * {@link ChannelHandler} for push streams {@code null} if no special | | 99 | + * handling should be done. When present, push ID will be passed as an | | 100 | + * argument to the {@link LongFunction}. | | 101 | + * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom | | 102 | + * {@link ChannelHandler} for unknown inbound stream types or | | 103 | + * {@code null} if no special handling should be done. | | 104 | + * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the | | 105 | + * remote peer or {@code null} if the default settings should be used. | | 106 | + * @param disableQpackDynamicTable If QPACK dynamic table should be disabled. | | 107 | + * @param nonStandardSettingsValidator the {@link Http3Settings.NonStandardHttp3SettingsValidator} to use | | 108 | + * when validating settings that are non-standard. | | 109 | + * @param maxUnknownFramePayloadLength the maximum payload size of an unknown frame. | | 110 | + */ | | 111 | + public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStreamHandler, | | 112 | + @Nullable LongFunction<ChannelHandler> pushStreamHandlerFactory, | | 113 | + @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory, | | 114 | + @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable, | | 115 | + @Nullable Http3Settings.NonStandardHttp3SettingsValidator | | 116 | + nonStandardSettingsValidator, | | 117 | + int maxUnknownFramePayloadLength) { | 86 | 118 | super(false, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings, | 87 | | - disableQpackDynamicTable, nonStandardSettingsValidator); | | 119 | + disableQpackDynamicTable, nonStandardSettingsValidator, maxUnknownFramePayloadLength); | 88 | 120 | this.pushStreamHandlerFactory = pushStreamHandlerFactory; | 89 | 121 | } | 90 | 122 |
| |
Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3CodecUtils.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,6 +63,9 @@ final class Http3CodecUtils { | 63 | 63 | */ | 64 | 64 | static final long DEFAULT_MAX_FIELD_SECTION_SIZE = 8192; | 65 | 65 |
| | 66 | + // Let's use 32kb as max | | 67 | + static final int DEFAULT_MAX_UNKNOWN_FRAME_PAYLOAD_LENGTH = 32 * 1024; | | 68 | + | 66 | 69 | private Http3CodecUtils() { } | 67 | 70 |
| 68 | 71 | static long checkIsReservedFrameType(long type) { | |
Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ConnectionHandler.java +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,11 +58,13 @@ public abstract class Http3ConnectionHandler extends ChannelInboundHandlerAdapte | 58 | 58 | * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the | 59 | 59 | * remote peer or {@code null} if the default settings should be used. | 60 | 60 | * @param disableQpackDynamicTable If QPACK dynamic table should be disabled. | | 61 | + * @param maxUnknownFramePayloadLength the maximum payload size of an unknown frame. | 61 | 62 | */ | 62 | 63 | Http3ConnectionHandler(boolean server, @Nullable ChannelHandler inboundControlStreamHandler, | 63 | 64 | @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory, | 64 | 65 | @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable, | 65 | | - @Nullable Http3Settings.NonStandardHttp3SettingsValidator nonStandardSettingsValidator) { | | 66 | + @Nullable Http3Settings.NonStandardHttp3SettingsValidator nonStandardSettingsValidator, | | 67 | + int maxUnknownFramePayloadLength) { | 66 | 68 | this.unknownInboundStreamHandlerFactory = unknownInboundStreamHandlerFactory; | 67 | 69 | this.disableQpackDynamicTable = disableQpackDynamicTable; | 68 | 70 | if (nonStandardSettingsValidator != null) { | @@ -95,7 +97,8 @@ public abstract class Http3ConnectionHandler extends ChannelInboundHandlerAdapte | 95 | 97 | ); | 96 | 98 | qpackDecoder = new QpackDecoder(maxTableCapacity, maxBlockedStreams); | 97 | 99 | qpackEncoder = new QpackEncoder(); | 98 | | - codecFactory = Http3FrameCodec.newFactory(qpackDecoder, maxFieldSectionSize, qpackEncoder); | | 100 | + codecFactory = Http3FrameCodec.newFactory( | | 101 | + qpackDecoder, maxFieldSectionSize, maxUnknownFramePayloadLength, qpackEncoder); | 99 | 102 | remoteControlStreamHandler = new Http3ControlStreamOutboundHandler(server, localSettings, | 100 | 103 | codecFactory.newCodec(Http3FrameTypeValidator.NO_VALIDATION, NO_STATE, NO_STATE, | 101 | 104 | this.nonStandardSettingsValidator)); | |
Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3FrameCodec.java +13-4Lines changed: 13 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 51 | 51 | import static io.netty.handler.codec.http3.Http3CodecUtils.writeVariableLengthInteger; | 52 | 52 | import static io.netty.util.internal.ObjectUtil.checkNotNull; | 53 | 53 | import static io.netty.util.internal.ObjectUtil.checkPositive; | | 54 | +import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; | 54 | 55 |
| 55 | 56 | /** | 56 | 57 | * Decodes / encodes {@link Http3Frame}s. | 57 | 58 | */ | 58 | 59 | final class Http3FrameCodec extends ByteToMessageDecoder implements ChannelOutboundHandler { | 59 | 60 | private final Http3FrameTypeValidator validator; | 60 | 61 | private final long maxHeaderListSize; | | 62 | + private final int maxUnknownFramePayloadLength; | 61 | 63 | private final QpackDecoder qpackDecoder; | 62 | 64 | private final QpackEncoder qpackEncoder; | 63 | 65 | private final Http3RequestStreamCodecState encodeState; | @@ -72,23 +74,29 @@ final class Http3FrameCodec extends ByteToMessageDecoder implements ChannelOutbo | 72 | 74 | private WriteResumptionListener writeResumptionListener; | 73 | 75 |
| 74 | 76 | static Http3FrameCodecFactory newFactory(QpackDecoder qpackDecoder, | 75 | | - long maxHeaderListSize, QpackEncoder qpackEncoder) { | | 77 | + long maxHeaderListSize, int maxUnknownFramePayloadLength, | | 78 | + QpackEncoder qpackEncoder) { | 76 | 79 | checkNotNull(qpackEncoder, "qpackEncoder"); | 77 | 80 | checkNotNull(qpackDecoder, "qpackDecoder"); | | 81 | + checkPositive(maxHeaderListSize, "maxHeaderListSize"); | | 82 | + checkPositive(maxUnknownFramePayloadLength, "maxUnknownFramePayloadLength"); | 78 | 83 |
| 79 | 84 | // QPACK decoder and encoder are shared between streams in a connection. | 80 | 85 | return (validator, encodeState, decodeState, | 81 | 86 | nonStandardSettingsValidator) -> new Http3FrameCodec(validator, qpackDecoder, | 82 | | - maxHeaderListSize, qpackEncoder, encodeState, decodeState, nonStandardSettingsValidator); | | 87 | + maxHeaderListSize, maxUnknownFramePayloadLength, qpackEncoder, | | 88 | + encodeState, decodeState, nonStandardSettingsValidator); | 83 | 89 | } | 84 | 90 |
| 85 | 91 | Http3FrameCodec(Http3FrameTypeValidator validator, QpackDecoder qpackDecoder, | 86 | | - long maxHeaderListSize, QpackEncoder qpackEncoder, Http3RequestStreamCodecState encodeState, | | 92 | + long maxHeaderListSize, int maxUnknownFramePayloadLength, | | 93 | + QpackEncoder qpackEncoder, Http3RequestStreamCodecState encodeState, | 87 | 94 | Http3RequestStreamCodecState decodeState, | 88 | 95 | Http3Settings.NonStandardHttp3SettingsValidator nonStandardSettingsValidator) { | 89 | 96 | this.validator = checkNotNull(validator, "validator"); | 90 | 97 | this.qpackDecoder = checkNotNull(qpackDecoder, "qpackDecoder"); | 91 | 98 | this.maxHeaderListSize = checkPositive(maxHeaderListSize, "maxHeaderListSize"); | | 99 | + this.maxUnknownFramePayloadLength = checkPositive(maxUnknownFramePayloadLength, "maxUnknownFramePayloadLength"); | 92 | 100 | this.qpackEncoder = checkNotNull(qpackEncoder, "qpackEncoder"); | 93 | 101 | this.encodeState = checkNotNull(encodeState, "encodeState"); | 94 | 102 | this.decodeState = checkNotNull(decodeState, "decodeState"); | @@ -335,7 +343,8 @@ private int decodeFrame(ChannelHandlerContext ctx, long longType, int payLoadLen | 335 | 343 | } | 336 | 344 | // Handling reserved frame types | 337 | 345 | // https://tools.ietf.org/html/draft-ietf-quic-http-32#section-7.2.8 | 338 | | - if (in.readableBytes() < payLoadLength) { | | 346 | + if (!enforceMaxPayloadLength(ctx, in, type, payLoadLength, | | 347 | + maxUnknownFramePayloadLength, Http3ErrorCode.H3_EXCESSIVE_LOAD)) { | 339 | 348 | return 0; | 340 | 349 | } | 341 | 350 | out.add(new DefaultHttp3UnknownFrame(longType, in.readRetainedSlice(payLoadLength))); | |
Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ServerConnectionHandler.java +31-1Lines changed: 31 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,8 +87,38 @@ public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler, | 87 | 87 | @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable, | 88 | 88 | @Nullable Http3Settings.NonStandardHttp3SettingsValidator | 89 | 89 | nonStandardSettingsValidator) { | | 90 | + this(requestStreamHandler, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings, | | 91 | + disableQpackDynamicTable, nonStandardSettingsValidator, | | 92 | + Http3CodecUtils.DEFAULT_MAX_UNKNOWN_FRAME_PAYLOAD_LENGTH); | | 93 | + } | | 94 | + | | 95 | + /** | | 96 | + * Create a new instance. | | 97 | + * @param requestStreamHandler the {@link ChannelHandler} that is used for each new request stream. | | 98 | + * This handler will receive {@link Http3HeadersFrame} and | | 99 | + * {@link Http3DataFrame}s. | | 100 | + * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about | | 101 | + * {@link Http3RequestStreamFrame}s or {@code null} if the user is not | | 102 | + * interested in these. | | 103 | + * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom | | 104 | + * {@link ChannelHandler} for unknown inbound stream types or | | 105 | + * {@code null} if no special handling should be done. | | 106 | + * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the | | 107 | + * remote peer or {@code null} if the default settings should be used. | | 108 | + * @param disableQpackDynamicTable If QPACK dynamic table should be disabled. | | 109 | + * @param nonStandardSettingsValidator the {@link Http3Settings.NonStandardHttp3SettingsValidator} to | | 110 | + * use when validating settings that are non-standard. | | 111 | + * @param maxUnknownFramePayloadLength the maximum payload size of an unknown frame. | | 112 | + */ | | 113 | + public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler, | | 114 | + @Nullable ChannelHandler inboundControlStreamHandler, | | 115 | + @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory, | | 116 | + @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable, | | 117 | + @Nullable Http3Settings.NonStandardHttp3SettingsValidator | | 118 | + nonStandardSettingsValidator, | | 119 | + int maxUnknownFramePayloadLength) { | 90 | 120 | super(true, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings, | 91 | | - disableQpackDynamicTable, nonStandardSettingsValidator); | | 121 | + disableQpackDynamicTable, nonStandardSettingsValidator, maxUnknownFramePayloadLength); | 92 | 122 | this.requestStreamHandler = ObjectUtil.checkNotNull(requestStreamHandler, "requestStreamHandler"); | 93 | 123 | } | 94 | 124 |
| |
Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/test/java/io/netty/handler/codec/http3/Http3FrameCodecTest.java +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,6 +94,11 @@ public static Collection<Object[]> dataNoFragment() { | 94 | 94 | private EmbeddedQuicStreamChannel codecChannel; | 95 | 95 |
| 96 | 96 | private void setUp(int maxBlockedStreams, boolean delayQpackStreams) throws Exception { | | 97 | + setUp(maxBlockedStreams, delayQpackStreams, Integer.MAX_VALUE); | | 98 | + } | | 99 | + | | 100 | + private void setUp(int maxBlockedStreams, boolean delayQpackStreams, int maxUnknownFramePayloadLength) | | 101 | + throws Exception { | 97 | 102 | parent = new EmbeddedQuicChannel(true); | 98 | 103 | qpackAttributes = new QpackAttributes(parent, false); | 99 | 104 | Http3.setQpackAttributes(parent, qpackAttributes); | @@ -129,7 +134,8 @@ protected void initChannel(QuicStreamChannel ch) { | 129 | 134 | Http3RequestStreamDecodeStateValidator decStateValidator = | 130 | 135 | new Http3RequestStreamDecodeStateValidator(); | 131 | 136 | ch.pipeline().addLast(new Http3FrameCodec(Http3FrameTypeValidator.NO_VALIDATION, decoder, | 132 | | - MAX_HEADER_SIZE, encoder, encStateValidator, decStateValidator, (id, v) -> false)); | | 137 | + MAX_HEADER_SIZE, maxUnknownFramePayloadLength, encoder, | | 138 | + encStateValidator, decStateValidator, (id, v) -> false)); | 133 | 139 | ch.pipeline().addLast(encStateValidator); | 134 | 140 | ch.pipeline().addLast(decStateValidator); | 135 | 141 | } | @@ -374,6 +380,17 @@ public void testHttp3UnknownFrame( | 374 | 380 | new DefaultHttp3UnknownFrame(Http3CodecUtils.MIN_RESERVED_FRAME_TYPE, Unpooled.buffer().writeLong(8))); | 375 | 381 | } | 376 | 382 |
| | 383 | + @ParameterizedTest(name = "{index}: fragmented = {0}, maxBlockedStreams = {1}, delayQpackStreams = {2}") | | 384 | + @MethodSource("data") | | 385 | + public void testHttp3UnknownFrameWithInvalidPayloadLength( | | 386 | + boolean fragmented, int maxBlockedStreams, boolean delayQpackStreams) throws Exception { | | 387 | + setUp(maxBlockedStreams, delayQpackStreams, 127); | | 388 | + assertThrows(Http3Exception.class, () -> | | 389 | + testFrameEncodedAndDecoded(fragmented, maxBlockedStreams, delayQpackStreams, | | 390 | + new DefaultHttp3UnknownFrame(Http3CodecUtils.MIN_RESERVED_FRAME_TYPE, | | 391 | + Unpooled.buffer().writeZero(128)))); | | 392 | + } | | 393 | + | 377 | 394 | // Reserved types that were used in HTTP/2 and should close the connection with an error | 378 | 395 | @ParameterizedTest(name = "{index}: fragmented = {0}, maxBlockedStreams = {1}, delayQpackStreams = {2}") | 379 | 396 | @MethodSource("data") | |
|