Skip to content

Commit c79252c

Browse files
committed
Correct handling of unsigned HTTP/2 SETTINGS values
1 parent 0b0a20e commit c79252c

2 files changed

Lines changed: 124 additions & 22 deletions

File tree

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ enum SettingsHandshake { READY, TRANSMITTED, ACKED }
138138
private int lowMark;
139139

140140
private volatile H2Config remoteConfig;
141+
private volatile long remoteHeaderTableSize;
142+
private volatile long remoteMaxConcurrentStreams;
143+
private volatile long remoteMaxHeaderListSize;
141144

142145
private Continuation continuation;
143146

@@ -205,6 +208,9 @@ enum SettingsHandshake { READY, TRANSMITTED, ACKED }
205208
this.hPackEncoder = new HPackEncoder(H2Config.INIT.getHeaderTableSize(), CharCodingSupport.createEncoder(charCodingConfig));
206209
this.hPackDecoder = new HPackDecoder(H2Config.INIT.getHeaderTableSize(), CharCodingSupport.createDecoder(charCodingConfig));
207210
this.remoteConfig = H2Config.INIT;
211+
this.remoteHeaderTableSize = H2Config.INIT.getHeaderTableSize();
212+
this.remoteMaxConcurrentStreams = H2Config.INIT.getMaxConcurrentStreams();
213+
this.remoteMaxHeaderListSize = H2Config.INIT.getMaxHeaderListSize();
208214
this.connInputWindow = new AtomicInteger(H2Config.INIT.getInitialWindowSize());
209215
this.connOutputWindow = new AtomicInteger(H2Config.INIT.getInitialWindowSize());
210216

@@ -576,7 +582,7 @@ public final void onOutput() throws HttpException, IOException {
576582
})));
577583
return;
578584
}
579-
while (streams.getLocalCount() < remoteConfig.getMaxConcurrentStreams()) {
585+
while (streams.getLocalCount() < remoteMaxConcurrentStreams) {
580586
final Command command = ioSession.poll();
581587
if (command == null) {
582588
break;
@@ -1262,34 +1268,30 @@ private void consumeContinuationFrame(final RawFrame frame, final H2Stream strea
12621268

12631269
private void consumeSettingsFrame(final ByteBuffer payload) throws IOException {
12641270
final H2Config.Builder configBuilder = H2Config.initial();
1271+
long headerTableSize = remoteHeaderTableSize;
1272+
long maxConcurrentStreams = remoteMaxConcurrentStreams;
1273+
long maxHeaderListSize = remoteMaxHeaderListSize;
12651274
while (payload.hasRemaining()) {
12661275
final int code = payload.getShort();
12671276
final int value = payload.getInt();
12681277
final H2Param param = H2Param.valueOf(code);
12691278
if (param != null) {
12701279
validateSetting(param, value);
1280+
final long unsignedValue = Integer.toUnsignedLong(value);
12711281
switch (param) {
12721282
case HEADER_TABLE_SIZE:
1273-
try {
1274-
configBuilder.setHeaderTableSize(value);
1275-
} catch (final IllegalArgumentException ex) {
1276-
throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, ex.getMessage());
1277-
}
1283+
headerTableSize = unsignedValue;
12781284
break;
12791285
case MAX_CONCURRENT_STREAMS:
1280-
try {
1281-
configBuilder.setMaxConcurrentStreams(value);
1282-
} catch (final IllegalArgumentException ex) {
1283-
throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, ex.getMessage());
1284-
}
1286+
maxConcurrentStreams = unsignedValue;
12851287
break;
12861288
case ENABLE_PUSH:
12871289
configBuilder.setPushEnabled(value == 1);
12881290
break;
12891291
case INITIAL_WINDOW_SIZE:
12901292
if (value < 0) {
12911293
throw new H2ConnectionException(H2Error.FLOW_CONTROL_ERROR,
1292-
"Invalid initial window size: " + (value & 0xffffffffL));
1294+
"Invalid initial window size: " + unsignedValue);
12931295
}
12941296
try {
12951297
configBuilder.setInitialWindowSize(value);
@@ -1305,19 +1307,19 @@ private void consumeSettingsFrame(final ByteBuffer payload) throws IOException {
13051307
}
13061308
break;
13071309
case MAX_HEADER_LIST_SIZE:
1308-
try {
1309-
configBuilder.setMaxHeaderListSize(value);
1310-
} catch (final IllegalArgumentException ex) {
1311-
throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, ex.getMessage());
1312-
}
1310+
maxHeaderListSize = unsignedValue;
13131311
break;
13141312
case SETTINGS_NO_RFC7540_PRIORITIES:
13151313
peerNoRfc7540Priorities = value == 1;
13161314
break;
13171315
}
13181316
}
13191317
}
1320-
applyRemoteSettings(configBuilder.build());
1318+
applyRemoteSettings(
1319+
configBuilder.build(),
1320+
headerTableSize,
1321+
maxConcurrentStreams,
1322+
maxHeaderListSize);
13211323
}
13221324

13231325
private void produceOutput() throws HttpException, IOException {
@@ -1339,12 +1341,19 @@ private void produceOutput() throws HttpException, IOException {
13391341
}
13401342
}
13411343

1342-
private void applyRemoteSettings(final H2Config config) throws H2ConnectionException {
1344+
private void applyRemoteSettings(
1345+
final H2Config config,
1346+
final long headerTableSize,
1347+
final long maxConcurrentStreams,
1348+
final long maxHeaderListSize) throws H2ConnectionException {
13431349
remoteConfig = config;
1350+
remoteHeaderTableSize = headerTableSize;
1351+
remoteMaxConcurrentStreams = maxConcurrentStreams;
1352+
remoteMaxHeaderListSize = maxHeaderListSize;
13441353

13451354
// The peer's HEADER_TABLE_SIZE is an upper bound for the encoder. Keep the local
13461355
// dynamic table bounded to limit memory usage and lookup cost per connection.
1347-
hPackEncoder.setMaxTableSize(Math.min(remoteConfig.getHeaderTableSize(), H2Config.INIT.getHeaderTableSize()));
1356+
hPackEncoder.setMaxTableSize((int) Math.min(remoteHeaderTableSize, H2Config.INIT.getHeaderTableSize()));
13481357
final int delta = remoteConfig.getInitialWindowSize() - initOutputWinSize;
13491358
initOutputWinSize = remoteConfig.getInitialWindowSize();
13501359

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

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ void testInputHeaderContinuationFramesMaxLimit() throws Exception {
780780
outBuffer.write(continuationFrame3, writableChannel);
781781

782782
Assertions.assertThrows(H2ConnectionException.class, () ->
783-
streamMultiplexer.onInput(ByteBuffer.wrap(writableChannel.toByteArray())));
783+
streamMultiplexer.onInput(ByteBuffer.wrap(writableChannel.toByteArray())));
784784
}
785785

786786
@Test
@@ -1027,7 +1027,7 @@ void testSubmitWithPriorityHeaderEmitsPriorityUpdateBeforeHeaders() throws Excep
10271027
new BasicHeader(":authority", "example.test"),
10281028
new BasicHeader(HttpHeaders.PRIORITY, "u=3,i")
10291029
);
1030-
mux.createStream(ch, new PriorityHeaderSender(ch, reqHeaders, true));
1030+
mux.createStream(ch, new PriorityHeaderSender(ch, reqHeaders, true));
10311031

10321032
// Drive output so the handler submits
10331033
mux.onOutput();
@@ -2531,6 +2531,99 @@ void testRemoteHeaderTableSizeChangesRemainLocallyBounded() throws Exception {
25312531
}
25322532
}
25332533

2534+
2535+
@Test
2536+
void testUnsignedHeaderTableSizeSettingAccepted() throws Exception {
2537+
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
2538+
protocolIOSession,
2539+
FRAME_FACTORY,
2540+
StreamIdGenerator.ODD,
2541+
httpProcessor,
2542+
CharCodingConfig.DEFAULT,
2543+
H2Config.custom().build(),
2544+
h2StreamListener,
2545+
() -> streamHandler);
2546+
try {
2547+
final ByteBuffer payload = ByteBuffer.allocate(6);
2548+
payload.putShort((short) H2Param.HEADER_TABLE_SIZE.getCode());
2549+
payload.putInt(-1); // 0xffffffff
2550+
payload.flip();
2551+
2552+
final RawFrame settingsFrame =
2553+
new RawFrame(FrameType.SETTINGS.getValue(), 0, 0, payload);
2554+
2555+
Assertions.assertDoesNotThrow(
2556+
() -> mux.onInput(ByteBuffer.wrap(encodeFrame(settingsFrame))));
2557+
Assertions.assertEquals(
2558+
H2Config.INIT.getHeaderTableSize(),
2559+
getHPackEncoder(mux).getMaxTableSize());
2560+
} finally {
2561+
mux.close();
2562+
}
2563+
}
2564+
2565+
@Test
2566+
void testUnsignedMaxConcurrentStreamsSettingAccepted() throws Exception {
2567+
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
2568+
protocolIOSession,
2569+
FRAME_FACTORY,
2570+
StreamIdGenerator.ODD,
2571+
httpProcessor,
2572+
CharCodingConfig.DEFAULT,
2573+
H2Config.custom().build(),
2574+
h2StreamListener,
2575+
() -> streamHandler);
2576+
try {
2577+
final ByteBuffer payload = ByteBuffer.allocate(6);
2578+
payload.putShort((short) H2Param.MAX_CONCURRENT_STREAMS.getCode());
2579+
payload.putInt(Integer.MIN_VALUE); // 0x80000000
2580+
payload.flip();
2581+
2582+
final RawFrame settingsFrame =
2583+
new RawFrame(FrameType.SETTINGS.getValue(), 0, 0, payload);
2584+
2585+
Assertions.assertDoesNotThrow(
2586+
() -> mux.onInput(ByteBuffer.wrap(encodeFrame(settingsFrame))));
2587+
Assertions.assertEquals(0x80000000L, getRemoteMaxConcurrentStreams(mux));
2588+
} finally {
2589+
mux.close();
2590+
}
2591+
}
2592+
2593+
@Test
2594+
void testUnsignedMaxHeaderListSizeSettingAccepted() throws Exception {
2595+
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
2596+
protocolIOSession,
2597+
FRAME_FACTORY,
2598+
StreamIdGenerator.ODD,
2599+
httpProcessor,
2600+
CharCodingConfig.DEFAULT,
2601+
H2Config.custom().build(),
2602+
h2StreamListener,
2603+
() -> streamHandler);
2604+
try {
2605+
final ByteBuffer payload = ByteBuffer.allocate(6);
2606+
payload.putShort((short) H2Param.MAX_HEADER_LIST_SIZE.getCode());
2607+
payload.putInt(-1); // 0xffffffff
2608+
payload.flip();
2609+
2610+
final RawFrame settingsFrame =
2611+
new RawFrame(FrameType.SETTINGS.getValue(), 0, 0, payload);
2612+
2613+
Assertions.assertDoesNotThrow(
2614+
() -> mux.onInput(ByteBuffer.wrap(encodeFrame(settingsFrame))));
2615+
} finally {
2616+
mux.close();
2617+
}
2618+
}
2619+
2620+
private static long getRemoteMaxConcurrentStreams(
2621+
final AbstractH2StreamMultiplexer multiplexer) throws Exception {
2622+
final Field field = AbstractH2StreamMultiplexer.class.getDeclaredField("remoteMaxConcurrentStreams");
2623+
field.setAccessible(true);
2624+
return field.getLong(multiplexer);
2625+
}
2626+
25342627
private static HPackEncoder getHPackEncoder(final AbstractH2StreamMultiplexer multiplexer) throws Exception {
25352628
final Field field = AbstractH2StreamMultiplexer.class.getDeclaredField("hPackEncoder");
25362629
field.setAccessible(true);

0 commit comments

Comments
 (0)