fix(client): validate response content-type - #2831
Open
Vishal-770 wants to merge 2 commits into
Open
Conversation
|
|
This checks that the response Content-Type begins with application/grpc and returns an error immediately if it does not, avoiding confusing decoding errors. Fixes grpc#2365
Vishal-770
force-pushed
the
fix-client-content-type-validation
branch
from
August 21, 2026 20:04
cd50591 to
8bb9a2d
Compare
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.
Fixes #2365.
Currently, if the gRPC client receives a response with an incorrect
Content-Type(for instance,text/htmlfrom a proxy like Envoy returning a502 Bad Gateway), it ignores the content type and proceeds to attempt decoding the HTML body as if it were a valid gRPC binary stream. This leads to confusing stream parsing errors, such asinvalid compression flag: 60 ... while receiving response with status: 502 Bad Gateway.According to the gRPC specification: "If the Content-Type does not begin with
application/grpc, the client SHOULD NOT assume that the body contains gRPC messages, and MUST fail the RPC with a status of UNKNOWN (or the equivalent status code translated from the HTTP status code, if one is present)."Solution
This PR adds a
Content-Typevalidation step intonic/src/client/grpc.rsduring the initialcreate_responsecall.We now check if the
Content-Typeheader starts withapplication/grpc(which also properly accommodatesapplication/grpc-webandapplication/grpc+proto). If it is missing or invalid, we immediately return an error. If there is a recognizable HTTP error code (e.g.,502), we utilizecrate::status::infer_grpc_statusto convert it to the appropriate gRPC status, but with a clear, custom error message stating that theContent-Typewas invalid.