Skip to content

Commit c547dcd

Browse files
committed
Fix an edge case HTTP/2 HPACK header decoding bug
1 parent a7ff733 commit c547dcd

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

java/org/apache/coyote/http2/HPackHuffman.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,14 @@ public static void decode(ByteBuffer data, int length, StringBuilder target) thr
387387
if ((val & LOW_TERMINAL_BIT) == 0) {
388388
treePos = val & LOW_MASK;
389389
eosBits = false;
390+
// Found a zero, can't be counting EOS bits
390391
eosBitCount = 0;
391392
} else {
392393
target.append((char) (val & LOW_MASK));
393394
treePos = 0;
394395
eosBits = true;
396+
// Output a character, reset eosBitCount
397+
eosBitCount = 0;
395398
}
396399
} else {
397400
if (eosBits) {
@@ -409,6 +412,8 @@ public static void decode(ByteBuffer data, int length, StringBuilder target) thr
409412
target.append((char) ((val >> 16) & LOW_MASK));
410413
treePos = 0;
411414
eosBits = true;
415+
// Output a character, reset eosBitCount
416+
eosBitCount = 0;
412417
}
413418
}
414419
bitPos--;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.coyote.http2;
18+
19+
import java.nio.ByteBuffer;
20+
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
25+
public class TestHPackHuffman {
26+
27+
/*
28+
* This specific String exposed an edge case that triggered an unexpected HPACK parsing failure.
29+
*/
30+
@Test
31+
public void testValueLeftBrace() throws Exception {
32+
ByteBuffer buf = ByteBuffer.allocate(10);
33+
String data = "x-value{";
34+
HPackHuffman.encode(buf, data, false);
35+
36+
buf.flip();
37+
// Remove the header byte (in Tomcat this is parsed before the bytes are passed to the HPACK decoder)
38+
buf.get();
39+
40+
StringBuilder target = new StringBuilder();
41+
HPackHuffman.decode(buf, buf.remaining(), target);
42+
43+
Assert.assertEquals("Value changed after encode/decode roundtrip", data, target.toString());
44+
}
45+
}

webapps/docs/changelog.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@
170170
Free private keys after use in FFM based connector configuration.
171171
(markt)
172172
</fix>
173+
<fix>
174+
Correct an unlikely edge-case parsing bug in the HTTP/2 HPACK header
175+
decoding that could result in a valid header triggering an unexpected
176+
connection close. (markt)
177+
</fix>
173178
</changelog>
174179
</subsection>
175180
<subsection name="Other">

0 commit comments

Comments
 (0)