Skip to content

fix(client): validate response content-type - #2831

Open
Vishal-770 wants to merge 2 commits into
grpc:masterfrom
Vishal-770:fix-client-content-type-validation
Open

fix(client): validate response content-type#2831
Vishal-770 wants to merge 2 commits into
grpc:masterfrom
Vishal-770:fix-client-content-type-validation

Conversation

@Vishal-770

@Vishal-770 Vishal-770 commented Aug 21, 2026

Copy link
Copy Markdown

Fixes #2365.

Currently, if the gRPC client receives a response with an incorrect Content-Type (for instance, text/html from a proxy like Envoy returning a 502 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 as invalid 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-Type validation step in tonic/src/client/grpc.rs during the initial create_response call.

We now check if the Content-Type header starts with application/grpc (which also properly accommodates application/grpc-web and application/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 utilize crate::status::infer_grpc_status to convert it to the appropriate gRPC status, but with a clear, custom error message stating that the Content-Type was invalid.

Copilot AI lite review requested due to automatic review settings August 21, 2026 19:55
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 21, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Vishal-770 / name: vishal-770 (cd50591)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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
Vishal-770 force-pushed the fix-client-content-type-validation branch from cd50591 to 8bb9a2d Compare August 21, 2026 20:04
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.

client must not attempt decode responses with wrong content type

2 participants