GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Auto-port 5.0: Http2: Reserve promised stream before running the requ… · netty/netty@88b0cc8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • DefaultHttp2ConnectionDecoder.java
    • test/java/io/netty/handler/codec/http2
      • DefaultHttp2ConnectionDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoder.java
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,11 @@ public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promi
650650
parentStream.id(), parentStream.state());
651651
}
652652

653+
// Reserve the push stream, with a priority based on the current stream's priority, before running
654+
// the request verifier below. This ensures the promised stream's existence is tracked even if the
655+
// request is subsequently rejected, so the stream ID cannot be silently reused or forgotten.
656+
connection.remote().reservePushStream(promisedStreamId, parentStream);
657+
653658
if (!requestVerifier.isAuthoritative(ctx, headers)) {
654659
throw streamError(promisedStreamId, PROTOCOL_ERROR,
655660
"Promised request on stream %d for promised stream %d is not authoritative",
@@ -666,9 +671,6 @@ public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promi
666671
streamId, promisedStreamId);
667672
}
668673

669-
// Reserve the push stream based with a priority based on the current stream's priority.
670-
connection.remote().reservePushStream(promisedStreamId, parentStream);
671-
672674
listener.onPushPromiseRead(ctx, streamId, promisedStreamId, headers, padding);
673675
}
674676

Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoderTest.java
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static io.netty.buffer.Unpooled.wrappedBuffer;
4545
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT;
4646
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
47+
import static io.netty.handler.codec.http2.Http2Error.STREAM_CLOSED;
4748
import static io.netty.handler.codec.http2.Http2PromisedRequestVerifier.ALWAYS_VERIFY;
4849
import static io.netty.handler.codec.http2.Http2Stream.State.IDLE;
4950
import static io.netty.handler.codec.http2.Http2Stream.State.OPEN;
@@ -855,6 +856,64 @@ public void pushPromiseReadShouldSucceed() throws Exception {
855856
eq(EmptyHttp2Headers.INSTANCE), eq(0));
856857
}
857858

859+
// The promised stream must still be reserved (i.e. its existence tracked by the connection) even when the
860+
// Http2PromisedRequestVerifier subsequently rejects the promised request, so that the stream ID cannot be
861+
// silently reused or its book keeping lost. A follow-up frame for that stream must then be rejected with a
862+
// stream error rather than a connection error, since the connection is now aware of the stream.
863+
@Test
864+
public void pushPromiseReadRejectedByVerifierShouldStillReserveStream() throws Exception {
865+
Http2PromisedRequestVerifier verifier = new Http2PromisedRequestVerifier() {
866+
@Override
867+
public boolean isAuthoritative(ChannelHandlerContext ctx, Http2Headers headers) {
868+
return false;
869+
}
870+
871+
@Override
872+
public boolean isCacheable(Http2Headers headers) {
873+
return true;
874+
}
875+
876+
@Override
877+
public boolean isSafe(Http2Headers headers) {
878+
return true;
879+
}
880+
};
881+
DefaultHttp2ConnectionDecoder rejecting =
882+
new DefaultHttp2ConnectionDecoder(connection, encoder, reader, verifier);
883+
rejecting.lifecycleManager(lifecycleManager);
884+
rejecting.frameListener(listener);
885+
// Prime the decoder past the connection preface so PUSH_PROMISE is not treated as an unexpected
886+
// first frame.
887+
decode(rejecting).onSettingsRead(ctx, new Http2Settings());
888+
final Http2FrameListener dec = decode(rejecting);
889+
890+
Http2Exception ex = assertThrows(Http2Exception.class, new Executable() {
891+
@Override
892+
public void execute() throws Throwable {
893+
dec.onPushPromiseRead(ctx, STREAM_ID, PUSH_STREAM_ID, EmptyHttp2Headers.INSTANCE, 0);
894+
}
895+
});
896+
assertEquals(PROTOCOL_ERROR, ex.error());
897+
assertEquals(PUSH_STREAM_ID, Http2Exception.streamId(ex));
898+
899+
verify(remote).reservePushStream(eq(PUSH_STREAM_ID), eq(stream));
900+
verify(listener, never()).onPushPromiseRead(eq(ctx), anyInt(), anyInt(), any(Http2Headers.class), anyInt());
901+
902+
// Because the promised stream was reserved despite the rejection, the connection now knows about its
903+
// existence. A follow-up HEADERS frame for that stream (e.g. once it has been reset and closed) must
904+
// therefore be rejected with a stream error scoped to that stream, rather than a connection error.
905+
when(connection.stream(PUSH_STREAM_ID)).thenReturn(pushStream);
906+
when(pushStream.state()).thenReturn(Http2Stream.State.CLOSED);
907+
Http2Exception followUpEx = assertThrows(Http2Exception.class, new Executable() {
908+
@Override
909+
public void execute() throws Throwable {
910+
dec.onHeadersRead(ctx, PUSH_STREAM_ID, EmptyHttp2Headers.INSTANCE, 0, true);
911+
}
912+
});
913+
assertEquals(STREAM_CLOSED, followUpEx.error());
914+
assertEquals(PUSH_STREAM_ID, Http2Exception.streamId(followUpEx));
915+
}
916+
858917
@Test
859918
public void priorityReadAfterGoAwaySentShouldAllowFramesForStreamCreatedByLocalEndpoint() throws Exception {
860919
mockGoAwaySentShouldAllowFramesForStreamCreatedByLocalEndpoint();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.