Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/http_server/flb_http_server_http2.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,48 @@ static int http2_data_chunk_recv_callback(nghttp2_session *inner_session,
}

if (stream->request.body == NULL) {
stream->request.body = cfl_sds_create_size(len);
/*
* Pre-allocate body buffer to avoid repeated reallocs as 16KB HTTP/2
* frames arrive. Three strategies, in priority order:
* 1. Content-Length header — exact size (non-gRPC HTTP/2)
* 2. gRPC 5-byte frame header — parse message length for exact size
* 3. Fallback to frame size (len) — original behavior
*/
size_t initial_size;

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.

We need to define variables on the top of functions due to coding conventions on this project.
See: https://github.com/fluent/fluent-bit/blob/master/CONTRIBUTING.md#variable-definitions


if (stream->request.content_length > 0) {
initial_size = stream->request.content_length;
}
else if (len >= 5 &&
stream->request.content_type != NULL &&
strncmp(stream->request.content_type, "application/grpc", 16) == 0) {
/* gRPC length-prefixed frame: 1 byte flags + 4 bytes message length */
uint32_t grpc_msg_len = ((uint32_t) data[1] << 24) |
((uint32_t) data[2] << 16) |
((uint32_t) data[3] << 8) |
((uint32_t) data[4]);
if ((size_t) grpc_msg_len > SIZE_MAX - 5) {
stream->status = HTTP_STREAM_STATUS_ERROR;
return -1;
}
initial_size = (size_t) grpc_msg_len + 5;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Allocate at least the bytes already received

When a gRPC DATA callback contains more bytes than the first length-prefixed message advertises (for example a zero-length first message followed by another message in the same DATA frame, or a malformed gRPC request), this sets initial_size below len; the later cfl_sds_create_size(initial_size) followed by memcpy(..., len) writes past the SDS allocation before any parser can reject the body. Clamp this preallocation to at least len and check the + 5 overflow before copying.

Useful? React with 👍 / 👎.

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.

}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
else {
initial_size = len;
}

if (initial_size < len) {
stream->status = HTTP_STREAM_STATUS_ERROR;
return -1;
}

if (http2_request_body_limit_exceeded(stream, initial_size)) {
stream->status = HTTP_STREAM_STATUS_ERROR;

return -1;
}

stream->request.body = cfl_sds_create_size(initial_size);

if (stream->request.body == NULL) {
stream->status = HTTP_STREAM_STATUS_ERROR;
Expand Down
Loading