GeezerVison is showing github.com — Don't send HTTP/2 informational responses that end the stream by allthingssecurity · Pull Request #17742 · netty/netty
Conversation
Don't send HTTP/2 informational responses that end the stream
Motivation:
RFC 9113, Section 8.1: "A HEADERS frame with the END_STREAM flag set that carries an informational status code is malformed". Netty can send such a frame:
- On a server,
DefaultHttp2ConnectionEncoder.writeHeaders(ctx, streamId, headers.status("100"), 0, true, promise) succeeds, writes HEADERS(END_STREAM) and half-closes the stream. validateHeadersSentState(...) only rejects a 1xx after the final headers.
HttpToHttp2ConnectionHandler does the same for every empty informational FullHttpResponse, for example new DefaultFullHttpResponse(HTTP_1_1, CONTINUE) (what HttpServerExpectContinueHandler writes), because an empty FullHttpMessage is written with endStream = true. A non-full informational HttpResponse is sent without END_STREAM, but the LastHttpContent after it is sent as a DATA frame with END_STREAM, with the same result.
With HttpToHttp2ConnectionHandler this breaks Expect: 100-continue and takes the connection down with it. On 4.2, a server that answers a request on stream 3 with 100 (Continue) and then 200:
- sends
HEADERS(3, :status 100, END_STREAM); the client sees the 100 as the end of the response,
- fails the write of the final response with
IllegalStateException: Stream 3 in unexpected state HALF_CLOSED_LOCAL,
- handles that as a connection error: it sends
GOAWAY(INTERNAL_ERROR) and closes the connection, with every other stream on it.
Http2StreamFrameToHttpObjectCodec already handles this and writes a 1xx FullHttpResponse (other than 101) as a Http2HeadersFrame with endStream = false. For HTTP/1, HttpResponseEncoder treats the content of a 1xx response as always empty (isContentAlwaysEmpty(...), except a 101 for WebSocket version 00): it removes Content-Length and Transfer-Encoding (sanitizeHeadersBeforeEncode(...)), does not write the content of a FullHttpResponse, and drops the content of a HttpResponse up to and including the next LastHttpContent. HttpToHttp2ConnectionHandler and Http2StreamFrameToHttpObjectCodec also sent the content-length of a 1xx, for example the content-length: 0 of HttpServerExpectContinueHandler's 100; a Netty client applies it to a final response without its own content-length and rejects the body.
#17684 handles the receiving side (a Netty client resets a stream that gets a 1xx with END_STREAM). This is the sending side, so a Netty server does not produce what a Netty client will reject.
Modification:
DefaultHttp2ConnectionEncoder.writeHeaders(...): on a server, fail the promise of informational headers with endOfStream = true with an IllegalStateException, before anything is written or the stream state changes. As for writeData(...) on a stream in the wrong state, only the write fails; the stream and the connection stay open, so the final response can still be sent. The check runs before the stream state switch rather than in validateHeadersSentState(...), because an exception thrown from there goes to lifecycleManager.onError(...) and would close the connection.
HttpToHttp2ConnectionHandler: write every informational HttpResponse as a HEADERS frame without END_STREAM and without content-length, and drop its content (of a FullHttpResponse, or up to and including the next LastHttpContent), as HttpResponseEncoder does for HTTP/1.
Http2StreamFrameToHttpObjectCodec.encode(...): remove content-length from the HEADERS of an informational FullHttpResponse. It already wrote them without END_STREAM and without their content, and rejects a non-full 1xx HttpResponse.
- New tests:
DefaultHttp2ConnectionEncoderTest.infoHeadersWithEOSFails: the write fails, nothing is written, onError(...) is not called, and the final headers can still be sent.
HttpToHttp2ConnectionHandlerTest.testInformationalResponseDoesNotEndStream: a client sends a request with Expect: 100-continue, the server writes 100 (Continue) and 200 as FullHttpResponses, and the client gets HEADERS(3, 100) without END_STREAM and then HEADERS(3, 200, END_STREAM).
HttpToHttp2ConnectionHandlerTest.testInformationalResponseContentIsDropped: a 100 FullHttpResponse with a body and content-length: 7, then a 200 with a body and no content-length. The client gets HEADERS(3, :status 100) without END_STREAM, then the 200 and only its body; the 100's content is released.
HttpToHttp2ConnectionHandlerTest.testNonFullInformationalResponseDoesNotEndStream: a non-full 100 HttpResponse, an HttpContent and LastHttpContent.EMPTY_LAST_CONTENT, then a 200. All writes succeed, the client gets the 100 without END_STREAM, no DATA, and the 200 with END_STREAM.
Http2StreamFrameToHttpObjectCodecTest: encodeInformationalResponseWithoutContentLength (a 100 and a 103 with content-length: 0), encodeInformationalFullResponseDropsContent (a 100 with a body and content-length: only the HEADERS frame is written and the body is released) and encode100ContinueThenFinalResponse (a 100, then a 200 with a body that keeps its content-length).
Result:
Netty no longer sends informational responses that end the stream, and 100 (Continue) works through HttpToHttp2ConnectionHandler.
- Both new tests fail on 4.2 (the encoder test: no failure; the handler test: the final response fails as above). Reverting only the change to
HttpToHttp2ConnectionHandler fails the handler test (the 100 is rejected by the encoder); reverting only the change to the encoder fails the encoder test. Both tests added in the follow-up also fail on the previous head 2e22e00 (the 100 carries content-length: 7; the non-full case fails the final response with Stream 3 in unexpected state HALF_CLOSED_LOCAL). Without only the content-length removal, the client never gets the 200's body. The Http2StreamFrameToHttpObjectCodecTest tests fail without the change to Http2StreamFrameToHttpObjectCodec (the 1xx carries content-length).
- The
io.netty.handler.codec.http2 tests pass (1638 tests, 5 skipped, with -Dio.netty.leakDetection.level=paranoid, without any LEAK: report), and DefaultHttp2ConnectionEncoderTest, HttpToHttp2ConnectionHandlerTest, Http2StreamFrameToHttpObjectCodecTest, InboundHttp2ToHttpAdapterTest, Http2FrameCodecTest, Http2MultiplexHandlerTest, StreamBufferingEncoderTest, Http2ConnectionRoundtripTest, DefaultHttp2ConnectionDecoderTest and Http2ConnectionHandlerTest (423 tests) also pass with -Dio.netty.leakDetection.level=paranoid, without any LEAK: report. Checkstyle and revapi pass.
- 4.1 and 5.0 have the same
validateHeadersSentState(...) and HttpToHttp2ConnectionHandler code.
Behaviour change: a server that writes 1xx headers with endOfStream = true through the encoder (directly or as a Http2HeadersFrame with endStream = true) now gets a failed write instead of sending a malformed frame. This also applies to 101, which RFC 9113 (Section 8.6) does not allow in HTTP/2 anyway.
The problem was found while reviewing #17684, and reproduced with the real classes. The change was prepared with Claude Code (Claude Opus 5.5), and the commits have a Co-Authored-By trailer.
Prepared with Claude Code on behalf of allthingssecurity.
🤖 Generated with Claude Code
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
RFC 9113, 8.1 says that a HEADERS frame with END_STREAM that carries an
informational (1xx) status code is malformed. Netty sends such frames:
- DefaultHttp2ConnectionEncoder.writeHeaders(...) on a server accepts
1xx headers with endOfStream = true, writes HEADERS with END_STREAM and
half-closes the stream.
- HttpToHttp2ConnectionHandler does this for every empty informational
FullHttpResponse, for example new DefaultFullHttpResponse(HTTP_1_1,
CONTINUE), because an empty FullHttpMessage is written with END_STREAM.
The final response on that stream then fails, as the stream is already
half-closed, and the connection is closed with GOAWAY.
Modification:
- DefaultHttp2ConnectionEncoder fails the write of informational headers
with END_STREAM with an IllegalStateException, without writing anything
or changing the stream state.
- HttpToHttp2ConnectionHandler writes an informational FullHttpResponse
as a HEADERS frame without END_STREAM, as
Http2StreamFrameToHttpObjectCodec already does.
- Add tests.
Result:
Netty no longer sends informational responses that end the stream, and
100 (Continue) works through HttpToHttp2ConnectionHandler.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The failures on macos-aarch64-java11-boringssl are KQueueDatagramUnicastIPv6MappedTest (kqueue UDP), the known flaky test that also failed on #17728 and #17699 this week; this change only touches codec-http2. A rerun of that job should clear it.
Prepared with Claude Code on behalf of allthingssecurity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ream
Motivation:
testInformationalResponseDoesNotEndStream failed now and then on CI
(linux-x86_64-java11-pooled, linux-x86_64-java24): the write of the
100 (Continue) response failed with
Http2Exception: Request stream 3 is not correct for server connection.
The test created the environment with bootstrapEnv(1, 1, 0), but the
request latch is counted down by every frame FrameCountDown sees,
including the client's SETTINGS. awaitRequests() therefore returned
before the server had read the request HEADERS (in 19561 of 20000 runs
the server listener had only seen SETTINGS and SETTINGS ACK). The test
then wrote the 100 response from the test thread. LocalChannel completes
the client's write promise before it schedules the read on the server's
event loop, so sometimes the server's event loop ran the write first,
stream 3 did not exist yet and the server tried to create it as a local
stream. This is a race in the test, not in the change.
Modification:
Use bootstrapEnv(2, 1, 0), like the other tests in the class, so
awaitRequests() waits for the client's SETTINGS and the request HEADERS.
Result:
The test no longer races. Before: 4 failures in 20000 runs. After: no
failures in 100000 runs, and the server always had stream 3 when the test
wrote the responses.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The intermittent failure of testInformationalResponseDoesNotEndStream on linux-x86_64-java11-pooled and linux-x86_64-java24 was a race in the new test, not in the change. 2e22e00 fixes it.
The test called bootstrapEnv(1, 1, 0), but FrameCountDown counts down the request latch for every frame it sees, including the client's SETTINGS. So awaitRequests() usually returned before the server had read the request HEADERS: in 19561 of 20000 local runs the server listener had only seen SETTINGS and SETTINGS ACK. The test then wrote the 100 response from the test thread. LocalChannel completes the client's write promise before it schedules the read on the server's event loop, so sometimes the server ran the write first. Stream 3 did not exist yet, and the write failed with Http2Exception: Request stream 3 is not correct for server connection (from DefaultHttp2ConnectionEncoder.writeHeaders0 -> createStream).
The fix is bootstrapEnv(2, 1, 0), as in the other tests in the class, so the latch waits for the SETTINGS and the request HEADERS. Locally, with -Pleak -Dio.netty.allocator.type=pooled: 4 failures in 20000 runs before, none in 100000 runs after (20000 of them under CPU load), and the server always had the request when the test wrote. On 4.2's production code the test still fails every time, on the final response.
Prepared with Claude Code on behalf of allthingssecurity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
For HTTP/1, HttpResponseEncoder treats the content of every
informational (1xx) response as always empty (except a 101 for WebSocket
version 00): it removes Content-Length and Transfer-Encoding, does not
write the content of a FullHttpResponse, and for a HttpResponse it drops
the content up to and including the next LastHttpContent.
HttpToHttp2ConnectionHandler only handled an informational
FullHttpResponse, and kept its Content-Length:
- A non-full informational HttpResponse was written without END_STREAM,
but the LastHttpContent that follows it was written as a DATA frame
with END_STREAM. The final response then failed, as the stream was
already half-closed.
- The Content-Length of an informational response was sent, for example
the "content-length: 0" of the 100 (Continue) that
HttpServerExpectContinueHandler writes. A Netty client applies it to
the final response if that has no Content-Length of its own, and
rejects a body of another length.
Modification:
Write every informational HttpResponse as HEADERS without END_STREAM
and without Content-Length, and drop its content: the content of a
FullHttpResponse, or the HttpContent up to and including the next
LastHttpContent. Add tests.
Result:
HttpToHttp2ConnectionHandler writes informational responses the way
HttpResponseEncoder does for HTTP/1.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HTTP/2 stuff looks fine to me. Someone else will need to verify that LastHttpContent/discardContent is appropriate, as it doesn't make any sense to me but I'd also have to dig deeper into the HTTP/1 stuff to try to understand what it is trying to do.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one minor comment from me.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…eToHttpObjectCodec
Motivation:
Http2StreamFrameToHttpObjectCodec writes an informational (1xx, other
than 101) FullHttpResponse as a HEADERS frame without END_STREAM and
without its content, but it kept its Content-Length, for example the
"content-length: 0" of the 100 (Continue) that
HttpServerExpectContinueHandler writes. A Netty client applies it to
the final response if that has no Content-Length of its own, and
rejects a body of another length. HttpResponseEncoder removes it for
HTTP/1, and so does HttpToHttp2ConnectionHandler since the previous
commit.
Modification:
Remove Content-Length from the HEADERS of an informational response in
Http2StreamFrameToHttpObjectCodec.encode(...), and add tests: a 100 and
a 103 with Content-Length, a 100 FullHttpResponse with content, and a
100 followed by the final response, which keeps its Content-Length.
Result:
Http2StreamFrameToHttpObjectCodec writes informational responses
without Content-Length, like HttpResponseEncoder and
HttpToHttp2ConnectionHandler.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
✅ All tests passed ✅
🏷️ Commit: 7754dcd
▶️ Tests: 56686 executed
⚪️ Checks: 26/26 completed
Learn more about TestLens at testlens.app/docs.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
ejona86
ejona86 left review comments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|