Skip to content

grpc: handle single-chunk bodies in grpc_web and grpc_http1_bridge - #49226

Merged
wbpcode merged 1 commit into
envoyproxy:mainfrom
wbpcode:dev-grpc-web-bridge-single-chunk
Nov 14, 2026
Merged

wbpcode merged 1 commit into
envoyproxy:mainfrom
wbpcode:dev-grpc-web-bridge-single-chunk

Conversation

@wbpcode

@wbpcode wbpcode commented Nov 13, 2026

Copy link
Copy Markdown
Member

Commit Message: grpc: handle single-chunk bodies in grpc_web and grpc_http1_bridge
Additional Description:

  • grpc_web: when a non-gRPC response body is transformed into the grpc-message header, the 16 KiB limit was only applied if the body had been buffered across several chunks. A body that arrived as a single chunk (e.g. a local reply, or one large upstream frame) was copied into the header uncapped. The single-chunk path is now limited the same way.
  • grpc_http1_bridge: when the whole request body arrives in one end_stream frame, the gRPC frame header is now prepended to that frame in place, instead of adding the frame to the decoding buffer while iterating and relying on the filter manager to forward the buffered data. Behavior is unchanged; this removes the dependency on that forwarding.

Both were found while reviewing #48660 but do not depend on it.
Risk Level: Low
Testing: Unit and integration tests updated/added.
Docs Changes: N/A
Release Notes: Added for grpc_web.

@wbpcode

wbpcode commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

/retest

@wbpcode
wbpcode requested a balanced review from Copilot November 13, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 insufficient remaining quota. Try requesting a review at a lower effort level.

@agrawroh agrawroh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💚 Approved. Both fixes take the right shape, and framing in place nicely drops the bridge's reliance on the filter manager forwarding data it added.

  • ❓ #14516 kept a single-chunk local or direct reply whole on purpose, and the test this PR edits pinned that, so a configured reply over 16 KiB now arrives truncated.
  • 💡 Behind a filter that buffers the body without streaming, such as the buffer filter, data is the decoding buffer, so this 5-byte prepend turns a body within 5 bytes of the limit into a 413 where the old path let it through.
  • 💡 A chunked request with no chunks used to reach the upstream with no frame and now gets the empty message.
  • 💡 A Content-Length: 0 request, the usual way to send an empty message, ends in decodeHeaders, so it still reaches the upstream with no frame.
  • 💡 These mocks can't show what the filter manager forwards, and no integration test enables upgrade_protobuf_to_grpc.
  • 💡 The bridge holds the whole body but tells the filter manager it streams, so an over-limit body stalls until a timeout, or passes over the limit if it arrives in one read.
  • 💡 The chunk that crosses 16 KiB and the final chunk are both buffered whole, so this overstates the bound.
  • 💡 The other branch still appends the final chunk to the shared buffer before capping, so a final chunk that pushes it over the limit turns the error into a plain 500 without grpc-status.
  • 💡 The cap applies to body bytes before percent-encoding, so the header can reach about 48 KiB.
  • 🔄 When the request carries a Content-Length, the bridge could read it before removing it, prepend the header to the first chunk and stream the body, as grpc_http1_reverse_bridge does with its length check at the end. That fits a separate PR.

output.move(*last_data);
// "encoding_buffer" is nullptr and we only have filled "last_data". A single chunk can be
// arbitrarily large too, so limit it the same way.
output.move(*last_data, MAX_BUFFERED_PLAINTEXT_LENGTH);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

❓ #14516 kept a single-chunk local or direct reply whole on purpose, and the test this PR edits pinned that, so a configured reply over 16 KiB now arrives truncated. Should that sit behind a runtime guard? A paused HTTP/2 upstream's body can also reach us as one chunk, which argues for the cap.

return Http::FilterDataStatus::Continue;
}

if (end_stream && !buffering_request_) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 Behind a filter that buffers the body without streaming, such as the buffer filter, data is the decoding buffer, so this 5-byte prepend turns a body within 5 bytes of the limit into a 413 where the old path let it through. Could we keep the old path when &data == decoder_callbacks_->decodingBuffer()?

}

if (end_stream && !buffering_request_) {
// The whole body arrived in this frame (possibly empty, e.g. a chunked request with no

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 A chunked request with no chunks used to reach the upstream with no frame and now gets the empty message. Could the description call that a fix instead of "Behavior is unchanged", with a bug_fixes entry under a new grpc_http1_bridge area?

{":path", "/v1/spotify.Concat/Concat"}};
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, false));

// E.g. a chunked HTTP/1.1 request with only the last chunk.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 A Content-Length: 0 request, the usual way to send an empty message, ends in decodeHeaders, so it still reaches the upstream with no frame. Could we add the frame there with addDecodedData once do_bridging_ and do_framing_ are set, as connect_grpc_bridge does? A follow-up is fine too.

EXPECT_EQ(Http::FilterDataStatus::Continue, filter_->decodeData(data, true));
// The whole body in one frame is framed in place.
Buffer::OwnedImpl request_data("helloworld");
EXPECT_CALL(decoder_callbacks_, addDecodedData(_, _)).Times(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 These mocks can't show what the filter manager forwards, and no integration test enables upgrade_protobuf_to_grpc. Could we add bridge integration cases that check the upstream bytes for a body, a chunked empty body and a body at the limit behind a buffer filter?

EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, false));

Buffer::OwnedImpl buffered;
EXPECT_CALL(decoder_callbacks_, addDecodedData(_, true))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The bridge holds the whole body but tells the filter manager it streams, so an over-limit body stalls until a timeout, or passes over the limit if it arrives in one read. Could we skip addDecodedData on non-final frames and keep addDecodedData(data, true) on the last? Both then get a 413, which fits a follow-up with its own release note.

// buffer filled with data. The value is arbitrarily chosen. This can be made configurable when it
// is required.
// This is the maximum length of the response body that is transformed into the grpc-message
// header value. It also limits how much body is buffered for that purpose. The value is arbitrarily

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The chunk that crosses 16 KiB and the final chunk are both buffered whole, so this overstates the bound. Could we reword it?

// "encoding_buffer" is nullptr and we only have filled "last_data".
output.move(*last_data);
// "encoding_buffer" is nullptr and we only have filled "last_data". A single chunk can be
// arbitrarily large too, so limit it the same way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The other branch still appends the final chunk to the shared buffer before capping, so a final chunk that pushes it over the limit turns the error into a plain 500 without grpc-status. Could both branches take only the remaining room from the final chunk instead?

@@ -0,0 +1,3 @@
The gRPC-Web filter now limits the ``grpc-message`` built from a non-gRPC response body to 16 KiB

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 The cap applies to body bytes before percent-encoding, so the header can reach about 48 KiB. Could we say it uses the first 16 KiB of the body?

@wbpcode
wbpcode merged commit 6bff309 into envoyproxy:main Nov 14, 2026
25 checks passed
@wbpcode
wbpcode deleted the dev-grpc-web-bridge-single-chunk branch November 14, 2026 01:26
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.

3 participants