Skip to content

Fix BZ 70366 - HTTP/2 content length read while headers are modified - #1266

Open
arturobernalg wants to merge 1 commit into
apache:mainfrom
arturobernalg:bz70366-h2-content-length
Open

arturobernalg wants to merge 1 commit into
apache:mainfrom
arturobernalg:bz70366-h2-content-length

Conversation

@arturobernalg

@arturobernalg arturobernalg commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

https://bz.apache.org/bugzilla/show_bug.cgi?id=70366

When a DATA frame is received, Stream.receivedData() looks up content-length in the request headers on the connection thread (until a value has been found and cached), while the container thread may already be modifying the same MimeHeaders (RemoteIpValve removes and restores X-Forwarded-For). The lookup can return the next header's value, which resets a valid request with PROTOCOL_ERROR, or miss the header, which disables the RFC 9113, section 8.1.1 check.

The content length is now resolved in receivedEndOfHeaders(), before the stream is passed to a container thread, and that value is used while the body is received. As a side effect, an invalid or duplicate content-length is rejected at the end of the headers instead of on the first DATA frame.

Testing:

  • New TestStream.testContentLengthIndependentOfHeaderChanges: a valve removes the header and the client sends 256 bytes for a declared 128. On main the request is accepted (200); with the fix the stream is reset with PROTOCOL_ERROR.
  • Local Tomcat built from main, h2c with RemoteIpValve, 32 concurrent POSTs with X-Forwarded-For: 2 of 10 five-second runs were reset with NumberFormatException: For input string: "Java-http-client/27" (the User-Agent value). With the fix, 0 of 20.
  • All org.apache.coyote.http2 tests pass.

@arturobernalg
arturobernalg force-pushed the bz70366-h2-content-length branch 2 times, most recently from 70d9078 to 8db8ed7 Compare November 21, 2026 18:37
The content length of an HTTP/2 request was looked up in the request
headers when a DATA frame was received on the connection thread, until
a value had been found. At that point the container thread may already
be processing the request and modifying the same, unsynchronised,
MimeHeaders instance. RemoteIpValve removes and restores headers, so the
lookup could return the value of another header, which reset a valid
request with PROTOCOL_ERROR, or miss the header, which disabled the
check required by RFC 9113, section 8.1.1.

Resolve the content length once the request headers have been received,
before the stream is passed to a container thread, and use that value
for the checks made as the request body is received.
@arturobernalg
arturobernalg force-pushed the bz70366-h2-content-length branch from 8db8ed7 to d6a5a6c Compare November 21, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant