cap Content-Length before narrowing to int in Http2Client - #3431
Merged
Merged
Conversation
velo
approved these changes
Jun 20, 2026
velo
left a comment
Member
There was a problem hiding this comment.
This fixes a real edge case in the HTTP/2 adapter without changing the API surface. The range check in java11/src/main/java/feign/http2client/Http2Client.java matches the Response.Body contract, and Http2ClientContentLengthTest covers the oversized, negative, and normal-length paths. Checks are green, so this looks good to me.
Merged
renechoi
added a commit
to renechoi/feign
that referenced
this pull request
Jul 28, 2026
`convertResponse` mapped only `-1` to `null`, so any other negative
`Content-Length` reached `Response.Body#length()` unchanged.
`HttpURLConnection#getContentLength()` returns the header verbatim once
it fits in an `int`, and already returns `-1` when the value is unknown
or above `Integer.MAX_VALUE`. Measured against a raw socket reply on
JDK 17, 21 and 26:
Content-Length: -5 -> getContentLength() = -5
Content-Length: -1 -> getContentLength() = -1
Content-Length: 3000000000 -> getContentLength() = -1
Widening the guard to `< 0` therefore reports a malformed value as
unknown while leaving both the `-1` and the over-2GB cases untouched.
This is the same defect fixed for `Http2Client` in OpenFeign#3431 and for
`GoogleHttpClient` in the previous commit. `Client.Default` and
`Client.Proxied` extend `DefaultClient`, so they inherit the fix.
Covered by tests mirroring `Http2ClientContentLengthTest`.
Co-authored-by: kdelay <[email protected]>
velo
pushed a commit
that referenced
this pull request
Jul 28, 2026
`convertResponse` narrowed the response `Content-Length` to `Integer` while only guarding the upper bound, so a negative header value was passed straight through to `Response.Body#length()`. That field is documented as "length in bytes, if known. Null if unknown or greater than Integer.MAX_VALUE", and every other client already rejects negative values before narrowing: - `OkHttpClient` (`contentLength() >= 0 && <= Integer.MAX_VALUE`) - `ApacheHttp5Client` and `ApacheHttpClient` (same guard on the entity) - `Http2Client` (same guard, added in #3431) `GoogleHttpClient` was the only remaining client without it, so a malformed `Content-Length: -1` surfaced as `length() == -1` instead of `null`. `InvocationContext` compares that value against `MAX_RESPONSE_BUFFER_SIZE` to decide whether a response is bufferable, and a negative length silently passes the check. Adds the missing lower-bound guard and reads the header once instead of twice. Covered by tests mirroring `Http2ClientContentLengthTest`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Http2Client.toFeignResponsenarrows the responseContent-Lengthwith(int) length.getAsLong()and no range check.Content-Length: 2147483648(2^31) wraps to-2147483648, andContent-Length: -1is forwarded as-is.Response.Body.length()is documented to benullwhen the length is unknown or greater thanInteger.MAX_VALUE, andokhttp,httpclient,hc5andgooglehttpclientalready cap before narrowing. The wrapped negative length also slips past theresponse.body().length() <= MAX_RESPONSE_BUFFER_SIZEcheck inInvocationContextthat decides whether to buffer the whole response into memory. Capped so theintis returned only for0 <= length <= Integer.MAX_VALUE, otherwisenull.repro:Http2ClientContentLengthTestbuilds anHttpResponsewith a givenContent-Lengthand readsbody().length().Content-Length: 2147483648expectednull, before-2147483648.Content-Length: -1expectednull, before-1.Content-Length: 1024stays1024.