Skip to content

Commit b66c031

Browse files
martinuyRealCLanger
authored andcommitted
8303965: java.net.http.HttpClient should reset the stream if response headers contain malformed header fields
Reviewed-by: abakhtin Backport-of: 466ffebcae1ee5817a83fdbc33f5ec3bd6de7e60
1 parent 98d695f commit b66c031

File tree

7 files changed

+226
-104
lines changed

7 files changed

+226
-104
lines changed

src/java.net.http/share/classes/jdk/internal/net/http/Exchange.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -602,7 +602,7 @@ HttpResponse.BodySubscriber<T> ignoreBody(HttpResponse.ResponseInfo hdrs) {
602602
.thenCompose((Http2Connection c) -> {
603603
boolean cached = c.offerConnection();
604604
if (cached) connectionAborter.disable();
605-
Stream<T> s = c.getStream(1);
605+
Stream<T> s = c.getInitialStream();
606606

607607
if (s == null) {
608608
// s can be null if an exception occurred

src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@
5353
import jdk.internal.net.http.HttpConnection.HttpPublisher;
5454
import jdk.internal.net.http.common.FlowTube;
5555
import jdk.internal.net.http.common.FlowTube.TubeSubscriber;
56+
import jdk.internal.net.http.common.HeaderDecoder;
5657
import jdk.internal.net.http.common.HttpHeadersBuilder;
5758
import jdk.internal.net.http.common.Log;
5859
import jdk.internal.net.http.common.Logger;
5960
import jdk.internal.net.http.common.MinimalFuture;
6061
import jdk.internal.net.http.common.SequentialScheduler;
6162
import jdk.internal.net.http.common.Utils;
63+
import jdk.internal.net.http.common.ValidatingHeadersConsumer;
6264
import jdk.internal.net.http.frame.ContinuationFrame;
6365
import jdk.internal.net.http.frame.DataFrame;
6466
import jdk.internal.net.http.frame.ErrorFrame;
@@ -279,6 +281,7 @@ private record PushContinuationState(HeaderDecoder pushContDecoder, PushPromiseF
279281
final ConnectionWindowUpdateSender windowUpdater;
280282
private volatile Throwable cause;
281283
private volatile Supplier<ByteBuffer> initial;
284+
private volatile Stream<?> initialStream;
282285

283286
static final int DEFAULT_FRAME_SIZE = 16 * 1024;
284287

@@ -332,6 +335,7 @@ private Http2Connection(HttpConnection connection,
332335

333336
Stream<?> initialStream = createStream(exchange);
334337
boolean opened = initialStream.registerStream(1, true);
338+
this.initialStream = initialStream;
335339
if (debug.on() && !opened) {
336340
debug.log("Initial stream was cancelled - but connection is maintained: " +
337341
"reset frame will need to be sent later");
@@ -765,7 +769,7 @@ void processFrame(Http2Frame frame) throws IOException {
765769
if (frame instanceof HeaderFrame) {
766770
// always decode the headers as they may affect
767771
// connection-level HPACK decoding state
768-
DecodingCallback decoder = new ValidatingHeadersConsumer();
772+
DecodingCallback decoder = new ValidatingHeadersConsumer()::onDecoded;
769773
try {
770774
decodeHeaders((HeaderFrame) frame, decoder);
771775
} catch (UncheckedIOException e) {
@@ -863,7 +867,7 @@ private <T> void handlePushPromise(Stream<T> parent, PushPromiseFrame pp)
863867
// decoding state
864868
assert pushContinuationState == null;
865869
HeaderDecoder decoder = new HeaderDecoder();
866-
decodeHeaders(pp, decoder);
870+
decodeHeaders(pp, decoder::onDecoded);
867871
int promisedStreamid = pp.getPromisedStream();
868872
if (pp.endHeaders()) {
869873
completePushPromise(promisedStreamid, parent, decoder.headers());
@@ -875,7 +879,7 @@ private <T> void handlePushPromise(Stream<T> parent, PushPromiseFrame pp)
875879
private <T> void handlePushContinuation(Stream<T> parent, ContinuationFrame cf)
876880
throws IOException {
877881
var pcs = pushContinuationState;
878-
decodeHeaders(cf, pcs.pushContDecoder);
882+
decodeHeaders(cf, pcs.pushContDecoder::onDecoded);
879883
// if all continuations are sent, set pushWithContinuation to null
880884
if (cf.endHeaders()) {
881885
completePushPromise(pcs.pushContFrame.getPromisedStream(), parent,
@@ -1122,6 +1126,21 @@ private void sendConnectionPreface() throws IOException {
11221126
subscriber.onNext(List.of(EMPTY_TRIGGER));
11231127
}
11241128

1129+
/**
1130+
* Called to get the initial stream after a connection upgrade.
1131+
* If the stream was cancelled, it might no longer be in the
1132+
* stream map. Therefore - we use the initialStream field
1133+
* instead, and reset it to null after returning it.
1134+
* @param <T> the response type
1135+
* @return the initial stream created during the upgrade.
1136+
*/
1137+
@SuppressWarnings("unchecked")
1138+
<T> Stream<T> getInitialStream() {
1139+
var s = (Stream<T>) initialStream;
1140+
initialStream = null;
1141+
return s;
1142+
}
1143+
11251144
/**
11261145
* Returns an existing Stream with given id, or null if doesn't exist
11271146
*/
@@ -1439,76 +1458,6 @@ final String dbgString() {
14391458
+ connection.getConnectionFlow() + ")";
14401459
}
14411460

1442-
static class HeaderDecoder extends ValidatingHeadersConsumer {
1443-
1444-
HttpHeadersBuilder headersBuilder;
1445-
1446-
HeaderDecoder() {
1447-
this.headersBuilder = new HttpHeadersBuilder();
1448-
}
1449-
1450-
@Override
1451-
public void onDecoded(CharSequence name, CharSequence value) {
1452-
String n = name.toString();
1453-
String v = value.toString();
1454-
super.onDecoded(n, v);
1455-
headersBuilder.addHeader(n, v);
1456-
}
1457-
1458-
HttpHeaders headers() {
1459-
return headersBuilder.build();
1460-
}
1461-
}
1462-
1463-
/*
1464-
* Checks RFC 7540 rules (relaxed) compliance regarding pseudo-headers.
1465-
*/
1466-
static class ValidatingHeadersConsumer implements DecodingCallback {
1467-
1468-
private static final Set<String> PSEUDO_HEADERS =
1469-
Set.of(":authority", ":method", ":path", ":scheme", ":status");
1470-
1471-
/** Used to check that if there are pseudo-headers, they go first */
1472-
private boolean pseudoHeadersEnded;
1473-
1474-
/**
1475-
* Called when END_HEADERS was received. This consumer may be invoked
1476-
* again after reset() is called, but for a whole new set of headers.
1477-
*/
1478-
void reset() {
1479-
pseudoHeadersEnded = false;
1480-
}
1481-
1482-
@Override
1483-
public void onDecoded(CharSequence name, CharSequence value)
1484-
throws UncheckedIOException
1485-
{
1486-
String n = name.toString();
1487-
if (n.startsWith(":")) {
1488-
if (pseudoHeadersEnded) {
1489-
throw newException("Unexpected pseudo-header '%s'", n);
1490-
} else if (!PSEUDO_HEADERS.contains(n)) {
1491-
throw newException("Unknown pseudo-header '%s'", n);
1492-
}
1493-
} else {
1494-
pseudoHeadersEnded = true;
1495-
if (!Utils.isValidName(n)) {
1496-
throw newException("Bad header name '%s'", n);
1497-
}
1498-
}
1499-
String v = value.toString();
1500-
if (!Utils.isValidValue(v)) {
1501-
throw newException("Bad header value '%s'", v);
1502-
}
1503-
}
1504-
1505-
private UncheckedIOException newException(String message, String header)
1506-
{
1507-
return new UncheckedIOException(
1508-
new IOException(String.format(message, header)));
1509-
}
1510-
}
1511-
15121461
static final class ConnectionWindowUpdateSender extends WindowUpdateSender {
15131462

15141463
final int initialWindowSize;

src/java.net.http/share/classes/jdk/internal/net/http/Stream.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ void otherFrame(Http2Frame frame) throws IOException {
460460
// The Hpack decoder decodes into one of these consumers of name,value pairs
461461

462462
DecodingCallback rspHeadersConsumer() {
463-
return rspHeadersConsumer;
463+
return rspHeadersConsumer::onDecoded;
464464
}
465465

466466
protected void handleResponse() throws IOException {
@@ -1504,9 +1504,10 @@ final String dbgString() {
15041504
return connection.dbgString() + "/Stream("+streamid+")";
15051505
}
15061506

1507-
private class HeadersConsumer extends Http2Connection.ValidatingHeadersConsumer {
1507+
private class HeadersConsumer extends ValidatingHeadersConsumer {
15081508

1509-
void reset() {
1509+
@Override
1510+
public void reset() {
15101511
super.reset();
15111512
responseHeadersBuilder.clear();
15121513
debug.log("Response builder cleared, ready to receive new headers.");
@@ -1516,15 +1517,28 @@ void reset() {
15161517
public void onDecoded(CharSequence name, CharSequence value)
15171518
throws UncheckedIOException
15181519
{
1519-
String n = name.toString();
1520-
String v = value.toString();
1521-
super.onDecoded(n, v);
1522-
responseHeadersBuilder.addHeader(n, v);
1523-
if (Log.headers() && Log.trace()) {
1524-
Log.logTrace("RECEIVED HEADER (streamid={0}): {1}: {2}",
1525-
streamid, n, v);
1520+
try {
1521+
String n = name.toString();
1522+
String v = value.toString();
1523+
super.onDecoded(n, v);
1524+
responseHeadersBuilder.addHeader(n, v);
1525+
if (Log.headers() && Log.trace()) {
1526+
Log.logTrace("RECEIVED HEADER (streamid={0}): {1}: {2}",
1527+
streamid, n, v);
1528+
}
1529+
} catch (UncheckedIOException uio) {
1530+
// reset stream: From RFC 9113, section 8.1
1531+
// Malformed requests or responses that are detected MUST be
1532+
// treated as a stream error (Section 5.4.2) of type
1533+
// PROTOCOL_ERROR.
1534+
onProtocolError(uio.getCause());
15261535
}
15271536
}
1537+
1538+
@Override
1539+
protected String formatMessage(String message, String header) {
1540+
return "malformed response: " + super.formatMessage(message, header);
1541+
}
15281542
}
15291543

15301544
private static final VarHandle STREAM_STATE;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.net.http.common;
26+
27+
import java.net.http.HttpHeaders;
28+
29+
public class HeaderDecoder extends ValidatingHeadersConsumer {
30+
31+
private final HttpHeadersBuilder headersBuilder;
32+
33+
public HeaderDecoder() {
34+
this.headersBuilder = new HttpHeadersBuilder();
35+
}
36+
37+
@Override
38+
public void onDecoded(CharSequence name, CharSequence value) {
39+
String n = name.toString();
40+
String v = value.toString();
41+
super.onDecoded(n, v);
42+
headersBuilder.addHeader(n, v);
43+
}
44+
45+
public HttpHeaders headers() {
46+
return headersBuilder.build();
47+
}
48+
}

src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,21 @@ public static URLPermission permissionForServer(URI uri,
392392
}
393393

394394

395+
private static final boolean[] LOWER_CASE_CHARS = new boolean[128];
396+
395397
// ABNF primitives defined in RFC 7230
396398
private static final boolean[] tchar = new boolean[256];
397399
private static final boolean[] fieldvchar = new boolean[256];
398400

399401
static {
400-
char[] allowedTokenChars =
401-
("!#$%&'*+-.^_`|~0123456789" +
402-
"abcdefghijklmnopqrstuvwxyz" +
403-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
404-
for (char c : allowedTokenChars) {
402+
char[] lcase = ("!#$%&'*+-.^_`|~0123456789" +
403+
"abcdefghijklmnopqrstuvwxyz").toCharArray();
404+
for (char c : lcase) {
405+
tchar[c] = true;
406+
LOWER_CASE_CHARS[c] = true;
407+
}
408+
char[] ucase = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
409+
for (char c : ucase) {
405410
tchar[c] = true;
406411
}
407412
for (char c = 0x21; c <= 0xFF; c++) {
@@ -410,6 +415,16 @@ public static URLPermission permissionForServer(URI uri,
410415
fieldvchar[0x7F] = false; // a little hole (DEL) in the range
411416
}
412417

418+
public static boolean isValidLowerCaseName(String token) {
419+
for (int i = 0; i < token.length(); i++) {
420+
char c = token.charAt(i);
421+
if (c > 255 || !LOWER_CASE_CHARS[c]) {
422+
return false;
423+
}
424+
}
425+
return !token.isEmpty();
426+
}
427+
413428
/*
414429
* Validates a RFC 7230 field-name.
415430
*/

0 commit comments

Comments
 (0)