Repository navigation
Conversation
`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 OpenFeign#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`.
kdelay
left a comment
There was a problem hiding this comment.
The guard change looks right, and it lines up with what the other clients already do: OkHttpClient, ApacheHttpClient, ApacheHttp5Client and Http2Client all require >= 0 before narrowing, so GoogleHttpClient was the odd one out.
One sibling that is still uncovered: core's DefaultClient has the same class of gap, and it is the client that is used when none is configured.
// core/src/main/java/feign/DefaultClient.java:113
Integer length = connection.getContentLength();
if (length == -1) {
length = null;
}It only special-cases -1, so any other negative value is passed straight to Response.Builder#body, and Response.Body#length() is documented as "length in bytes, if known. Null if unknown or greater than Integer.MAX_VALUE". HttpURLConnection does surface such values verbatim rather than normalising them to -1.
Measured on JDK 17 against a raw socket that replies HTTP/1.1 200 OK with Content-Length: -5:
Content-Length: -5 -> body().length() = -5
Content-Length: -1 -> body().length() = null
Content-Length: 5 -> body().length() = 5
(HttpURLConnection#getContentLength() itself returns -5 for that response, so the value is not synthesised by feign.)
Widening the check to cover every negative value keeps the -1 behaviour and also keeps the over-2GB case correct, since getContentLength() already returns -1 when the header does not fit in an int:
Integer length = connection.getContentLength();
if (length < 0) {
length = null;
}Happy to leave that out of this PR if you would rather keep the scope to the Google client, but it seemed worth flagging while the same code path is being fixed elsewhere.
`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 <90545043+kdelay@users.noreply.github.com>
|
Confirmed, and thanks for catching it. I reproduced it independently rather than taking the numbers on trust, and got the same result plus two more data points: (raw socket reply, JDK 17 / 21 / 26, so it is not version specific) The last row is what makes Two things on top of what you flagged:
Added it here as a separate commit with a regression test mirroring Keeping it as its own commit so it is a one-command drop if the maintainers would rather scope this PR to the Google client and take |
velo
left a comment
There was a problem hiding this comment.
Approved via agent-assisted review: change includes tests, no backwards-compatibility break, no security concerns, CI green.
GoogleHttpClient#convertResponsenarrows the responseContent-Lengthto anIntegerbut only guards the upper bound:A negative header value therefore passes through unchanged.
Response.Body#length()is documented as:so a malformed
Content-Length: -1ends up reported aslength() == -1instead of
null.Clients audited
OkHttpClient>= 0 && <= Integer.MAX_VALUEApacheHttpClient>= 0 && <= Integer.MAX_VALUEApacheHttp5Client>= 0 && <= Integer.MAX_VALUEHttp2Client>= 0 && <= Integer.MAX_VALUEGoogleHttpClientDefaultClient(core)== -1onlyThe first revision of this PR said
GoogleHttpClientwas the only client leftwithout the guard. That was wrong. @kdelay pointed out that
core'sDefaultClientspecial-cases-1alone, and it is the client used when none isconfigured.
Client.DefaultandClient.Proxiedextend it, so all three sharethat code path.
Is a negative value other than -1 actually reachable?
Yes.
HttpURLConnection#getContentLength()returns the header verbatim once itfits in an
int, and collapses to-1only when the value is absent or does notfit. Measured against a raw socket reply on JDK 17, 21 and 26:
Widening
DefaultClientfrom== -1to< 0therefore reports a malformedvalue as unknown while leaving both the
-1and the over-2GB cases untouched.Why it matters
InvocationContextdecides whether a response can be buffered with:A negative length is non-null and compares below the threshold, so it silently
passes a check that is meant to reject unknown lengths.
Verification
One test per client, both mirroring
Http2ClientContentLengthTestfrom #3431.Each one fails on master for the negative case. Reverting only the
DefaultClientguard while keeping its test:With both fixes in place:
mvn -pl googlehttpclient verify-> 24 tests, 0 failures, 0 errors, BUILD SUCCESSmvn -pl core test-> 637 tests, 0 failures, 3 skipped, 2 errorsThe two
coreerrors areOptionsTest.normalResponseTestandOptionsTest.normalResponseForChildOptionsTest, bothRead timed out. Theyreproduce identically on an unmodified
mastercheckout on this machine, sothey are environmental and unrelated to this change.
license:check,git-code-format:validate-code-formatandjapicmppass onboth modules.
Scope
The
DefaultClientchange is a separate commit, so it is easy to drop if youwould rather keep this PR to the Google client and take
coreseparately.