Skip to content

Commit cc30ee0

Browse files
committed
Enforce configured HPACK header list size limit on initialization
Initialize the HPACK decoder with the local max header list size instead of the HTTP/2 initial unlimited value.
1 parent 4a8a512 commit cc30ee0

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ enum SettingsHandshake { READY, TRANSMITTED, ACKED }
211211
this.initInputWinSize = H2Config.INIT.getInitialWindowSize();
212212
this.initOutputWinSize = H2Config.INIT.getInitialWindowSize();
213213

214-
this.hPackDecoder.setMaxListSize(H2Config.INIT.getMaxHeaderListSize());
215-
214+
this.hPackDecoder.setMaxListSize(this.localConfig.getMaxHeaderListSize());
216215
this.lowMark = H2Config.INIT.getInitialWindowSize() / 2;
217216
this.streamListener = streamListener;
218217
this.lastActivityNanos = System.nanoTime();

httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
4949
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
5050
import org.apache.hc.core5.http.nio.AsyncPushProducer;
51+
import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
5152
import org.apache.hc.core5.http.nio.HandlerFactory;
5253
import org.apache.hc.core5.http.protocol.HttpContext;
5354
import org.apache.hc.core5.http.protocol.HttpProcessor;
@@ -66,6 +67,7 @@
6667
import org.apache.hc.core5.http2.frame.FrameType;
6768
import org.apache.hc.core5.http2.frame.RawFrame;
6869
import org.apache.hc.core5.http2.frame.StreamIdGenerator;
70+
import org.apache.hc.core5.http2.hpack.HPackDecoder;
6971
import org.apache.hc.core5.http2.hpack.HPackEncoder;
7072
import org.apache.hc.core5.http2.hpack.HPackException;
7173
import org.apache.hc.core5.reactor.ProtocolIOSession;
@@ -2279,5 +2281,41 @@ void testHeadersWithPrioritySelfDependencyIsStreamProtocolError() throws Excepti
22792281
.consumeHeader(ArgumentMatchers.anyList(), ArgumentMatchers.anyBoolean());
22802282
}
22812283

2284+
@Test
2285+
void testHPackDecoderUsesConfiguredMaxHeaderListSizeImmediately() throws Exception {
2286+
final int maxHeaderListSize = 128;
2287+
2288+
final H2Config h2Config = H2Config.custom()
2289+
.setMaxHeaderListSize(maxHeaderListSize)
2290+
.build();
2291+
2292+
final ProtocolIOSession ioSession = Mockito.mock(ProtocolIOSession.class);
2293+
final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
2294+
final HandlerFactory<AsyncServerExchangeHandler> handlerFactory = Mockito.mock(HandlerFactory.class);
2295+
2296+
final ServerH2StreamMultiplexer multiplexer = new ServerH2StreamMultiplexer(
2297+
ioSession,
2298+
httpProcessor,
2299+
handlerFactory,
2300+
CharCodingConfig.DEFAULT,
2301+
h2Config);
2302+
2303+
final HPackDecoder hPackDecoder = getHPackDecoder(multiplexer);
2304+
2305+
Assertions.assertEquals(maxHeaderListSize, getMaxListSize(hPackDecoder));
2306+
}
2307+
2308+
private static HPackDecoder getHPackDecoder(final AbstractH2StreamMultiplexer multiplexer) throws Exception {
2309+
final Field field = AbstractH2StreamMultiplexer.class.getDeclaredField("hPackDecoder");
2310+
field.setAccessible(true);
2311+
return (HPackDecoder) field.get(multiplexer);
2312+
}
2313+
2314+
private static int getMaxListSize(final HPackDecoder hPackDecoder) throws Exception {
2315+
final Field field = HPackDecoder.class.getDeclaredField("maxListSize");
2316+
field.setAccessible(true);
2317+
return field.getInt(hPackDecoder);
2318+
}
2319+
22822320

22832321
}

0 commit comments

Comments
 (0)